-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathneocli.py
executable file
·126 lines (94 loc) · 2.92 KB
/
neocli.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
import asyncio
import sys
import argparse
import json
import logging
import socket
import os
from neohub import NeoHub, NeoDevice
logging.basicConfig(level=logging.DEBUG)
def ok(what):
if what:
return 0
else:
print(repr(what))
return 1
async def main(neo, cmd, args):
await neo.async_setup()
if cmd == "call":
print(json.dumps(await neo.call(json.loads(args[0])), sort_keys=True, indent=2))
return 0
if cmd == "stat":
print(json.dumps((await neo.update())[args[0]], sort_keys=True, indent=2))
return 0
if cmd == "set_diff":
return ok(await neo.set_diff(args[0], args[1]))
if cmd == "switch_on":
return (await neo.neoplugs()[args[0]].switch_on())
if cmd == "switch_off":
return (await neo.neoplugs()[args[0]].switch_off())
if cmd == "script":
p = neo.neoplugs()["F1 Hall Plug"]
print(repr(p))
await p.switch_off()
print(repr(p))
await p.switch_on()
print(repr(p))
if cmd == "rename_zone":
return ok(await neo.zone_title(args[0], args[1]))
if cmd == "remove_zone":
return ok(await neo.remove_zone(args[0]))
# pass 4 digits as PIN
if cmd == "lock":
pin_str = args[1]
return ok(await neo.set_locked(args[0], pin_str))
if cmd == "unlock":
return ok(await neo.set_unlocked(args[0]))
if cmd == "frost_on":
return ok(await neo.frost_on(args[0]))
if cmd == "frost_off":
return ok(await neo.frost_off(args[0]))
if cmd == "set_program_mode":
return ok(await neo.set_program_mode(args[0]))
if cmd == "set_temp":
return ok(await neo.set_temp(args[0], args[1]))
if cmd == "set_cool_temp":
return ok(await neo.set_cool_temp(args[0], args[1]))
if cmd == "list":
for name in neo.neostats():
ns = neo.neostats()[name]
print(repr(ns))
print("")
for name in neo.neoplugs():
ns = neo.neoplugs()[name]
print(repr(ns))
return 0
if cmd == "list-stats":
for name in neo.neostats():
ns = neo.neostats()[name]
print(repr(ns))
return 0
if cmd == "stat-names":
for name in neo.neostats():
print(name)
return 0
if cmd == "list-plugs":
for name in neo.neoplugs():
ns = neo.neoplugs()[name]
print(repr(ns))
return 0
return 1
if __name__ == '__main__':
host = os.environ.get("NEOHUB_IP")
if host is None:
print("Please set the NEOHUB_IP environment variable")
print("eg: NEOHUB_IP=\"192.168.0.123\" %s ..." % sys.argv[0])
sys.exit(1)
loop = asyncio.get_event_loop()
neo = NeoHub(host, 4242)
cmd = sys.argv[1]
args = sys.argv[2:]
retval = loop.run_until_complete(main(neo, cmd, args))
loop.close()
sys.exit(retval)