Fix python 2 compatibility

Ugly but appears to work
This commit is contained in:
Matthew Garrett 2016-12-03 16:40:58 -08:00
parent 31cd077165
commit d10a43531c

View file

@ -238,7 +238,7 @@ class device:
if (time.time() - starttime) < self.timeout: if (time.time() - starttime) < self.timeout:
pass pass
raise raise
return bytes(response[0]) return bytearray(response[0])
class sp1(device): class sp1(device):
@ -289,9 +289,18 @@ class a1(device):
data = {} data = {}
aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv)) aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.decrypt(bytes(response[0x38:])) payload = aes.decrypt(bytes(response[0x38:]))
if type(payload[0x4]) == int:
data['temperature'] = (payload[0x4] * 10 + payload[0x5]) / 10.0 data['temperature'] = (payload[0x4] * 10 + payload[0x5]) / 10.0
data['humidity'] = (payload[0x6] * 10 + payload[0x7]) / 10.0 data['humidity'] = (payload[0x6] * 10 + payload[0x7]) / 10.0
light = payload[0x8] light = payload[0x8]
air_quality = payload[0x0a]
noise = payload[0xc]
else:
data['temperature'] = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0
data['humidity'] = (ord(payload[0x6]) * 10 + ord(payload[0x7])) / 10.0
light = ord(payload[0x8])
air_quality = ord(payload[0x0a])
noise = ord(payload[0xc])
if light == 0: if light == 0:
data['light'] = 'dark' data['light'] = 'dark'
elif light == 1: elif light == 1:
@ -302,7 +311,6 @@ class a1(device):
data['light'] = 'bright' data['light'] = 'bright'
else: else:
data['light'] = 'unknown' data['light'] = 'unknown'
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:
@ -313,7 +321,6 @@ class a1(device):
data['air_quality'] = 'bad' data['air_quality'] = 'bad'
else: else:
data['air_quality'] = 'unknown' data['air_quality'] = 'unknown'
noise = payload[0xc]
if noise == 0: if noise == 0:
data['noise'] = 'quiet' data['noise'] = 'quiet'
elif noise == 1: elif noise == 1:
@ -358,7 +365,10 @@ class rm(device):
if err == 0: if err == 0:
aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv)) aes = AES.new(bytes(self.key), AES.MODE_CBC, bytes(self.iv))
payload = aes.decrypt(bytes(response[0x38:])) payload = aes.decrypt(bytes(response[0x38:]))
if type(payload[0x4]) == int:
temp = (payload[0x4] * 10 + payload[0x5]) / 10.0 temp = (payload[0x4] * 10 + payload[0x5]) / 10.0
else:
temp = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0
return temp return temp
# For legay compatibility - don't use this # For legay compatibility - don't use this