This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
__init__.py
executable file
·117 lines (104 loc) · 4.13 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>.
from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill
from mycroft.util.log import getLogger
from os.path import dirname, abspath
from .Domoticz import Domoticz
import sys
import re
__author__ = 'mTreussart'
sys.path.append(abspath(dirname(__file__)))
LOGGER = getLogger(__name__)
class DomoticzSkill(MycroftSkill):
def __init__(self):
super(DomoticzSkill, self).__init__(name="DomoticzSkill")
def initialize(self):
domoticz_switch_intent = IntentBuilder("SwitchIntent")\
.optionally("TurnKeyword")\
.require("StateKeyword")\
.require("WhatKeyword")\
.require("WhereKeyword").build()
self.register_intent(domoticz_switch_intent,
self.handle_domoticz_switch_intent)
domoticz_infos_intent = IntentBuilder("InfosIntent")\
.require("InfosKeyword")\
.require("WhatKeyword")\
.optionally("WhereKeyword")\
.optionally("StateKeyword").build()
self.register_intent(domoticz_infos_intent,
self.handle_domoticz_infos_intent)
def handle_domoticz_switch_intent(self, message):
domoticz = Domoticz(
self.settings.get("hostname"),
self.settings.get("port"),
self.settings.get("protocol"),
self.settings.get("authentication"),
self.settings.get("username"),
self.settings.get("password"))
state = message.data.get("StateKeyword")
what = message.data.get("WhatKeyword")
where = message.data.get("WhereKeyword")
action = message.data.get("TurnKeyword")
data = {
'what': what,
'where': where
}
LOGGER.debug("message : " + str(message.data))
response = domoticz.switch(state, what, where, action)
edng = re.compile(str(state).title(), re.I)
ending = "ed"
if edng.search('on') or edng.search('off'):
ending = ""
if response is None:
self.speak_dialog("NotFound", data)
elif response is 0:
self.speak("The " + str(what) + " is already " + str(state).title() + ending)
elif response is 1:
self.speak("The " + str(what) + " can not be operated with " + str(state).title())
def handle_domoticz_infos_intent(self, message):
what = message.data.get("WhatKeyword")
where = message.data.get("WhereKeyword")
domoticz = Domoticz(
self.settings.get("hostname"),
self.settings.get("port"),
self.settings.get("protocol"),
self.settings.get("authentication"),
self.settings.get("username"),
self.settings.get("password"))
data = {
'what': what,
'where': where
}
response = domoticz.get(what, where)
data = str(response['Data'])
if data is None:
if where is None:
self.speak_dialog("NotFoundShort", data)
else:
self.speak_dialog("NotFound", data)
if re.search('\d\s+C', data):
data = data.replace(' C', ' degrees celsius')
if re.search('\d\s+F', data):
data = data.replace(' F', ' degrees fahrenheit')
data = "It's " + data
LOGGER.debug("result : " + str(data))
self.speak(str(data))
def stop(self):
pass
def create_skill():
return DomoticzSkill()