-
Notifications
You must be signed in to change notification settings - Fork 16
/
__init__.py
484 lines (411 loc) · 16.1 KB
/
__init__.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
"""
mycroft-hue : A Mycroft skill for controlling Phillips Hue
Copyright (C) 2016 Christopher Rogers
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from adapt.intent import IntentBuilder
from collections import defaultdict
from mycroft.skills.core import MycroftSkill
from mycroft.util.log import getLogger
from mycroft.messagebus.message import Message
from os.path import dirname
from phue import Bridge
from phue import Group
from phue import PhueRegistrationException
from phue import PhueRequestTimeout
from time import sleep
from requests import ConnectionError
from requests import get
import socket
__author__ = 'ChristopherRogers1991'
LOGGER = getLogger(__name__)
DEFAULT_BRIGHTNESS_STEP = 50
DEFAULT_COLOR_TEMPERATURE_STEP = 1000
class DeviceNotFoundException(Exception):
pass
class UnauthorizedUserException(Exception):
def __init__(self, username):
msg = "User '{0}' is not registered with the bridge"
super(UnauthorizedUserException, self).__init__(msg.format(username))
def intent_handler(handler_function):
"""
Decorate handler functions with connection and
error handling.
Parameters
----------
handler_function : callable
Returns
-------
callable
"""
def handler(self, message: Message):
if message.msg_type == 'ConnectLightsIntent' \
or self.connected or self._connect_to_bridge():
group = self.default_group
if "Group" in message.data:
name = message.data["Group"].lower()
group_id = self.groups_to_ids_map[name]
group = Group(self.bridge, group_id)
try:
handler_function(self, message, group)
except PhueRequestTimeout:
self.speak_dialog('unable.to.perform.action')
except Exception as e:
if 'No route to host' in e.args:
if self.user_supplied_ip:
self.speak_dialog('no.route')
return
else:
self.speak_dialog('could.not.communicate')
if self._connect_to_bridge(True):
self.handle_intent(message)
else:
raise
return handler
class PhillipsHueSkill(MycroftSkill):
def __init__(self):
super(PhillipsHueSkill, self).__init__(name="PhillipsHueSkill")
self.brightness_step = int(self.settings.get('brightness_step',
DEFAULT_BRIGHTNESS_STEP))
self.color_temperature_step = \
int(self.settings.get('color_temperature_step',
DEFAULT_COLOR_TEMPERATURE_STEP))
verbose = self.settings.get('verbose', False)
if type(verbose) == str:
verbose = verbose.lower()
verbose = True if verbose == 'true' else False
self.verbose = verbose
self.username = self.settings.get('username')
if self.username == '':
self.username = None
self.ip = None # set in _connect_to_bridge
self.bridge = None
self.default_group = None
self.groups_to_ids_map = dict()
self.scenes_to_ids_map = defaultdict(dict)
@property
def connected(self):
return self.bridge is not None
@property
def user_supplied_ip(self):
return self.settings.get('ip') != ''
@property
def user_supplied_username(self):
return self.settings.get('username') != ''
def _register_with_bridge(self):
"""
Helper for connecting to the bridge. If we don't
have a valid username for the bridge (ip) we are trying
to use, this will cause one to be generated.
"""
self.speak_dialog('connect.to.bridge')
i = 0
while i < 30:
sleep(1)
try:
self.bridge = Bridge(self.ip)
except PhueRegistrationException:
continue
else:
break
if not self.connected:
self.speak_dialog('failed.to.register')
else:
self.speak_dialog('successfully.registered')
def _update_bridge_data(self):
"""
This should be called any time a successful
connection is established. It sets some
member variables, and ensures that scenes and
groups are registered as vocab.
"""
self.username = self.bridge.username
with self.file_system.open('username', 'w') as conf_file:
conf_file.write(self.username)
if not self.default_group:
self._set_default_group(self.settings.get('default_group'))
self._register_groups_and_scenes()
def _attempt_connection(self):
"""
This will attempt to connect to the bridge,
but will not handle any errors on it's own.
Raises
------
UnauthorizedUserException
If self.username is not None, and is not registered with the bridge
"""
if self.user_supplied_ip:
self.ip = self.settings.get('ip')
else:
self.ip = _discover_bridge()
if self.username:
url = 'http://{ip}/api/{user}'.format(ip=self.ip,
user=self.username)
data = get(url).json()
data = data[0] if isinstance(data, list) else data
error = data.get('error')
if error:
description = error.get('description')
if description == "unauthorized user":
raise UnauthorizedUserException(self.username)
else:
raise Exception('Unknown Error: {0}'.format(description))
self.bridge = Bridge(self.ip, self.username)
def _connect_to_bridge(self, acknowledge_successful_connection=False):
"""
Calls _attempt_connection, handling various exceptions
by either alerting the user to issues with the config/setup,
or registering the application with the bridge.
Parameters
----------
acknowledge_successful_connection : bool
Speak when a successful connection is established.
Returns
-------
bool
True if a connection is established.
"""
try:
self._attempt_connection()
except DeviceNotFoundException:
self.speak_dialog('bridge.not.found')
return False
except ConnectionError:
self.speak_dialog('failed.to.connect')
if self.user_supplied_ip:
self.speak_dialog('ip.in.config')
return False
except socket.error as e:
if 'No route to host' in e.args:
self.speak_dialog('no.route')
else:
self.speak_dialog('failed.to.connect')
return False
except UnauthorizedUserException:
if self.user_supplied_username:
self.speak_dialog('invalid.user')
return False
else:
self._register_with_bridge()
except PhueRegistrationException:
self._register_with_bridge()
if not self.connected:
return False
if acknowledge_successful_connection:
self.speak_dialog('successfully.connected')
self._update_bridge_data()
return True
def _set_default_group(self, identifier):
"""
Sets the group to which commands will be applied, when
a group is not specified in the command.
Parameters
----------
identifier : str or int
The name of the group, or it's integer id
Notes
-----
If the group does not exist, 0 (all lights) will be
used.
"""
try:
self.default_group = Group(self.bridge, identifier)
except LookupError:
self.speak_dialog('could.not.find.group', {'name': identifier})
self.speak_dialog('using.group.0')
self.default_group = Group(self.bridge, 0)
def _register_groups_and_scenes(self):
"""
Register group and scene names as vocab,
and update our caches.
"""
groups = self.bridge.get_group()
for id, group in groups.items():
name = group['name'].lower()
self.groups_to_ids_map[name] = id
self.register_vocabulary(name, "Group")
scenes = self.bridge.get_scene()
for id, scene in scenes.items():
name = scene['name'].lower()
group_id = scene.get('group')
group_id = int(group_id) if group_id else None
self.scenes_to_ids_map[group_id][name] = id
self.register_vocabulary(name, "Scene")
def initialize(self):
"""
Attempt to connect to the bridge,
and build/register intents.
"""
self.load_data_files(dirname(__file__))
if self.file_system.exists('username'):
if not self.user_supplied_username:
with self.file_system.open('username', 'r') as conf_file:
self.username = conf_file.read().strip(' \n')
try:
self._attempt_connection()
self._update_bridge_data()
except (PhueRegistrationException,
DeviceNotFoundException,
UnauthorizedUserException,
ConnectionError,
socket.error):
# Swallow it for now; _connect_to_bridge will deal with it
pass
toggle_intent = IntentBuilder("ToggleIntent") \
.one_of("OffKeyword", "OnKeyword") \
.one_of("Group", "LightsKeyword") \
.build()
self.register_intent(toggle_intent, self.handle_toggle_intent)
activate_scene_intent = IntentBuilder("ActivateSceneIntent") \
.require("Scene") \
.one_of("Group", "LightsKeyword") \
.build()
self.register_intent(activate_scene_intent,
self.handle_activate_scene_intent)
adjust_brightness_intent = IntentBuilder("AdjustBrightnessIntent") \
.one_of("IncreaseKeyword", "DecreaseKeyword", "DimKeyword") \
.one_of("Group", "LightsKeyword") \
.optionally("BrightnessKeyword") \
.build()
self.register_intent(adjust_brightness_intent,
self.handle_adjust_brightness_intent)
set_brightness_intent = IntentBuilder("SetBrightnessIntent") \
.require("Value") \
.one_of("Group", "LightsKeyword") \
.optionally("BrightnessKeyword") \
.build()
self.register_intent(set_brightness_intent,
self.handle_set_brightness_intent)
adjust_color_temperature_intent = \
IntentBuilder("AdjustColorTemperatureIntent") \
.one_of("IncreaseKeyword", "DecreaseKeyword") \
.one_of("Group", "LightsKeyword") \
.require("ColorTemperatureKeyword") \
.build()
self.register_intent(adjust_color_temperature_intent,
self.handle_adjust_color_temperature_intent)
connect_lights_intent = \
IntentBuilder("ConnectLightsIntent") \
.require("ConnectKeyword") \
.one_of("Group", "LightsKeyword") \
.build()
self.register_intent(connect_lights_intent,
self.handle_connect_lights_intent)
@intent_handler
def handle_toggle_intent(self, message, group):
if "OffKeyword" in message.data:
dialog = 'turn.off'
group.on = False
else:
dialog = 'turn.on'
group.on = True
if self.verbose:
self.speak_dialog(dialog)
@intent_handler
def handle_activate_scene_intent(self, message, group):
scene_name = message.data['Scene'].lower()
scene_id = self.scenes_to_ids_map[group.group_id].get(scene_name)
if not scene_id:
scene_id = self.scenes_to_ids_map[None].get(scene_name)
if scene_id:
if self.verbose:
self.speak_dialog('activate.scene',
{'scene': scene_name})
self.bridge.activate_scene(group.group_id, scene_id)
else:
self.speak_dialog('scene.not.found',
{'scene': scene_name})
@intent_handler
def handle_adjust_brightness_intent(self, message, group):
if "IncreaseKeyword" in message.data:
brightness = group.brightness + self.brightness_step
group.brightness = \
brightness if brightness < 255 else 254
dialog = 'increase.brightness'
else:
brightness = group.brightness - self.brightness_step
group.brightness = brightness if brightness > 0 else 0
dialog = 'decrease.brightness'
if self.verbose:
self.speak_dialog(dialog)
@intent_handler
def handle_set_brightness_intent(self, message, group):
value = int(message.data['Value'].rstrip('%'))
brightness = int(value / 100.0 * 254)
group.on = True
group.brightness = brightness
if self.verbose:
self.speak_dialog('set.brightness', {'brightness': value})
@intent_handler
def handle_adjust_color_temperature_intent(self, message, group):
if "IncreaseKeyword" in message.data:
color_temperature = \
group.colortemp_k + self.color_temperature_step
group.colortemp_k = \
color_temperature if color_temperature < 6500 else 6500
dialog = 'increase.color.temperature'
else:
color_temperature = \
group.colortemp_k - self.color_temperature_step
group.colortemp_k = \
color_temperature if color_temperature > 2000 else 2000
dialog = 'decrease.color.temperature'
if self.verbose:
self.speak_dialog(dialog)
@intent_handler
def handle_connect_lights_intent(self, message, group):
if self.user_supplied_ip:
self.speak_dialog('ip.in.config')
return
if self.verbose:
self.speak_dialog('connecting')
self._connect_to_bridge(acknowledge_successful_connection=True)
def stop(self):
pass
def _discover_bridge():
"""
Naive method to find a phillips hue bridge on
the network, via UPNP.
Raises
------
DeviceNotFoundException
If the bridge is not found.
Returns
-------
str
An IP address representing the bridge that was found
"""
SSDP_ADDR = "239.255.255.250"
SSDP_PORT = 1900
SSDP_MX = 1
SSDP_ST = "urn:schemas-upnp-org:device:Basic:1"
ssdpRequest = "M-SEARCH * HTTP/1.1\r\n" + \
"HOST: %s:%d\r\n" % (SSDP_ADDR, SSDP_PORT) + \
"MAN: \"ssdp:discover\"\r\n" + \
"MX: %d\r\n" % (SSDP_MX,) + \
"ST: %s\r\n" % (SSDP_ST,) + "\r\n"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(5.0)
sock.sendto(ssdpRequest.encode(), (SSDP_ADDR, SSDP_PORT))
try:
result = sock.recv(4096).decode()
lines = result.splitlines()
for i in range(len(lines)):
if lines[i].startswith('hue-bridgeid'):
location_index = i - 2
sock.close()
return lines[location_index].split('/')[2]
except:
pass
raise DeviceNotFoundException()
def create_skill():
return PhillipsHueSkill()