-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
plugin.py
1559 lines (1301 loc) · 63.9 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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# deCONZ Bridge
#
# Author: Smanar
#
"""
<plugin key="deCONZ" name="deCONZ plugin" author="Smanar" version="1.0.32" wikilink="https://github.com/Smanar/Domoticz-deCONZ" externallink="https://phoscon.de/en/conbee2">
<description>
<br/><br/>
<h2>deCONZ Bridge</h2><br/>
It use the deCONZ rest api to make a bridge beetween your zigbee network and Domoticz (Using Conbee or Raspbee)
<br/><br/>
<h3>Remark</h3>
<ul style="list-style-type:square">
<li>You can use the file API_KEY.py if you have problems to get your API Key or your Websocket Port.</li>
<li>You can find updated files for deCONZ on their github : https://github.com/dresden-elektronik/deconz-rest-plugin.</li>
<li>If you want the plugin works without connection, use as IP 127.0.0.1 (if deCONZ and domoticz are on same machine).</li>
<li>If you are running the plugin for the first time, better to enable debug log (Take Debug info Only).</li>
<li>You can find a front-end on the Domoticz menu "Custom" > "Deconz", you can use it to get the API key, configure sensors or alarm system.</li>
</ul>
<h3>Supported Devices</h3>
<ul style="list-style-type:square">
<li>https://github.com/dresden-elektronik/deconz-rest-plugin/wiki/Supported-Devices</li>
</ul>
<h3>Configuration</h3>
Gateway configuration
</description>
<params>
<param field="Address" label="deCONZ IP" width="150px" required="true" default="127.0.0.1"/>
<param field="Port" label="Port" width="150px" required="true" default="80"/>
<param field="Mode2" label="API KEY" width="100px" required="true" default="1234567890" />
<param field="Mode4" label="Specials settings, separated by comma" width="200px" required="false" default="" />
<param field="Mode3" label="Debug" width="150px">
<options>
<option label="None" value="0" default="true" />
<option label="Debug info Only" value="2"/>
<option label="Basic Debugging" value="62"/>
<option label="Basic+Messages" value="126"/>
<option label="Connections Only" value="16"/>
<option label="Connections+Python" value="18"/>
<option label="Connections+Queue" value="144"/>
<option label="All" value="-1"/>
</options>
</param>
</params>
</plugin>
"""
# All imports
import Domoticz
import urllib, time
try:
import simplejson as json
except ImportError:
import json
REQUESTPRESENT = True
try:
import requests
except:
REQUESTPRESENT = False
from fonctions import rgb_to_xy, rgb_to_hsv, xy_to_rgb
from fonctions import Count_Type, ProcessAllState, ProcessAllConfig, First_Json, JSON_Repair, get_JSON_payload
from fonctions import ButtonconvertionXCUBE, ButtonconvertionXCUBE_R, ButtonconvertionTradfriRemote, ButtonconvertionTradfriSwitch
from fonctions import ButtonconvertionXCUBET1, ButtonconvertionXCUBET1_R
from fonctions import ButtonConvertion, VibrationSensorConvertion
from fonctions import installFE, uninstallFE
from widget import Createdatawidget
#Better to use 'localhost' ?
DOMOTICZ_IP = '127.0.0.1'
LIGHTLOG = True #To disable some activation, log will be lighter, but less informations.
SETTODEFAULT = False #To set device in default state after a rejoin
ENABLEMORESENSOR = False #Create more sensors, like tension and current
ENABLEBATTERYWIDGET = False #Create 1 more widget by battery devices
FullSpecialDeviceList = ["orientation", "heatsetpoint", "mode", "preset", "lock", "current", "voltage"]
#https://github.com/febalci/DomoticzEarthquake/blob/master/plugin.py
#https://stackoverflow.com/questions/32436864/raw-post-request-with-json-in-body
# option list
#1 = Power+Consumption
#2 = Consumption_2
#3 = pm2_5
class BasePlugin:
#enabled = False
def __init__(self):
self.Devices = {} # id, type, state (banned/missing/working) , model, option
self.NeedToReset = []
self.Ready = False
self.Buffer_Command = []
self.Buffer_Time = ''
self.WebSocket = None
self.WebsoketBuffer = ''
self.Banned_Devices = []
self.BufferReceive = ''
self.DeconzInfoUnit = False
self.IDGateway = -1
self.INIT_STEP = ['config', 'lights', 'sensors', 'groups', 'alarmsystems']
self.SpecialDeviceList = ["orientation", "heatsetpoint", "mode", "preset", "lock"]
return
def onStart(self):
Domoticz.Debug("onStart called")
#CreateDevice('sirene test','En test','Warning device')
#Check Domoticz IP
if Parameters["Address"] != '127.0.0.1' and Parameters["Address"] != 'localhost':
global DOMOTICZ_IP
DOMOTICZ_IP = get_ip()
Domoticz.Log("You are not using 127.0.0.1 as IP, so I suppose deCONZ and Domoticz aren't on same machine")
Domoticz.Log("Taking " + DOMOTICZ_IP + " as Domoticz IP")
if DOMOTICZ_IP == Parameters["Address"]:
Domoticz.Status("You seem to use the IP for deCONZ and Domoticz. Why don't you use 127.0.0.1 as IP?")
else:
Domoticz.Log("Domoticz and deCONZ are installed on the same machine.")
if Parameters["Mode3"] != "0":
Domoticz.Debugging(int(Parameters["Mode3"]))
#DumpConfigToLog()
#Create info widget
self.DeconzInfoUnit = GetDomoDeviceInfo("DeconzInfo")
if not self.DeconzInfoUnit:
Domoticz.Log("Creation of Info Widget.")
Domoticz.Device(Name="Status", DeviceID="DeconzInfo", Unit=FreeUnit(), TypeName='Alert').Create()
if "ENABLEMORESENSOR" in Parameters["Mode4"]:
Domoticz.Status("Enabling special setting ENABLEMORESENSOR")
global ENABLEMORESENSOR
ENABLEMORESENSOR = True
self.SpecialDeviceList = self.SpecialDeviceList + ["current", "voltage"]
if "ENABLEBATTERYWIDGET" in Parameters["Mode4"]:
Domoticz.Status("Enabling special setting ENABLEBATTERYWIDGET")
global ENABLEBATTERYWIDGET
ENABLEBATTERYWIDGET = True
#Custom icon files for battery level
#https://github.com/999LV/BatteryLevel
icons = {"batterylevelfull": "icons/batterylevelfull_icons.zip",
"batterylevelok": "icons/batterylevelok_icons.zip",
"batterylevellow": "icons/batterylevellow_icons.zip",
"batterylevelempty": "icons/batterylevelempty_icons.zip"}
# load custom battery images
for key, value in icons.items():
if key not in Images:
Domoticz.Image(value).Create()
Domoticz.Status("Added icon: " + key + " from file " + value)
Domoticz.Status("Number of icons loaded = " + str(len(Images)))
for image in Images:
Domoticz.Log("Icon Used by the plugin : " + str(Images[image].ID) + ">" + Images[image].Name)
#Read banned devices
try:
with open(Parameters["HomeFolder"]+"banned_devices.txt", 'r') as myPluginConfFile:
for line in myPluginConfFile:
if not line.startswith('#'):
Domoticz.Log("Adding banned device : " + line.strip())
self.Banned_Devices.append(line.strip())
except (IOError,FileNotFoundError):
#File not exist create it with example
Domoticz.Status("Creating banned device file")
with open(Parameters["HomeFolder"]+"banned_devices.txt", 'w') as myPluginConfFile:
myPluginConfFile.write("#Alarm on Detector\n00:15:8d:00:02:36:c2:3f-01-0500")
myPluginConfFile.close()
#check and load Front end
installFE(Parameters['HomeFolder'], Parameters['StartupFolder'])
#Read and Set config
#json = '{"websocketnotifyall":true}'
#url = '/api/' + Parameters["Mode2"] + '/config/'
#self.SendCommand(url,json)
# Disabled, not working for selector ...
#check for new icons
#if 'bulbs_group' not in Images:
# try:
# Domoticz.Image('icons/bulbs_group.zip').Create()
# except:
# Domoticz.Error("Can't create new icons")
def onStop(self):
Domoticz.Debug("onStop called")
if self.WebSocket:
self.WebSocket.Disconnect()
def onConnect(self, Connection, Status, Description):
Domoticz.Debug("onConnect called")
if Connection.Name == 'deCONZ_WebSocket':
if (Status != 0):
Domoticz.Error("WebSocket connection error : " + str(Connection))
Domoticz.Error("Status : " + str(Status) + " Description : " + str(Description) )
return
Domoticz.Status("Launching WebSocket on port " + str(Connection.Port) )
#Need to Add Sec-Websocket-Protocol : domoticz ????
#Boring error > Socket Shutdown Error: 9, Bad file descriptor
wsHeader = "GET / HTTP/1.1\r\n" \
"Host: "+ Parameters["Address"] + ':' + str(Connection.Port) + "\r\n" \
"User-Agent: Domoticz/1.0\r\n" \
"Sec-WebSocket-Version: 13\r\n" \
"Origin: http://" + DOMOTICZ_IP + "\r\n" \
"Sec-WebSocket-Key: qqMLBxyyjz9Tog1bll7K6A==\r\n" \
"Connection: keep-alive, Upgrade\r\n" \
"Upgrade: websocket\r\n\r\n"
#"Accept: Content-Type: text/html; charset=UTF-8\r\n" \
#"Pragma: no-cache\r\n" \
#"Cache-Control: no-cache\r\n" \
#"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" \
self.WebSocket.Send(wsHeader)
else:
Domoticz.Error("Unknown Connection: " + str(Connection))
Domoticz.Error("Status : " + str(Status) + " Description : " + str(Description) )
return
def onMessage(self, Connection, Data):
Domoticz.Debug("onMessage called")
_Data = []
if self.WebsoketBuffer:
Data = self.WebsoketBuffer + Data
self.WebsoketBuffer = ''
#Domoticz.Log("Data : " + str(Data))
#Domoticz.Log("Connexion : " + str(Connection))
#Domoticz.Log("Byte needed : " + str(Connection.BytesTransferred()) + "ATM : " + str(len(Data)))
#The max is 4096 so if the data size excess 4096 byte it will be cut
#Websocket data ?
if (Connection.Name == 'deCONZ_WebSocket'):
#Data = b'\x81W{"e":"changed","id":"7","r":"groups","state":{"all_on":true,"any_on":true}}'
#Data = b'\x81W{"e":"changed","id":"5","r":"groups","state":{"all_on":true,"any_on"'
if Data.startswith(b'\x81'):
while len(Data) > 0:
try:
payload, extra_data = get_JSON_payload(Data)
except:
if (Data[0:1] == b'\x81') and (len(str(Data)) < 1000) :
self.WebsoketBuffer = Data
Domoticz.Log("Incomplete JSON keep it for later : " + str(self.WebsoketBuffer) )
else:
Domoticz.Error("Malformed JSON response, can't repair : " + str(Data) )
break
_Data.append(payload)
Data = extra_data
for js in _Data:
self.WebSocketConnexion(js)
else:
Domoticz.Log("WebSocket Handshake : " + str(Data.decode("utf-8", "ignore").replace('\n','***')) )
else:
Domoticz.Log("Unknown Connection" + str(Connection))
Domoticz.Log("Data : " + str(Data))
return
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level) + ", Hue: " + str(Hue))
if not self.Ready == True:
Domoticz.Error("deCONZ not ready")
return
_type,deCONZ_ID = self.GetDevicedeCONZ(Devices[Unit].DeviceID)
if not deCONZ_ID:
# Not in deconz but Alarm System ?
if Devices[Unit].DeviceID == 'Alarm_System_1':
if Devices[Unit].Description:
url = '/api/' + Parameters["Mode2"] + '/alarmsystems/1/' + ['disarm','arm_away','arm_stay','arm_night'][int(Level/10)]
self.SendCommand(url,{'code0':str(Devices[Unit].Description)})
else:
Domoticz.Error("Missing code0 in alarm system widget description")
else:
Domoticz.Error("Device not ready : " + str(Unit) )
return
if _type == 'sensors':
Domoticz.Error("This device doesn't support action")
return
IEEE = Devices[Unit].DeviceID
#Get device type
device_type = self.Devices[IEEE].get('model','Unknow')
_json = {}
#on/off
if Command == 'On':
if device_type == 'Warning device':
_json['alert'] = 'lselect'
#Force Update using domoticz, because some device don't have return
UpdateDeviceProc({'nValue': 1, 'sValue': 'On'}, Unit)
elif device_type.startswith('Window covering'):
_json['open'] = False
else:
_json['on'] = True
if Level:
_json['bri'] = round(Level*254/100)
if _type == 'config':
if Devices[Unit].DeviceID.endswith('_lock'):
_json = {'lock':True}
elif Command == 'Off':
if device_type == 'Warning device':
_json['alert'] = 'none'
#Force Update using domoticz, because some device don't have return
UpdateDeviceProc({'nValue': 0, 'sValue': 'Off'}, Unit)
elif device_type.startswith('Window covering'):
_json['open'] = True
else:
_json['on'] = False
if _type == 'config':
if Devices[Unit].DeviceID.endswith('_mode'):
_json = {'mode':'off'}
elif Devices[Unit].DeviceID.endswith('_lock'):
_json = {'lock':False}
#level
if Command == 'Set Level':
if device_type.startswith('Window covering'):
_json['lift'] = Level
else:
#To prevent bug
_json['on'] = True
_json['bri'] = round(Level*254/100)
#Special situation
if _type == 'config':
_json.clear()
#Ventilator
if device_type == 'Purifier_Mode':
v = ["off","auto","speed_1","speed_2","speed_3","speed_4","speed_5"][int(Level/10)]
_json['mode'] = v
#Thermostat
elif Devices[Unit].DeviceID.endswith('_heatsetpoint'):
_json['heatsetpoint'] = int(Level * 100)
dummy,deCONZ_ID_2 = self.GetDevicedeCONZ(Devices[Unit].DeviceID.replace('_heatsetpoint','_mode'))
if deCONZ_ID_2 and ("auto" in Devices[Unit].Options.get('LevelNames','')):
_json['mode'] = "auto"
elif Devices[Unit].DeviceID.endswith('_preset'):
v = ["off","holiday","auto","manual","comfort","eco","boost","complex","program"][int(Level/10)]
_json['preset'] = v
elif Devices[Unit].DeviceID.endswith('_mode'):
if Level == 0:
_json['mode'] = "off"
if Level == 10:
_json['mode'] = "heat"
if Level == 20:
_json['mode'] = "auto"
#retreive previous value from domoticz
IEEE2 = Devices[Unit].DeviceID.replace('_mode','_heatsetpoint')
Hp = int(100*float(Devices[GetDomoDeviceInfo(IEEE2)].sValue))
_json['heatsetpoint'] = Hp
#Chritsmas tree
elif Devices[Unit].DeviceID.endswith('_effect'):
v = ["none","steady","snow","rainbow","snake","twinkle","fireworks","flag","waves","updown","vintage","fading","collide","strobe","sparkles","carnival","glow"][int(Level/10) - 1]
_json['effect'] = v
UpdateDeviceProc({'nValue': Level, 'sValue': str(Level)}, Unit)
#Set special options
try :
for o in Devices[Unit].Description.split("\n"):
o2 = o.split("=")
if o2[0] == 'effectSpeed':
_json['effectSpeed'] = int(o2[1])
if o2[0] == 'effectColours':
_json['effectColours'] = json.loads(o2[1])
except:
Domoticz.Log("No special effect options")
# Get light device
_type,deCONZ_ID = self.GetDevicedeCONZ(Devices[Unit].DeviceID.replace("_effect",""))
#Special code to force devive update for group
elif _type == 'groups':
UpdateDeviceProc({'nValue': 1, 'sValue': str(Level)}, Unit)
#Special devices
if device_type == 'Warning device':
#Heyman Siren
_json.clear()
if Level == 10:
_json['alert'] = "select"
elif Level == 20:
_json['alert'] = "lselect"
elif Level == 30:
_json['alert'] = "blink"
else:
_json['alert'] = "none"
#Force Update using domoticz, because some device don't have return
UpdateDeviceProc({'nValue': Level, 'sValue': str(Level)}, Unit)
#Pach for special device
if 'NO DIMMER' in Devices[Unit].Description and 'bri' in _json:
_json.pop('bri')
_json['transitiontime'] = 0
#covering
if Command == 'Open':
_json['open'] = False
elif Command == 'Close':
_json['open'] = True
elif Command == 'Stop':
_json = {'stop':True}
#color
if Command == 'Set Color':
#To prevent bug
_json['on'] = True
Hue_List = json.loads(Hue)
#ColorModeNone = 0 // Illegal
#ColorModeNone = 1 // White. Valid fields: none
if Hue_List['m'] == 1:
ww = int(Hue_List['ww']) # Can be used as level for monochrome white
#TODO : Jamais vu un device avec ca encore
Domoticz.Debug("Not implemented device color 1")
#ColorModeTemp = 2 // White with color temperature. Valid fields: t
if Hue_List['m'] == 2:
#Value is in mireds (not kelvin)
#Correct values are from 153 (6500K) up to 588 (1700K)
# t is 0 > 255
TempKelvin = int(((255 - int(Hue_List['t']))*(6500-1700)/255)+1700);
TempMired = 1000000 // TempKelvin
#if previous not working
#TempMired = round(float(Hue_List['t'])*(500.0f - 153.0f) / 255.0f + 153.0f)
_json['ct'] = TempMired
# temporary patch
if self.Devices.get(IEEE + "_effect"):
_json.clear()
_json['sat'] = 0
_json['bri'] = int(Hue_List['t'])
#ColorModeRGB = 3 // Color. Valid fields: r, g, b.
elif Hue_List['m'] == 3:
if self.Devices[IEEE].get('colormode','Unknow') == 'hs':
h,s,v = rgb_to_hsv((int(Hue_List['r']),int(Hue_List['g']),int(Hue_List['b'])))
hue = int(h * 65535)
saturation = int(s * 254)
lightness = int(v * 254)
_json['hue'] = hue
_json['sat'] = saturation
#Using a hack here, because this mode is not for this kind of bulb
_json['bri'] = round(Level*lightness/100)
_json['transitiontime'] = 0
else:
x, y = rgb_to_xy((int(Hue_List['r']),int(Hue_List['g']),int(Hue_List['b'])))
x = round(x,6)
y = round(y,6)
_json['xy'] = [x,y]
#ColorModeCustom = 4, // Custom (color + white). Valid fields: r, g, b, cw, ww, depending on device capabilities
elif Hue_List['m'] == 4:
#process white color
ww = int(Hue_List['ww'])
cw = int(Hue_List['cw'])
TempKelvin = int(((255 - int(Hue_List['t']))*(6500-1700)/255)+1700);
TempMired = 1000000 // TempKelvin
_json['ct'] = TempMired
#process RGB color now
h,s,v = rgb_to_hsv((int(Hue_List['r']),int(Hue_List['g']),int(Hue_List['b'])))
hue = int(h * 65535)
saturation = int(s * 254)
lightness = int(v * 254)
_json['hue'] = hue
_json['sat'] = saturation
_json['bri'] = lightness
_json['transitiontime'] = 0
#To prevent bug
if 'bri' not in _json:
_json['bri'] = round(Level*254/100)
_json['transitiontime'] = 0
url = '/api/' + Parameters["Mode2"] + '/' + _type + '/' + str(deCONZ_ID)
if _type == 'lights':
url = url + '/state'
elif _type == 'config':
url = '/api/' + Parameters["Mode2"] + '/sensors/' + str(deCONZ_ID) + '/config'
elif _type == 'scenes':
url = '/api/' + Parameters["Mode2"] + '/groups/' + deCONZ_ID.split('/')[0] + '/scenes/' + deCONZ_ID.split('/')[1] + '/recall'
_json = {} # to force PUT
else:
url = url + '/action'
#if 'Thermostat' in self.Devices[IEEE]['model']:
# Domoticz.Status("Thermostat debug : " + url + ' with ' + str(_json))
self.SendCommand(url,_json)
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self, Connection):
Domoticz.Status("onDisconnect called for " + str(Connection.Name) )
def onHeartbeat(self):
Domoticz.Debug("onHeartbeat called")
#Check for freeze
if len(self.Buffer_Command) > 0:
self.UpdateBuffer()
#Initialisation
if self.Ready != True:
if len(self.INIT_STEP) > 0:
Domoticz.Debug("### Initialisation > " + str(self.INIT_STEP[0]))
self.ManageInit()
#Stop all here
return
else:
self.Ready = True
#Check websocket connexion
if self.WebSocket:
if not self.WebSocket.Connected():
Domoticz.Error("WebSocket Disconnected, reconnecting... ")
self.WebSocket.Connect()
#reset switchs
if len(self.NeedToReset) > 0 :
for IEEE in self.NeedToReset:
_id = False
for i in self.Devices:
if i == IEEE:
_id = self.Devices[i]['id']
UpdateDevice(_id,'sensors', { 'nValue' : 0 , 'sValue' : 'Off' }, self.SpecialDeviceList )
self.NeedToReset = []
#Devices[27].Update(nValue=0, sValue='11;22' )
def onDeviceRemoved(self,unit):
Domoticz.Log("Device Removed")
#TODO : Need to rescan all
#---------------------------------------------------------------------------------------
def ManageInit(self,pop = False):
if pop:
self.INIT_STEP.pop(0)
if len(self.INIT_STEP) < 1:
self.Ready = True
Domoticz.Status("### deCONZ ready")
l,s,g,b,o,c = Count_Type(self.Devices)
Domoticz.Status("### Found " + str(l) + " Operators, " + str(s) + " Sensors, " + str(g) + " Groups, " + str(c) + " Scenes and " + str(o) + " others, with " + str(b) + " Ignored")
try:
Domoticz.Status("### You can still create " + str(255-len(Devices.keys())) + " widgets in domoticz")
except:
pass
self.DisplayDeconzInfo("Deconz ready !",1)
# Compare devices bases
for i in Devices:
if Devices[i].DeviceID not in self.Devices:
if Devices[i].DeviceID != "Alarm_System_1":
Domoticz.Status('### Device ' + Devices[i].DeviceID + '(' + Devices[i].Name + ') Not in deCONZ ATM, the device is deleted or not ready.')
return
#No flood during initialisation
if len(self.Buffer_Command) > 0:
u,d = self.Buffer_Command[-1]
if "/" + self.INIT_STEP[0] + "/" in u:
Domoticz.Log("### Still waiting")
return
Domoticz.Log("### Request " + self.INIT_STEP[0])
self.SendCommand("/api/" + Parameters["Mode2"] + "/" + self.INIT_STEP[0] + "/")
def InitDomoticzDB(self,key,_Data,Type_device):
#Lights or sensors ?
if not 'devicemembership' in _Data:
IEEE = str(_Data['uniqueid'])
Name = str(_Data['name'])
Type = str(_Data['type'])
Model = str(_Data.get('modelid',''))
Manuf = str(_Data.get('manufacturername',''))
StateList = _Data.get('state',[])
ConfigList = _Data.get('config',[])
Domoticz.Log("### Device > " + str(key) + ' Name:' + Name + ' Type:' + Type + ' Details:' + str(StateList) + ' and ' + str(ConfigList) )
#Skip useless devices
if Type == 'Configuration tool' :
Domoticz.Log("Skipping Device (Useless) : " + str(IEEE) )
self.IDGateway = key
return
if (Type == 'Unknown') and (len(StateList) == 1) and ('reachable' in StateList):
Domoticz.Log("Skipping Device (Useless) : " + str(IEEE) )
if self.IDGateway == -1:
self.IDGateway = key
return
if Type == 'CLIPDaylightOffset':
self.Banned_Devices.append(str(IEEE))
self.Devices[IEEE] = {'id' : key , 'type' : Type_device , 'model' : Type , 'state' : 'working'}
#Skip banned devices
if IEEE in self.Banned_Devices:
Domoticz.Log("Skipping Device (Banned) : " + str(IEEE) )
self.Devices[IEEE]['state'] = 'banned'
return
if Type == 'ZHATime':
self.Devices[IEEE]['state'] = 'banned'
return
#Get some infos
kwarg = {}
if StateList:
kwarg.update(ProcessAllState(StateList,Model,0))
if 'colormode' in StateList:
cm = StateList['colormode']
if (cm == 'xy') and ('hue' in StateList):
cm = 'hs'
self.Devices[IEEE]['colormode'] = StateList['colormode']
if ConfigList:
kwarg.update(ProcessAllConfig(ConfigList,Model,0))
#It's a switch ? Need special process
if Type == 'ZHASwitch' or Type == 'ZGPSwitch' or Type == 'CLIPSwitch':
#Set it to off
kwarg.update({'sValue': 'Off', 'nValue': 0})
#ignore ZHASwitch if vibration sensor
if 'sensitivity' in ConfigList:
return
#Used by Xiaomi Cube T1
if 'lumi.remote.cagl01' in Model:
if IEEE.endswith('-03-000c'):
Type = 'XCubeT1_R'
elif IEEE.endswith('-02-0012'):
Type = 'XCubeT1_C'
else:
# Useless device
self.Devices[IEEE]['state'] = 'banned'
return
#Used by Xiaomi Cube T1 Pro
elif 'lumi.remote.cagl02' in Model:
if IEEE.endswith('-03-000c'):
Type = 'XCubeT1_R'
elif IEEE.endswith('-02-0012'):
Type = 'XCubeT1_C'
else:
# Useless device
self.Devices[IEEE]['state'] = 'banned'
return
elif 'lumi.sensor_cube' in Model:
if IEEE.endswith('-03-000c'):
Type = 'XCube_R'
elif IEEE.endswith('-02-0012'):
Type = 'XCube_C'
else:
# Useless device
self.Devices[IEEE]['state'] = 'banned'
return
elif 'TRADFRI remote control' in Model:
Type = 'Tradfri_remote'
elif 'TRADFRI on/off switch' in Model:
Type = 'Tradfri_on/off_switch'
elif 'lumi.remote.b186acn01' in Model:
Type = 'Xiaomi_single_gang'
elif Model.startswith('lumi.remote.b286acn0'):
Type = 'Xiaomi_double_gang'
#Used for all opple switches
elif Model.endswith('86opcn01'):
Type = 'Xiaomi_Opple_6_button_switch'
#Used for all tuya switch
elif Model.startswith('TS004'):
Type = 'Tuya_button_switch'
#Used by philips remote
elif Model == 'RWL021':
Type = 'Philips_button_switch'
#used by ikea Stybar
elif 'Remote Control N2' in Model:
Type = 'Styrbar_remote'
#used by Develco
elif 'IOMZB-110' in Model:
Type = 'Binary_module'
else:
Type = 'Switch_Generic'
self.Devices[IEEE]['model'] = Type
if self.Ready == True:
Domoticz.Status("Adding missing device: " + str(key) + ' Type:' + str(Type))
#lidl strip
if Model == 'HG06467':
#Create a widget for effect
self.Devices[IEEE + "_effect"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'Chrismast_E' }
self.CreateIfnotExist(IEEE + "_effect",'Chrismast_E',Name)
#Correction
self.Devices[IEEE]['colormode'] = 'hs'
Type = 'Color Temperature dimmable light'
#lidl led strip
if Model == 'HG06104A':
#Correction
self.Devices[IEEE]['colormode'] = 'xy'
Type = 'Extended color light'
#Special devices
if Type == 'ZHAThermostat':
# Not working for cable outlet yet.
if not Model == 'Cable outlet':
#Create a setpoint device
if 'heatsetpoint' in ConfigList:
self.Devices[IEEE + "_heatsetpoint"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'ZHAThermostat' }
self.CreateIfnotExist(IEEE + "_heatsetpoint",'ZHAThermostat',Name)
#Create a mode device
if 'mode' in ConfigList:
self.Devices[IEEE + "_mode"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'Thermostat_Mode' }
self.CreateIfnotExist(IEEE + "_mode",'Thermostat_Mode',Name)
#Create a preset device
if 'preset' in ConfigList:
self.Devices[IEEE + "_preset"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'Thermostat_Preset' }
self.CreateIfnotExist(IEEE + "_preset",'Thermostat_Preset',Name)
#Create the current device but as temperature device
self.CreateIfnotExist(IEEE,'ZHATemperature',Name)
elif Type == 'ZHAAirPurifier':
#Create a mode fan
if 'mode' in ConfigList:
self.Devices[IEEE + "_mode"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'Purifier_Mode' }
self.CreateIfnotExist(IEEE + "_mode",'Purifier_Mode',Name)
#Create fan speed
self.CreateIfnotExist(IEEE,'ZHAAirPurifier',Name)
elif Type == 'ZHAAirQuality' or Type == 'ZHAParticulateMatter':
self.Devices[IEEE]['option'] = 3
if 'pm2_5' in StateList:
self.CreateIfnotExist(IEEE,'ZHAAirQuality',Name,1)
else:
self.CreateIfnotExist(IEEE,'ZHAAirQuality',Name)
elif Type == 'ZHAVibration':
#Create a Angle device
self.Devices[IEEE + "_orientation"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'Vibration_Orientation' }
self.CreateIfnotExist(IEEE + "_orientation",'Vibration_Orientation',Name)
#Create the current device
self.CreateIfnotExist(IEEE,'ZHAVibration',Name)
elif Type == 'ZHADoorLock':
#Create a locker device
self.Devices[IEEE + "_lock"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'Door Lock' }
self.CreateIfnotExist(IEEE + "_lock",'Door Lock',Name)
#Create the current device
self.CreateIfnotExist(IEEE,'ZHADoorLock',Name)
if Type == 'ZHAConsumption':
# power and consumption on the same endpoint
if Model == 'ZHEMI101' or Model == 'TH1124ZB' or Model == 'OTH4000-ZB' or Model == '45856' or Model == 'E1C-NB7':
self.Devices[IEEE]['option'] = 1
self.CreateIfnotExist(IEEE,Type,Name,1)
# Support of consumption_2
elif 'consumption_2' in StateList:
self.Devices[IEEE]['option'] = 2
self.CreateIfnotExist(IEEE,Type,Name,3)
#Classic one
else:
self.CreateIfnotExist(IEEE,Type,Name)
#defaut sensor
else:
self.CreateIfnotExist(IEEE,Type,Name)
#Bonus sensor ?
if ENABLEMORESENSOR:
# Voltage sensor ?
if 'voltage' in StateList:
self.Devices[IEEE + "_voltage"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'ZHAPower_voltage' }
self.CreateIfnotExist(IEEE + "_voltage",'ZHAPower_voltage',Name)
# Current Sensor ?
if 'current' in StateList:
self.Devices[IEEE + "_current"] = {'id' : key , 'type' : 'config' , 'state' : 'working' , 'model' : 'ZHAPower_current' }
self.CreateIfnotExist(IEEE + "_current",'ZHAPower_current',Name)
if ENABLEBATTERYWIDGET:
# Battery sensor ?
if 'battery' in ConfigList:
#But only 1 by device
NewIEE = IEEE.split("-")[0]
if NewIEE + "_battery" not in self.Devices:
self.Devices[NewIEE + "_battery"] = {'id' : key , 'type' : 'state' , 'state' : 'working' , 'model' : 'ZHABattery' }
self.CreateIfnotExist(NewIEE + "_battery",'ZHABattery',Name)
#update
if kwarg:
UpdateDevice(key, Type_device, kwarg, self.SpecialDeviceList)
#groups
else:
Name = str(_Data['name'])
Type = str(_Data['type'])
Domoticz.Log("### Groupe > " + str(key) + ' Name:' + Name )
Dev_name = 'GROUP_' + Name.replace(' ','_')
self.Devices[Dev_name] = {'id' : key , 'type' : 'groups' , 'model' : 'groups', 'state' : 'working'}
# Skip banned group
if Dev_name in self.Banned_Devices:
Domoticz.Log("Skipping Group (Banned) : " + str(Dev_name) )
self.Devices[Dev_name]['state'] = 'banned'
else:
#Check for scene
scenes = _Data.get('scenes',[])
if len(scenes) > 0:
for j in scenes:
Domoticz.Log("### Scenes associated with group " + str(key) + " > ID:" + str(j['id']) + " Name:" + str(j['name']) )
Scene_name = 'SCENE_' + str(j['name']).replace(' ','_')
self.Devices[Scene_name] = {'id' : str(key) + '/' + str(j['id']) , 'type' : 'scenes' , 'model' : 'scenes'}
#^scene not exist > create
if GetDomoDeviceInfo(Scene_name) == False:
CreateDevice(Scene_name,str(j['name']),'Scenes')
#Group not exist > create
if GetDomoDeviceInfo(Dev_name) == False:
CreateDevice(Dev_name,Name,Type)
def CreateIfnotExist(self, __IEEE, __Type, Name, opt = 0):
if GetDomoDeviceInfo(__IEEE) == False:
CreateDevice(__IEEE, Name, __Type, opt)
def NormalConnexion(self,_Data):
Domoticz.Debug("Classic Data : " + str(_Data) )
#JSON with data returned >> _Data = [{'success': {'/lights/2/state/on': True}}, {'success': {'/lights/2/state/bri': 251}}]
if isinstance(_Data, list):
self.ReadReturn(_Data)
else:
if (self.Ready != True):
#JSON with config
if 'bridgeid' in _Data:
if 'websocketnotifyall' in _Data:
self.ReadConfig(_Data)
else:
Domoticz.Error("Incorrect or unknown API KEY!")
else:
#JSON with device info like {'1': {'data:1}}
for i in _Data:
if 'config' in _Data[i] and 'disarmed_entry_delay' in _Data[i]['config']:
# Alarm System
Domoticz.Status("Alarm System configured :" + str(_Data[i]['config']['configured']))
nbre = len(_Data[i]['devices'])
if nbre > 0 :
CreateAlarmSystemControl()
Domoticz.Status("Number of devices inside :" + str(nbre))
UpdatelarmSystemControl(_Data[i]['state']['armstate'])
else:
self.InitDomoticzDB(i,_Data[i],self.INIT_STEP[0])
#Update initialisation
self.ManageInit(True)
else:
#JSON with device info like {'data:1}
# Groups ?
if 'devicemembership' in _Data:
_id = _Data.get('id','')
if _id:
self.InitDomoticzDB(_id,_Data,'groups')
# simple device ?
else:
typ,_id = self.GetDevicedeCONZ(_Data.get('uniqueid','') )
if _id:
self.InitDomoticzDB(_id,_Data,typ)
def ReadReturn(self,_Data):
kwarg = {}
_id = False
_type = False
for _Data2 in _Data:
First_item = next(iter(_Data2))
#Command Error
if First_item == 'error':
Domoticz.Error("deCONZ error :" + str(_Data2))
self.DisplayDeconzInfo("Error: " + _Data2['error']['address'] + " > " + _Data2['error']['description'],4)
if (_Data2['error']['type'] == 3) or (_Data2['error']['type'] == 202):
Domoticz.Log("Seems like disconnected")
dev = _Data2['error']['address'].split('/')
_id = dev[2]
_type = dev[1]
#Set red header
kwarg.update({'TimedOut':1})
#Command sucess
elif First_item == 'success':
data = _Data2['success']
dev = (list(data.keys())[0] ).split('/')
val = data[list(data.keys())[0]]
if len(dev) < 3:
pass
else:
if not _id:
_id = dev[2]
_type = dev[1]
if dev[1] == 'config':
Domoticz.Status("Editing configuration: " + str(data))
#if dev[1] == 'lights' and dev[4] == 'alert':
# kwarg.update(ProcessAllState({'alert':val} ,''))
else:
Domoticz.Error("Not managed return JSON: " + str(_Data2) )
if kwarg:
UpdateDevice(_id, _type ,kwarg, self.SpecialDeviceList)
def ReadConfig(self,_Data):
#trick to test is deconz is ready
fw = _Data['fwversion']
if fw == '0x00000000':
Domoticz.Error("Startup failed. retrying....")
#Cancel this part to restart it after 1 heartbeat (10s)
return
Domoticz.Status("Firmware version: " + _Data['fwversion'] )
Domoticz.Status("Websocketnotifyall: " + str(_Data['websocketnotifyall']))
if not _Data['websocketnotifyall'] == True:
Domoticz.Error("Websocketnotifyall is not set to True")
if len(_Data['whitelist']) > 10:
Domoticz.Status("You have " + str(len(_Data['whitelist'])) + " API keys memorised, some of them are probably useless, can use the API_KEY.py tool or the Front end to clean them")
#Launch Web socket connexion
self.WebSocket = Domoticz.Connection(Name="deCONZ_WebSocket", Transport="TCP/IP", Address=Parameters["Address"], Port=str(_Data['websocketport']) )
self.WebSocket.Connect()
self.ManageInit(True)
def WebSocketConnexion(self,_Data):
Domoticz.Debug("### WebSocket Data : " + str(_Data) )
if not self.Ready == True:
Domoticz.Error("deCONZ not ready")
return
if 'e' in _Data:
if _Data['e'] == 'deleted':
return
if _Data['e'] == 'added':
return
if _Data['e'] == 'scene-called':
Domoticz.Log("Playing scene > group:" + str(_Data['gid']) + " Scene:" + str(_Data['scid']) )
return
#Remove all uniqueid that can be have in group
if 'r' in _Data:
if _Data['r'] == 'groups' and 'uniqueid' in _Data:
_Data.pop('uniqueid')
#Take care, no uniqueid for groups
IEEE,state = self.GetDeviceIEEE(_Data['id'],_Data['r'])
#Patch for device with double UniqueID, can't be light
if (not IEEE) and ('uniqueid' in _Data) and _Data['r'] != 'lights':
typ,_id = self.GetDevicedeCONZ(_Data['uniqueid'] )
if _id and (typ == _Data['r']):
Domoticz.Log("Double UniqueID correction : " + _Data['id'] + ' > ' + str(_id) )
_Data['id'] = _id
IEEE,state = self.GetDeviceIEEE(_Data['id'],_Data['r'])
if not IEEE:
if 'uniqueid' in _Data:
Domoticz.Error("Websocket error, unknown device > " + str(_Data['id']) + ' (' + str(_Data['r']) + ') Asking for information')