diff --git a/example.py b/example.py index ee253fc..3ad1750 100644 --- a/example.py +++ b/example.py @@ -34,73 +34,6 @@ def authenticate(gateway): return wideq.Auth.from_url(gateway, callback_url) -class Client(object): - """A higher-level API wrapper that provides a session. - """ - - def __init__(self): - # The three steps required to get access to call the API. - self._gateway = None - self._auth = None - self._session = None - - # The last list of devices we got from the server. - self._devices = None - - @property - def gateway(self): - if not self._gateway: - self._gateway = wideq.Gateway.discover() - return self._gateway - - @property - def auth(self): - if not self._auth: - assert False, "unauthenticated" - return self._auth - - @property - def session(self): - if not self._session: - self._session, self._devices = self.auth.start_session() - return self._session - - @property - def devices(self): - if not self._devices: - self._devices = self.session.get_devices() - return self._devices - - def load(self, state): - """Load the client objects from the encoded state data. - """ - - if 'gateway' in state: - self._gateway = wideq.Gateway.load(state['gateway']) - - if 'auth' in state: - self._auth = wideq.Auth.load(self._gateway, state['auth']) - - if 'session' in state: - self._session = wideq.Session.load(self._auth, state['session']) - - def dump(self): - """Serialize the client state.""" - - out = {} - if self._gateway: - out['gateway'] = self._gateway.dump() - if self._auth: - out['auth'] = self._auth.dump() - if self._session: - out['session'] = self._session.dump() - return out - - def refresh(self): - self._auth = self.auth.refresh() - self._session, self._devices = self.auth.start_session() - - def example_command(client, args): if not args or args[0] == 'ls': for device in client.devices: @@ -126,13 +59,12 @@ def example_command(client, args): temp = args[1] device_id = args[2] - client.session.set_device_controls(device_id, - {'TempCfg': temp}) + client.session.set_device_controls(device_id, {'TempCfg': temp}) def example(args): state = load_state() - client = Client() + client = wideq.Client() client.load(state) if not client._auth: diff --git a/wideq.py b/wideq.py index 499680b..1035ab1 100644 --- a/wideq.py +++ b/wideq.py @@ -364,3 +364,71 @@ class Monitor(object): def __exit__(self, type, value, tb): self.session.monitor_stop(self.device_id, self.work_id) + + +class Client(object): + """A higher-level API wrapper that provides a session more easily + and allows serialization of state. + """ + + def __init__(self): + # The three steps required to get access to call the API. + self._gateway = None + self._auth = None + self._session = None + + # The last list of devices we got from the server. + self._devices = None + + @property + def gateway(self): + if not self._gateway: + self._gateway = Gateway.discover() + return self._gateway + + @property + def auth(self): + if not self._auth: + assert False, "unauthenticated" + return self._auth + + @property + def session(self): + if not self._session: + self._session, self._devices = self.auth.start_session() + return self._session + + @property + def devices(self): + if not self._devices: + self._devices = self.session.get_devices() + return self._devices + + def load(self, state): + """Load the client objects from the encoded state data. + """ + + if 'gateway' in state: + self._gateway = Gateway.load(state['gateway']) + + if 'auth' in state: + self._auth = Auth.load(self._gateway, state['auth']) + + if 'session' in state: + self._session = Session.load(self._auth, state['session']) + + def dump(self): + """Serialize the client state.""" + + out = {} + if self._gateway: + out['gateway'] = self._gateway.dump() + if self._auth: + out['auth'] = self._auth.dump() + if self._session: + out['session'] = self._session.dump() + return out + + def refresh(self): + self._auth = self.auth.refresh() + self._session, self._devices = self.auth.start_session()