mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-20 09:00:09 -07:00
Merge branch 'master' of https://github.com/NorDroN/wideq
This commit is contained in:
commit
2ea2459f50
24
wideq.py
24
wideq.py
@ -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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user