mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-19 16:50:16 -07:00
Little "subcommands" in the example
This commit is contained in:
parent
d2735be4ad
commit
b8b50e5309
45
example.py
45
example.py
@ -1,6 +1,7 @@
|
|||||||
import wideq
|
import wideq
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
STATE_FILE = 'wideq_state.json'
|
STATE_FILE = 'wideq_state.json'
|
||||||
|
|
||||||
@ -24,11 +25,6 @@ def save_state(state):
|
|||||||
json.dump(state, f)
|
json.dump(state, f)
|
||||||
|
|
||||||
|
|
||||||
def print_devices(devices):
|
|
||||||
for device in devices:
|
|
||||||
print('{deviceId}: {alias} ({modelNm})'.format(**device))
|
|
||||||
|
|
||||||
|
|
||||||
def authenticate(gateway):
|
def authenticate(gateway):
|
||||||
login_url = gateway.oauth_url()
|
login_url = gateway.oauth_url()
|
||||||
print('Log in here:')
|
print('Log in here:')
|
||||||
@ -85,33 +81,36 @@ def get_session(state, reauth=False):
|
|||||||
return session, devices
|
return session, devices
|
||||||
|
|
||||||
|
|
||||||
def example():
|
def example(args):
|
||||||
state = load_state()
|
state = load_state()
|
||||||
|
|
||||||
session, devices = get_session(state)
|
session, devices = get_session(state)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Request a list of devices, if we didn't get them "for free"
|
if not args or args[0] == 'ls':
|
||||||
# already by starting the session.
|
# Request a list of devices, if we didn't get them "for free"
|
||||||
if not devices:
|
# already by starting the session.
|
||||||
devices = session.get_devices()
|
if not devices:
|
||||||
|
devices = session.get_devices()
|
||||||
|
|
||||||
|
for device in devices:
|
||||||
|
print('{deviceId}: {alias} ({modelNm})'.format(**device))
|
||||||
|
|
||||||
|
elif args[0] == 'mon':
|
||||||
|
device_id = args[1]
|
||||||
|
|
||||||
|
with wideq.Monitor(session, device_id) as mon:
|
||||||
|
for i in range(4):
|
||||||
|
time.sleep(1)
|
||||||
|
print('Polling...')
|
||||||
|
res = mon.poll()
|
||||||
|
if res:
|
||||||
|
print(res)
|
||||||
|
|
||||||
except wideq.NotLoggedInError:
|
except wideq.NotLoggedInError:
|
||||||
print('Session expired.')
|
print('Session expired.')
|
||||||
return
|
return
|
||||||
|
|
||||||
print_devices(devices)
|
|
||||||
|
|
||||||
# For fun, try monitoring the first device
|
|
||||||
first_device_id = devices[0]['deviceId']
|
|
||||||
with wideq.Monitor(session, first_device_id) as mon:
|
|
||||||
for i in range(4):
|
|
||||||
time.sleep(1)
|
|
||||||
print('Polling...')
|
|
||||||
res = mon.poll()
|
|
||||||
if res:
|
|
||||||
print(res)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
example()
|
example(sys.argv[1:])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user