1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-28 04:50:23 -07:00

Simplify retry loop

This commit is contained in:
Adrian Sampson 2018-01-20 21:39:37 -05:00
parent b2bc017d99
commit f50c8d0221

View File

@ -35,6 +35,9 @@ def authenticate(gateway):
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
@ -98,6 +101,35 @@ class Client(object):
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:
print('{deviceId}: {alias} ({modelNm})'.format(**device))
elif args[0] == 'mon':
device_id = args[1]
with wideq.Monitor(client.session, device_id) as mon:
try:
while True:
time.sleep(1)
print('Polling...')
res = mon.poll()
if res:
print('setting: {}°C'.format(res['TempCfg']))
print('current: {}°C'.format(res['TempCur']))
except KeyboardInterrupt:
pass
elif args[0] == 'set-temp':
temp = args[1]
device_id = args[2]
client.session.set_device_controls(device_id,
{'TempCfg': temp})
def example(args):
state = load_state()
client = Client()
@ -109,33 +141,7 @@ def example(args):
# Loop to retry if session has expired.
while True:
try:
if not args or args[0] == 'ls':
for device in client.devices:
print('{deviceId}: {alias} ({modelNm})'.format(**device))
elif args[0] == 'mon':
device_id = args[1]
with wideq.Monitor(client.session, device_id) as mon:
try:
while True:
time.sleep(1)
print('Polling...')
res = mon.poll()
if res:
print('setting: {}°C'.format(res['TempCfg']))
print('current: {}°C'.format(res['TempCur']))
except KeyboardInterrupt:
pass
elif args[0] == 'set-temp':
temp = args[1]
device_id = args[2]
client.session.set_device_controls(device_id,
{'TempCfg': temp})
example_command(client, args)
break
except wideq.NotLoggedInError: