-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonhub_lights_handler.py
81 lines (69 loc) · 2.62 KB
/
buttonhub_lights_handler.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
from base_handler import *
from buttonhub_service import ButtonhubError
from utils import PERM_ADMIN
UPDATE_LIST = 'up'
class ButtonhubLightsHandler(BaseHandler):
def __init__(self, config, messenger, service_hub):
super().__init__(config, messenger, service_hub, key="buttonhub_lights", name="Buttonhub Lights")
self.buttonhub_service = service_hub.buttonhub
self.enabled = False
if self.buttonhub_service.enabled:
self.lights = self.buttonhub_service.get_config('lights')
self.rooms = self.buttonhub_service.get_config('rooms')
self.enabled = self.lights and self.rooms
def help(self, permission):
if not self.enabled:
return
if permission < PERM_ADMIN:
return
return {
'summary': "Can check which lights are on",
'examples': ["lights"],
}
def matches_message(self, message):
if not self.enabled:
return
return message.lower().strip() == 'lights'
def handle(self, message, **kwargs):
if kwargs['permission'] < PERM_ADMIN:
return "Sorry, you can't do this."
return self.check_lights()
def check_lights(self):
default_field = self.lights['default_field']
default_on_state = self.lights['default_on_state']
try:
lights_on_in = []
buttonhub_state = self.buttonhub_service.get_state()
for room in self.rooms:
for device in room['lights']:
device_name = device['name']
is_on = (buttonhub_state.get(device_name) or {}).get(default_field) == default_on_state
if is_on:
lights_on_in.append(room['name'])
break
if not lights_on_in:
message = 'No lights are on'
else:
message = 'Lights are on in:\n- ' + '\n- '.join(lights_on_in)
return {
'message': message,
'buttons': [
[
{
'text': 'Update',
'data': UPDATE_LIST,
}
]
],
}
except ButtonhubError:
return {
'message': 'Failed to check lights :/',
'answer': 'Error!',
}
def handle_button(self, data, **kwargs):
if data == UPDATE_LIST:
msg = self.check_lights()
msg['answer'] = "Updated."
return msg
return "Uh oh, something is off"