-
Notifications
You must be signed in to change notification settings - Fork 2
/
domoticz_tools.py
330 lines (290 loc) · 12.9 KB
/
domoticz_tools.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python
################################################################################
# Generic helper functions
################################################################################
__all__ = ['TIMEDOUT', 'MINUTE', 'DEBUG_OFF', 'DEBUG_ON', 'DEBUG_ON_NO_FRAMEWORK',\
'DumpConfigToLog', \
'DomoticzAPI', \
'GetNextFreeUnit', 'FindUnitFromName', 'FindUnitFromDescription', 'AddTagToDescription', 'GetTagFromDescription', 'UpdateDevice', 'GetDevicesValue', 'GetDevicenValue', 'UpdateDeviceBatSig', 'TimeoutDevice', 'TimeoutDevicesByName', 'UpdateDeviceOptions', 'SecondsSinceLastUpdate', 'DateStringtoDateTime', \
'getConfigItemDB', 'setConfigItemDB', 'eraseConfigItemDB', 'getConfigItemFile', 'setConfigItemFile', \
'getCPUtemperature', \
'FormatWebSocketMessage', 'FormatWebSocketPong', 'FormatWebSocketMessageDisconnect', \
'getDistance', 'Average' \
]
#IMPORTS
import Domoticz
from datetime import datetime
import json
import os
#CONSTANTS
TIMEDOUT = 1 # timeout
MINUTE = 6 # heartbeat is every 10s
DEBUG_OFF = 0 # set debug off
DEBUG_ON = 1 # set debug on
DEBUG_ON_NO_FRAMEWORK= 2 # set debug on but only message by Domoticz.Debug()
#DUMP THE PARAMETER
def DumpConfigToLog(Parameters, Devices):
for parameter in Parameters:
if Parameters[parameter] != '':
Domoticz.Debug('Parameter {}: {}'.format(parameter, Parameters[parameter]))
Domoticz.Debug('Got {} devices:'.format(len(Devices)))
for device in Devices:
Domoticz.Debug('Device {} {}'.format(device, Devices[device]))
Domoticz.Debug('Device {} DeviceID: {}'.format(device, Devices[device].DeviceID))
Domoticz.Debug('Device {} Description: {}'.format(device, Devices[device].Description))
Domoticz.Debug('Device {} LastLevel: {}'.format(device, Devices[device].LastLevel))
#GET NEXT FREE DEVICE
def GetNextFreeUnit(Devices):
# Find the next available unit, starting from 1
unit = 1
while unit in Devices:
unit += 1
Domoticz.Debug('Next free device unit {}'.format(unit))
return unit
#GET DEVICE UNIT BY NAME
def FindUnitFromName(Devices, Parameters, Name, TruncSubName=False):
if TruncSubName:
for unit in [Unit for Unit in Devices if Devices[Unit].Name.startswith('{} - {}'.format(Parameters['Name'], Name))]:
return unit
else:
for unit in [Unit for Unit in Devices if Devices[Unit].Name == '{} - {}'.format(Parameters['Name'], Name)]:
return unit
return False
#GET DEVICE UNIT BY USING THE DESCRIPTION FIELD
def FindUnitFromDescription(Devices, Parameters, Name):
for unit in [Unit for Unit in Devices if GetTagFromDescription(Devices, Unit, 'Name') == '{} - {}'.format(Parameters['Name'], Name)]:
return unit
return False
#ADD TAG TO DESCRIPTION OF A DEVICE
def AddTagToDescription(Devices, Unit, tagName, tag):
descriptions = [] if Devices[Unit].Description == '' else [ Devices[Unit].Description ]
if 'Do not remove: ' in descriptions[0]:
descriptions = descriptions[0].split('; ')
for index, description in enumerate(descriptions):
if description.startswith('Do not remove: '):
Donotremove = json.loads(description[15:])
Donotremove[0][tagName] = tag
descriptions[index] = 'Do not remove: {}'.format(json.dumps(Donotremove))
else:
descriptions += [ 'Do not remove: [{{"{}":"{}"}}]'.format(tagName, tag) ]
Devices[Unit].Update(Description='; '.join(descriptions), nValue=Devices[Unit].nValue, sValue=Devices[Unit].sValue)
#GET TAG FROM DESCRIPTION OF A DEVICE
def GetTagFromDescription(Devices, Unit, tagName):
tag = None
descriptions = Devices[Unit].Description
if 'Do not remove: ' in descriptions:
descriptions = descriptions.split('; ')
for description in descriptions:
if description.startswith('Do not remove: '):
Donotremove = json.loads(description[15:])
if tagName in Donotremove[0]:
tag = Donotremove[0][tagName]
return tag
#UPDATE THE DEVICE
def UpdateDevice(AlwaysUpdate, Devices, Unit, nValue, sValue, **kwargs):
Updated = False
if Unit in Devices:
kwargs = { key : value for key, value in kwargs.items() if value != getattr(Devices[Unit], key, None) }
default_kwargs = { 'TimedOut': 0 }
kwargs = { **default_kwargs, **kwargs }
if AlwaysUpdate or Devices[Unit].nValue != int(nValue) or Devices[Unit].sValue != str(sValue) or Devices[Unit].TimedOut != kwargs['TimedOut'] or len(kwargs)>1:
#Domoticz.Debug('Update {}: nValue {} - sValue {} - Other: {}'.format(Devices[Unit].Name, nValue, sValue, kwargs))
Devices[Unit].Update(nValue=int(nValue), sValue=str(sValue), **kwargs)
Updated = True
else:
if not kwargs.get('TimedOut', 0):
#Touch() updates the LastSeen property with a new date and timestamp!!
#Devices[Unit].Touch()
pass
return Updated
#GET sVALUE OF DEVICE
def GetDevicesValue(Devices, Unit):
if Unit in Devices:
return Devices[Unit].sValue
#GET nVALUE OF DEVICE
def GetDevicenValue(Devices, Unit):
if Unit in Devices:
return int(Devices[Unit].nValue)
#UPDATE THE BATTERY LEVEL AND SIGNAL STRENGTH OF A DEVICE
def UpdateDeviceBatSig(AlwaysUpdate, Devices, Unit, BatteryLevel=255, SignalLevel=12):
if Unit in Devices:
UpdateDevice(AlwaysUpdate, Devices, Unit, Devices[Unit].nValue, Devices[Unit].sValue, BatteryLevel=BatteryLevel, SignalLevel=SignalLevel)
#SET DEVICE ON TIMED-OUT (OR ALL DEVICES)
def TimeoutDevice(Devices, All=True, Unit=0):
if All:
for x in Devices:
if not Devices[x].TimedOut:
UpdateDevice(False, Devices, x, Devices[x].nValue, Devices[x].sValue, TimedOut=TIMEDOUT)
else:
if not Devices[Unit].TimedOut:
UpdateDevice(False, Devices, Unit, Devices[Unit].nValue, Devices[Unit].sValue, TimedOut=TIMEDOUT)
#SET DEVICES ON TIMED-OUT BY USING A TEXT IN THE DEVICE NAME
def TimeoutDevicesByName(Devices, Name):
for x in Devices:
if Name in Devices[x].Name:
if not Devices[x].TimedOut:
UpdateDevice(False, Devices, x, Devices[x].nValue, Devices[x].sValue, TimedOut=TIMEDOUT)
return x
#UPDATE THE OPTIONS OF A DEVICE
def UpdateDeviceOptions(Devices, Unit, Options={}):
if Unit in Devices:
for key, value in Options.items():
if key in Devices[Unit].Options and value != Devices[Unit].Options[key]:
Devices[Unit].Update(nValue=Devices[Unit].nValue, sValue=Devices[Unit].sValue, Options=Options)
Domoticz.Debug('Update options for {}: {}'.format(Devices[Unit].Name, Options))
break
#GET THE SECONDS SINCE THE LASTUPDATE
def SecondsSinceLastUpdate(Devices, Unit):
timeDiff = datetime.now() - DateStringtoDateTime(Devices[Unit].LastUpdate)
return timeDiff.total_seconds()
#CONVERT STRING IN FORMAT %Y-%m-%d %H:%M:%S TO DATETIME OBJECT
def DateStringtoDateTime(DateStr):
# try/catch due to http://bugs.python.org/issue27400
try:
Datim = datetime.strptime(DateStr,'%Y-%m-%d %H:%M:%S')
except TypeError:
import time
Datim = datetime(*(time.strptime(DateStr,'%Y-%m-%d %H:%M:%S')[0:6]))
return Datim
#GET CONFIGURATION VARIALBE (STORED IN DB)
def getConfigItemDB(Key=None, Default={}):
Value = Default
try:
Config = Domoticz.Configuration()
if Key != None:
Value = Config[Key] # only return requested key if there was one
else:
Value = Config # return the whole configuration if no key
except KeyError:
Value = Default
except Exception as inst:
Domoticz.Error('Domoticz.Configuration read failed: {}'.format(inst))
return Value
#SET CONFIGURATION VARIALBE (STORED IN DB)
def setConfigItemDB(Key=None, Value=None):
Config = {}
try:
Config = Domoticz.Configuration()
if Key != None:
Config[Key] = Value
else:
Config = Value # set whole configuration if no key specified
Config = Domoticz.Configuration(Config)
except Exception as inst:
Domoticz.Error('Domoticz.Configuration operation failed: {}'.format(inst))
return Config
#ERASE CONFIGURATION VARIALBE (STORED IN DB)
def eraseConfigItemDB(Key=None):
Config = {}
try:
Config = Domoticz.Configuration()
if Key != None:
del Config[Key]
else:
Config.clear()
Config = Domoticz.Configuration(Config)
except Exception as inst:
Domoticz.Error('Domoticz.Configuration operation failed: {}'.format(inst))
return Config
#CONFIGURATION HELPER: GET ITEM FROM FILE
def getConfigItemFile(Parameters, Key=None, Default={}):
value = Default
try:
with open(Parameters['HomeFolder']+_PLUGIN_PARAMETERS_FILE) as infile:
data = json.load(infile)
if (Key != None):
value = data[Key] # only return requested key if there was one
else:
value = data # return the whole configuration if no key
except KeyError:
value = Default
except Exception as inst:
Domoticz.Error('PluginParamter read file failed: {}.'.format(inst))
return value
#CONFIGURATION HELPER: SET ITEM TO FILE
def setConfigItemFile(Parameters, Key=None, Value=None):
data = {}
try:
if (Key != None):
data[Key] = Value
else:
data = Value # set whole configuration if no key specified
with open(Parameters['HomeFolder']+_PLUGIN_PARAMETERS_FILE, 'w') as outfile:
json.dump(data, outfile)
except Exception as inst:
Domoticz.Error('PluginParamter read file failed: {}.'.format(inst))
return data
#CREATE WEBSOCKET TEXT MESSAGE (https://tools.ietf.org/html/rfc6455)
#CONDITIONS : ONLY TEXT FRAMES, ONE MESSAGE (FIN=1), NO MASKING, SMALLER THAN 127 BYTES PAYLOAD
def FormatWebSocketMessage(PayLoad):
FormattedText = ''
if len(PayLoad) < 127:
FormattedText += '81'
FormattedText += '{:02X}'.format(len(PayLoad))
return bytes.fromhex(FormattedText) + bytes(PayLoad, 'utf-8')
#CREATE WEBSOCKET PONG MESSAGE (https://tools.ietf.org/html/rfc6455)
def FormatWebSocketPong(PayLoad):
FormattedText = ''
if len(PayLoad) < 127:
FormattedText += '8A'
FormattedText += '{:02X}'.format(len(PayLoad))
return bytes.fromhex(FormattedText) + bytes(PayLoad, 'utf-8')
#CREATE WEBSOCKET DISCONNECT MESSAGE (https://tools.ietf.org/html/rfc6455)
def FormatWebSocketMessageDisconnect():
FormattedText = '8800'
return bytes.fromhex(FormattedText)
#GET CPU TEMPERATURE
def getCPUtemperature():
try:
res = os.popen('cat /sys/class/thermal/thermal_zone0/temp').readline()
except:
res = '0'
return round(float(res)/1000,1)
#CALCULATE DISTANCE BASED ON GPS COORDINATES
from math import radians, sin, cos, atan2, sqrt
def getDistance(origin, destination, unit='km'):
radius = 6371 # km
dlat = radians(destination[0]-origin[0])
dlon = radians(destination[1]-origin[1])
a = sin(dlat/2) * sin(dlat/2) + cos(radians(origin[0])) \
* cos(radians(destination[0])) * sin(dlon/2) * sin(dlon/2)
c = 2 * atan2(sqrt(a), sqrt(1-a))
d = radius * c
return d*1000 if unit=='m' else d
#CALCULATE AVERAGE
def Average(mylist):
tot = 0
nbr = 0
for el in mylist:
if type(el) == int or type(el) == float:
tot += el
nbr += 1
if nbr:
return tot/nbr
else:
return None
#CALL DOMOTICZ API
def DomoticzAPI(Parameters, APICall):
from urllib import parse, request
import base64
resultJson = None
url = 'http://{}:{}/json.htm?{}'.format(Parameters['Address'], Parameters['Port'], parse.quote(APICall, safe='&='))
try:
req = request.Request(url)
if Parameters["Username"]:
credentials = ('{}:{}'.format(Parameters['Username'], Parameters['Password']))
encoded_credentials = base64.b64encode(credentials.encode('ascii'))
req.add_header('Authorization', 'Basic {}'.format(encoded_credentials.decode('ascii')))
Domoticz.Debug('Sending DomoticzAPI: {}'.format(url))
response = request.urlopen(req)
Domoticz.Debug('Receiving status code DomoticzAPI: {}'.format(response.status))
if response.status == 200:
resultJson = json.loads(response.read().decode('utf-8'))
if resultJson['status'] != 'OK':
Domoticz.Error("Domoticz API returned an error: status = {}".format(resultJson["status"]))
resultJson = None
else:
Domoticz.Error("Domoticz API: http error = {}".format(response.status))
except:
Domoticz.Error("Error calling '{}'".format(url))
return resultJson