1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-21 09:30:13 -07:00

Not all devices return JSON status data

This commit is contained in:
Adrian Sampson 2018-04-03 10:17:04 -04:00
parent 8588c109f8
commit 86c63a92b5
2 changed files with 43 additions and 21 deletions

View File

@ -39,22 +39,27 @@ def mon(client, device_id):
while True:
time.sleep(1)
print('Polling...')
res = mon.poll()
if res:
for key, value in res.items():
try:
desc = model.value(key)
except KeyError:
print('- {}: {}'.format(key, value))
if isinstance(desc, wideq.EnumValue):
print('- {}: {}'.format(
key, desc.options.get(value, value)
))
elif isinstance(desc, wideq.RangeValue):
print('- {0}: {1} ({2.min}-{2.max})'.format(
key, value, desc,
data = mon.poll()
if data:
try:
res = mon.decode_json(data)
except ValueError:
print('status data: {!r}'.format(data))
else:
for key, value in res.items():
try:
desc = model.value(key)
except KeyError:
print('- {}: {}'.format(key, value))
if isinstance(desc, wideq.EnumValue):
print('- {}: {}'.format(
key, desc.options.get(value, value)
))
elif isinstance(desc, wideq.RangeValue):
print('- {0}: {1} ({2.min}-{2.max})'.format(
key, value, desc,
))
))
except KeyboardInterrupt:
pass

View File

@ -296,8 +296,9 @@ class Session(object):
def monitor_poll(self, device_id, work_id):
"""Get the result of a monitoring task.
`work_ids` is a mapping from device IDs to work IDs. Return the
device status or None if the monitoring is not yet ready.
`work_id` is a string ID retrieved from `monitor_start`. Return
a status result, which is a bytestring, or None if the
monitoring is not yet ready.
"""
work_list = [{'deviceId': device_id, 'workId': work_id}]
@ -305,9 +306,7 @@ class Session(object):
# Weirdly, the main response data is base64-encoded JSON.
if 'returnData' in res:
return json.loads(
base64.b64decode(res['returnData']).decode('utf8')
)
return base64.b64decode(res['returnData'])
else:
return None
@ -351,8 +350,26 @@ class Monitor(object):
self.session.monitor_stop(self.device_id, self.work_id)
def poll(self):
"""Get the current status data (a bytestring) or None if the
device is not yet ready.
"""
return self.session.monitor_poll(self.device_id, self.work_id)
@staticmethod
def decode_json(data):
"""Decode a bytestring that encodes JSON status data."""
return json.loads(data.decode('utf8'))
def poll_json(self):
"""For devices where status is reported via JSON data, get the
decoded status result (or None if status is not available).
"""
data = self.poll()
return self.decode_json(data) if data else None
def __enter__(self):
self.start()
return self
@ -702,7 +719,7 @@ class ACDevice(object):
available.
"""
res = self.mon.poll()
res = self.mon.poll_json()
if res:
return ACStatus(self, res)
else: