-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonhub_modes_handler.py
85 lines (76 loc) · 2.95 KB
/
buttonhub_modes_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
82
83
84
85
from base_handler import *
from buttonhub_service import ButtonhubError
from utils import PERM_ADMIN, get_timestamp
UPDATE_LIST = 'up'
class ButtonhubModesHandler(BaseHandler):
def __init__(self, config, messenger, service_hub):
super().__init__(config, messenger, service_hub, key="buttonhub_modes", name="Buttonhub Modes")
self.buttonhub_service = service_hub.buttonhub
self.enabled = False
if self.buttonhub_service.enabled:
self.modes = self.buttonhub_service.get_config('modes')
self.enabled = bool(self.modes)
def help(self, permission):
if not self.enabled:
return
if permission < PERM_ADMIN:
return
return {
'summary': "Can check which modes are on and such",
'examples': ["modes"],
}
def matches_message(self, message):
if not self.enabled:
return
return message.lower().strip() == 'modes'
def handle(self, message, **kwargs):
if kwargs['permission'] < PERM_ADMIN:
return "Sorry, you can't do this."
return self.check_modes()
def check_modes(self, include_timestamp=False):
try:
result = []
buttonhub_state = self.buttonhub_service.get_state()
for mode in self.modes:
type = mode.get('type')
display_name = mode.get('display_name')
topic = mode.get('topic') or ''
path = mode.get('path') or ''
value = (buttonhub_state.get(topic) or {}).get(path)
if type == 'boolean':
if value:
result.append(f'{display_name}: ON')
elif type == 'enum':
values = (mode.get('values') or [])
for possible_value in values:
if value == possible_value.get('name'):
result.append(f'{display_name}: {possible_value.get('display_name')}')
break
if result:
message = '\n- '+('\n- '.join(result))
else:
message = 'No modes are active'
if include_timestamp:
message = message + f'\n\nUpdated: {get_timestamp()}'
return {
'message': message,
'buttons': [
[
{
'text': 'Update',
'data': UPDATE_LIST,
}
]
],
}
except ButtonhubError:
return {
'message': 'Failed to check modes :/',
'answer': 'Error!',
}
def handle_button(self, data, **kwargs):
if data == UPDATE_LIST:
msg = self.check_modes(include_timestamp=True)
msg['answer'] = "Updated."
return msg
return "Uh oh, something is off"