From 2dc014f17ff17abced35e95cce1081032d4b6c84 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 11 Mar 2018 09:59:17 -0400 Subject: [PATCH] Wrap devices in lists (fix #1, fix #2) --- wideq.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/wideq.py b/wideq.py index bac84e7..f8c9a5f 100644 --- a/wideq.py +++ b/wideq.py @@ -41,6 +41,19 @@ def oauth2_signature(message, secret): return base64.b64encode(digest) +def as_list(obj): + """Wrap non-lists in lists. + + If `obj` is a list, return it unchanged. Otherwise, return a + single-element list containing it. + """ + + if isinstance(obj, list): + return obj + else: + return [obj] + + class APIError(Exception): """An error reported by the API.""" @@ -226,12 +239,12 @@ class Auth(object): def start_session(self): """Start an API session for the logged-in user. Return the - Session object and the user's devices. + Session object and a list of the user's devices. """ session_info = login(self.gateway.api_root, self.access_token) session_id = session_info['jsessionId'] - return Session(self, session_id), session_info['item'] + return Session(self, session_id), as_list(session_info['item']) def refresh(self): """Refresh the authentication, returning a new Auth object. @@ -263,7 +276,7 @@ class Session(object): Return a list of dicts with information about the devices. """ - return self.post('device/deviceList')['item'] + return as_list(self.post('device/deviceList')['item']) def monitor_start(self, device_id): """Begin monitoring a device's status.