Repository navigation
Expand file tree
/
Copy pathplugin_old.py
More file actions
370 lines (306 loc) · 14.1 KB
/
Copy pathplugin_old.py
File metadata and controls
370 lines (306 loc) · 14.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Basic Python Plugin Example
#
# Author: Jan-Jaap Kostelijk
#
"""
<plugin key="DysonPureLink" name="Dyson Pure Link" author="jan-jaap kostelijk" version="1.1.18" >
<description>
<h2>Dyson Pure Link plugin</h2><br/>
Connects to Dyson Pure Link device, CoolLink 475<br/>
Dikke vette yoyo!
</description>
<params>
<param field="Address" label="IP Address" width="200px" required="true" default="192.168.1.15"/>
<param field="Port" label="Port" width="30px" required="true" default="1883"/>
<param field="Mode1" label="Dyson type (Pure Cool only at this moment)">
<options>
<option label="455" value="455"/>
<option label="465" value="465"/>
<option label="475" value="475" default="true"/>
</options>
</param>
<param field="Mode2" label="Dyson Serial No." default="NN2-EU-JEA3830A" required="true"/>
<param field="Password" label="Dyson Password (see machine)" required="true" password="true"/>
<param field="Mode5" label="Update count (10 sec)" default="3" required="true"/>
<param field="Mode4" label="Debug" width="75px">
<options>
<option label="True" value="Debug" default="true"/>
<option label="False" value="Normal"/>
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import base64, json, hashlib, os, time
import paho.mqtt.client as mqtt
from queue import Queue, Empty
from value_types import CONNECTION_STATE, DISCONNECTION_STATE, FanMode, StandbyMonitoring, ConnectionError, DisconnectionError, SensorsData, StateData
#from dyson_pure_link_device import DysonPureLinkDevice
class DysonPureLink:
#define class variables
enabled = False
IThinkIAmConnected = False
#unit numbers for devices to create
#for Pure Cool models
fanModeUnit = 1
nightModeUnit = 2
fanSpeedUnit = 3
fanOscillationUnit = 4
standbyMonitoringUnit = 5
filterLifeUnit = 6
qualityTargetUnit = 7
tempHumUnit = 8
volatileUnit = 9
particlesUnit = 10
sleepTimeUnit = 11
runCounter = 0
def __init__(self):
self.client = None
self.config = None
self.connected = Queue()
self.disconnected = Queue()
self.state_data_available = Queue()
self.sensor_data_available = Queue()
self.sensor_data = None
self.state_data = None
self._is_connected = None
self.dyson_pure_link = None
def onStart(self):
Domoticz.Log("onStart called")
if Parameters['Mode4'] == 'Debug':
Domoticz.Debugging(1)
DumpConfigToLog()
#PureLink needs polling, get from config
self.runCounter = int(Parameters["Mode5"])
Domoticz.Heartbeat(10)
#check, per device, if it is created. If not,create it
if self.fanModeUnit not in Devices:
Domoticz.Device(Name='Fan mode', Unit=self.fanModeUnit, Type=244, Subtype=62, Switchtype=0).Create()
if self.nightModeUnit not in Devices:
Domoticz.Device(Name='Night mode', Unit=self.nightModeUnit, Type=244, Subtype=62, Switchtype=0).Create()
Options = {"LevelActions" : "|||||||||||",
"LevelNames" : "|Auto|L1|L2|L3|L4|L5|L6|L7|L8|L9|L10",
"LevelOffHidden" : "true",
"SelectorStyle" : "1"}
if self.fanSpeedUnit not in Devices:
Domoticz.Device(Name='Fan speed', Unit=self.fanSpeedUnit, TypeName="Selector Switch", Image=7, Options=Options).Create()
if self.fanOscillationUnit not in Devices:
Domoticz.Device(Name='Oscilation mode', Unit=self.fanOscillationUnit, Type=244, Subtype=62, Switchtype=0).Create()
if self.standbyMonitoringUnit not in Devices:
Domoticz.Device(Name='Standby monitor', Unit=self.standbyMonitoringUnit, Type=244, Subtype=62, Switchtype=0).Create()
if self.filterLifeUnit not in Devices:
Domoticz.Device(Name='Remaining filter life', Unit=self.filterLifeUnit, TypeName="Custom").Create()
if self.qualityTargetUnit not in Devices:
Domoticz.Device(Name='Air quality setpoint', Unit=self.qualityTargetUnit, TypeName="Custom").Create()
if self.tempHumUnit not in Devices:
Domoticz.Device(Name='Temperature and Humidity', Unit=self.tempHumUnit, TypeName="Temp+Hum").Create()
#read out parameters
self.ip_address = Parameters["Address"].replace(" ", "")
self.port_number = int(Parameters["Port"].replace(" ", ""))
self.serial_number = Parameters['Mode2']
self.device_type = Parameters['Mode1']
self.password = Parameters['Password']
# Connect device and print result
Domoticz.Log('Connected: ' + str(self.connect_device()))
def connect_device(self):
"""
Connects to device using provided connection arguments
Returns: True/False depending on the result of connection
"""
Domoticz.Debug("DysonPureLink: connect device")
#Domoticz.Trace(True) #start trace
self.client = mqtt.Client(clean_session=True, protocol=mqtt.MQTTv311, userdata=self)
self.client.username_pw_set(self.serial_number, self._hashed_password())
#connect callbacks to MQTT client iso domoticz since we don't use Domoticz connection object
self.client.on_connect = self.onConnect
self.client.on_disconnect = self.onDisconnect
self.client.on_message = self.onMessage
try:
self.client.connect(self.ip_address, port=self.port_number)
Domoticz.Debug("Connection made, yeaha!")
except ConnectionRefusedError as e:
self.IThinkIAmConnected = False
Domoticz.Error("Connect device: Connection Refused")
#Domoticz.Trace(False) #stop trace
return False
#Domoticz.Trace(False) #stop trace
self.client.loop_start()
#self._is_connected = self.connected.get(timeout=15)
if self._is_connected:
self._request_state()
self.state_data = self.state_data_available.get(timeout=5)
self.sensor_data = self.sensor_data_available.get(timeout=5)
# Return True in case of successful connect and data retrieval
self.IThinkIAmConnected = True
return True
# If any issue occurred return False
self.client = None
self.IThinkIAmConnected = False
return False
@property
def has_valid_data(self):
return self.sensor_data and self.sensor_data.has_data
@property
def device_command(self):
return '{0}/{1}/command'.format(self.device_type, self.serial_number)
@property
def device_status(self):
return '{0}/{1}/status/current'.format(self.device_type, self.serial_number)
def _request_state(self):
"""Publishes request for current state message"""
if self.client:
command = json.dumps({
'msg': 'REQUEST-CURRENT-STATE',
'time': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())})
self.client.publish(self.device_command, command);
def _change_state(self, data):
"""Publishes request for change state message"""
if self.client:
command = json.dumps({
'msg': 'STATE-SET',
'time': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
'mode-reason': 'LAPP',
'data': data
})
self.client.publish(self.device_command, command, 1)
self.state_data = self.state_data_available.get(timeout=5)
def _hashed_password(self):
"""Hash password (found in manual) to a base64 encoded of its shad512 value"""
hash = hashlib.sha512()
hash.update(self.password.encode('utf-8'))
return base64.b64encode(hash.digest()).decode('utf-8')
def get_data(self):
return (self.state_data, self.sensor_data) if self.has_valid_data else tuple()
def request_data(self):
"""send requets for new data to device"""
if self.IThinkIAmConnected:
self._request_state()
self.state_data = self.state_data_available.get(timeout=5)
self.sensor_data = self.sensor_data_available.get(timeout=5)
# Return data in case of successful connect and data retrieval
return (self.state_data, self.sensor_data) if self.has_valid_data else ('noValidData','noValidData')
else:
return ('disconnected','disconnected')
def disconnect_device(self):
"""Disconnects device and return the boolean result"""
Domoticz.Debug("DysonPureLink: disconnect_device")
if self.client:
self.client.loop_stop()
self.client.disconnect()
self.IThinkIAmConnected = False
# Wait until we get on disconnect message
# self._is_connected = not(self.disconnected.get(timeout=5))
# return self._is_connected
return self.disconnected.get(timeout=5)
def onStop(self):
Domoticz.Log("DysonPureLink: onStop called")
# if self.IThinkIAmConnected:
# Domoticz.Debug('onStop: Try to disconnect')
# self.disconnect_device()
# Domoticz.Debug('Disconnected')
@staticmethod
def onConnect(client, userdata, flags, return_code):
"""Static callback to handle on_connect event"""
Domoticz.Log("DysonPureLink: onConnect called")
dummyBool=True
Domoticz.Debug("onConnect: return_code: " + str(return_code))
Domoticz.Debug("onConnect: flags: " + str(flags) + ", self.IThinkIAmConnected: " + str(self.IThinkIAmConnected))
# Connection is successful with return_code: 0
if return_code:
userdata.connected.put_nowait(False)
raise ConnectionError(return_code)
# We subscribe to the status message
client.subscribe(userdata.device_status)
userdata.connected.put_nowait(True)
self.IThinkIAmConnected = True
@staticmethod
def onMessage(client, userdata, message):
"""Static callback to handle incoming messages"""
Domoticz.Log("DysonPureLink: onMessage called")
payload = message.payload.decode("utf-8")
json_message = json.loads(payload)
Domoticz.Debug("spit the json_message:" + json_message)
if StateData.is_state_data(json_message):
userdata.state_data_available.put_nowait(StateData(json_message))
if SensorsData.is_sensors_data(json_message):
userdata.sensor_data_available.put_nowait(SensorsData(json_message))
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("DysonPureLink: onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("DysonPureLink: onNotification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
@staticmethod
def onDisconnect(client, userdata, return_code):
"""Static callback to handle on_disconnect event"""
Domoticz.Debug("onDisconnect: return_code: " + return_code)
Domoticz.Log("DysonPureLink: onDisconnect called")
#self._is_connected = False
if return_code:
raise DisconnectionError(return_code)
userdata.disconnected.put_nowait(True)
def onHeartbeat(self):
Domoticz.Log("DysonPureLink: onHeartbeat called, version: " + Parameters["Version"])
self.runCounter = self.runCounter - 1
if self.runCounter <= 0:
Domoticz.Debug("Poll unit")
self.runCounter = int(Parameters["Mode5"])
if not self.IThinkIAmConnected:
Domoticz.Debug("DysonPureLink: onHeartbeat, try to reconnect")
self.connect_device()
#return True
# Get and print state and sensors data
if self.IThinkIAmConnected:
Domoticz.Debug("unit is connected, lets show some data")
for entry in self.request_data():
Domoticz.Debug(str(entry))
#self.disconnect_device()
else:
Domoticz.Debug("unit not connected")
def onDeviceRemoved(self):
Domoticz.Log("DysonPureLink: onDeviceRemoved called")
global _plugin
_plugin = DysonPureLink()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Connection, Status, Description):
global _plugin
Domoticz.Debug("base plugin onConnect, skipping it")
#_plugin.onConnect(Connection, Status, Description)
def onMessage(Connection, Data):
global _plugin
#_plugin.onMessage(Connection, Data)
Domoticz.Debug("base plugin onMessage, skipping it")
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect(Connection):
global _plugin
#_plugin.onDisconnect(Connection)
Domoticz.Debug("base plugin onDisconnect, skipping it")
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
def onDeviceRemoved():
global _plugin
_plugin.onDeviceRemoved()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return