1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-18 08:10:17 -07:00

Merge pull request #7 from NorDroN/master

Decode binary monitoring status data
This commit is contained in:
Adrian Sampson 2018-09-12 13:07:52 -04:00 committed by GitHub
commit 49350f6fd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -42,7 +42,7 @@ def mon(client, device_id):
data = mon.poll()
if data:
try:
res = mon.decode_json(data)
res = model.decode_monitor(data)
except ValueError:
print('status data: {!r}'.format(data))
else:
@ -58,7 +58,6 @@ def mon(client, device_id):
elif isinstance(desc, wideq.RangeValue):
print('- {0}: {1} ({2.min}-{2.max})'.format(
key, value, desc,
))
except KeyboardInterrupt:

View File

@ -685,6 +685,39 @@ class ModelInfo(object):
options = self.value(key).options
return options[value]
@property
def binary_monitor_data(self):
"""Check that type of monitoring is BINARY(BYTE).
"""
return self.data['Monitoring']['type'] == 'BINARY(BYTE)'
def decode_monitor_binary(self, data):
"""Decode binary encoded status data.
"""
decoded = {}
for item in self.data['Monitoring']['protocol']:
key = item['value']
value = 0
for v in data[item['startByte']:item['startByte'] + item['length']]:
value = (value << 8) + v
decoded[key] = str(value)
return decoded
def decode_monitor_json(self, data):
"""Decode a bytestring that encodes JSON status data."""
return json.loads(data.decode('utf8'))
def decode_monitor(self, data):
"""Decode status data."""
if self.binary_monitor_data:
return self.decode_monitor_binary(data)
else:
return self.decode_monitor_json(data)
class Device(object):
"""A higher-level interface to a specific device.