forked from enesbcs/Shelly_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
691 lines (666 loc) · 26.1 KB
/
plugin.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
"""
<plugin key="ShellyMQTT" name="Shelly MQTT" version="0.3.5">
<description>
Simple plugin to manage Shelly switches through MQTT
<br/>
</description>
<params>
<param field="Address" label="MQTT Server address" width="300px" required="true" default="127.0.0.1"/>
<param field="Port" label="Port" width="300px" required="true" default="1883"/>
<param field="Username" label="Username" width="300px"/>
<param field="Password" label="Password" width="300px" default="" password="true"/>
<param field="Mode1" label="Invert Roller mode globally" width="75px">
<options>
<option label="True" value="1"/>
<option label="False" value="0" default="true" />
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="Verbose" value="Verbose"/>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
errmsg = ""
try:
import Domoticz
except Exception as e:
errmsg += "Domoticz core start error: "+str(e)
try:
import json
except Exception as e:
errmsg += " Json import error: "+str(e)
try:
import time
except Exception as e:
errmsg += " time import error: "+str(e)
try:
import re
except Exception as e:
errmsg += " re import error: "+str(e)
try:
from mqtt import MqttClientSH2
except Exception as e:
errmsg += " MQTT client import error: "+str(e)
class BasePlugin:
mqttClient = None
def __init__(self):
return
def onStart(self):
global errmsg
if errmsg =="":
try:
Domoticz.Heartbeat(10)
self.debugging = Parameters["Mode6"]
if self.debugging == "Verbose":
Domoticz.Debugging(2+4+8+16+64)
if self.debugging == "Debug":
Domoticz.Debugging(2)
self.base_topic = "shellies" # hardwired
self.mqttserveraddress = Parameters["Address"].strip()
self.mqttserverport = Parameters["Port"].strip()
self.mqttClient = MqttClientSH2(self.mqttserveraddress, self.mqttserverport, "", self.onMQTTConnected, self.onMQTTDisconnected, self.onMQTTPublish, self.onMQTTSubscribed)
except Exception as e:
Domoticz.Error("MQTT client start error: "+str(e))
self.mqttClient = None
else:
Domoticz.Error("Your Domoticz Python environment is not functional! "+errmsg)
self.mqttClient = None
def checkDevices(self):
Domoticz.Debug("checkDevices called")
def onStop(self):
Domoticz.Debug("onStop called")
def onCommand(self, Unit, Command, Level, Color): # react to commands arrived from Domoticz
if self.mqttClient is None:
return False
Domoticz.Debug("Command: " + Command + " (" + str(Level) + ") Color:" + Color)
try:
device = Devices[Unit]
devname = device.DeviceID.replace("shellyplug-s","shellyplugs",1) # ugly fix for ShellyPlug-S "-"
device_id = devname.split('-') # get device name from DeviceID field
except Exception as e:
Domoticz.Debug(str(e))
return False
relnum = -1
try:
relnum = int(device_id[2].strip()) # get channel if applicable
except:
relnum = -1
device_id[0] = device_id[0].replace("shellyplugs","shellyplug-s",1) # ugly fix for ShellyPlug-S "-"
if relnum in range(0,4) and len(device_id)==3: # check if is it a normal relay
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/relay/"+device_id[2]+"/command" # reconstrutct necessarry mqtt path
cmd = Command.strip().lower()
Domoticz.Debug(mqttpath+" "+cmd)
if cmd in ["on","off"]: # commands are simply on or off
try:
self.mqttClient.publish(mqttpath, cmd)
if cmd=="off":
device.Update(nValue=int(Level),sValue=str(Command)) # force device update if it is offline
except Exception as e:
Domoticz.Debug(str(e))
# otherwise check if it is a roller shutter
elif relnum in range(0,4) and len(device_id)==4 and device_id[len(device_id)-1]=="roller":
cmd = Command.strip().lower()
scmd = "" # Translate Domoticz command to Shelly command
if str(Parameters["Mode1"])!="1": # check if global inversion requested
if cmd == "stop":
scmd = "stop"
elif cmd == "on":
scmd = "close"
elif cmd == "off":
scmd = "open"
else:
if cmd == "stop":
scmd = "stop"
elif cmd == "on":
scmd = "open"
elif cmd == "off":
scmd = "close"
if scmd != "":
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/roller/"+device_id[2]+"/command"
try:
self.mqttClient.publish(mqttpath, scmd)
except Exception as e:
Domoticz.Debug(str(e))
# support for v1.4 Percentage poisitioning
elif relnum in range(0,4) and len(device_id)==4 and device_id[len(device_id)-1]=="pos":
cmnd = str(Command).strip().lower()
if (cmnd=="set level"): # percentage requested
pos = str(100-Level).strip().lower()
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/roller/"+device_id[2]+"/command/pos"
Domoticz.Debug(mqttpath+" "+str(Command)+" "+str(Level))
try:
self.mqttClient.publish(mqttpath, pos)
except Exception as e:
Domoticz.Debug(str(e))
else: # command arrived
scmd = "" # Translate Domoticz command to Shelly command
if str(Parameters["Mode1"])!="1": # check if global inversion requested
if cmnd == "on":
scmd = "close"
elif cmnd == "off":
scmd = "open"
else:
if cmnd == "on":
scmd = "open"
elif cmnd == "off":
scmd = "close"
if scmd != "":
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/roller/"+device_id[2]+"/command"
try:
self.mqttClient.publish(mqttpath, scmd)
except Exception as e:
Domoticz.Debug(str(e))
# RGB device
elif relnum in range(0,3) and len(device_id)==4 and device_id[len(device_id)-1] in ["rgb","w"]:
if (Command == "Set Level"):
mqttpath = ""
if int(Level)>0:
amode = "on"
else:
amode = "off"
if device_id[3]=="rgb":
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/color/"+device_id[2]+"/set"
scmd = '{"turn":"'+amode+'","mode":"color","gain":'+str(Level)+'}'
else:
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/white/"+device_id[2]+"/set"
scmd = '{"turn":"'+amode+'","brightness":'+str(Level)+'}'
Domoticz.Debug('RGB Level:' + scmd)
if mqttpath:
try:
self.mqttClient.publish(mqttpath, scmd)
except Exception as e:
Domoticz.Debug(str(e))
elif (Command == "Set Color"):
try:
color = json.loads(Color)
except Exception as e:
Domoticz.Debug(str(e))
if len(color)>0:
if device_id[3]=="rgb":
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/color/"+device_id[2]+"/set"
scmd = '{"turn":"on","mode":"color","red":'+str(color["r"])+',"green":'+str(color["g"])+',"blue":'+str(color["b"]) +',"white":'+str(color["cw"])+'}'
Domoticz.Debug('RGB Color:' + scmd)
try:
self.mqttClient.publish(mqttpath, scmd)
except Exception as e:
Domoticz.Debug(str(e))
else:
if device_id[3]=="rgb":
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/color/"+device_id[2]+"/set"
else:
mqttpath = self.base_topic+"/"+device_id[0]+"-"+device_id[1]+"/white/"+device_id[2]+"/set"
cmd = Command.strip().lower()
if cmd in ["on","off"]: # commands are simply on or off
scmd = '{"turn":"'+str(cmd)+'"}'
try:
self.mqttClient.publish(mqttpath, scmd)
if cmd=="off":
device.Update(nValue=int(Level),sValue=str(Command)) # force device update if it is offline
except Exception as e:
Domoticz.Debug(str(e))
def onConnect(self, Connection, Status, Description):
if self.mqttClient is not None:
self.mqttClient.onConnect(Connection, Status, Description)
def onDisconnect(self, Connection):
if self.mqttClient is not None:
self.mqttClient.onDisconnect(Connection)
def onMessage(self, Connection, Data):
if self.mqttClient is not None:
self.mqttClient.onMessage(Connection, Data)
def onHeartbeat(self):
Domoticz.Debug("Heartbeating...")
if self.mqttClient is not None:
try:
# Reconnect if connection has dropped
if (self.mqttClient._connection is None) or (not self.mqttClient.isConnected):
Domoticz.Debug("Reconnecting")
self.mqttClient._open()
else:
self.mqttClient.ping()
except Exception as e:
Domoticz.Error(str(e))
def onMQTTConnected(self):
if self.mqttClient is not None:
self.mqttClient.subscribe([self.base_topic + '/#'])
def onMQTTDisconnected(self):
Domoticz.Debug("onMQTTDisconnected")
def onMQTTSubscribed(self):
Domoticz.Debug("onMQTTSubscribed")
def onMQTTPublish(self, topic, message): # process incoming MQTT statuses
if "/announce" in topic: # announce did not contain any information for us
return False
try:
topic = str(topic)
message = str(message)
except:
Domoticz.Debug("MQTT message is not a valid string!") #if message is not a real string, drop it
return False
Domoticz.Debug("MQTT message: " + topic + " " + str(message))
mqttpath = topic.split('/')
if (mqttpath[0] == self.base_topic):
# RELAY type, not command->process
if (len(mqttpath)>3) and (mqttpath[2] == "relay") and ("/command" not in topic):
unitname = mqttpath[1]+"-"+mqttpath[3]
unitname = unitname.strip()
devtype = 1
funcid = -1
try:
funcid = int(mqttpath[3].strip())
devtype=0
except:
devtype = 1
if len(mqttpath)==5 and devtype==0:
devtype = 2
subval=""
if devtype==1:
subval = mqttpath[3].strip()
elif devtype==2:
subval = mqttpath[4].strip()
if subval=="power" or subval=="energy":
if funcid in [0,1,2,3]:
unitname=mqttpath[1]+"-"+str(funcid)+"-energy" # fix 2.5 and 4pro support
else:
unitname=mqttpath[1]+"-energy" # shelly2
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit<0: # if device does not exists in Domoticz, than create it
try:
iUnit = 0
for x in range(1,256):
if x not in Devices:
iUnit=x
break
if iUnit==0:
iUnit=len(Devices)+1
if devtype==0:
Domoticz.Device(Name=unitname, Unit=iUnit,TypeName="Switch",Used=1,DeviceID=unitname).Create()
else:
if subval=="energy" or subval=="power":
Domoticz.Device(Name=unitname, Unit=iUnit,Type=243,Subtype=29,Used=1,DeviceID=unitname).Create()
except Exception as e:
Domoticz.Debug(str(e))
return False
if devtype==0:
try:
scmd = str(message).strip().lower()
if (str(Devices[iUnit].sValue).lower() != scmd):
if (scmd == "on"): # set device status if needed
Devices[iUnit].Update(nValue=1,sValue="On")
else:
Devices[iUnit].Update(nValue=0,sValue="Off")
except Exception as e:
Domoticz.Debug(str(e))
return False
else:
try:
curval = Devices[iUnit].sValue
prevdata = curval.split(";")
except:
prevdata = []
if len(prevdata)<2:
prevdata.append(0)
prevdata.append(0)
try:
mval = float(str(message).strip())
except:
mval = str(message).strip()
sval = ""
if subval=="power":
sval = str(mval)+";"+str(prevdata[1])
elif subval=="energy":
try:
mval2 = (mval/100) # 10*Wh?
except:
mval2 = str(mval)
sval = str(prevdata[0])+";"+str(mval2)
try:
Devices[iUnit].Update(nValue=0,sValue=str(sval))
except Exception as e:
Domoticz.Debug(str(e))
return True
# ROLLER type, not command->process
elif (len(mqttpath)>3) and (mqttpath[2] == "roller") and ("/command" not in topic):
if mqttpath[len(mqttpath)-1]=="pos":
unitname = mqttpath[1]+"-"+mqttpath[3]+"-pos"
else:
unitname = mqttpath[1]+"-"+mqttpath[3]+"-roller"
unitname = unitname.strip()
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit<0: # if device does not exists in Domoticz, than create it
try:
iUnit = 0
for x in range(1,256):
if x not in Devices:
iUnit=x
break
if iUnit==0:
iUnit=len(Devices)+1
if "-pos" in unitname:
Domoticz.Device(Name=unitname, Unit=iUnit,Type=244, Subtype=62, Switchtype=13,Used=1,DeviceID=unitname).Create() # create Blinds Percentage
else:
Domoticz.Device(Name=unitname, Unit=iUnit,Type=244, Subtype=62, Switchtype=15,Used=1,DeviceID=unitname).Create() # create Venetian Blinds EU type
except Exception as e:
Domoticz.Debug(str(e))
return False
if "-pos" in unitname:
try:
pval = 100-int(str(message).strip())
nval = 0
if pval>0 and pval<100:
nval = 2
if pval>99:
nval = 1
Devices[iUnit].Update(nValue=int(nval),sValue=str(pval))
except:
Domoticz.Debug("MQTT message error " + str(topic) + ":"+ str(message))
else:
try:
bcmd = str(message).strip().lower()
if bcmd == "stop" and str(Devices[iUnit].sValue).lower() !="stop":
Devices[iUnit].Update(nValue=17,sValue="Stop") # stop
return True
elif bcmd == "open" and str(Devices[iUnit].sValue).lower() !="off":
Devices[iUnit].Update(nValue=0,sValue="Off") # open
return True
elif bcmd == "close" and str(Devices[iUnit].sValue).lower() !="on":
Devices[iUnit].Update(nValue=1,sValue="On") # close
return True
except Exception as e:
Domoticz.Debug(str(e))
return False
# INPUT type, not command->process
elif (len(mqttpath)>3) and (mqttpath[2] == "input") and (mqttpath[len(mqttpath)-1]!="command"):
unitname = mqttpath[1]+"-"+mqttpath[3]+"-input"
unitname = unitname.strip()
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit<0: # if device does not exists in Domoticz, than create it
try:
iUnit = 0
for x in range(1,256):
if x not in Devices:
iUnit=x
break
if iUnit==0:
iUnit=len(Devices)+1
Domoticz.Device(Name=unitname+" BUTTON", Unit=iUnit,TypeName="Switch",Used=0,DeviceID=unitname).Create()
except Exception as e:
Domoticz.Debug(str(e))
return False
try:
if str(message).lower=="on" or str(message)=="1":
scmd = "on"
else:
scmd = "off"
if (str(Devices[iUnit].sValue).lower() != scmd):
if (scmd == "on"): # set device status if needed
Devices[iUnit].Update(nValue=1,sValue="On")
else:
Devices[iUnit].Update(nValue=0,sValue="Off")
except Exception as e:
Domoticz.Debug(str(e))
return False
return True
# LONGPUSH type, not command->process
elif (len(mqttpath)>3) and (mqttpath[2] == "longpush") and (mqttpath[len(mqttpath)-1]!="command"):
unitname = mqttpath[1]+"-"+mqttpath[3]+"-lpush"
unitname = unitname.strip()
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit<0: # if device does not exists in Domoticz, than create it
try:
iUnit = 0
for x in range(1,256):
if x not in Devices:
iUnit=x
break
if iUnit==0:
iUnit=len(Devices)+1
Domoticz.Device(Name=unitname+" LONGPUSH", Unit=iUnit,TypeName="Switch",Used=0,DeviceID=unitname).Create()
except Exception as e:
Domoticz.Debug(str(e))
return False
try:
if str(message).lower=="on" or str(message)=="1":
scmd = "on"
else:
scmd = "off"
if (str(Devices[iUnit].sValue).lower() != scmd):
if (scmd == "on"): # set device status if needed
Devices[iUnit].Update(nValue=1,sValue="On")
else:
Devices[iUnit].Update(nValue=0,sValue="Off")
except Exception as e:
Domoticz.Debug(str(e))
return False
return True
# SENSOR type, not command->process ShellySense and ShellyHT
elif (len(mqttpath)>3) and (mqttpath[2] == "sensor") and (mqttpath[3] in ['temperature','humidity','battery']) and (("shellysense" in mqttpath[1]) or ("shellyht" in mqttpath[1])):
unitname = mqttpath[1]+"-sensor"
unitname = unitname.strip()
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit<0: # if device does not exists in Domoticz, than create it
try:
iUnit = 0
for x in range(1,256):
if x not in Devices:
iUnit=x
break
if iUnit==0:
iUnit=len(Devices)+1
Domoticz.Device(Name=unitname, Unit=iUnit, TypeName="Temp+Hum",Used=1,DeviceID=unitname).Create() # create Temp+Hum Type=82
except Exception as e:
Domoticz.Debug(str(e))
return False
stype = mqttpath[3].strip().lower()
try:
curval = Devices[iUnit].sValue
except:
curval = 0
try:
mval = float(message)
except:
mval = str(message).strip()
if stype=="battery":
try:
Devices[iUnit].Update(nValue=0,sValue=str(curval),BatteryLevel=int(mval))
except Exception as e:
Domoticz.Debug(str(e))
elif stype=="temperature":
try:
env = curval.split(";")
except:
env = [0,0]
if len(env)<3:
env.append(0)
env.append(0)
env.append(0)
sval = str(mval)+";"+str(env[1])+";"+str(env[2])
try:
Devices[iUnit].Update(nValue=0,sValue=str(sval))
except Exception as e:
Domoticz.Debug(str(e))
elif stype=="humidity":
hstat = 0
try:
env = curval.split(";")
except:
env = [0,0]
if len(env)<1:
env.append(0)
if int(mval)>= 50 and int(mval)<=70:
hstat = 1
elif int(mval)<40:
hstat = 2
elif int(mval)>70:
hstat = 3
sval = str(env[0]) + ";"+ str(mval)+";"+str(hstat)
try:
Devices[iUnit].Update(nValue=0,sValue=str(sval))
except Exception as e:
Domoticz.Debug(str(e))
# SENSOR type, not command->process - device inside temperature!
elif (len(mqttpath)==3) and (mqttpath[2] == "temperature"):
unitname = mqttpath[1]+"-temp"
unitname = unitname.strip()
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit<0: # if device does not exists in Domoticz, than create it
try:
iUnit = 0
for x in range(1,256):
if x not in Devices:
iUnit=x
break
if iUnit==0:
iUnit=len(Devices)+1
Domoticz.Device(Name=unitname, Unit=iUnit, TypeName="Temperature",Used=1,DeviceID=unitname).Create()
except Exception as e:
Domoticz.Debug(str(e))
return False
try:
mval = float(message)
except:
mval = str(message).strip()
try:
Devices[iUnit].Update(nValue=0,sValue=str(mval))
except Exception as e:
Domoticz.Debug(str(e))
# RGB type, not command->process
elif (len(mqttpath)>3) and ((mqttpath[2] == "color") or (mqttpath[2] == "white")) and ("/command" not in topic) and ("/set" not in topic):
unitname = mqttpath[1]+"-"+mqttpath[3]
if (mqttpath[2] == "white"):
unitname = unitname+"-w"
else:
unitname = unitname+"-rgb"
unitname = unitname.strip()
iUnit = -1
for Device in Devices:
try:
if (Devices[Device].DeviceID.strip() == unitname):
iUnit = Device
break
except:
pass
if iUnit<0: # if device does not exists in Domoticz, than create it
try:
iUnit = 0
for x in range(1,256):
if x not in Devices:
iUnit=x
break
if iUnit==0:
iUnit=len(Devices)+1
if (mqttpath[2] == "white"):
Domoticz.Device(Name=unitname, Unit=iUnit,Type=241, Subtype=3, Switchtype=7, Used=1,DeviceID=unitname).Create() # create Color White device
else:
Domoticz.Device(Name=unitname, Unit=iUnit,Type=241, Subtype=6, Switchtype=7, Used=1,DeviceID=unitname).Create() # create RGBZW device
except Exception as e:
Domoticz.Debug(str(e))
return False
tmsg = str(message).strip()
if "{" in tmsg:
tmsg = tmsg.replace("'",'"').lower() # OMG replace single quotes and non-standard upper case letters
try:
jmsg = json.loads(tmsg)
except Exception as e:
Domoticz.Debug(str(e))
jmsg = []
if jmsg:
status = 0
if "ison" in jmsg:
if str(jmsg["ison"])=="on" or str(jmsg["ison"])=="1" or jmsg["ison"]==True:
status = 1
elif "turn" in jmsg:
if jmsg["turn"]=="on" or jmsg["turn"]=="1" or jmsg["turn"]==True:
status = 1
if "red" in jmsg: # rgbw
color = {}
color["m"] = 4
color["t"] = 0
color["ww"] = 0
color["r"] = int(jmsg["red"])
color["g"] = int(jmsg["green"])
color["b"] = int(jmsg["blue"])
color["cw"] = int(jmsg["white"])
dimmer = str(jmsg["gain"])
if (Devices[iUnit].nValue != status or Devices[iUnit].sValue != dimmer or json.loads(Devices[iUnit].Color) != color):
jColor = json.dumps(color)
Domoticz.Debug('Updating device #' + str(Devices[iUnit].ID))
Domoticz.Debug('nValue: ' + str(Devices[iUnit].nValue) + ' -> ' + str(status))
Domoticz.Debug('sValue: ' + Devices[iUnit].sValue + ' -> ' + dimmer)
Domoticz.Debug('Color: ' + Devices[iUnit].Color + ' -> ' + jColor)
Devices[iUnit].Update(nValue=status, sValue=dimmer, Color=jColor)
else: # white
dimmer = str(jmsg["brightness"])
Domoticz.Debug('Updating device #' + str(Devices[iUnit].ID))
Domoticz.Debug('nValue: ' + str(Devices[iUnit].nValue) + ' -> ' + str(status))
Domoticz.Debug('sValue: ' + Devices[iUnit].sValue + ' -> ' + dimmer)
Devices[iUnit].Update(nValue=status, sValue=dimmer)
return True
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Connection, Status, Description):
global _plugin
_plugin.onConnect(Connection, Status, Description)
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Color):
global _plugin
_plugin.onCommand(Unit, Command, Level, Color)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()