forked from Xenomes/Domoticz-TinyTUYA-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
3188 lines (2990 loc) · 214 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
# Domoticz TUYA Plugin
#
# Author: Xenomes (xenomes@outlook.com)
#
"""
<plugin key="tinytuya" name="TinyTUYA (Cloud)" author="Xenomes" version="1.7.3" wikilink="" externallink="https://github.com/Xenomes/Domoticz-TinyTUYA-Plugin.git">
<description>
Support forum: <a href="https://www.domoticz.com/forum/viewtopic.php?f=65&t=39441">https://www.domoticz.com/forum/viewtopic.php?f=65&t=39441</a><br/>
<br/>
<h2>TinyTUYA Plugin version 1.7.3</h2><br/>
The plugin make use of IoT Cloud Platform account for setup up see https://github.com/jasonacox/tinytuya step 3 or see PDF https://github.com/jasonacox/tinytuya/files/8145832/Tuya.IoT.API.Setup.pdf
<h3>Features</h3>
<ul style="list-style-type:square">
<li>Auto-detection of devices on network</li>
<li>On/Off control, state and available status display</li>
</ul>
<h3>Devices</h3>
<ul style="list-style-type:square">
<li>AMany devices are supported.</li>
</ul>
<h3>Configuration</h3>
<ul style="list-style-type:square">
<li>Enter your Region, Access ID/Client ID, Access Secret/Client Secret, and a Search deviceID from your Tuya IOT Account. Keep the 'Data Timeout' setting disabled.</li>
<li>A deviceID can be found in your Tuya IOT account. Go to Cloud => your project => Devices => Select one of your device IDs. (This ID is used to detect all the other devices.)</li>
<li>Complete the initial setup of your devices using the app, and this plugin will automatically detect and use the same settings to find and add the devices into Domoticz.<br/></li>
</ul>
If your subscription to the cloud development plan has expired, you can extend it <a href="https://iot.tuya.com/cloud/products/apply-extension">HERE</a><br/>
</description>
<params>
<param field="Mode1" label="Region" width="150px" required="true" default="EU">
<options>
<option label="EU" value="eu" default="true" />
<option label="US" value="us"/>
<option label="CN" value="cn"/>
</options>
</param>
<param field="Username" label="Access ID" width="300px" required="true" default="" />
<param field="Password" label="Access Secret" width="300px" required="true" default="" password="true" />
<param field="Mode2" label="Search DeviceID" width="300px" required="true" />
<param field="Mode6" label="Debug" width="150px">
<options>
<option label="None" value="0" default="true" />
<option label="Python Only" value="2"/>
<option label="Basic Debugging" value="62"/>
<option label="Basic + Messages" value="126"/>
<option label="Queue" value="128"/>
<option label="Connections Only" value="16"/>
<option label="Connections + Queue" value="144"/>
<option label="All" value="-1"/>
</options>
</param>
</params>
</plugin>
"""
try:
import DomoticzEx as Domoticz
except ImportError:
import fakeDomoticz as Domoticz
import tinytuya
import os
import sys
import ast
import json
import colorsys
import time
import re
import base64
class BasePlugin:
enabled = False
def __init__(self):
return
def onStart(self):
Domoticz.Log('TinyTUYA ' + Parameters['Version'] + ' plugin started')
Domoticz.Log('TinyTuyaVersion:' + tinytuya.version )
if Parameters['Mode6'] != '0':
Domoticz.Debugging(int(Parameters['Mode6']))
# Domoticz.Log('Debugger started, use 'telnet 0.0.0.0 4444' to connect')
# import rpdb
# rpdb.set_trace()
DumpConfigToLog()
global testData
if os.path.isfile(Parameters['HomeFolder'] + '/debug_devices.json'):
testData = True
Domoticz.Heartbeat(5)
Domoticz.Error('!! Warning Plugin overruled by local json files !!')
else:
testData = False
Domoticz.Heartbeat(10)
onHandleThread(True)
def onStop(self):
try:
devs = Devices
for dev in devs:
# Delete device is not reconised
if Devices[dev].Units[1].sValue == 'This device is not reconised, edit and run the debug_discovery with python from the tools directory and receate a issue report at https://github.com/Xenomes/Domoticz-TinyTUYA-Plugin/issues so the device can be added.':
Devices[dev].Units[1].Delete()
if Devices[dev].Units[1].sValue == 'This device is not recognized. Please edit and run the debug_discovery with Python from the tools directory and recreate an issue report at https://github.com/Xenomes/Domoticz-TinyTUYA-Plugin/issues so that the device can be added.':
Devices[dev].Units[1].Delete()
except:
Domoticz.Log('onStop called')
def onConnect(self, Connection, Status, Description):
Domoticz.Log('onConnect called')
def onMessage(self, Connection, Data):
Domoticz.Log('onMessage called')
def onCommand(self, DeviceID, Unit, Command, Level, Color):
Domoticz.Debug("onCommand called for Device " + str(DeviceID) + " Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level) + "', Color: " + str(Color))
# device for the Domoticz
dev = Devices[DeviceID].Units[Unit]
Domoticz.Debug('Device ID: ' + str(DeviceID))
Domoticz.Debug('nValue: ' + str(dev.nValue))
Domoticz.Debug('sValue: ' + str(dev.sValue) + ' Type ' + str(type(dev.sValue)))
Domoticz.Debug('LastLevel: ' + str(dev.LastLevel))
if Error is not None:
Domoticz.Error(Error['Payload'])
else:
# Control device and update status in Domoticz
dev_type = getConfigItem(DeviceID, 'category')
# scalemode = getConfigItem(DeviceID, 'scalemode')
product_id = getConfigItem(DeviceID, 'product_id')
# if len(properties) == 0:
# properties = {}
# for dev in devs:
# properties[dev['id']] = tuya.getproperties(dev['id'])['result']
function = properties[DeviceID]['functions']
status = properties[DeviceID]['status']
if len(Color) != 0: Color = ast.literal_eval(Color)
if dev_type == 'switch':
if searchCode('switch', function):
if Command == 'Off':
SendCommandCloud(DeviceID, 'switch', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On':
SendCommandCloud(DeviceID, 'switch', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
elif Command == 'Set Level':
SendCommandCloud(DeviceID, 'switch', True)
UpdateDevice(DeviceID, Unit, Level, 1, 0)
if not searchCode('switch', function):
if Command == 'Off':
SendCommandCloud(DeviceID, 'switch_' + str(Unit), False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On':
SendCommandCloud(DeviceID, 'switch_' + str(Unit), True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
elif Command == 'Set Level':
SendCommandCloud(DeviceID, 'switch_' + str(Unit), True)
UpdateDevice(DeviceID, Unit, Level, 1, 0)
elif dev_type in ('dimmer'):
if Command == 'Off':
SendCommandCloud(DeviceID, 'switch_led_' + str(Unit), False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On':
SendCommandCloud(DeviceID, 'switch_led_' + str(Unit), True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
elif Command == 'Set Level':
SendCommandCloud(DeviceID, 'bright_value_' + str(Unit), Level)
UpdateDevice(DeviceID, Unit, Level, 1, 0)
elif dev_type in ('light') or ((dev_type in ('fanlight') or dev_type in ('pirlight')) and Unit == 1):
switch = 'led_switch' if searchCode('led_switch', function) else 'switch_led'
if Command == 'Off':
SendCommandCloud(DeviceID, switch, False)
UpdateDevice(DeviceID, 1, 'Off', 0, 0)
elif Command == 'On':
SendCommandCloud(DeviceID, switch, True)
UpdateDevice(DeviceID, 1, 'On', 1, 0)
elif Command == 'Set Level':
if searchCode('bright_value_v2', function):
SendCommandCloud(DeviceID, switch, True)
SendCommandCloud(DeviceID, 'bright_value_v2', Level)
else:
SendCommandCloud(DeviceID, switch, True)
SendCommandCloud(DeviceID, 'bright_value', Level)
UpdateDevice(DeviceID, 1, Level, 1, 0)
elif (Command == 'Set Color' or Command == 'Set Level') and len(Color) != 0:
if Color['m'] == 2:
SendCommandCloud(DeviceID, switch, True)
SendCommandCloud(DeviceID, 'work_mode', 'white')
if searchCode('bright_value_v2', function):
SendCommandCloud(DeviceID, 'bright_value_v2', Level)
SendCommandCloud(DeviceID, 'temp_value_v2', int(Color['t']))
else:
SendCommandCloud(DeviceID, 'bright_value', Level)
SendCommandCloud(DeviceID, 'temp_value', int(Color['t']))
UpdateDevice(DeviceID, 1, Level, 1, 0)
UpdateDevice(DeviceID, 1, Color, 1, 0)
# elif Color['m'] == 3:
# if scalemode == 'v2':
# h, s, v = rgb_to_hsv_v2(int(Color['r']), int(Color['g']), int(Color['b']))
# hvs = {'h':h, 's':s, 'v':Level * 10}
# SendCommandCloud(DeviceID, switch, True)
# SendCommandCloud(DeviceID, 'colour_data', hvs)
# else:
# h, s, v = rgb_to_hsv(int(Color['r']), int(Color['g']), int(Color['b']))
# hvs = {'h':h, 's':s, 'v':Level * 2.55}
# SendCommandCloud(DeviceID, switch, True)
# SendCommandCloud(DeviceID, 'colour_data', hvs)
# UpdateDevice(DeviceID, 1, Level, 1, 0)
# UpdateDevice(DeviceID, 1, Color, 1, 0)
elif Color['m'] == 3:
rgbcolor = format(rgb_temp(Color['r'], Level), '02x') + format(rgb_temp(Color['g'], Level), '02x') + format(rgb_temp(Color['b'], Level), '02x') + '0000ffff'
SendCommandCloud(DeviceID, switch, True)
SendCommandCloud(DeviceID, 'work_mode', 'colour')
if searchCode('colour_data_v2', function):
SendCommandCloud(DeviceID, 'colour_data_v2', rgbcolor)
else:
SendCommandCloud(DeviceID, 'colour_data', rgbcolor)
UpdateDevice(DeviceID, 1, Level, 1, 0)
UpdateDevice(DeviceID, 1, Color, 1, 0)
if dev_type == ('cover'):
if Command == 'Open':
if searchCode('mach_operate', function):
SendCommandCloud(DeviceID, 'mach_operate', 'FZ')
else:
SendCommandCloud(DeviceID, 'control', 'open')
UpdateDevice(DeviceID, 1, 'Open', 0, 0)
elif Command == 'Close':
if searchCode('mach_operate', function):
SendCommandCloud(DeviceID, 'mach_operate', 'ZZ')
else:
SendCommandCloud(DeviceID, 'control', 'close')
UpdateDevice(DeviceID, 1, 'Close', 1, 0)
elif Command == 'Stop':
if searchCode('mach_operate', function):
SendCommandCloud(DeviceID, 'mach_operate', 'STOP')
else:
SendCommandCloud(DeviceID, 'control', 'stop')
UpdateDevice(DeviceID, 1, 'Stop', 1, 0)
elif Command == 'Set Level':
SendCommandCloud(DeviceID, 'position', Level)
UpdateDevice(DeviceID, 1, Level, 1, 0)
elif dev_type == 'smartheatpump' :
if searchCode('switch', function):
switch = 'switch'
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, switch, False)
UpdateDevice(DeviceID, 1, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, switch, True)
UpdateDevice(DeviceID, 1, 'On', 1, 0)
if searchCode('ach_stemp', function):
switch = 'ach_stemp'
if Command == 'Set Level' and Unit == 11:
SendCommandCloud(DeviceID, switch, Level)
UpdateDevice(DeviceID, 11, Level, 1, 0)
if searchCode('wth_stemp', function):
switch = 'wth_stemp'
if Command == 'Set Level' and Unit == 12:
SendCommandCloud(DeviceID, switch, Level)
UpdateDevice(DeviceID, 12, Level, 1, 0)
if searchCode('aircond_temp_diff', function):
switch = 'aircond_temp_diff'
if Command == 'Set Level' and Unit == 13:
SendCommandCloud(DeviceID, switch, Level)
UpdateDevice(DeviceID, 13, Level, 1, 0)
if searchCode('wth_temp_diff', function):
switch = 'wth_temp_diff'
if Command == 'Set Level' and Unit == 14:
SendCommandCloud(DeviceID, switch, Level)
UpdateDevice(DeviceID, 14, Level, 1, 0)
if searchCode('acc_stemp', function):
switch = 'acc_stemp'
if Command == 'Set Level' and Unit == 15:
SendCommandCloud(DeviceID, switch, Level)
UpdateDevice(DeviceID, 15, Level, 1, 0)
if searchCode('mode', function):
if Command == 'Set Level' and Unit == 16:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'mode', mode[int(Level / 10)])
UpdateDevice(DeviceID, 16, Level, 1, 0)
if searchCode('work_mode', function):
if Command == 'Set Level' and Unit == 17:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'work_mode', mode[int(Level / 10)])
UpdateDevice(DeviceID, 17, Level, 1, 0)
elif dev_type == 'thermostat' or dev_type == 'heater'or dev_type == 'heatpump':
if searchCode('switch_1', function):
switch = 'switch_1'
else:
switch = 'switch'
if searchCode('temp_set', function):
switch3 = 'temp_set'
elif searchCode('set_temp', function):
switch3 = 'set_temp'
elif searchCode('temperature_c', function):
switch3 = 'temperature_c'
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, switch, False)
UpdateDevice(DeviceID, 1, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, switch, True)
UpdateDevice(DeviceID, 1, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 3:
SendCommandCloud(DeviceID, switch3, Level)
UpdateDevice(DeviceID, 3, Level, 1, 0)
elif Command == 'Set Level' and Unit == 4:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'mode', mode[int(Level / 10)])
UpdateDevice(DeviceID, 4, Level, 1, 0)
if Command == 'Off' and Unit == 5:
SendCommandCloud(DeviceID, 'window_check', False)
UpdateDevice(DeviceID, 5, 'Off', 0, 0)
elif Command == 'On' and Unit == 5:
SendCommandCloud(DeviceID, 'window_check', True)
UpdateDevice(DeviceID, 5, 'On', 1, 0)
if Command == 'Off' and Unit == 6:
SendCommandCloud(DeviceID, 'child_lock', False)
UpdateDevice(DeviceID, 6, 'Off', 0, 0)
elif Command == 'On' and Unit == 6:
SendCommandCloud(DeviceID, 'child_lock', True)
UpdateDevice(DeviceID, 6, 'On', 1, 0)
if Command == 'Off' and Unit == 7:
SendCommandCloud(DeviceID, 'eco', False)
UpdateDevice(DeviceID, 7, 'Off', 0, 0)
elif Command == 'On' and Unit == 7:
SendCommandCloud(DeviceID, 'eco', True)
UpdateDevice(DeviceID, 7, 'On', 1, 0)
if dev_type == 'fan':
if Command == 'Off':
SendCommandCloud(DeviceID, 'switch', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On':
SendCommandCloud(DeviceID, 'switch', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
if dev_type == 'fanlight':
if Command == 'Off' and Unit == 2:
SendCommandCloud(DeviceID, 'fan_switch', False)
UpdateDevice(DeviceID, 2, 'Off', 0, 0)
elif Command == 'On' and Unit == 2:
SendCommandCloud(DeviceID, 'fan_switch', True)
UpdateDevice(DeviceID, 2, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 3:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'fan_speed', int(mode[int(Level / 10)]))
UpdateDevice(DeviceID, 3, Level, 1, 0)
elif Command == 'Set Level' and Unit == 4:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'fan_direction', mode[int(Level / 10)])
UpdateDevice(DeviceID, 4, Level, 1, 0)
if dev_type == 'powermeter' :
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'switch_1', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'switch_1', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
if dev_type == 'siren':
if Command == 'Off':
SendCommandCloud(DeviceID, 'AlarmSwitch', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On':
SendCommandCloud(DeviceID, 'AlarmSwitch', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 2:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'Alarmtype', mode[int(Level / 10)])
UpdateDevice(DeviceID, 2, Level, 1, 0)
elif Command == 'Set Level' and Unit == 3:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'Alarmtype', mode[int(Level / 10)])
UpdateDevice(DeviceID, 3, Level, 1, 0)
# Other Type of alarm with same code
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'muffling', False)
UpdateDevice(DeviceID, 1, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'muffling', True)
UpdateDevice(DeviceID, 1, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 2:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'alarm_state', mode[int(Level / 10)])
UpdateDevice(DeviceID, 2, Level, 1, 0)
elif Command == 'Set Level' and Unit == 3:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'alarm_volume', mode[int(Level / 10)])
UpdateDevice(DeviceID, 3, Level, 1, 0)
if dev_type == 'pirlight':
if Command == 'Off' and Unit == 2:
SendCommandCloud(DeviceID, 'switch_pir', False)
UpdateDevice(DeviceID, 2, 'Off', 0, 0)
elif Command == 'On' and Unit == 2:
SendCommandCloud(DeviceID, 'switch_pir', True)
UpdateDevice(DeviceID, 2, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 3:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'device_mode', mode[int(Level / 10)])
UpdateDevice(DeviceID, 3, Level, 1, 0)
elif Command == 'Set Level' and Unit == 4:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'pir_sensitivity', mode[int(Level / 10)])
UpdateDevice(DeviceID, 4, Level, 1, 0)
if dev_type == 'garagedooropener':
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'switch_1', False)
UpdateDevice(DeviceID, 1, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'switch_1', True)
UpdateDevice(DeviceID, 1, 'On', 1, 0)
if dev_type == 'feeder':
if Command == 'Off' and Unit == 5:
SendCommandCloud(DeviceID, 'light', False)
UpdateDevice(DeviceID, 5, 'Off', 0, 0)
elif Command == 'On' and Unit == 5:
SendCommandCloud(DeviceID, 'light', True)
UpdateDevice(DeviceID, 5, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 1:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'manual_feed', int(mode[int(Level / 10)]))
UpdateDevice(DeviceID, 1, Level, 1, 0)
if dev_type == 'irrigation':
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'switch', False)
UpdateDevice(DeviceID, 1, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'switch', True)
UpdateDevice(DeviceID, 1, 'On', 1, 0)
if dev_type == 'wswitch':
if Command == 'Set Level':
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
if searchCode('switch' + str(Unit) + '_value', status):
SendCommandCloud(DeviceID, 'switch' + str(Unit) + '_value', mode[int(Level / 10)])
if searchCode('switch_type_' + str(Unit), status):
SendCommandCloud(DeviceID, 'switch_type_' + str(Unit), mode[int(Level / 10)])
if searchCode('switch_mode' + str(Unit), status):
SendCommandCloud(DeviceID, 'switch_mode' + str(Unit), mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
if dev_type == 'starlight':
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'switch_led', False)
SendCommandCloud(DeviceID, 'colour_switch', False)
UpdateDevice(DeviceID, 1, 'Off', 0, 0)
UpdateDevice(DeviceID, 2, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'switch_led', True)
SendCommandCloud(DeviceID, 'colour_switch', True)
UpdateDevice(DeviceID, 1, 'On', 1, 0)
UpdateDevice(DeviceID, 2, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 1:
Color = Devices[DeviceID].Units[1].Color
if Color == '': Color ={"b":255,"cw":0,"g":255,"m":3,"r":255,"t":0,"ww":0}
h, s, v = rgb_to_hsv_v2(int(Color['r']), int(Color['g']), int(Color['b']))
hvs = {'h':h, 's':s, 'v':Level * 10}
SendCommandCloud(DeviceID, 'colour_data', hvs)
SendCommandCloud(DeviceID, 'colour_switch', True)
UpdateDevice(DeviceID, 1, Color, 1, 0)
elif Command == 'Set Color' and Unit == 1: #
h, s, v = rgb_to_hsv_v2(int(Color['r']), int(Color['g']), int(Color['b']))
hvs = {'h':h, 's':s, 'v':Level * 10}
SendCommandCloud(DeviceID, 'colour_data', hvs)
SendCommandCloud(DeviceID, 'colour_switch', True)
UpdateDevice(DeviceID, 1, Color, 1, 0)
if Command == 'Off' and Unit == 2:
SendCommandCloud(DeviceID, 'colour_switch', False)
UpdateDevice(DeviceID, 2, 'Off', 0, 0)
UpdateDevice(DeviceID, 1, 'Off', 0, 0)
elif Command == 'On' and Unit == 2:
SendCommandCloud(DeviceID, 'colour_switch', True)
UpdateDevice(DeviceID, 2, 'On', 1, 0)
UpdateDevice(DeviceID, 1, 'On', 1, 0)
if Command == 'Off' and Unit == 3:
SendCommandCloud(DeviceID, 'laser_switch', False)
UpdateDevice(DeviceID, 3, 'Off', 0, 0)
elif Command == 'On' and Unit == 3:
SendCommandCloud(DeviceID, 'laser_switch', True)
UpdateDevice(DeviceID, 3, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 3:
SendCommandCloud(DeviceID, 'laser_switch', True)
SendCommandCloud(DeviceID, 'laser_bright', 21.25 + ((Level / 100) * 78.75)) # 21.25 + ((Level / 100) * 78.75) ) * 10
UpdateDevice(DeviceID, 3, 'On', 1, 0)
UpdateDevice(DeviceID, 3, Level, 1, 0)
if Command == 'Off' and Unit == 4:
SendCommandCloud(DeviceID, 'fan_switch', False)
UpdateDevice(DeviceID, 4, 'Off', 0, 0)
elif Command == 'On' and Unit == 4:
SendCommandCloud(DeviceID, 'fan_switch', True)
UpdateDevice(DeviceID, 4, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 4:
SendCommandCloud(DeviceID, 'fan_switch', True)
SendCommandCloud(DeviceID, 'fan_speed', Level)
UpdateDevice(DeviceID, 4, 'On', 1, 0)
UpdateDevice(DeviceID, 4, Level, 1, 0)
# if dev_type == 'smartlock':
# if Command == 'Off' and Unit == 3:
# SendCommandCloud(DeviceID, 'switch', False)
# UpdateDevice(DeviceID, 1, 10, 0, 0)
# elif Command == 'On' and Unit == 3:
# SendCommandCloud(DeviceID, 'switch', True)
# UpdateDevice(DeviceID, 1, 0, 1, 0)
if dev_type == 'dehumidifier':
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'switch', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'switch', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 2:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'mode', mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
elif Command == 'Set Level' and Unit == 3:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'fan_speed_enum', mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
elif Command == 'Set Level' and Unit == 4:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'mode', mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
if Command == 'Off' and Unit == 5:
SendCommandCloud(DeviceID, 'switch', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On' and Unit == 5:
SendCommandCloud(DeviceID, 'switch', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
if dev_type == 'vacuum':
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'power_go', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'power_go', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 3:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'mode', mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
elif Command == 'Set Level' and Unit == 4:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'suction', mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
elif Command == 'Set Level' and Unit == 5:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'cistern', mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
if dev_type == 'purifier':
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'switch', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'switch', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 3:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'mode', mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
elif Command == 'Set Level' and Unit == 4:
mode = Devices[DeviceID].Units[Unit].Options['LevelNames'].split('|')
SendCommandCloud(DeviceID, 'speed', mode[int(Level / 10)])
UpdateDevice(DeviceID, Unit, Level, 1, 0)
if dev_type == 'smartkettle':
if Command == 'Off' and Unit == 1:
SendCommandCloud(DeviceID, 'start', False)
UpdateDevice(DeviceID, Unit, 'Off', 0, 0)
elif Command == 'On' and Unit == 1:
SendCommandCloud(DeviceID, 'start', True)
UpdateDevice(DeviceID, Unit, 'On', 1, 0)
elif Command == 'Set Level' and Unit == 4:
SendCommandCloud(DeviceID, 'cook_temperature', Level)
UpdateDevice(DeviceID, 4, Level, 1, 0)
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log('Notification: ' + Name + ', ' + Subject + ', ' + Text + ', ' + Status + ', ' + str(Priority) + ', ' + Sound + ', ' + ImageFile)
def onDeviceRemoved(self, DeviceID, Unit):
Domoticz.Log('onDeviceDeleted called')
def onDisconnect(self, Connection):
Domoticz.Log('onDisconnect called')
def onHeartbeat(self):
Domoticz.Debug('onHeartbeat called')
if time.time() - last_update < 60 and testData == False:
Domoticz.Debug("onHeartbeat called skipped")
return
Domoticz.Debug("onHeartbeat called last run: " + str(time.time() - last_update))
if testData == False:
if Error is not None:
Domoticz.Error(Error['Payload'])
else:
onHandleThread(False)
else:
onHandleThread(False)
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 onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)
def onCommand(DeviceID, Unit, Command, Level, Color):
global _plugin
_plugin.onCommand(DeviceID, Unit, Command, Level, Color)
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)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
def onHandleThread(startup):
# Run for every device on startup and heartbeat
try:
if startup == True:
global tuya
global devs
global properties
global ResultValue
global FunctionProperties
global StatusProperties
global Error
global scan
global last_update
global product_id
global t
last_update = time.time()
if testData == True:
tuya = Domoticz.Log
with open(Parameters['HomeFolder'] + '/debug_devices.json') as dFile:
devs = json.load(dFile)
token = 'Fake'
Error = None
properties = {}
with open(Parameters['HomeFolder'] + '/debug_functions.json') as fFile:
for dev in devs:
properties[dev['id']] = json.load(fFile)['result']
try:
properties[dev['id']]['functions']
except:
properties[dev['id']]['functions'] = []
Domoticz.Error('!! Warning Functions data is missing !!')
try:
properties[dev['id']]['status']
except:
properties[dev['id']]['status'] = []
Domoticz.Error('!! Warning Status data is missing !!')
# Domoticz.Debug(properties[dev['id']])
else:
# if version(tinytuya.version) >= version('1.11.0'):
# tuya = tinytuya.Cloud(apiRegion=Parameters['Mode1'], apiKey=Parameters['Username'], apiSecret=Parameters['Password'])
# else:
tuya = tinytuya.Cloud(apiRegion=Parameters['Mode1'], apiKey=Parameters['Username'], apiSecret=Parameters['Password'], apiDeviceID=Parameters['Mode2'])
tuya.use_old_device_list = True
tuya.new_sign_algorithm = True
Error = tuya.error
if Error is not None:
raise Exception(Error['Payload'])
devs = []
i = 0
while len(devs) == 0 and i < 4:
devs = tuya.getdevices()
Domoticz.Log('No device data returned for Tuya. Trying again!')
i += 1
if i > 4:
raise Exception('No device data returned for Tuya. Check if subscription cloud development plan has expired!')
token = tuya.token
# # Check credentials
# if 'sign invalid' in str(devs) or token == None:
# login = False
# raise Exception('Credentials are incorrect!')
# # Check ID search device is valid
# if 'permission deny' in str(devs):
# login = False
# raise Exception('ID search device not found!')
properties = {}
for dev in devs:
properties[dev['id']] = tuya.getproperties(dev['id'])['result']
try:
properties[dev['id']]['functions']
except:
properties[dev['id']]['functions'] = []
try:
properties[dev['id']]['status']
except:
properties[dev['id']]['status'] = []
Domoticz.Log('Scanning for tuya devices on network...')
if testData == False:
scan = tinytuya.deviceScan(verbose=False, maxretry=None, byID=True)
# Initialize/Update devices from TUYA API
run = 0
for dev in devs:
run += 1
Domoticz.Debug( 'Device name=' + str(dev['name']) + ' id=' + str(dev['id']) + ' category=' + str(DeviceType(dev['category'])))
try:
last_update = time.time()
if testData == True:
online = True
else:
online = tuya.getconnectstatus(dev['id'])
# Set last update
FunctionProperties = properties[dev['id']]['functions']
dev_type = DeviceType(properties[dev['id']]['category'])
StatusProperties = properties[dev['id']]['status']
except:
raise Exception('Credentials are incorrect or tuya subscription has expired!')
return
if testData == True:
with open(Parameters['HomeFolder'] + '/debug_result.json') as rFile:
rData = json.load(rFile)
ResultValue = rData['result']
t = rData['t']
else:
ResultValue = tuya.getstatus(dev['id'])['result']
t = tuya.getstatus(dev['id'])['t']
product_id = getConfigItem(dev['id'],'product_id')
# Define scale mode
# if '_v2' in str(FunctionProperties):
# scalemode = 'v2'
# else:
# scalemode = 'v1'
# Domoticz.Debug( 'functions= ' + str(functions))
# Domoticz.Debug( 'Device name= ' + str(dev['name']) + ' id= ' + str(dev['id']) + ' result= ' + ResultValue)
# Domoticz.Debug( 'Device name= ' + str(dev['name']) + ' id= ' + str(dev['id']) + ' FunctionProperties= ' + str(properties[dev['id']]))
# Create devices
if startup == True:
if run == 1:
Domoticz.Debug('Run Startup script')
try:
deviceinfo = scan[dev['id']]
except:
deviceinfo = {'ip': None, 'version': 3.3}
if dev_type in ('light', 'fanlight', 'pirlight') and createDevice(dev['id'], 1):
# for localcontol: and deviceinfo['ip'] != None
if (searchCode('switch_led', StatusProperties) or searchCode('led_switch', StatusProperties)) and searchCode('work_mode', StatusProperties) and (searchCode('colour_data', StatusProperties) or searchCode('colour_data_v2', StatusProperties)) and (searchCode('temp_value', StatusProperties) or searchCode('temp_value_v2', StatusProperties)) and (searchCode('bright_value', StatusProperties) or searchCode('bright_value_v2', StatusProperties)):
Domoticz.Log('Create device Light RGBWW')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=241, Subtype=4, Switchtype=7, Used=1).Create()
elif (searchCode('switch_led', StatusProperties) or searchCode('led_switch', StatusProperties)) and 'dc' == str(properties[dev['id']]['category']) and searchCode('work_mode', StatusProperties) and (searchCode('colour_data', StatusProperties) or searchCode('colour_data_v2', StatusProperties)):
Domoticz.Log('Create device Light Stringlight')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=241, Subtype=4, Switchtype=7, Used=1).Create()
elif (searchCode('switch_led', StatusProperties) or searchCode('led_switch', StatusProperties)) and searchCode('work_mode', StatusProperties) and (searchCode('colour_data', StatusProperties) or searchCode('colour_data_v2', StatusProperties)) and (not searchCode('temp_value', StatusProperties) or not searchCode('temp_value_v2', StatusProperties)) and (searchCode('bright_value', StatusProperties) or searchCode('bright_value_v2', StatusProperties)):
Domoticz.Log('Create device Light RGBW')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=241, Subtype=1, Switchtype=7, Used=1).Create()
elif (searchCode('switch_led', StatusProperties) or searchCode('led_switch', StatusProperties)) and not searchCode('work_mode', StatusProperties) and (searchCode('colour_data', StatusProperties) or searchCode('colour_data_v2', StatusProperties)) and (not searchCode('temp_value', StatusProperties) or not searchCode('temp_value_v2', StatusProperties)) and (searchCode('bright_value', StatusProperties) or searchCode('bright_value_v2', StatusProperties)):
Domoticz.Log('Create device Light RGB')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=241, Subtype=2, Switchtype=7, Used=1).Create()
elif (searchCode('switch_led', StatusProperties) or searchCode('led_switch', StatusProperties)) and searchCode('work_mode', StatusProperties) and not (searchCode('colour_data', StatusProperties) or searchCode('colour_data_v2', StatusProperties)) and (searchCode('temp_value', StatusProperties) or searchCode('temp_value_v2', StatusProperties)) and (searchCode('bright_value', StatusProperties) or searchCode('bright_value_v2', StatusProperties)):
Domoticz.Log('Create device Light WWCW')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=241, Subtype=8, Switchtype=7, Used=1).Create()
elif (searchCode('switch_led', StatusProperties) or searchCode('led_switch', StatusProperties)) and not searchCode('work_mode', StatusProperties) and not (searchCode('colour_data', StatusProperties) or searchCode('colour_data_v2', StatusProperties)) and (not searchCode('temp_value', StatusProperties) or not searchCode('temp_value_v2', StatusProperties)) and (searchCode('bright_value', StatusProperties) or searchCode('bright_value_v2', StatusProperties)):
Domoticz.Log('Create device Light Dimmer')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=241, Subtype=3, Switchtype=7, Used=1).Create()
elif (searchCode('switch_led', StatusProperties) or searchCode('led_switch', StatusProperties)) and not searchCode('work_mode', StatusProperties) and not (searchCode('colour_data', StatusProperties) or searchCode('colour_data_v2', StatusProperties)) and (not searchCode('temp_value', StatusProperties) or not searchCode('temp_value_v2', StatusProperties)) and (not searchCode('bright_value', StatusProperties) or not searchCode('bright_value_v2', StatusProperties)):
Domoticz.Log('Create device Light On/Off')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=7, Used=1).Create()
else:
Domoticz.Log('Create device Light On/Off (Unknown Light Device)')
Domoticz.Unit(Name=dev['name'] + ' (Unknown Light Device)', DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=7, Used=1).Create()
if dev_type == 'dimmer':
if createDevice(dev['id'], 1) and searchCode('switch_led_1', FunctionProperties) and not searchCode('switch_led_2', FunctionProperties):
Domoticz.Log('Create device Dimmer')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=241, Subtype=3, Switchtype=7, Used=1).Create()
if searchCode('switch_led_2', FunctionProperties):
if createDevice(dev['id'], 1):
Domoticz.Unit(Name=dev['name'] + ' (Dimmer 1)', DeviceID=dev['id'], Unit=1, Type=241, Subtype=3, Switchtype=7, Used=1).Create()
if createDevice(dev['id'], 2):
Domoticz.Unit(Name=dev['name'] + ' (Dimmer 2)', DeviceID=dev['id'], Unit=2, Type=241, Subtype=3, Switchtype=7, Used=1).Create()
if dev_type == 'switch':
if createDevice(dev['id'], 1) and (searchCode('switch_1', FunctionProperties) or searchCode('switch', FunctionProperties)) and not searchCode('switch_2', FunctionProperties):
Domoticz.Log('Create device Switch')
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=0, Image=9, Used=1).Create()
if searchCode('switch_2', FunctionProperties):
Domoticz.Log('Create device Switch')
if createDevice(dev['id'], 1):
Domoticz.Unit(Name=dev['name'] + ' (Switch 1)', DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=0, Image=9, Used=1).Create()
if createDevice(dev['id'], 2):
Domoticz.Unit(Name=dev['name'] + ' (Switch 2)', DeviceID=dev['id'], Unit=2, Type=244, Subtype=73, Switchtype=0, Image=9, Used=1).Create()
if createDevice(dev['id'], 3) and searchCode('switch_3', FunctionProperties):
Domoticz.Unit(Name=dev['name'] + ' (Switch 3)', DeviceID=dev['id'], Unit=3, Type=244, Subtype=73, Switchtype=0, Image=9, Used=1).Create()
if createDevice(dev['id'], 4) and searchCode('switch_4', FunctionProperties):
Domoticz.Unit(Name=dev['name'] + ' (Switch 4)', DeviceID=dev['id'], Unit=4, Type=244, Subtype=73, Switchtype=0, Image=9, Used=1).Create()
if createDevice(dev['id'], 5) and searchCode('switch_5', FunctionProperties):
Domoticz.Unit(Name=dev['name'] + ' (Switch 5)', DeviceID=dev['id'], Unit=5, Type=244, Subtype=73, Switchtype=0, Image=9, Used=1).Create()
if createDevice(dev['id'], 11) and ((searchCode('cur_current', ResultValue) and get_unit('cur_current', StatusProperties) == 'A') or searchCode('phase_a', ResultValue)):
Domoticz.Unit(Name=dev['name'] + ' (A)', DeviceID=dev['id'], Unit=11, Type=243, Subtype=23, Used=1).Create()
if createDevice(dev['id'], 12) and (searchCode('cur_power', ResultValue) or searchCode('phase_a', ResultValue)):
Domoticz.Unit(Name=dev['name'] + ' (W)', DeviceID=dev['id'], Unit=12, Type=248, Subtype=1, Used=1).Create()
if createDevice(dev['id'], 13) and (searchCode('cur_voltage', ResultValue) or searchCode('phase_a', ResultValue)):
Domoticz.Unit(Name=dev['name'] + ' (V)', DeviceID=dev['id'], Unit=13, Type=243, Subtype=8, Used=1).Create()
if createDevice(dev['id'], 14) and (searchCode('cur_power', ResultValue) or searchCode('phase_a', ResultValue)):
Domoticz.Unit(Name=dev['name'] + ' (kWh)', DeviceID=dev['id'], Unit=14, Type=243, Subtype=29, Used=1).Create()
#UpdateDevice(dev['id'], 14, '0;0', 0, 0, 1)
if createDevice(dev['id'], 15) and (searchCode('cur_current', ResultValue) and get_unit('cur_current', StatusProperties) == 'mA' or searchCode('leakage_current', ResultValue)):
options = {}
options['Custom'] = '1;mA'
Domoticz.Unit(Name=dev['name'] + ' (mA)', DeviceID=dev['id'], Unit=15, Type=243, Subtype=31, Options=options, Used=1).Create()
if createDevice(dev['id'], 16) and searchCode('temp_current', ResultValue):
Domoticz.Unit(Name=dev['name'] + ' (Temperature)', DeviceID=dev['id'], Unit=16, Type=80, Subtype=5, Used=1).Create()
if dev_type == 'cover' and createDevice(dev['id'], 1):
Domoticz.Log('Create device Cover')
if searchCode('position', StatusProperties):
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=21, Used=1).Create()
else:
Domoticz.Unit(Name=dev['name'], DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=14, Used=1).Create()
if dev_type == 'smartheatpump':
if createDevice(dev['id'], 1) and searchCode('switch', FunctionProperties):
Domoticz.Log('Create device Smartheatpump')
Domoticz.Unit(Name=dev['name'] + ' (On/Off)', DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=0, Image=9, Used=1).Create()
if createDevice(dev['id'], 2) and searchCode('intemp', ResultValue):
Domoticz.Unit(Name=dev['name'] + ' (INtemp)', DeviceID=dev['id'], Unit=2, Type=80, Subtype=5, Used=1).Create()
if createDevice(dev['id'], 3) and searchCode('outtemp', ResultValue):
Domoticz.Unit(Name=dev['name'] + ' (OUTtemp)', DeviceID=dev['id'], Unit=3, Type=80, Subtype=5, Used=1).Create()
if createDevice(dev['id'], 4) and searchCode('whjtemp', ResultValue):
Domoticz.Unit(Name=dev['name'] + ' (AMBtemp)', DeviceID=dev['id'], Unit=4, Type=80, Subtype=5, Used=1).Create()
if createDevice(dev['id'], 5) and searchCode('cmptemp', ResultValue):
Domoticz.Unit(Name=dev['name'] + ' (COMPRtemp)', DeviceID=dev['id'], Unit=5, Type=80, Subtype=5, Used=1).Create()
if createDevice(dev['id'], 6) and searchCode('wttemp', ResultValue):
Domoticz.Unit(Name=dev['name'] + ' (DHWtemp)', DeviceID=dev['id'], Unit=6, Type=80, Subtype=5, Used=1).Create()
if createDevice(dev['id'], 7) and searchCode('hqtemp', ResultValue):
Domoticz.Unit(Name=dev['name'] + ' (rGAStemp)', DeviceID=dev['id'], Unit=7, Type=80, Subtype=5, Used=1).Create()
if createDevice(dev['id'], 8) and searchCode('cmp_act_frep', ResultValue):
options = {}
options['Custom'] = '1;Hz'
Domoticz.Unit(Name=dev['name'] + ' (COMPfrq)', DeviceID=dev['id'], Unit=8, Type=243, Subtype=31, Options=options, Used=1).Create()
if createDevice(dev['id'], 9) and searchCode('cmp_cur', ResultValue):
Domoticz.Unit(Name=dev['name'] + ' (COMPRcur)', DeviceID=dev['id'], Unit=9, Type=243, Subtype=23, Used=1).Create()
if createDevice(dev['id'], 10) and searchCode('dc_fan_speed', ResultValue):
options = {}
options['Custom'] = '1;Speed'
Domoticz.Unit(Name=dev['name'] + ' (FANspeed)', DeviceID=dev['id'], Unit=10, Type=243, Subtype=31, Options=options, Used=1).Create()
if createDevice(dev['id'], 11) and searchCode('ach_stemp', ResultValue):
for item in StatusProperties:
if item['code'] == 'ach_stemp':
the_values = json.loads(item['values'])
options = {}
options['ValueStep'] = the_values.get('step')
options['ValueMin'] = the_values.get('min')
options['ValueMax'] = the_values.get('max')
options['ValueUnit'] = the_values.get('unit')
Domoticz.Unit(Name=dev['name'] + ' (HEATtemp)', DeviceID=dev['id'], Unit=11, Type=242, Subtype=1, Options=options, Used=1).Create()
if createDevice(dev['id'], 12) and searchCode('wth_stemp', ResultValue):
for item in StatusProperties:
if item['code'] == 'wth_stemp':
the_values = json.loads(item['values'])
options = {}
options['ValueStep'] = the_values.get('step')
options['ValueMin'] = the_values.get('min')
options['ValueMax'] = the_values.get('max')
options['ValueUnit'] = the_values.get('unit')
Domoticz.Unit(Name=dev['name'] + ' (DHWtemp)', DeviceID=dev['id'], Unit=12, Type=242, Subtype=1, Options=options, Used=1).Create()
if createDevice(dev['id'], 13) and searchCode('aircond_temp_diff', ResultValue):
for item in StatusProperties:
if item['code'] == 'aircond_temp_diff':
the_values = json.loads(item['values'])
options = {}
options['ValueStep'] = the_values.get('step')
options['ValueMin'] = the_values.get('min')
options['ValueMax'] = the_values.get('max')
options['ValueUnit'] = the_values.get('unit')
Domoticz.Unit(Name=dev['name'] + ' (HE/COtemp-diff)', DeviceID=dev['id'], Unit=13, Type=242, Subtype=1, Options=options, Used=1).Create()
if createDevice(dev['id'], 14) and searchCode('wth_temp_diff', ResultValue):
for item in StatusProperties:
if item['code'] == 'wth_temp_diff':
the_values = json.loads(item['values'])
options = {}
options['ValueStep'] = the_values.get('step')
options['ValueMin'] = the_values.get('min')
options['ValueMax'] = the_values.get('max')
options['ValueUnit'] = the_values.get('unit')
Domoticz.Unit(Name=dev['name'] + ' (DHWtemp-diff)', DeviceID=dev['id'], Unit=14, Type=242, Subtype=1, Options=options, Used=1).Create()
if createDevice(dev['id'], 15) and searchCode('acc_stemp', ResultValue):
for item in StatusProperties:
if item['code'] == 'acc_stemp':
the_values = json.loads(item['values'])
options = {}
options['ValueStep'] = the_values.get('step')
options['ValueMin'] = the_values.get('min')
options['ValueMax'] = the_values.get('max')
options['ValueUnit'] = the_values.get('unit')
Domoticz.Unit(Name=dev['name'] + ' (ACCtemp)', DeviceID=dev['id'], Unit=15, Type=242, Subtype=1, Options=options, Used=1).Create()
if createDevice(dev['id'], 16) and searchCode('mode', FunctionProperties):
for item in FunctionProperties:
if item['code'] == 'mode':
the_values = json.loads(item['values'])
mode = ['off']
mode.extend(the_values.get('range'))
options = {}
options['LevelOffHidden'] = 'true'
options['LevelActions'] = ''
options['LevelNames'] = '|'.join(mode)
options['SelectorStyle'] = '0'
Domoticz.Unit(Name=dev['name'] + ' (Mode)', DeviceID=dev['id'], Unit=16, Type=244, Subtype=62, Switchtype=18, Options=options, Image=9, Used=1).Create()
if createDevice(dev['id'], 17) and searchCode('work_mode', FunctionProperties):
for item in FunctionProperties:
if item['code'] == 'work_mode':
the_values = json.loads(item['values'])
mode = ['off']
mode.extend(the_values.get('range'))
options = {}
options['LevelOffHidden'] = 'true'
options['LevelActions'] = ''
options['LevelNames'] = '|'.join(mode)
options['SelectorStyle'] = '0'
Domoticz.Unit(Name=dev['name'] + ' (WorkMode)', DeviceID=dev['id'], Unit=17, Type=244, Subtype=62, Switchtype=18, Options=options, Image=9, Used=1).Create()
if dev_type == 'thermostat' or dev_type == 'heater' or dev_type == 'heatpump':
if createDevice(dev['id'], 1):
Domoticz.Log('Create device Thermostat/heater/heatpump')
if searchCode('switch', FunctionProperties) or searchCode('switch_1', FunctionProperties):
Domoticz.Unit(Name=dev['name'] + ' (Power)', DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=0, Image=9, Used=1).Create()
else:
Domoticz.Unit(Name=dev['name'] + ' (Power)', DeviceID=dev['id'], Unit=1, Type=244, Subtype=73, Switchtype=0, Image=9, Used=0).Create()
if createDevice(dev['id'], 2) and (searchCode('temp_current', StatusProperties) or searchCode('upper_temp', StatusProperties) or searchCode('c_temperature', StatusProperties)):
Domoticz.Unit(Name=dev['name'] + ' (Temperature)', DeviceID=dev['id'], Unit=2, Type=80, Subtype=5, Used=1).Create()
if createDevice(dev['id'], 3) and (searchCode('set_temp', FunctionProperties) or searchCode('temp_set', FunctionProperties)):
if searchCode('set_temp', FunctionProperties):
temp = 'set_temp'
else:
temp = 'temp_set'
for item in StatusProperties:
if item['code'] == temp:
the_values = json.loads(item['values'])
options = {}
options['ValueStep'] = the_values.get('step')
options['ValueMin'] = the_values.get('min')
options['ValueMax'] = the_values.get('max')
options['ValueUnit'] = the_values.get('unit')
Domoticz.Unit(Name=dev['name'] + ' (Thermostat)', DeviceID=dev['id'], Unit=3, Type=242, Subtype=1, Options=options, Used=1).Create()
if createDevice(dev['id'], 4) and searchCode('mode', FunctionProperties) and product_id != 'al8g1qdamyu5cfcc':
if dev_type == 'thermostat':
image = 16
elif dev_type == 'heater':
image = 15
else:
image = 7
for item in FunctionProperties:
if searchCode('work_mode', FunctionProperties):
mode = 'work_mode'
else:
mode = 'mode'
if item['code'] == mode:
# if product_id == 'al8g1qdamyu5cfcc':
# options = {}
# options['LevelOffHidden'] = 'true'
# options['LevelActions'] = ''
# options['LevelNames'] = 'off|auto|a_silent|a_powerful|heat|h_powerful|h_silent|cool|c_powerful|c_silent'
# options['SelectorStyle'] = '1'
# else:
the_values = json.loads(item['values'])