mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-21 01:20:11 -07:00
Refactor example commands
This commit is contained in:
parent
f0cb5b8165
commit
7f7988effa
50
example.py
50
example.py
@ -7,6 +7,10 @@ STATE_FILE = 'wideq_state.json'
|
||||
|
||||
|
||||
def authenticate(gateway):
|
||||
"""Interactively authenticate the user via a browser to get an OAuth
|
||||
session.
|
||||
"""
|
||||
|
||||
login_url = gateway.oauth_url()
|
||||
print('Log in here:')
|
||||
print(login_url)
|
||||
@ -15,13 +19,18 @@ def authenticate(gateway):
|
||||
return wideq.Auth.from_url(gateway, callback_url)
|
||||
|
||||
|
||||
def example_command(client, args):
|
||||
if not args or args[0] == 'ls':
|
||||
def ls(client):
|
||||
"""List the user's devices."""
|
||||
|
||||
for device in client.devices:
|
||||
print('{0.id}: {0.name} ({0.model_id})'.format(device))
|
||||
|
||||
elif args[0] == 'mon':
|
||||
device_id = args[1]
|
||||
|
||||
def mon(client, device_id):
|
||||
"""Monitor any device, displaying generic information about its
|
||||
status.
|
||||
"""
|
||||
|
||||
device = client.get_device(device_id)
|
||||
model = client.model_info(device)
|
||||
|
||||
@ -50,8 +59,12 @@ def example_command(client, args):
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
elif args[0] == 'ac-mon':
|
||||
device_id = args[1]
|
||||
|
||||
def ac_mon(client, device_id):
|
||||
"""Monitor an AC/HVAC device, showing higher-level information about
|
||||
its status such as its temperature and operation mode.
|
||||
"""
|
||||
|
||||
ac = wideq.ACDevice(client, client.get_device(device_id))
|
||||
|
||||
try:
|
||||
@ -76,11 +89,28 @@ def example_command(client, args):
|
||||
finally:
|
||||
ac.monitor_stop()
|
||||
|
||||
elif args[0] == 'set-temp':
|
||||
temp = args[1]
|
||||
device_id = args[2]
|
||||
|
||||
client.session.set_device_controls(device_id, {'TempCfg': temp})
|
||||
def set_temp(client, device_id, temp):
|
||||
"""Set the configured temperature for an AC device."""
|
||||
|
||||
ac = wideq.ACDevice(client, client.get_device(device_id))
|
||||
ac.set_fahrenheit(int(temp))
|
||||
|
||||
|
||||
EXAMPLE_COMMANDS = {
|
||||
'ls': ls,
|
||||
'mon': mon,
|
||||
'ac-mon': ac_mon,
|
||||
'set-temp': set_temp,
|
||||
}
|
||||
|
||||
|
||||
def example_command(client, args):
|
||||
if not args:
|
||||
ls(client)
|
||||
else:
|
||||
func = EXAMPLE_COMMANDS[args[0]]
|
||||
func(client, *args[1:])
|
||||
|
||||
|
||||
def example(args):
|
||||
|
Loading…
x
Reference in New Issue
Block a user