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
/
Domoticz.py
executable file
·153 lines (146 loc) · 5.66 KB
/
Domoticz.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""For controlling Domoticz."""
import urllib
import urllib.request
import json
import re
from mycroft.util.log import getLogger
LOGGER = getLogger(__name__)
class Domoticz:
"""Class for controlling Domoticz."""
def __init__(self, host, port, protocol, authentication, login, password):
"""Recover settings for accessing to Domoticz instance."""
self.host = host
self.port = port
protocol = protocol
authentication = authentication
if protocol:
self.protocol = "https"
else:
self.protocol = "http"
if authentication:
self.login = login
self.password = password
self.url = self.protocol + "://" + self.login + ":" + self.password + "@" + self.host + ":" + self.port
else:
self.url = self.protocol + "://" + self.host + ":" + self.port
def findid(self, what, where, state):
i = 0
wht = re.compile(what, re.I)
whr = re.compile(where, re.I)
f = urllib.request.urlopen(self.url + "/json.htm?type=devices&filter=all&used=true")
response = f.read()
payload = json.loads(response.decode('utf-8'))
idx = False
stype = False
dlevel = False
while i < len(payload['result']):
if whr.search(payload['result'][i]['Name']) and wht.search(payload['result'][i]['Name']):
stype = payload['result'][i]['Type']
typ = re.compile(stype, re.I)
dlevel = "100"
if typ.search("Group") or typ.search("Scene"):
stype = "scene"
elif typ.search("Light/Switch"):
stype = "light"
dlevel = payload['result'][i]['Level']
else:
stype = "light"
idx = payload['result'][i]['idx']
rslt = re.compile(" " + str(state).title(), re.I)
if rslt.search(" " + payload['result'][i]['Data']):
result = 0
else:
result = 1
break
elif i is len(payload['result']) - 1:
result = None
break
i += 1
return [idx, result, stype, dlevel]
def findcmd(self, state, action, dlevel):
dsrdst = str(state).title()
act = str(action).title()
if dsrdst == "None":
dsrdst = "25%"
rslt = re.compile(dsrdst, re.I)
rslt2 = re.compile(act, re.I)
if dsrdst.find('%') > -1:
if len(dsrdst) == 3:
dsrdst = int(dsrdst[0:2])
elif len(dsrdst) == 4:
dsrdst = 100
else:
dsrdst = 5
cmd = False
if rslt2.search('dim') or rslt2.search('decrease'):
stlvl = int(dlevel) - int(dsrdst)
if stlvl < 0:
stlvl = 0
cmd = "Set%20Level&level=" + str(stlvl)
elif rslt2.search('brighten') or rslt2.search('increase'):
stlvl = int(dlevel) + int(dsrdst)
if stlvl > 100:
stlvl = 100
cmd = "Set%20Level&level=" + str(stlvl)
elif rslt2.search('set'):
stlvl = int(cmd)
if stlvl > 100:
stlvl = 100
elif stlvl < 0:
stlvl = 0
cmd = "Set%20Level&level=" + str(stlvl)
else:
if rslt.search('lock') or rslt.search('open') or rslt.search('on'):
cmd = "On"
elif rslt.search('unlock') or rslt.search('close') or rslt.search('off'):
cmd = "Off"
return cmd
def switch(self, state, what, where, action):
"""Switch the device in Domoticz."""
data = []
data = self.findid(what, where, state)
idx = data[0]
result = data[1]
stype = data[2]
dlevel = data[3]
if result is 1:
cmd = self.findcmd(state, action, dlevel)
if cmd:
try:
f = urllib.request.urlopen(self.url + "/json.htm?type=command¶m=switch" + stype + "&idx=" + str(idx) + "&switchcmd=" + str(cmd))
response = f.read()
LOGGER.debug(str(response))
return response
except IOError as e:
LOGGER.error(str(e) + ' : ' + str(e.read()))
else:
LOGGER.debug("no command found")
return result
def get(self, what, where):
"""Get the device's data in Domoticz."""
try:
f = urllib.request.urlopen(self.url + "/json.htm?type=devices&filter=all&used=true")
response = f.read()
payload = json.loads(response.decode('utf-8'))
wht = re.compile(what, re.I)
i = 0
if where is not None:
whr = re.compile(where, re.I)
while i < len(payload['result']):
if whr.search(payload['result'][i]['Name']) and wht.search(payload['result'][i]['Name']):
break
elif i is len(payload['result']) - 1:
payload['result'][i]['Data'] = None
break
i += 1
elif where is None:
while i < len(payload['result']):
if wht.search(payload['result'][i]['Name']):
break
elif i is len(payload['result']) - 1:
payload['result'][i]['Data'] = None
break
i += 1
return payload['result'][i]
except IOError as e:
LOGGER.error(str(e) + ' : ' + str(e.read()))