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:
parent
8588c109f8
commit
86c63a92b5
35
example.py
35
example.py
@ -39,22 +39,27 @@ def mon(client, device_id):
|
|||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
print('Polling...')
|
print('Polling...')
|
||||||
res = mon.poll()
|
data = mon.poll()
|
||||||
if res:
|
if data:
|
||||||
for key, value in res.items():
|
try:
|
||||||
try:
|
res = mon.decode_json(data)
|
||||||
desc = model.value(key)
|
except ValueError:
|
||||||
except KeyError:
|
print('status data: {!r}'.format(data))
|
||||||
print('- {}: {}'.format(key, value))
|
else:
|
||||||
if isinstance(desc, wideq.EnumValue):
|
for key, value in res.items():
|
||||||
print('- {}: {}'.format(
|
try:
|
||||||
key, desc.options.get(value, value)
|
desc = model.value(key)
|
||||||
))
|
except KeyError:
|
||||||
elif isinstance(desc, wideq.RangeValue):
|
print('- {}: {}'.format(key, value))
|
||||||
print('- {0}: {1} ({2.min}-{2.max})'.format(
|
if isinstance(desc, wideq.EnumValue):
|
||||||
key, value, desc,
|
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:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
29
wideq.py
29
wideq.py
@ -296,8 +296,9 @@ class Session(object):
|
|||||||
def monitor_poll(self, device_id, work_id):
|
def monitor_poll(self, device_id, work_id):
|
||||||
"""Get the result of a monitoring task.
|
"""Get the result of a monitoring task.
|
||||||
|
|
||||||
`work_ids` is a mapping from device IDs to work IDs. Return the
|
`work_id` is a string ID retrieved from `monitor_start`. Return
|
||||||
device status or None if the monitoring is not yet ready.
|
a status result, which is a bytestring, or None if the
|
||||||
|
monitoring is not yet ready.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
work_list = [{'deviceId': device_id, 'workId': work_id}]
|
work_list = [{'deviceId': device_id, 'workId': work_id}]
|
||||||
@ -305,9 +306,7 @@ class Session(object):
|
|||||||
|
|
||||||
# Weirdly, the main response data is base64-encoded JSON.
|
# Weirdly, the main response data is base64-encoded JSON.
|
||||||
if 'returnData' in res:
|
if 'returnData' in res:
|
||||||
return json.loads(
|
return base64.b64decode(res['returnData'])
|
||||||
base64.b64decode(res['returnData']).decode('utf8')
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -351,8 +350,26 @@ class Monitor(object):
|
|||||||
self.session.monitor_stop(self.device_id, self.work_id)
|
self.session.monitor_stop(self.device_id, self.work_id)
|
||||||
|
|
||||||
def poll(self):
|
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)
|
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):
|
def __enter__(self):
|
||||||
self.start()
|
self.start()
|
||||||
return self
|
return self
|
||||||
@ -702,7 +719,7 @@ class ACDevice(object):
|
|||||||
available.
|
available.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
res = self.mon.poll()
|
res = self.mon.poll_json()
|
||||||
if res:
|
if res:
|
||||||
return ACStatus(self, res)
|
return ACStatus(self, res)
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user