-
Notifications
You must be signed in to change notification settings - Fork 5
/
tradfri
executable file
·224 lines (171 loc) · 6.97 KB
/
tradfri
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
#!/usr/bin/env python3
import asyncio
import logging, sys
from ikeatradfri import cli, config, console, devices, exceptions, signal_handler
from pytradfri import Gateway
from pytradfri.api.aiocoap_api import APIFactory
from pytradfri import error as pyerror
from concurrent.futures import CancelledError
hostConfig = {}
def hexToRgb(hex):
rgb = {}
rgb["red"] = int(hex[:2], 16)
rgb["green"] = int(hex[2:4], 16)
rgb["blue"] = int(hex[-2:], 16)
return rgb
async def run(args):
api_factory = APIFactory(
hostConfig["Gateway"], hostConfig["Identity"], hostConfig["Passkey"]
)
api = api_factory.request
gateway = Gateway()
try:
if args.command == "on":
device = await devices.get_device(api, gateway, args.ID)
await device.set_state(True)
if args.command == "off":
device = await devices.get_device(api, gateway, args.ID)
await device.set_state(False)
if args.command == "name":
device = await devices.get_device(api, gateway, args.ID)
await device.set_name(args.NAME)
if args.command == "level":
device = await devices.get_device(api, gateway, args.ID)
await device.set_level(args.value, transition_time=args.transition_time)
if args.command == "wb":
device = await devices.get_device(api, gateway, args.ID)
from ikeatradfri.colors import hex_whites
for key, a in hex_whites.items():
if a["Name"].lower() == args.value.lower():
await device.set_hex(a["Hex"], args.transition_time)
break
if args.command == "hex":
from ikeatradfri import colors
device = await devices.get_device(api, gateway, args.ID)
if args.value == "show":
print(
"Current hex: {} ({}) in colorspace: {}".format(
device.hex,
colors.color_name_for_hex(device.hex, device.colorspace),
device.colorspace,
)
)
if args.list:
print(colors.list_hexes(device.colorspace))
else:
await device.set_hex(args.value.lower(), args.transition_time)
if args.command == "list":
await console.list_devices(api, gateway, expand_groups=args.expand_groups)
if args.command == "pair":
from ikeatradfri import pair
shutdown = asyncio.Future()
await pair.pair(api_factory, shutdown)
if args.command == "raw":
device = await devices.get_device(api, gateway, args.ID)
print(device.raw)
if args.command == "hsb":
device = await devices.get_device(api, gateway, args.ID)
await device.set_hsb(
int(args.hue) * 65535 / 360,
int(args.saturation) * 65279 / 100,
args.brightness,
)
if args.command == "rgb":
device = await devices.get_device(api, gateway, args.ID)
await device.set_rgb(args.red, args.green, args.blue)
if args.command == "test":
device = await devices.get_device(api, gateway, 65551)
print(device.hex)
if args.command == "color":
from ikeatradfri import colors
device = await devices.get_device(api, gateway, args.ID)
if args.color_command == "list":
print(colors.list_hexes(colorspace=device.colorspace, levels=True))
if args.color_command == "set":
device = await devices.get_device(api, gateway, args.ID)
await device.set_hex(
colors.color(level=args.color, colorspace=device.colorspace)["Hex"]
)
except devices.UnsupportedDeviceCommand:
logging.error(
"Unsupported command '{0}' for device {1}".format(args.command, args.ID)
)
except pyerror.ClientError:
logging.critical("Device not found")
except Exception:
raise
await api_factory.shutdown()
def exception_handler(loop, context):
print("Caught the following exception")
print(context["message"])
# print(context['exception'])
if __name__ == "__main__":
args, parser = cli.getArgs()
if args.version:
print("{} version {}".format(parser.prog, cli.get_version()))
exit()
if args.debug:
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
elif args.verbose:
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
else:
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.CRITICAL)
loop = asyncio.get_event_loop()
if args.verbose is None and args.debug is None:
loop.set_exception_handler(exception_handler)
# loop.set_debug(True)
if args.command == "config":
config.handle_config_command(args)
exit()
try:
hostConfig = config.get_config(args)
except exceptions.ConfigNotFound:
logging.critical("No config file found!")
exit()
except exceptions.NoGatewaySpecified:
logging.critical("No gateway specified!")
exit()
try:
if args.command == "service":
from ikeatradfri import service
if args.service_command == "create":
service.create_service_file(args.user, args.group)
exit()
else:
service.show_service_file()
if args.command == "server":
from ikeatradfri import tcp_server
from ikeatradfri import http_server
loop = asyncio.get_event_loop()
loop.create_task(signal_handler.handle_signals(loop))
server = {
"Tcp": lambda: loop.create_task(
tcp_server.tcp_server().main(hostConfig)
),
"Http": lambda: loop.create_task(http_server.start(hostConfig)),
"Both": lambda: loop.create_task(
tcp_server.tcp_server().main(hostConfig)
)
and loop.create_task(http_server.start(hostConfig)),
}
server.get(hostConfig["Server_type"], lambda: "nothing")()
loop.run_forever()
elif args.command == "observe":
from ikeatradfri import observe
loop.create_task(signal_handler.handle_signals(loop))
loop.create_task(observe.observe())
loop.run_forever()
else:
loop.run_until_complete(run(args))
except KeyboardInterrupt:
print("Received exit, exiting")
except CancelledError:
logging.debug("Cancelled")
loop.stop()
except exceptions.ConfigNotFound:
print("NoConfig")
pass
except pyerror.ServerError:
logging.critical("Gateway error!")
except pyerror.RequestTimeout:
logging.error("Gateway error: Timeout")