-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxcli.py
100 lines (80 loc) · 2.04 KB
/
maxcli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import click
from explorer import Explorer
class Config(object):
def __init__(self):
self.device = None
self.broadcast_address = None
config = click.make_pass_decorator(Config, ensure=True)
@click.group()
@click.option('--broadcast', default='192.168.1.255')
@config
def cli(config, broadcast):
config.broadcast_address = broadcast
@cli.command()
@config
def list(config):
devices = Explorer().discover(config.broadcast_address)
for device in devices:
print "{} {} {}".format(device.name, device.ip, device.sn)
@cli.group()
@click.argument('sn')
@config
def device(config, sn):
devices = Explorer().discover(config.broadcast_address)
for device in devices:
if device.sn == sn:
config.device = device
@device.command()
@config
def status(config):
status_info = Explorer().status(config.device)
switch = 'On' if status_info['switch'][0] == 1 else 'Off'
watt = status_info['watt'][0]
click.echo('%s %s W' % (switch, watt))
@device.command()
@config
def on(config):
Explorer().switch(config.device, 'on')
@device.command()
@config
def off(config):
Explorer().switch(config.device, 'off')
@device.command()
@config
@click.argument('name', required=False)
def name(config, name):
if name:
Explorer().set_name(config.device, name)
else:
click.echo(config.device.name)
@device.command()
@config
def time(config):
click.echo(config.device.time())
@device.command()
@config
@click.argument('datetime', required=False)
def timer(config, datetime):
if datetime:
click.echo(config.device.set_timer(datetime))
else:
click.echo(config.device.timer())
@device.command()
@config
def rules(config):
config.device.rules()
@device.group()
@config
def rule(config):
pass
@rule.command()
@config
@click.argument('time')
@click.argument('state')
def add(config, time, state):
config.device.add_rule(time, state)
@rule.command()
@config
@click.argument('rule_id')
def delete(config, rule_id):
config.device.delete_rule(rule_id)