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