-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatcher.py
More file actions
80 lines (63 loc) · 3.03 KB
/
dispatcher.py
File metadata and controls
80 lines (63 loc) · 3.03 KB
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
import functools
from formatter import MarkdownFormatter as MF
from radiator_control import RadiatorControl
available_commands = []
def command(description):
def command_decorator(func):
available_commands.append('/' + func.__name__ + ' \- ' + description)
@functools.wraps(func)
def wrapper(*args, **kwargs):
func(*args, **kwargs)
return wrapper
return command_decorator
class Dispatcher:
def __init__(self, config):
self.control = RadiatorControl(config.rooms)
self.available_commands = available_commands
@command('show help')
def help(self, update, context):
answer = MF.list_with_heading('Available commands', self.available_commands, False)
update.message.reply_text(answer, parse_mode='MarkdownV2', quote=False)
@command('show available radiators')
def radiators(self, update, context):
answer = MF.list_with_heading('Available radiators', self.control.get_radiators())
update.message.reply_text(answer, parse_mode='MarkdownV2', quote=False)
@command('show available rooms')
def rooms(self, update, context):
answer = MF.list_with_heading('Available rooms', self.control.get_rooms())
update.message.reply_text(answer, parse_mode='MarkdownV2', quote=False)
@command('mode off')
def off(self, update, context):
answer = {}
for radiator in self.__parse_radiator(context.args):
answer[radiator] = self.control.mode_off(radiator)
update.message.reply_text(MF.map_with_heading('New mode\(s\)', answer), parse_mode='MarkdownV2', quote=False)
@command('mode auto')
def auto(self, update, context):
answer = {}
for radiator in self.__parse_radiator(context.args):
answer[radiator] = self.control.mode_auto(radiator)
update.message.reply_text(MF.map_with_heading('New mode\(s\)', answer), parse_mode='MarkdownV2', quote=False)
@command('mode manual')
def manual(self, update, context):
answer = {}
for radiator, temperature in self.__parse_radiator(context.args):
answer[radiator] = self.control.mode_manual(radiator, temperature)
update.message.reply_text(MF.map_with_heading('New mode\(s\)', answer), parse_mode='MarkdownV2', quote=False)
def __parse_radiator(self, args):
if not args:
return self.control.get_radiators()
elif len(args) == 1:
try:
temperature = float(args[0])
return [(radiator, temperature) for radiator in self.control.get_radiators()]
except ValueError:
return [args[0]]
else:
return [(args[0], float(args[1]))]
@command('request status')
def status(self, update, context):
answer = {}
for radiator in self.__parse_radiator(context.args):
answer[radiator] = self.control.get_status(radiator)
update.message.reply_text(MF.map_with_heading('Status', answer), parse_mode='MarkdownV2', quote=False)