Merge remote-tracking branch 'zenitraM/python3'

This commit is contained in:
Matthew Garrett 2016-12-03 14:16:32 -08:00
commit 9628b87572

View file

@ -62,7 +62,7 @@ def discover(timeout=None):
devices = [] devices = []
timezone = time.timezone/-3600 timezone = int(time.timezone/-3600)
packet = bytearray(0x30) packet = bytearray(0x30)
year = datetime.now().year year = datetime.now().year
@ -156,20 +156,20 @@ class device:
payload[0x12] = 0x31 payload[0x12] = 0x31
payload[0x1e] = 0x01 payload[0x1e] = 0x01
payload[0x2d] = 0x01 payload[0x2d] = 0x01
payload[0x30] = 'T' payload[0x30] = ord('T')
payload[0x31] = 'e' payload[0x31] = ord('e')
payload[0x32] = 's' payload[0x32] = ord('s')
payload[0x33] = 't' payload[0x33] = ord('t')
payload[0x34] = ' ' payload[0x34] = ord(' ')
payload[0x35] = ' ' payload[0x35] = ord(' ')
payload[0x36] = '1' payload[0x36] = ord('1')
response = self.send_packet(0x65, payload) response = self.send_packet(0x65, payload)
enc_payload = response[0x38:] enc_payload = response[0x38:]
aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.decrypt(str(enc_payload)) payload = aes.decrypt(bytes(enc_payload))
self.id = payload[0x00:0x04] self.id = payload[0x00:0x04]
self.key = payload[0x04:0x14] self.key = payload[0x04:0x14]
@ -206,8 +206,8 @@ class device:
checksum += payload[i] checksum += payload[i]
checksum = checksum & 0xffff checksum = checksum & 0xffff
aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.encrypt(str(payload)) payload = aes.encrypt(bytes(payload))
packet[0x34] = checksum & 0xff packet[0x34] = checksum & 0xff
packet[0x35] = checksum >> 8 packet[0x35] = checksum >> 8
@ -233,7 +233,7 @@ class device:
if (time.time() - starttime) < self.timeout: if (time.time() - starttime) < self.timeout:
pass pass
raise raise
return response[0] return bytes(response[0])
class sp1(device): class sp1(device):
@ -262,11 +262,11 @@ class sp2(device):
packet = bytearray(16) packet = bytearray(16)
packet[0] = 1 packet[0] = 1
response = self.send_packet(0x6a, packet) response = self.send_packet(0x6a, packet)
err = ord(response[0x22]) | (ord(response[0x23]) << 8) err = response[0x22] | (response[0x23] << 8)
if err == 0: if err == 0:
aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.decrypt(str(response[0x38:])) payload = aes.decrypt(bytes(response[0x38:]))
return bool(ord(payload[0x4])) return bool(payload[0x4])
class a1(device): class a1(device):
def __init__ (self, host, mac): def __init__ (self, host, mac):
@ -276,14 +276,14 @@ class a1(device):
packet = bytearray(16) packet = bytearray(16)
packet[0] = 1 packet[0] = 1
response = self.send_packet(0x6a, packet) response = self.send_packet(0x6a, packet)
err = ord(response[0x22]) | (ord(response[0x23]) << 8) err = response[0x22] | (response[0x23] << 8)
if err == 0: if err == 0:
data = {} data = {}
aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.decrypt(str(response[0x38:])) payload = aes.decrypt(bytes(response[0x38:]))
data['temperature'] = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0 data['temperature'] = (payload[0x4] * 10 + payload[0x5]) / 10.0
data['humidity'] = (ord(payload[0x6]) * 10 + ord(payload[0x7])) / 10.0 data['humidity'] = (payload[0x6] * 10 + payload[0x7]) / 10.0
light = ord(payload[0x8]) light = payload[0x8]
if light == 0: if light == 0:
data['light'] = 'dark' data['light'] = 'dark'
elif light == 1: elif light == 1:
@ -294,7 +294,7 @@ class a1(device):
data['light'] = 'bright' data['light'] = 'bright'
else: else:
data['light'] = 'unknown' data['light'] = 'unknown'
air_quality = ord(payload[0x0a]) air_quality = payload[0x0a]
if air_quality == 0: if air_quality == 0:
data['air_quality'] = 'excellent' data['air_quality'] = 'excellent'
elif air_quality == 1: elif air_quality == 1:
@ -305,7 +305,7 @@ class a1(device):
data['air_quality'] = 'bad' data['air_quality'] = 'bad'
else: else:
data['air_quality'] = 'unknown' data['air_quality'] = 'unknown'
noise = ord(payload[0xc]) noise = payload[0xc]
if noise == 0: if noise == 0:
data['noise'] = 'quiet' data['noise'] = 'quiet'
elif noise == 1: elif noise == 1:
@ -325,10 +325,10 @@ class rm(device):
packet = bytearray(16) packet = bytearray(16)
packet[0] = 4 packet[0] = 4
response = self.send_packet(0x6a, packet) response = self.send_packet(0x6a, packet)
err = ord(response[0x22]) | (ord(response[0x23]) << 8) err = response[0x22] | (response[0x23] << 8)
if err == 0: if err == 0:
aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.decrypt(str(response[0x38:])) payload = aes.decrypt(bytes(response[0x38:]))
return payload[0x04:] return payload[0x04:]
def send_data(self, data): def send_data(self, data):
@ -345,11 +345,11 @@ class rm(device):
packet = bytearray(16) packet = bytearray(16)
packet[0] = 1 packet[0] = 1
response = self.send_packet(0x6a, packet) response = self.send_packet(0x6a, packet)
err = ord(response[0x22]) | (ord(response[0x23]) << 8) err = response[0x22] | (response[0x23] << 8)
if err == 0: if err == 0:
aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.decrypt(str(response[0x38:])) payload = aes.decrypt(bytes(response[0x38:]))
temp = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0 temp = (payload[0x4] * 10 + payload[0x5]) / 10.0
return temp return temp
# For legay compatibility - don't use this # For legay compatibility - don't use this