1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-19 08:40:25 -07:00

Little "subcommands" in the example

This commit is contained in:
Adrian Sampson 2018-01-08 14:21:09 -08:00
parent d2735be4ad
commit b8b50e5309

View File

@ -1,6 +1,7 @@
import wideq
import json
import time
import sys
STATE_FILE = 'wideq_state.json'
@ -24,11 +25,6 @@ def save_state(state):
json.dump(state, f)
def print_devices(devices):
for device in devices:
print('{deviceId}: {alias} ({modelNm})'.format(**device))
def authenticate(gateway):
login_url = gateway.oauth_url()
print('Log in here:')
@ -85,26 +81,25 @@ def get_session(state, reauth=False):
return session, devices
def example():
def example(args):
state = load_state()
session, devices = get_session(state)
try:
if not args or args[0] == 'ls':
# Request a list of devices, if we didn't get them "for free"
# already by starting the session.
if not devices:
devices = session.get_devices()
except wideq.NotLoggedInError:
print('Session expired.')
return
for device in devices:
print('{deviceId}: {alias} ({modelNm})'.format(**device))
print_devices(devices)
elif args[0] == 'mon':
device_id = args[1]
# For fun, try monitoring the first device
first_device_id = devices[0]['deviceId']
with wideq.Monitor(session, first_device_id) as mon:
with wideq.Monitor(session, device_id) as mon:
for i in range(4):
time.sleep(1)
print('Polling...')
@ -112,6 +107,10 @@ def example():
if res:
print(res)
except wideq.NotLoggedInError:
print('Session expired.')
return
if __name__ == '__main__':
example()
example(sys.argv[1:])