-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcli.py
304 lines (262 loc) · 9.41 KB
/
cli.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""Command-line tool for working with myStrom devices."""
import click
import requests
import asyncio
from functools import wraps
from pymystrom.bulb import MyStromBulb
URI = "api/v1/device"
TIMEOUT = 5
def coro(f):
"""Allow to use async in click."""
@wraps(f)
def wrapper(*args, **kwargs):
"""Async wrapper."""
return asyncio.run(f(*args, **kwargs))
return wrapper
@click.group()
@click.version_option()
def main():
"""Simple command-line tool to get and set the values of myStrom devices.
This tool can also set the targets of a myStrom button for the different
available actions single, double, long and touch.
"""
@main.group("config")
def config():
"""Get and set the configuration of a myStrom device."""
@config.command("read")
@click.option(
"--ip", prompt="IP address of the device", help="IP address of the device."
)
@click.option(
"--mac", prompt="MAC address of the device", help="MAC address of the device."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
def read_config(ip, mac, token):
"""Read the current configuration of a myStrom device."""
click.echo("Read configuration from %s" % ip)
try:
request = requests.get("http://{}/{}/{}/".format(ip, URI, mac), timeout=TIMEOUT, headers={'Token': token})
click.echo(request.json())
except requests.exceptions.ConnectionError:
click.echo("Communication issue with the device")
@main.group("button")
def button():
"""Get and set details of a myStrom button."""
@button.command("generic")
@click.option(
"--ip", prompt="IP address of the button", help="IP address of the button."
)
@click.option(
"--mac", prompt="MAC address of the button", help="MAC address of the button."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
@click.option(
"--single", prompt="URL for a single tap", default="", help="URL for a single tap."
)
@click.option(
"--double", prompt="URL for a double tap", default="", help="URL for a double tap."
)
@click.option(
"--long", prompt="URL for a long tab", default="", help="URL for a long tab."
)
@click.option("--touch", prompt="URL for a touch", default="", help="URL for a touch.")
def write_config(ip, mac, token, single, double, long, touch):
"""Write the current configuration of a myStrom button."""
click.echo("Write configuration to device %s" % ip)
data = {
"single": single,
"double": double,
"long": long,
"touch": touch,
}
try:
request = requests.post(
"http://{}/{}/{}/".format(ip, URI, mac), data=data, timeout=TIMEOUT, headers={'Token': token}
)
if request.status_code == 200:
click.echo("Configuration of %s set" % mac)
except requests.exceptions.ConnectionError:
click.echo("Communication issue with the device. No action performed")
@button.command("home-assistant")
@click.option(
"--ip", prompt="IP address of the button", help="IP address of the button."
)
@click.option(
"--mac", prompt="MAC address of the button", help="MAC address of the button."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
@click.option(
"--hass",
prompt="IP address of the Home Assistant instance",
help="IP address of Home Assistant instance to use.",
)
@click.option(
"--port",
prompt="Port of Home Assistant instance",
default="8123",
help="Port where Home Assistant instance is listening.",
)
@click.option(
"--id", prompt="ID of the button", default="", help="ID of the myStrom button."
)
def write_ha_config(ip, mac, token, hass, port, id):
"""Write the configuration for Home Assistant to a myStrom button."""
click.echo("Write configuration for Home Assistant to device %s..." % ip)
action = "get://{1}:{2}/api/mystrom?{0}={3}"
data = {
"single": action.format("single", hass, port, id),
"double": action.format("double", hass, port, id),
"long": action.format("long", hass, port, id),
"touch": action.format("touch", hass, port, id),
}
try:
request = requests.post(
"http://{}/{}/{}/".format(ip, URI, mac), data=data, timeout=TIMEOUT, headers={'Token': token}
)
if request.status_code == 200:
click.echo("Configuration for %s set" % ip)
click.echo(
"After using the push pattern the first time then "
"the myStrom WiFi Button will show up as %s" % id
)
except requests.exceptions.ConnectionError:
click.echo("Communication issue with the device")
@button.command("reset")
@click.option(
"--ip", prompt="IP address of the WiFi Button", help="P address of the WiFi Button."
)
@click.option(
"--mac", prompt="MAC address of the button", help="MAC address of the Wifi Button."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
def reset_config(ip, mac, token):
"""Reset the current configuration of a myStrom WiFi Button."""
click.echo("Reset configuration of button %s..." % ip)
data = {
"single": "",
"double": "",
"long": "",
"touch": "",
}
try:
request = requests.post(
"http://{}/{}/{}/".format(ip, URI, mac), data=data, timeout=TIMEOUT, headers={'Token': token}
)
if request.status_code == 200:
click.echo("Reset configuration of %s" % mac)
except requests.exceptions.ConnectionError:
click.echo("Communication issue with the device. No action performed")
@button.command("read")
@click.option(
"--ip", prompt="IP address of the WiFi Button", help="P address of the WiFi Button."
)
@click.option(
"--mac", prompt="MAC address of the button", help="MAC address of the Wifi Button."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
def read_config(ip, mac, token):
"""Read the current configuration of a myStrom WiFi Button."""
click.echo("Read the configuration of button %s..." % ip)
try:
request = requests.get("http://{}/{}/{}/".format(ip, URI, mac), timeout=TIMEOUT, headers={'Token': token})
click.echo(request.json())
except requests.exceptions.ConnectionError:
click.echo("Communication issue with the device. No action performed")
@main.group("bulb")
def bulb():
"""Get and set details of a myStrom bulb."""
@bulb.command("on")
@coro
@click.option("--ip", prompt="IP address of the bulb", help="IP address of the bulb.")
@click.option(
"--mac", prompt="MAC address of the bulb", help="MAC address of the bulb."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
async def on(ip, mac, token):
"""Switch the bulb on."""
async with MyStromBulb(ip, mac, token) as bulb:
await bulb.set_color_hex("00FFFFFF")
@bulb.command("color")
@coro
@click.option("--ip", prompt="IP address of the bulb", help="IP address of the bulb.")
@click.option(
"--mac", prompt="MAC address of the bulb", help="MAC address of the bulb."
)
@click.option(
"--hue", prompt="Set the hue of the bulb", help="Set the hue of the bulb."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
@click.option(
"--saturation",
prompt="Set the saturation of the bulb",
help="Set the saturation of the bulb.",
)
@click.option(
"--value", prompt="Set the value of the bulb", help="Set the value of the bulb."
)
async def color(ip, mac, token, hue, saturation, value):
"""Switch the bulb on with the given color."""
async with MyStromBulb(ip, mac, token) as bulb:
await bulb.set_color_hsv(hue, saturation, value)
@bulb.command("off")
@coro
@click.option("--ip", prompt="IP address of the bulb", help="IP address of the bulb.")
@click.option(
"--mac", prompt="MAC address of the bulb", help="MAC address of the bulb."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
async def off(ip, mac, token):
"""Switch the bulb off."""
async with MyStromBulb(ip, mac, token) as bulb:
await bulb.set_off()
@bulb.command("flash")
@coro
@click.option("--ip", prompt="IP address of the bulb", help="IP address of the bulb.")
@click.option(
"--mac", prompt="MAC address of the bulb", help="MAC address of the bulb."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
@click.option(
"--time", prompt="Time to flash", help="Time to flash the bulb in seconds.", default=10,
)
async def flash(ip, mac, token, time):
"""Flash the bulb off."""
async with MyStromBulb(ip, mac, token) as bulb:
await bulb.set_flashing(time, [100, 50, 30], [200, 0, 71])
@bulb.command("rainbow")
@coro
@click.option("--ip", prompt="IP address of the bulb", help="IP address of the bulb.")
@click.option(
"--mac", prompt="MAC address of the bulb", help="MAC address of the bulb."
)
@click.option(
"--token", prompt="Token of the device", help="Token of the device."
)
@click.option(
"--time", prompt="Time for the complete rainbow", help="Time to perform the rainbow in seconds.", default=30,
)
async def rainbow(ip, mac, token, time):
"""Let the buld change the color and show a rainbow."""
async with MyStromBulb(ip, mac, token) as bulb:
await bulb.set_rainbow(time)
await bulb.set_transition_time(1000)
if __name__ == "__main__":
main()