-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
fonctions.py
1097 lines (908 loc) · 30.2 KB
/
fonctions.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
#!/usr/bin/env python3
# coding: utf-8 -*-
from struct import unpack
import json
#import DomoticzEx as Domoticz
import Domoticz
buffercommand = {}
BOOLEAN_SENSOR = ['flag', 'water', 'fire', 'presence', 'carbonmonoxide', 'daylight', 'alarm', 'lock']
#****************************************************************************************************
# Global fonctions
def get_JSON_payload(data):
"""Parse length of payload and return it."""
start = 2
length = ord(data[1:2])
if length == 126:
# Payload information are an extra 2 bytes.
start = 4
length, = unpack(">H", data[2:4])
elif length == 127:
# Payload information are an extra 6 bytes.
start = 8
length, = unpack(">I", data[2:6])
end = start + length
payload = json.loads(data[start:end].decode())
extra_data = data[end:]
return payload, extra_data
def DecodeByteArray(stringStreamIn):
# Turn string values into opererable numeric byte values
byteArray = [ord(character) for character in stringStreamIn]
datalength = byteArray[1] & 127
indexFirstMask = 2
if datalength == 126:
indexFirstMask = 4
elif datalength == 127:
indexFirstMask = 10
indexFirstDataByte = indexFirstMask
#Data are masked ?
if byteArray[1] & 128:
# Extract masks
masks = [m for m in byteArray[indexFirstMask : indexFirstMask+4]]
indexFirstDataByte = indexFirstDataByte + 4
print (str(masks))
else:
masks = [0,0,0,0]
# List of decoded characters
decodedChars = []
i = indexFirstDataByte
j = 0
# Loop through each byte that was received
while i < len(byteArray):
# Unmask this byte and add to the decoded buffer
decodedChars.append( chr(byteArray[i] ^ masks[j % 4]) )
i += 1
j += 1
# Return the decoded string
return ''.join(decodedChars).strip()
def rgb_to_xy(rgb):
''' convert rgb tuple to xy tuple '''
red, green, blue = rgb
r = ((red + 0.055) / (1.0 + 0.055))**2.4 if (red > 0.04045) else (red / 12.92)
g = ((green + 0.055) / (1.0 + 0.055))**2.4 if (green > 0.04045) else (green / 12.92)
b = ((blue + 0.055) / (1.0 + 0.055))**2.4 if (blue > 0.04045) else (blue / 12.92)
X = r * 0.664511 + g * 0.154324 + b * 0.162028
Y = r * 0.283881 + g * 0.668433 + b * 0.047685
Z = r * 0.000088 + g * 0.072310 + b * 0.986039
cx = 0
cy = 0
if (X + Y + Z) != 0:
cx = X / (X + Y + Z)
cy = Y / (X + Y + Z)
return (cx, cy)
def rgb_to_hsl(rgb):
''' convert rgb tuple to hls tuple '''
r, g, b = rgb
r = float(r/255)
g = float(g/255)
b = float(b/255)
high = max(r, g, b)
low = min(r, g, b)
h, s, l = ((high + low) / 2,)*3
if high == low:
h = 0.0
s = 0.0
else:
d = high - low
s = d / (2 - high - low) if l > 0.5 else d / (high + low)
h = {
r: (g - b) / d + (6 if g < b else 0),
g: (b - r) / d + 2,
b: (r - g) / d + 4,
}[high]
h /= 6
return h, s, l
def hsl_to_rgb(h, s, l):
def hue_to_rgb(p, q, t):
t += 1 if t < 0 else 0
t -= 1 if t > 1 else 0
if t < 1/6: return p + (q - p) * 6 * t
if t < 1/2: return q
if t < 2/3: p + (q - p) * (2/3 - t) * 6
return p
if s == 0:
r, g, b = l, l, l
else:
q = l * (1 + s) if l < 0.5 else l + s - l * s
p = 2 * l - q
r = hue_to_rgb(p, q, h + 1/3)
g = hue_to_rgb(p, q, h)
b = hue_to_rgb(p, q, h - 1/3)
return r, g, b
def xy_to_rgb(x, y, brightness = 1):
x = float(x)
y = float(y)
z = 1.0 - x - y;
#Bad values
if x == 0 or y == 0:
return {'r': 0, 'g': 0, 'b': 0}
Y = brightness
X = (Y / y) * x
Z = (Y / y) * z
r = X * 1.656492 - Y * 0.354851 - Z * 0.255038;
g = -X * 0.707196 + Y * 1.655397 + Z * 0.036152;
b = X * 0.051713 - Y * 0.121364 + Z * 1.011530;
r = 12.92 * r if r <= 0.0031308 else (1.0 + 0.055) * pow(r, (1.0 / 2.4)) - 0.055
g = 12.92 * g if g <= 0.0031308 else (1.0 + 0.055) * pow(g, (1.0 / 2.4)) - 0.055
b = 12.92 * b if b <= 0.0031308 else (1.0 + 0.055) * pow(b, (1.0 / 2.4)) - 0.055
if (r > b) and (r > g):
if (r > 1.0):
g = g / r
b = b / r
r = 1.0
elif (g > b) and (g > r):
if (g > 1.0):
r = r / g
b = b / g
g = 1.0
elif (b > r) and (b > g):
if (b > 1.0):
r = r / b
g = g / b
b = 1.0
return {'r': int(r * 255), 'g': int(g * 255), 'b': int(b * 255)}
def rgb_to_hsv(rgb):
''' convert rgb tuple to hls tuple '''
r, g, b = rgb
r = float(r/255)
g = float(g/255)
b = float(b/255)
high = max(r, g, b)
low = min(r, g, b)
h, s, v = high, high, high
d = high - low
s = 0 if high == 0 else d/high
if high == low:
h = 0.0
else:
h = {
r: (g - b) / d + (6 if g < b else 0),
g: (b - r) / d + 2,
b: (r - g) / d + 4,
}[high]
h /= 6
return h, s, v
def hsv_to_rgb(h, s, v):
i = math.floor(h*6)
f = h*6 - i
p = v * (1-s)
q = v * (1-f*s)
t = v * (1-(1-f)*s)
r, g, b = [
(v, t, p),
(q, v, p),
(p, v, t),
(p, q, v),
(t, p, v),
(v, p, q),
][int(i%6)]
return r, g, b
def Count_Type(d):
b = l = s = g = o = c = 0
for i in d:
if d[i]['type'] == 'lights':
l += 1
elif d[i]['type'] == 'sensors':
s += 1
elif d[i]['type'] == 'groups':
g += 1
elif d[i]['type'] == 'scenes':
c += 1
else:
o += 1
if d[i].get('state','unknow') == 'banned':
b += 1
return l,s,g,b,o,c
def First_Json(data):
s = ''
b = 0
for c in data:
s += c
if c == '{':
b += 1
if c == '}':
b -= 1
if b == 0:
return s
return False
def JSON_Repair(data):
s = e = p = 0
c = 0
b = ''
while True:
if c == 0:
p = data.find('[',p)
s = p
else:
p = data.find(']',p)
e = p
c = 1 - c
if p == -1:
break
if e > s:
if len(b) > 0:
b += ','
b += data[s + 1:e]
return '[' + b + ']'
#*********************************
# Table for switch
#
#*********************************
DevelcoModuleTable = ['1003','1001']
XiaomiSingleGangButtonSwitchTable = ['1002','1004','1001']
XiaomiDoubleGangButtonSwitchTable = ['1002','2002','3002','1004','2004','3004','1001','2001','3001']
IkeaStyrbarButtonSwitchTable = ['1001','1002','1003','2001','2002','2003','3001','3002','3003','4001','4002','4003','5001','5002','5003']
#0 - Off
#single press
#10 - B1 - 1002
#20 - B2 - 2002
#30 - B3 - 3002
#40 - B4 - 4002
#50 - B5 - 5002
#60 - B6 - 6002
#long press
#70 - B1L - 1001
#80 - B2L - 2001
#90 - B3L - 3001
#100 - B4L - 4001
#110 - B5L - 5001
#120 - B6L - 6001
#release afer long press (not working for some reason)
#130 - B1RL - 1003
#140 - B2RL - 2003
#150 - B3RL - 3003
#160 - B4RL - 4003
#170 - B5RL - 5003
#180 - B6RL - 6003
#double press
#190 - B1D - 1004
#200 - B2D - 2004
#210 - B3D - 3004
#220 - B4D - 4004
#230 - B5D - 5004
#240 - B6D - 6004
#tripple press
#250 - B1T - 1005
#260 - B2T - 2005
#270 - B3T - 3005
#280 - B4T - 4005
#290 - B5T - 5005
#300 - B6T - 6005
XiaomiOpple6ButtonSwitchTable = ['1002','2002','3002','4002','5002','6002','1001','2001','3001','4001','5001','6001','1003','2003','3003','4003','5003','6003',
'1004','2004','3004','4004','5004','6004','1005','2005','3005','4005','5005','6005']
TuyaXGangButtonSwitchTable = ['1002','1003','1004','2002','2003','2004','3002','3003','3004','4002','4003','4004']
PhilipsRWL02ButtonSwitchTable = ['1002','1003','2002','2003','3002','3003','4002','4003']
#**************************************************************************************************
# Domoticz fonctions
#
#https://github.com/dresden-elektronik/deconz-rest-plugin/wiki/Supported-Devices
def ProcessAllConfig(data,model,option):
kwarg = {}
buffercommand.clear()
if 'battery' in data:
kwarg.update(ReturnUpdateValue( 'battery' , data['battery'] ) )
if 'heatsetpoint' in data:
kwarg.update(ReturnUpdateValue( 'heatsetpoint' , data['heatsetpoint'] ) )
if 'mode' in data:
#if not (data['mode'] == 'off' and data['on'] == True):
kwarg.update(ReturnUpdateValue( 'mode' , data['mode'] , model ) )
if 'preset' in data:
kwarg.update(ReturnUpdateValue( 'preset' , data['preset'] ) )
if 'lock' in data:
kwarg.update(ReturnUpdateValue( 'lock' , data['lock'] ) )
if 'reachable' in data:
if data['reachable'] == False:
kwarg.update({'TimedOut':1})
return kwarg
def ProcessAllState(data,model,option):
# Lux need to be > lightlevel > daylight > dark
# xy > ct > bri > on/off
# consumption > power
# status > daylight > all
# alert need to be first, bcause less important than other
buffercommand.clear()
kwarg = {}
if 'alert' in data:
kwarg.update(ReturnUpdateValue('alert', data['alert'], model))
if 'status' in data:
kwarg.update(ReturnUpdateValue('status', data['status']))
if 'measured_value' in data:
kwarg.update(ReturnUpdateValue('airqualityppb', data['measured_value'], option))
if 'pm2_5' in data:
kwarg.update(ReturnUpdateValue('airqualityppb', data['pm2_5'], option))
if 'on' in data:
kwarg.update(ReturnUpdateValue('on', data['on'], model))
if 'x' in data:
kwarg.update(ReturnUpdateValue('x', data['x']))
if 'y' in data:
kwarg.update(ReturnUpdateValue('y', data['y']))
if 'xy' in data:
kwarg.update(ReturnUpdateValue('xy', data['xy']))
if 'ct' in data:
kwarg.update(ReturnUpdateValue('ct', data['ct']))
if 'temperature' in data:
kwarg.update(ReturnUpdateValue('temperature', data['temperature']))
if 'pressure' in data:
kwarg.update(ReturnUpdateValue('pressure', data['pressure']))
if 'humidity' in data:
kwarg.update(ReturnUpdateValue('humidity', data['humidity']))
if 'moisture' in data:
kwarg.update(ReturnUpdateValue('moisture', data['moisture']))
if 'open' in data:
kwarg.update(ReturnUpdateValue('open', data['open']))
if 'presence' in data:
kwarg.update(ReturnUpdateValue('presence', data['presence']))
if 'daylight' in data:
kwarg.update(ReturnUpdateValue('daylight', data['daylight']))
#if 'lightlevel' in data:
# kwarg.update(ReturnUpdateValue('lightlevel', data['lightlevel']))
if 'lux' in data:
kwarg.update(ReturnUpdateValue('lux', data['lux']))
if 'power' in data:
kwarg.update(ReturnUpdateValue('power', data['power']))
if 'consumption' in data:
kwarg.update(ReturnUpdateValue('consumption', data['consumption'], option))
if 'battery' in data:
kwarg.update(ReturnUpdateValue('battery', data['battery']))
if 'buttonevent' in data:
kwarg.update(ReturnUpdateValue('buttonevent', data['buttonevent']))
if 'flag' in data:
kwarg.update(ReturnUpdateValue('flag', data['flag']))
if 'water' in data:
kwarg.update(ReturnUpdateValue('water', data['water']))
if 'fire' in data:
kwarg.update(ReturnUpdateValue('fire', data['fire']))
if 'alarm' in data:
kwarg.update(ReturnUpdateValue('alarm', data['alarm']))
if 'carbonmonoxide' in data:
kwarg.update(ReturnUpdateValue('carbonmonoxide', data['carbonmonoxide']))
if 'lockstate' in data:
kwarg.update(ReturnUpdateValue('lockstate', data['lockstate']))
if 'airqualityppb' in data:
kwarg.update(ReturnUpdateValue('airqualityppb', data['airqualityppb']))
if 'bri' in data:
kwarg.update(ReturnUpdateValue('bri', data['bri'], model) )
if 'lift' in data:
kwarg.update(ReturnUpdateValue('lift', data['lift'], model) )
if 'voltage' in data:
kwarg.update(ReturnUpdateValue( 'voltage' , data['voltage'], model) )
if 'current' in data:
kwarg.update(ReturnUpdateValue( 'current' , data['current'], model ) )
if 'action' in data:
kwarg.update(ReturnUpdateValue( 'action' , data['action'], model ) )
if 'speed' in data:
kwarg.update(ReturnUpdateValue( 'speed' , data['speed'], model ) )
if 'expectedrotation' in data:
kwarg.update(ReturnUpdateValue( 'expectedrotation' , data['expectedrotation'], model ) )
#if 'lastupdated' in data:
# kwarg.update(ReturnUpdateValue('lastupdated', data['lastupdated']))
#For groups
if 'all_on' in data:
kwarg.update(ReturnUpdateValue('all_on', data['all_on']))
if 'any_on' in data:
kwarg.update(ReturnUpdateValue('any_on', data['any_on']))
#Special
if 'reachable' in data:
if data['reachable'] == False:
kwarg.update({'TimedOut':1})
return kwarg
def ReturnUpdateValue(command, val ,option = None):
if not val:
val = 0
val = str(val)
command = str(command)
kwarg = {}
#operator
if command == 'on':
if val == 'True':
kwarg['nValue'] = 1
if option == 'Window covering device':
kwarg['sValue'] = '100'
else:
kwarg['sValue'] = 'on'
else:
kwarg['nValue'] = 0
if option == 'Window covering device':
kwarg['sValue'] = '0'
else:
kwarg['sValue'] = 'off'
if command == 'alert':
#Can be none, lselect, select, blink
if val == 'select':
kwarg['nValue'] = 10
kwarg['sValue'] = '10'
elif val == 'lselect':
kwarg['nValue'] = 20
kwarg['sValue'] = '20'
elif val == 'blink':
kwarg['nValue'] = 30
kwarg['sValue'] = '30'
else:
kwarg['nValue'] = 0
kwarg['sValue'] = 'Off'
if command == 'lift':
val = int(val)
if val <= 0:
kwarg['sValue'] = '0'
kwarg['nValue'] = 0
elif val >= 100:
kwarg['sValue'] = '100'
kwarg['nValue'] = 1
else:
kwarg['sValue'] = str(val)
kwarg['nValue'] = 2
if command == 'bri':
#kwarg['nValue'] = 1
val = int(float(val) * 100 / 255 )
if option == 'Window covering device':
if val < 2:
kwarg['sValue'] = '0'
kwarg['nValue'] = 0
elif val > 99:
kwarg['sValue'] = '100'
kwarg['nValue'] = 1
else:
kwarg['sValue'] = str(val)
kwarg['nValue'] = 2
else:
kwarg['sValue'] = str(val)
if command == 'x' or command == 'y':
buffercommand[command] = val
if buffercommand.get('x') and buffercommand.get('y'):
rgb = xy_to_rgb(int(buffercommand['x'])/65536.0,int(buffercommand['y'])/65536.0,1)
kwarg['Color'] = '{"b":' + str(rgb['b']) + ',"cw":0,"g":' + str(rgb['g']) + ',"m":3,"r":' + str(rgb['r']) + ',"t":0,"ww":0}'
buffercommand.clear()
if command == 'xy':
x,y = eval(str(val))
rgb = xy_to_rgb(x,y,1)
#kwarg['nValue'] = 1
#kwarg['sValue'] = str(255)
kwarg['Color'] = '{"b":' + str(rgb['b']) + ',"cw":0,"g":' + str(rgb['g']) + ',"m":3,"r":' + str(rgb['r']) + ',"t":0,"ww":0}'
if command == 'ct':
#Correct values are from 153 (6500K) up to 588 (1700K) so uselss if < 1
if int(val) > 1:
ct = 1000000 // int(val)
ct = -((ct - 1700) / ((6500.0-1700.0)/255.0) - 255 )
ct = int(ct)
if ct < 0:
ct = 0
if ct > 255:
ct = 255
#kwarg['nValue'] = 1
#kwarg['sValue'] = str(255)
if not 'Color' in kwarg:
kwarg['Color'] = '{"m":2,"r":0,"g":0,"b":0,"t":' + str(ct) + ',"ww":' + str(ct) + ',"cw":' + str(255 - ct) + '}'
#groups
if command == 'all_on' or command == 'any_on':
if val == 'True':
kwarg['nValue'] = 1
kwarg['sValue'] = 'on'
else:
kwarg['nValue'] = 0
kwarg['sValue'] = 'off'
#sensor
if command == 'open':
if val == 'True':
kwarg['nValue'] = 1
kwarg['sValue'] = 'Open'
else:
kwarg['nValue'] = 0
kwarg['sValue'] = 'Closed'
if command == 'lockstate':
if val == 'locked':
kwarg['nValue'] = 0
kwarg['sValue'] = 'Closed'
else:
kwarg['nValue'] = 1
kwarg['sValue'] = 'Open'
if command in BOOLEAN_SENSOR:
if val == 'True':
kwarg['nValue'] = 1
kwarg['sValue'] = 'On'
else:
kwarg['nValue'] = 0
kwarg['sValue'] = 'Off'
if command == 'temperature':
kwarg['nValue'] = 0
val = round(float(val) / 100, 2 )
kwarg['sValue'] = str(val)
if command == 'heatsetpoint':
val = round( int(val) / 100, 2 )
#ignore boost and off value
if val != 5 and val != 30:
kwarg['heatsetpoint'] = str(val)
if command == 'mode':
if val == 'off':
kwarg['mode'] = 0
if option == 'ZHAAirPurifier':
#ventilator
if val == 'auto':
kwarg['mode'] = 10
if val == 'speed_1':
kwarg['mode'] = 20
if val == 'speed_2':
kwarg['mode'] = 30
if val == 'speed_3':
kwarg['mode'] = 40
if val == 'speed_4':
kwarg['mode'] = 50
if val == 'speed_5':
kwarg['mode'] = 26
else:
#thermostat
if val == 'heat':
kwarg['mode'] = 10
if val == 'auto':
kwarg['mode'] = 20
if command == 'preset':
if val == 'off':
kwarg['preset'] = 0
if val == 'holiday':
kwarg['preset'] = 10
if val == 'auto':
kwarg['preset'] = 20
if val == 'manual':
kwarg['preset'] = 30
if val == 'comfort':
kwarg['preset'] = 40
if val == 'eco':
kwarg['preset'] = 50
if val == 'boost':
kwarg['preset'] = 60
if val == 'complex':
kwarg['preset'] = 70
if val == 'program':
kwarg['preset'] = 80
if command == 'status':
if int(float(val)) == 0:
kwarg['nValue'] = 0
kwarg['sValue'] = str(val)
else:
kwarg['nValue'] = 1
kwarg['sValue'] = str(val)
if command == 'pressure':
val = int(float(val))
if val < 1000:
Bar_forecast = 4
elif val < 1020:
Bar_forecast = 3
elif val < 1030:
Bar_forecast = 2
else:
Bar_forecast = 1
kwarg['nValue'] = 0
kwarg['sValue'] = str(val) + ';' + str(Bar_forecast)
# 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
if command == 'humidity':
val = int(float(val) / 100)
kwarg['nValue'] = val
if val <= 40:
kwarg['sValue'] = '2'
elif val<=70:
kwarg['sValue'] = '1'
else:
kwarg['sValue'] = '3'
if command == 'moisture':
val = int(float(val) / 100)
# Val is a value 0 to 100, need to be converted in 0 to 200
#00 - 09 = saturated
#10 - 19 = adequately wet
#20 - 59 = irrigation advice
#60 - 99 = irrigation
#100-200 = Dangerously dry
kwarg['nValue'] = (100-val) * 2
if (command == 'lightlevel') or (command == 'lux'):
kwarg['nValue'] = 0
kwarg['sValue'] = str(val)
if command == 'voltage':
kwarg['voltage'] = int(val)
if command == 'current':
kwarg['current'] = int(val)
if command == 'airqualityppb':
kwarg['sValue'] = str(float(val))
kwarg['nValue'] = int(val)
if command == 'action':
kwarg['nValue'] = 0
kwarg['sValue'] = str(val)
if command == 'speed':
kwarg['nValue'] = 0
kwarg['sValue'] = str(val)
if command == 'consumption_2':
buffercommand['consumption_2'] = round(float(val) * 1, 3)
if command == 'consumption':
#Wh to Kwh
kwh = round(float(val) * 1, 3)
#Device with power and comsuption
if option == 1:
if buffercommand.get('power'):
p = buffercommand['power']
buffercommand.clear()
kwarg['nValue'] = 0
kwarg['sValue'] = str(p) + ';' + str(kwh)
#device with only consumption
elif option == 0:
kwarg['nValue'] = 0
kwarg['sValue'] = str(kwh)
#Device with consumption_2
else:
if buffercommand.get('consumption_2'):
p = buffercommand['consumption_2']
buffercommand.clear()
kwarg['nValue'] = 0
kwarg['sValue'] = str(kwh) + ';' + str(p) + ';0;0;0;0'
if command == 'power':
buffercommand['power'] = val
kwarg['nValue'] = 0
kwarg['sValue'] = str(val)
if command == 'expectedrotation':
kwarg['nValue'] = int(val)
if kwarg['nValue'] == 0:
kwarg['sValue'] = 'Off'
else:
kwarg['sValue'] = str( kwarg['nValue'] )
#switch
if command == 'buttonevent':
kwarg['nValue'] = int(val)
kwarg['sValue'] = str(val)
# other
if command == 'battery':
if (not val.isdigit()) or (val == '0'):
kwarg['BatteryLevel'] = 255
else:
kwarg['BatteryLevel'] = int(val)
if command == 'xxxx':
kwarg['SignalLevel'] = 100
#if command == 'lastupdated':
# kwarg['LastUpdate'] = val.replace('T',' ')
return kwarg
#https://github.com/dresden-elektronik/deconz-rest-plugin/issues/138
def ButtonconvertionXCUBE_R(val):
kwarg = {}
kwarg['nValue'] = int(val)
if kwarg['nValue'] == 0:
kwarg['sValue'] = 'Off'
else:
kwarg['sValue'] = str( kwarg['nValue'] )
return kwarg
def ButtonconvertionXCUBET1_R(val):
kwarg = {}
kwarg['nValue'] = int(val)
if kwarg['nValue'] == 0:
kwarg['sValue'] = 'Off'
else:
kwarg['sValue'] = str( kwarg['nValue'] )
return kwarg
def ButtonconvertionXCUBE(val):
kwarg = {}
val = str(val)
v = 0
if val == '7007':# shake
v = 10
elif val == '7000': # wake
v = 20
elif val == '7008':# drop
v = 30
elif int(val[3]) == (7 - int(val[0])) :# 180 flip
v = 50
elif val[1:] == '000':# push
v = 60
elif val[0] == val[3]:# double tap
v = 70
else:# 90 flip
v = 40
if v == 0:
kwarg['sValue'] = 'Off'
else:
kwarg['sValue'] = str( v )
kwarg['nValue'] = int(val)
return kwarg
def ButtonconvertionXCUBET1(val, gesture):
kwarg = {}
gest = int(gesture)
face = str(val)
v = 0
if gest == 0: # wake
v = 20
elif gest == 1: # shake
v = 10
elif gest == 2: # Drop
v = 30
elif gest == 3: # 90 flip
v = 40
elif gest == 4: # 180 flip
v = 50
elif gest == 5: # push
v = 60
elif gest == 6: # double tap
v = 70
else: # Unknown
v = 0
if v == 0:
kwarg['sValue'] = 'Off'
else:
kwarg['sValue'] = str( v )
kwarg['nValue'] = v
return kwarg
# <=4002 >=5002 +=2002 -=3002 4001/5001/2001/3001
def ButtonconvertionTradfriRemote(val):
kwarg = {}
val = str(val)
if val == '1002' or val == 1001:
kwarg['nValue'] = 10
elif val == '2002':
kwarg['nValue'] = 20
elif val == '3002':
kwarg['nValue'] = 30
elif val == '4002':
kwarg['nValue'] = 40
elif val == '5002':
kwarg['nValue'] = 50
elif val == '2001':
kwarg['nValue'] = 60
elif val == '3001':
kwarg['nValue'] = 70
elif val == '4001':
kwarg['nValue'] = 80
elif val == '5001':
kwarg['nValue'] = 90
else:
kwarg['nValue'] = 0
if kwarg['nValue'] == 0:
kwarg['sValue'] = 'Off'
else:
kwarg['sValue'] = str( kwarg['nValue'] )
return kwarg
def ButtonconvertionTradfriSwitch(val):
kwarg = {}
val = str(val)
if val == '1002':
kwarg['nValue'] = 10
elif val == '1003':
kwarg['nValue'] = 20
elif val == '2002':
kwarg['nValue'] = 30
elif val == '2003':
kwarg['nValue'] = 40
elif val == '3002':
kwarg['nValue'] = 50
elif val == '4002':
kwarg['nValue'] = 60
elif val == '5002':
kwarg['nValue'] = 70
else:
kwarg['nValue'] = 0
if kwarg['nValue'] == 0:
kwarg['sValue'] = 'Off'
else:
kwarg['sValue'] = str( kwarg['nValue'] )
return kwarg
def ButtonConvertion(val,model = 0):
kwarg = {}
kwarg['nValue'] = 0
#Generic procedure
if model == 0:
v = 0
Button_Number = 1
e = int(val or 0)
#Green power device
if e < 1000:
t = [(1,16),(2,17,32),(3,18,33),(4,34),(5,98),(6,99),(7,100),(8,101)]
for i in range(len(t)):
if e in t[i]:
v = (i + 1) * 10
break
#Normal switch
else:
val = "%04d" % val
Button_Number = val[0]
Button_Action = val[3]
if Button_Action == '2': # Release (after press)
v = 10
if Button_Action == '3': # Release (after hold)
v = 20
if Button_Action == '4': # Double press
v = 30
if Button_Action == '5': # Triple press
v = 40
if Button_Action == '6': # Quadruple press
v = 50
if Button_Action == '7': # shake
v = 60
kwarg['nValue'] = v * int(Button_Number)
else:
val = str(val)
#Xaomi double gang
if model == 1:
if val in XiaomiDoubleGangButtonSwitchTable:
kwarg['nValue'] = 10 * (1 + XiaomiDoubleGangButtonSwitchTable.index(val))
#Xaomi Opple 6 buttons
if model == 2:
if val in XiaomiOpple6ButtonSwitchTable:
kwarg['nValue'] = 10 * (1 + XiaomiOpple6ButtonSwitchTable.index(val))
if model == 3:
if val in XiaomiSingleGangButtonSwitchTable:
kwarg['nValue'] = 10 * (1 + XiaomiSingleGangButtonSwitchTable.index(val))
#Tuya generic
if model == 4:
if val in TuyaXGangButtonSwitchTable:
kwarg['nValue'] = 10 * (1 + TuyaXGangButtonSwitchTable.index(val))
#Philips remote
if model == 5:
if val in PhilipsRWL02ButtonSwitchTable:
kwarg['nValue'] = 10 * (1 + PhilipsRWL02ButtonSwitchTable.index(val))
#Ikea Styrbar
if model == 6:
if val in IkeaStyrbarButtonSwitchTable:
kwarg['nValue'] = 10 * (1 + IkeaStyrbarButtonSwitchTable.index(val))
#Develco Module
if model == 7:
if val in DevelcoModuleTable:
kwarg['nValue'] = 10 * (1 + DevelcoModuleTable.index(val))
if kwarg['nValue'] == 0:
kwarg['sValue'] = 'Off'
else:
kwarg['sValue'] = str( kwarg['nValue'] )