-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathac_db.py
More file actions
1495 lines (1269 loc) · 51.8 KB
/
ac_db.py
File metadata and controls
1495 lines (1269 loc) · 51.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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
"""Protocol implementantion for Broadlink AC DB devices. From https://github.com/liaan/broadlink_ac_mqtt"""
from datetime import datetime
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import time
import random
import socket
import threading
import struct
version = "1.1.3"
def gendevice(devtype, host, mac, name=None, cloud=None, update_interval=0):
# print format(devtype,'02x')
##We only care about 1 device type...
if devtype == 0x4E2A: # Danham Bush
return ac_db(
host=host,
mac=mac,
name=name,
cloud=cloud,
devtype=devtype,
update_interval=0,
)
if devtype == 0xFFFFFFF: # test
return ac_db_debug(
host=host,
mac=mac,
name=name,
cloud=cloud,
devtype=devtype,
update_interval=0,
)
else:
return device(
host=host, mac=mac, devtype=devtype, update_interval=update_interval
)
def discover(timeout=None, bind_to_ip=None):
if bind_to_ip is None:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 53)) # connecting to a UDP address doesn't send packets
bind_to_ip = s.getsockname()[0]
address = bind_to_ip.split(".")
cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
cs.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
cs.bind((bind_to_ip, 0))
port = cs.getsockname()[1]
starttime = time.time()
devices = []
timezone = int(time.timezone / -3600)
packet = bytearray(0x30)
year = datetime.now().year
if timezone < 0:
packet[0x08] = 0xFF + timezone - 1
packet[0x09] = 0xFF
packet[0x0A] = 0xFF
packet[0x0B] = 0xFF
else:
packet[0x08] = timezone
packet[0x09] = 0
packet[0x0A] = 0
packet[0x0B] = 0
packet[0x0C] = year & 0xFF
packet[0x0D] = year >> 8
packet[0x0E] = datetime.now().minute
packet[0x0F] = datetime.now().hour
subyear = str(year)[2:]
packet[0x10] = int(subyear)
packet[0x11] = datetime.now().isoweekday()
packet[0x12] = datetime.now().day
packet[0x13] = datetime.now().month
packet[0x18] = int(address[0])
packet[0x19] = int(address[1])
packet[0x1A] = int(address[2])
packet[0x1B] = int(address[3])
packet[0x1C] = port & 0xFF
packet[0x1D] = port >> 8
packet[0x26] = 6
checksum = 0xBEAF
for i in range(len(packet)):
checksum += packet[i]
checksum = checksum & 0xFFFF
packet[0x20] = checksum & 0xFF
packet[0x21] = checksum >> 8
cs.sendto(packet, ("255.255.255.255", 80))
if timeout is None:
response = cs.recvfrom(1024)
responsepacket = bytearray(response[0])
host = response[1]
mac = responsepacket[0x3A:0x40]
mac = mac[::-1] ##Flip correct
devtype = responsepacket[0x34] | responsepacket[0x35] << 8
name = responsepacket[0x40:].split(b"\x00")[0].decode("utf-8")
if not name:
name = mac
cloud = bool(responsepacket[-1])
cs.close()
return gendevice(devtype, host, mac, name=name, cloud=cloud)
else:
while (time.time() - starttime) < timeout:
cs.settimeout(timeout - (time.time() - starttime))
try:
response = cs.recvfrom(1024)
except socket.timeout:
return devices
responsepacket = bytearray(response[0])
# print ":".join("{:02x}".format(c) for c in responsepacket)
# print ":".join("{:c}".format(c) for c in responsepacket)
host = response[1]
devtype = responsepacket[0x34] | responsepacket[0x35] << 8
mac = responsepacket[0x3A:0x40]
mac = mac[::-1] ##flip Correct
name = responsepacket[0x40:].split(b"\x00")[0].decode("utf-8")
##Make sure there is some name
if not name:
name = mac
cloud = bool(responsepacket[-1])
dev = gendevice(devtype, host, mac, name=name, cloud=cloud)
devices.append(dev)
cs.close()
return devices
class device:
__INIT_KEY = "097628343fe99e23765c1513accf8b02"
__INIT_VECT = "562e17996d093d28ddb3ba695a2e6f58"
def __init__(
self,
host,
mac,
timeout=10,
name=None,
cloud=None,
devtype=None,
update_interval=0,
bind_to_ip=None,
):
self.host = host
self.mac = mac
self.name = name
self.cloud = cloud
self.timeout = timeout
self.devtype = devtype
self.count = random.randrange(0xFFFF)
##AES
self.key = bytearray(
[
0x09,
0x76,
0x28,
0x34,
0x3F,
0xE9,
0x9E,
0x23,
0x76,
0x5C,
0x15,
0x13,
0xAC,
0xCF,
0x8B,
0x02,
]
)
self.iv = bytearray(
[
0x56,
0x2E,
0x17,
0x99,
0x6D,
0x09,
0x3D,
0x28,
0xDD,
0xB3,
0xBA,
0x69,
0x5A,
0x2E,
0x6F,
0x58,
]
)
self.id = bytearray([0, 0, 0, 0])
self.cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.cs.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# self.cs.bind(('',0))
self.type = "Unknown"
self.lock = threading.Lock()
self.update_interval = update_interval
self.bind_to_ip = bind_to_ip
self.aes = None
self.update_aes(bytes.fromhex(self.__INIT_KEY))
def update_aes(self, key: bytes) -> None:
"""Update AES."""
self.aes = Cipher(
algorithms.AES(bytes(key)), modes.CBC(self.iv), backend=default_backend()
)
def encrypt(self, payload: bytes) -> bytes:
"""Encrypt the payload."""
encryptor = self.aes.encryptor()
return encryptor.update(bytes(payload)) + encryptor.finalize()
def decrypt(self, payload: bytes) -> bytes:
"""Decrypt the payload."""
decryptor = self.aes.decryptor()
return decryptor.update(bytes(payload)) + decryptor.finalize()
def auth(self):
payload = bytearray(0x50)
payload[0x04] = 0x31
payload[0x05] = 0x31
payload[0x06] = 0x31
payload[0x07] = 0x31
payload[0x08] = 0x31
payload[0x09] = 0x31
payload[0x0A] = 0x31
payload[0x0B] = 0x31
payload[0x0C] = 0x31
payload[0x0D] = 0x31
payload[0x0E] = 0x31
payload[0x0F] = 0x31
payload[0x10] = 0x31
payload[0x11] = 0x31
payload[0x12] = 0x31
payload[0x1E] = 0x01
payload[0x2D] = 0x01
payload[0x30] = ord("T")
payload[0x31] = ord("e")
payload[0x32] = ord("s")
payload[0x33] = ord("t")
payload[0x34] = ord(" ")
payload[0x35] = ord(" ")
payload[0x36] = ord("1")
response = self.send_packet(0x65, payload)
enc_payload = response[0x38:]
payload = self.decrypt(bytes(enc_payload))
if not payload:
return False
key = payload[0x04:0x14]
if len(key) % 16 != 0:
return False
self.id = payload[0x00:0x04]
self.key = key
self.update_aes(payload[0x04:0x14])
return True
def get_type(self):
return self.type
def send_packet(self, command, payload):
self.count = (self.count + 1) & 0xFFFF
packet = bytearray(0x38)
packet[0x00] = 0x5A
packet[0x01] = 0xA5
packet[0x02] = 0xAA
packet[0x03] = 0x55
packet[0x04] = 0x5A
packet[0x05] = 0xA5
packet[0x06] = 0xAA
packet[0x07] = 0x55
packet[0x24] = 0x2A # ==> Type
packet[0x25] = 0x4E # ==> Type
packet[0x26] = command
packet[0x28] = self.count & 0xFF
packet[0x29] = self.count >> 8
packet[0x2A] = self.mac[0]
packet[0x2B] = self.mac[1]
packet[0x2C] = self.mac[2]
packet[0x2D] = self.mac[3]
packet[0x2E] = self.mac[4]
packet[0x2F] = self.mac[5]
packet[0x30] = self.id[0]
packet[0x31] = self.id[1]
packet[0x32] = self.id[2]
packet[0x33] = self.id[3]
checksum = 0xBEAF
for i in range(len(payload)):
checksum += payload[i]
checksum = checksum & 0xFFFF
payload = self.encrypt(bytes(payload))
packet[0x34] = checksum & 0xFF
packet[0x35] = checksum >> 8
for i in range(len(payload)):
packet.append(payload[i])
checksum = 0xBEAF
for i in range(len(packet)):
checksum += packet[i]
checksum = checksum & 0xFFFF
packet[0x20] = checksum & 0xFF
packet[0x21] = checksum >> 8
# print 'Sending Packet:\n'+''.join(format(x, '02x') for x in packet)+"\n"
starttime = time.time()
with self.lock:
while True:
try:
self.cs.sendto(packet, self.host)
self.cs.settimeout(5)
response = self.cs.recvfrom(1024)
break
except socket.timeout:
if (time.time() - starttime) < self.timeout:
pass
raise ConnectTimeout(200, self.host)
return bytearray(response[0])
# ******************************************************** ac db debug class *****************************************
class ac_db(device):
import logging
type = "ac_db"
class STATIC:
##Static stuff
class FIXATION:
class VERTICAL:
# STOP= 0b00000000
TOP = 0b00000001
MIDDLE1 = 0b00000010
MIDDLE2 = 0b00000011
MIDDLE3 = 0b00000100
BOTTOM = 0b00000101
SWING = 0b00000110
AUTO = 0b00000111
class HORIZONTAL: ##Don't think this really works for all devices.
LEFT_FIX = 2
LEFT_FLAP = 1
LEFT_RIGHT_FIX = 7
LEFT_RIGHT_FLAP = 0
RIGHT_FIX = 6
RIGHT_FLAP = 5
ON = 0
OFF = 1
class FAN:
LOW = 0b00000011
MEDIUM = 0b00000010
HIGH = 0b00000001
AUTO = 0b00000101
NONE = 0b00000000
class MODE:
COOLING = 0b00000001
DRY = 0b00000010
HEATING = 0b00000100
AUTO = 0b00000000
FAN = 0b00000110
class ONOFF:
OFF = 0
ON = 1
def get_ac_status(self, force_update=False):
##Get AC info(also populates the current temp)
self.logger.debug("Getting AC Info")
status = self.get_ac_info()
self.logger.debug("AC Info Retrieved")
return status
def __init__(
self,
host,
mac,
name=None,
cloud=None,
debug=False,
update_interval=0,
devtype=None,
bind_to_ip=None,
):
device.__init__(
self,
host,
mac,
name=name,
cloud=cloud,
devtype=devtype,
update_interval=update_interval,
)
devtype = devtype
self.status = {}
self.logger = self.logging.getLogger(__name__)
self.update_interval = update_interval
##Set default values
# mac = mac[::-1]
self.set_default_values()
self.status["macaddress"] = "".join(format(x, "02x") for x in mac)
self.status["hostip"] = host
self.status["name"] = name
self.status["lastupdate"] = 0
self.logging.basicConfig(
level=(self.logging.DEBUG if debug else self.logging.INFO)
)
self.logger.debug("Debugging Enabled")
##Populate array with latest data
self.logger.debug("Authenticating")
if self.auth() == False:
self.logger.critical("Authentication Failed to AC")
return False
self.logger.debug("Getting current details in init")
##Get the current details
self.get_ac_status(force_update=True)
def get_ac_status(self, force_update=False):
##Check if the status is up to date to reduce timeout issues. Can be overwritten by force_update
self.logger.debug("Last update was: %s" % self.status["lastupdate"])
if (
force_update == False
and (self.status["lastupdate"] + self.update_interval) > time.time()
):
return self.make_nice_status(self.status)
##Get AC info(also populates the current temp)
self.logger.debug("Getting AC Info")
self.get_ac_info()
self.logger.debug("AC Info Retrieved")
##Get the current status ... get_ac_states does make_nice_status in return.
self.logger.debug("Getting AC States")
status = self.get_ac_states(True)
self.logger.debug("AC States retrieved")
return status
def set_default_values(self):
self.status["temp"] = float(19)
self.status["fixation_v"] = self.STATIC.FIXATION.VERTICAL.AUTO
self.status["power"] = self.STATIC.ONOFF.ON
self.status["mode"] = self.STATIC.MODE.AUTO
self.status["sleep"] = self.STATIC.ONOFF.OFF
self.status["display"] = self.STATIC.ONOFF.ON
self.status["health"] = self.STATIC.ONOFF.OFF
self.status["ifeel"] = self.STATIC.ONOFF.OFF
self.status["fixation_h"] = self.STATIC.FIXATION.HORIZONTAL.LEFT_RIGHT_FIX
self.status["fanspeed"] = self.STATIC.FAN.AUTO
self.status["turbo"] = self.STATIC.ONOFF.OFF
self.status["mute"] = self.STATIC.ONOFF.OFF
self.status["clean"] = self.STATIC.ONOFF.OFF
self.status["mildew"] = self.STATIC.ONOFF.OFF
self.status["macaddress"] = None
self.status["hostip"] = None
self.status["lastupdate"] = None
self.status["ambient_temp"] = None
self.status["devicename"] = None
def set_temperature(self, temperature):
self.logger.debug("Setting temprature to %s", temperature)
self.get_ac_states()
self.status["temp"] = float(temperature)
self.set_ac_status()
return self.make_nice_status(self.status)
def switch_off(self):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
self.status["power"] = self.STATIC.ONOFF.OFF
self.set_ac_status()
return self.make_nice_status(self.status)
def switch_on(self):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
def set_mode(self, mode_text):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.MODE.__dict__.get(mode_text.upper())
if mode != None:
self.status["mode"] = mode
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found mode value %s", str(mode_text))
return False
def set_fanspeed(self, mode_text):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.FAN.__dict__.get(mode_text.upper())
if mode != None:
self.status["fanspeed"] = mode
self.status["turbo"] = self.STATIC.ONOFF.OFF
self.status["mute"] = self.STATIC.ONOFF.OFF
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found mode value %s", str(mode_text))
return False
def set_mute(self, value):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.ONOFF.__dict__.get(value)
if mode != None:
self.status["mute"] = mode
self.status["turbo"] = self.STATIC.ONOFF.OFF
self.status["fanspeed"] = self.STATIC.FAN.NONE
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found mute value %s", str(value))
return False
def set_turbo(self, value):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.ONOFF.__dict__.get(value)
if mode != None:
self.status["turbo"] = mode
self.status["mute"] = self.STATIC.ONOFF.OFF
self.status["fanspeed"] = self.STATIC.FAN.NONE
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found Turbo value %s", str(value))
return False
def set_fixation_v(self, fixation_text):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
fixation = self.STATIC.FIXATION.VERTICAL.__dict__.get(fixation_text.upper())
if fixation != None:
self.status["fixation_v"] = fixation
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found mode value %s", str(fixation_text))
return False
def set_fixation_h(self, fixation_text):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
fixation = self.STATIC.FIXATION.HORIZONTAL.__dict__.get(fixation_text.upper())
if fixation != None:
self.status["fixation_h"] = fixation
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found mode value %s", str(fixation_text))
return False
def set_display(self, value):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.ONOFF.__dict__.get(value)
if mode != None:
self.status["display"] = mode
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found display value %s", str(value))
return False
def set_mildew(self, value):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.ONOFF.__dict__.get(value)
if mode != None:
self.status["mildew"] = mode
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found display value %s", str(value))
return False
def set_clean(self, value):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.ONOFF.__dict__.get(value)
if mode != None:
self.status["clean"] = mode
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found display value %s", str(value))
return False
def set_health(self, value):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.ONOFF.__dict__.get(value)
if mode != None:
self.status["health"] = mode
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found display value %s", str(value))
return False
def set_sleep(self, value):
##Make sure latest info as cannot just update one things, have set all
self.get_ac_states()
mode = self.STATIC.ONOFF.__dict__.get(value)
if mode != None:
self.status["sleep"] = mode
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Not found display value %s", str(value))
return False
def set_homekit_mode(self, status):
if type(status) is not str:
self.logger.debug("Status variable is not string %s", type(status))
return False
if status.lower() == "coolon":
self.status["mode"] = self.STATIC.MODE.COOLING
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
elif status.lower() == "heaton":
self.status["mode"] = self.STATIC.MODE.HEATING
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
elif status.lower() == "auto":
self.status["mode"] = self.STATIC.MODE.AUTO
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
if status.lower() == "dry":
self.status["mode"] = self.STATIC.MODE.DRY
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
if status.lower() == "fan_only":
self.status["mode"] = self.STATIC.MODE.FAN
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
elif status.lower() == "off":
self.status["power"] = self.STATIC.ONOFF.OFF
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Invalid status for homekit %s", status)
return False
def set_homeassistant_mode(self, status):
if type(status) is not str:
self.logger.debug("Status variable is not string %s", type(status))
return False
if status.lower() == "cool":
self.status["mode"] = self.STATIC.MODE.COOLING
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
elif status.lower() == "heat":
self.status["mode"] = self.STATIC.MODE.HEATING
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
elif status.lower() == "auto":
self.status["mode"] = self.STATIC.MODE.AUTO
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
if status.lower() == "dry":
self.status["mode"] = self.STATIC.MODE.DRY
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
if status.lower() == "fan_only":
self.status["mode"] = self.STATIC.MODE.FAN
self.status["power"] = self.STATIC.ONOFF.ON
self.set_ac_status()
return self.make_nice_status(self.status)
elif status.lower() == "off":
self.status["power"] = self.STATIC.ONOFF.OFF
self.set_ac_status()
return self.make_nice_status(self.status)
else:
self.logger.debug("Invalid status for homekit %s", status)
return False
def get_ac_info(self):
GET_AC_INFO = bytearray.fromhex("0C00BB0006800000020021011B7E0000")
response = self.send_packet(0x6A, GET_AC_INFO)
# print "Resposnse:" + ''.join(format(x, '02x') for x in response)
# print "Response:" + ' '.join(format(x, '08b') for x in response[9:])
err = response[0x22] | (response[0x23] << 8)
if err == 0:
# response = bytearray.fromhex("5aa5aa555aa5aa55000000000000000000000000000000000000000000000000c6d000002a4e6a0055b9af41a70d43b401000000b9c00000aeaac104468cf91b485f38c67f7bf57f");
# response = bytearray.fromhex("5aa5aa555aa5aa5547006f008d9904312c003e00000000003133a84d00400000d8d500002a4e6a0070a1b88c08b043a001000000b9c0000038821c66e3b38a5afe79dcb145e215d7")
response_payload = self.decrypt(bytes(response[0x38:]))
response_payload = bytearray(response_payload)
self.logger.debug(
"Acinfo Raw Response: "
+ " ".join(format(x, "08b") for x in response_payload)
)
self.logger.debug(
"Acinfo Raw Hex: "
+ " ".join(format(x, "02x") for x in response_payload)
)
response_payload = response_payload[2:] ##Drop leading stuff as dont need
self.logger.debug(
"AcInfo: " + " ".join(format(x, "08b") for x in response_payload[9:])
)
if (
len(response_payload) < 40
): ##Hack for some invalid packets. should get proper length at some point. #54
self.logger.debug("AcInfo: Invalid, seems to short?")
return 0
##Its only the last 5 bits?
ambient_temp = response_payload[15] & 0b00011111
self.logger.debug(
"Ambient Temp Decimal: %s" % float(response_payload[31] & 0b00011111)
) ## @Anonym-tsk
if ambient_temp:
self.status["ambient_temp"] = ambient_temp
return self.make_nice_status(self.status)
else:
self.logger.debug("Invalid packet received Errorcode %s" % err)
self.logger.debug(
"Failed Raw Response: " + " ".join(format(x, "08b") for x in response)
)
return 0
### Get AC Status
## GEt the current status of the aircon and parse into status array a one have to send full status each time for update, cannot just send one setting
##
def get_ac_states(self, force_update=False):
GET_STATES = bytearray.fromhex(
"0C00BB0006800000020011012B7E0000"
) ##From app queryAuxinfo:bb0006800000020011012b7e
##Check if the status is up to date to reduce timeout issues. Can be overwritten by force_update
self.logger.debug("Last update was: %s" % self.status["lastupdate"])
if (
force_update == False
and (self.status["lastupdate"] + self.update_interval) > time.time()
):
return self.make_nice_status(self.status)
response = self.send_packet(0x6A, GET_STATES)
##Check response, the checksums should be 0
err = response[0x22] | (response[0x23] << 8)
if err == 0:
response_payload = bytes(self.decrypt(bytes(response[0x38:])))
response_payload = bytearray(response_payload)
packet_type = response_payload[4]
if (
packet_type != 0x07
): ##Should be result packet, otherwise something weird
return False
packet_len = response_payload[0]
if packet_len != 0x19: ##should be 25, if not, then wrong packet
return False
self.logger.debug(
"Raw AC Status: "
+ " ".join(format(x, "08b") for x in response_payload[9:])
)
response_payload = response_payload[2:] ##Drop leading stuff as dont need
self.logger.debug(
"Raw AC Status: " + " ".join(format(x, "02x") for x in response_payload)
)
# self.logger.debug ("" + ' '.join(format(x, '08b') for x in response_payload[9:] ) )
# AuxInfo [tem=18, panMode=7, panType=1, nowTimeHour=5, setTem05=0, antoSenseYards=0, nowTimeMin=51, windSpeed=5, timerHour=0, voice=0, timerMin=0, mode=4, hasDew=0, hasSenseYards=0, hasSleep=0, isFollow=0, roomTem=0, roomHum=0, timeEnable=0, open=1, hasElectHeat=0, hasEco=0, hasClean=0, hasHealth=0, hasAir=0, weedSet=0, electronicLock=0, showDisplyBoard=1, mouldProof=0, controlMode=0, sleepMode=0]
self.status["temp"] = (
8
+ (response_payload[10] >> 3)
+ (0.5 * float(response_payload[12] >> 7))
)
self.status["power"] = response_payload[18] >> 5 & 0b00000001
self.status["fixation_v"] = response_payload[10] & 0b00000111
self.status["mode"] = response_payload[15] >> 5 & 0b00001111
self.status["sleep"] = response_payload[15] >> 2 & 0b00000001
self.status["display"] = response_payload[20] >> 4 & 0b00000001
self.status["mildew"] = response_payload[20] >> 3 & 0b00000001
self.status["health"] = response_payload[18] >> 1 & 0b00000001
self.status["fixation_h"] = response_payload[10] & 0b00000111
self.status["fanspeed"] = response_payload[13] >> 5 & 0b00000111
self.status["ifeel"] = response_payload[15] >> 3 & 0b00000001
self.status["mute"] = response_payload[14] >> 7 & 0b00000001
self.status["turbo"] = response_payload[14] >> 6 & 0b00000001
self.status["clean"] = response_payload[18] >> 2 & 0b00000001
self.status["lastupdate"] = time.time()
return self.make_nice_status(self.status)
else:
return 0
return self.status
def make_nice_status(self, status):
status_nice = {}
status_nice["temp"] = status["temp"]
status_nice["ambient_temp"] = status["ambient_temp"]
status_nice["power"] = self.get_key(self.STATIC.ONOFF.__dict__, status["power"])
status_nice["fixation_v"] = self.get_key(
self.STATIC.FIXATION.VERTICAL.__dict__, status["fixation_v"]
)
status_nice["mode"] = self.get_key(self.STATIC.MODE.__dict__, status["mode"])
status_nice["sleep"] = self.get_key(self.STATIC.ONOFF.__dict__, status["sleep"])
status_nice["display"] = self.get_key(
self.STATIC.ONOFF.__dict__, status["display"]
)
status_nice["mildew"] = self.get_key(
self.STATIC.ONOFF.__dict__, status["mildew"]
)
status_nice["health"] = self.get_key(
self.STATIC.ONOFF.__dict__, status["health"]
)
status_nice["fixation_h"] = self.get_key(
self.STATIC.FIXATION.HORIZONTAL.__dict__, status["fixation_h"]
)
status_nice["ifeel"] = self.get_key(self.STATIC.ONOFF.__dict__, status["ifeel"])
status_nice["mute"] = self.get_key(self.STATIC.ONOFF.__dict__, status["mute"])
status_nice["turbo"] = self.get_key(self.STATIC.ONOFF.__dict__, status["turbo"])
status_nice["clean"] = self.get_key(self.STATIC.ONOFF.__dict__, status["clean"])
status_nice["macaddress"] = status["macaddress"]
status_nice["device_name"] = status["devicename"]
##HomeKit topics
if self.status["power"] == self.STATIC.ONOFF.OFF:
status_nice["mode_homekit"] = "Off"
elif (
status["power"] == self.STATIC.ONOFF.ON
and status["mode"] == self.STATIC.MODE.AUTO
):
status_nice["mode_homekit"] = "Auto"
elif (
status["power"] == self.STATIC.ONOFF.ON
and status["mode"] == self.STATIC.MODE.HEATING
):
status_nice["mode_homekit"] = "HeatOn"
elif (
status["power"] == self.STATIC.ONOFF.ON
and status["mode"] == self.STATIC.MODE.COOLING
):
status_nice["mode_homekit"] = "CoolOn"
else:
status_nice["mode_homekit"] = "Error"
##Home Assist topic
if self.status["power"] == self.STATIC.ONOFF.OFF:
status_nice["mode_homeassistant"] = "off"
elif (
status["power"] == self.STATIC.ONOFF.ON
and status["mode"] == self.STATIC.MODE.AUTO
):
status_nice["mode_homeassistant"] = "auto"
elif (
status["power"] == self.STATIC.ONOFF.ON
and status["mode"] == self.STATIC.MODE.HEATING
):
status_nice["mode_homeassistant"] = "heat"
elif (
status["power"] == self.STATIC.ONOFF.ON
and status["mode"] == self.STATIC.MODE.COOLING
):
status_nice["mode_homeassistant"] = "cool"
elif (
status["power"] == self.STATIC.ONOFF.ON
and status["mode"] == self.STATIC.MODE.DRY
):
status_nice["mode_homeassistant"] = "dry"
elif (
status["power"] == self.STATIC.ONOFF.ON
and status["mode"] == self.STATIC.MODE.FAN
):
status_nice["mode_homeassistant"] = "fan_only"
else:
status_nice["mode_homeassistant"] = "Error"
##Make fanspeed logic
status_nice["fanspeed"] = self.get_key(
self.STATIC.FAN.__dict__, status["fanspeed"]
)
status_nice["fanspeed_homeassistant"] = self.get_key(
self.STATIC.FAN.__dict__, status["fanspeed"]
).title()
if status_nice["mute"] == "ON":
status_nice["fanspeed_homeassistant"] = "Mute"
status_nice["fanspeed"] = "MUTE"
elif status_nice["turbo"] == "ON":
status_nice["fanspeed_homeassistant"] = "Turbo"
status_nice["fanspeed"] = "TURBO"
return status_nice
def get_key(self, list, search_value):
for key, value in list.items():
if value == search_value:
return key
##Not found so return value;
return search_value
### UDP checksum function
def checksum_func(self, data):
checksum = 0
data_len = len(data)
if (data_len % 2) == 1:
data_len += 1
data += struct.pack("!B", 0)
for i in range(0, len(data), 2):
w = (data[i] << 8) + (data[i + 1])
checksum += w
checksum = (checksum >> 16) + (checksum & 0xFFFF)
checksum = ~checksum & 0xFFFF
return checksum