1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-20 09:00:09 -07:00
This commit is contained in:
nordron 2018-09-11 23:56:14 +03:00
commit 2ea2459f50

View File

@ -41,17 +41,23 @@ def oauth2_signature(message, secret):
return base64.b64encode(digest) return base64.b64encode(digest)
def as_list(obj): def get_list(obj, key):
"""Wrap non-lists in lists. """Look up a list using a key from an object.
If `obj` is a list, return it unchanged. Otherwise, return a If `obj[key]` is a list, return it unchanged. If is something else,
single-element list containing it. return a single-element list containing it. If the key does not
exist, return an empty list.
""" """
if isinstance(obj, list): try:
return obj val = obj[key]
except KeyError:
return []
if isinstance(val, list):
return val
else: else:
return [obj] return [val]
class APIError(Exception): class APIError(Exception):
@ -254,7 +260,7 @@ class Auth(object):
session_info = login(self.gateway.api_root, self.access_token) session_info = login(self.gateway.api_root, self.access_token)
session_id = session_info['jsessionId'] session_id = session_info['jsessionId']
return Session(self, session_id), as_list(session_info['item']) return Session(self, session_id), get_list(session_info, 'item')
def refresh(self): def refresh(self):
"""Refresh the authentication, returning a new Auth object. """Refresh the authentication, returning a new Auth object.
@ -286,7 +292,7 @@ class Session(object):
Return a list of dicts with information about the devices. Return a list of dicts with information about the devices.
""" """
return as_list(self.post('device/deviceList')['item']) return get_list(self.post('device/deviceList'), 'item')
def monitor_start(self, device_id): def monitor_start(self, device_id):
"""Begin monitoring a device's status. """Begin monitoring a device's status.