diff options
author | Matthew Garrett <mjg59@coreos.com> | 2016-10-25T03·41-0700 |
---|---|---|
committer | Matthew Garrett <mjg59@coreos.com> | 2016-10-25T03·50-0700 |
commit | 69afd4ce52b47e488e15562b1c04edbffe5f58b0 (patch) | |
tree | 1c5be213aea3f3612e7a5bda8c09733ce0e0bb2c /broadlink/__init__.py | |
parent | ef77bc7ea8e3b7f54515220256703a8bd2f6cca5 (diff) |
Add support for sensor data
Add a check_temperature() function that'll work for both the RM2 and the A1 sensor platform, and a check_sensors() function that returns the full set of sensor data for the A1 as a dict.
Diffstat (limited to 'broadlink/__init__.py')
-rwxr-xr-x | broadlink/__init__.py | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py index 2891052708a3..63ad76b38677 100755 --- a/broadlink/__init__.py +++ b/broadlink/__init__.py @@ -168,10 +168,65 @@ class rm2: packet[0] = 3 self.send_packet(0x6a, packet) + def check_sensors(self): + packet = bytearray(16) + packet[0] = 1 + response = self.send_packet(0x6a, packet) + err = ord(response[0x22]) | (ord(response[0x23]) << 8) + if err == 0: + data = {} + aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) + payload = aes.decrypt(str(response[0x38:])) + 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]) + if light == 0: + data['light'] = 'dark' + elif light == 1: + data['light'] = 'dim' + elif light == 2: + data['light'] = 'normal' + elif light == 3: + data['light'] = 'bright' + else: + data['light'] = 'unknown' + air_quality = ord(payload[0x0a]) + if air_quality == 0: + data['air_quality'] = 'excellent' + elif air_quality == 1: + data['air_quality'] = 'good' + elif air_quality == 2: + data['air_quality'] = 'normal' + elif air_quality == 3: + data['air_quality'] = 'bad' + else: + data['air_quality'] = 'unknown' + noise = ord(payload[0xc]) + if noise == 0: + data['noise'] = 'quiet' + elif noise == 1: + data['noise'] = 'normal' + elif noise == 2: + data['noise'] = 'noisy' + else: + data['noise'] = 'unknown' + return data + + def check_temperature(self): + packet = bytearray(16) + packet[0] = 1 + response = self.send_packet(0x6a, packet) + err = ord(response[0x22]) | (ord(response[0x23]) << 8) + if err == 0: + aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) + payload = aes.decrypt(str(response[0x38:])) + temp = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0 + return temp + def check_data(self): packet = bytearray(16) packet[0] = 4 - response = self.send_packet(0x6a, packet) + response = self.send_packet(0x6a, packet) err = ord(response[0x22]) | (ord(response[0x23]) << 8) if err == 0: aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) |