Skip to content

Commit 0dc117d

Browse files
committed
cover-control.py: Use different commands for window control
1 parent a7a300e commit 0dc117d

File tree

2 files changed

+33
-39
lines changed

2 files changed

+33
-39
lines changed

cover-control.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,16 @@ async def apply_schedule():
181181
logging.exception('Fehler beim Anwenden des Plans')
182182

183183

184-
async def open_window(args):
184+
async def on_window_command(command, args):
185185
global window_is_closed
186186

187-
if len(args) != 1:
188-
return
189-
190-
if args[0] == 'auf':
187+
if command == 'fenster_auf':
191188
logging.info('Fenster werden geöffnet')
192189
arduinoclient.open_window()
193190
window_is_closed = False
194191
await telegram.bot_send(text='Die Fenster werden geöffnet')
195192

196-
if args[0] == 'zu':
193+
if command == 'fenster_zu':
197194
logging.info('Fenster werden geschlossen')
198195
arduinoclient.close_window()
199196
window_is_closed = True
@@ -208,7 +205,7 @@ async def main():
208205

209206
mqttclient.connect(server=config['MQTT_SERVER'], user=config['MQTT_USER'], password=config['MQTT_PASSWORD'], topic=config['MQTT_TOPIC'])
210207

211-
await telegram.bot_start(token=config['TELEGRAM_BOT_TOKEN'], chat_id=config['TELEGRAM_CHAT_ID'], command='Fenster', command_callback=open_window)
208+
await telegram.bot_start(token=config['TELEGRAM_BOT_TOKEN'], chat_id=config['TELEGRAM_CHAT_ID'], commands=['fenster_auf', 'fenster_zu'], command_callback=on_window_command)
212209

213210
update_schedule()
214211

modules/telegram.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,46 @@
2727

2828
application = None
2929
default_chat_id = None
30-
custom_command = None
31-
custom_command_callback = None
3230

33-
async def bot_start(token, chat_id, command = None, command_callback = None):
31+
class MyCommandHandler(telegram.ext.CommandHandler):
32+
__slots__ = ('my_command', 'my_callback')
33+
34+
def __init__(self, command, callback):
35+
super().__init__(command, self.on_command)
36+
self.my_command = command
37+
self.my_callback = callback
38+
39+
async def on_command(self, update, context: telegram.ext.ContextTypes.DEFAULT_TYPE):
40+
try:
41+
if self.my_command is not None:
42+
await self.my_callback(self.my_command, context.args)
43+
except:
44+
logging.exception('Fehler beim Bearbeiten des Kommandos')
45+
46+
47+
async def bot_start(token, chat_id, commands = [ ], command_callback = None):
3448
global application
3549
global default_chat_id
36-
global custom_command
37-
global custom_command_callback
3850

3951
default_chat_id = chat_id
40-
52+
4153
application = telegram.ext.Application.builder().token(token).read_timeout(20).get_updates_read_timeout(30).build()
4254

4355
application.add_error_handler(on_error)
4456

45-
if command is not None:
46-
application.add_handler(telegram.ext.CommandHandler(command, on_custom_command))
47-
48-
custom_command = command
49-
custom_command_callback = command_callback
50-
57+
for command in commands:
58+
application.add_handler(MyCommandHandler(command, command_callback))
59+
5160
await application.initialize()
5261
await application.updater.start_polling()
5362
await application.start()
5463

5564

56-
async def on_custom_command(update, context: telegram.ext.ContextTypes.DEFAULT_TYPE):
57-
global custom_command_callback
58-
59-
try:
60-
if custom_command_callback is not None:
61-
await custom_command_callback(context.args)
62-
63-
except:
64-
logging.exception('Fehler beim Bearbeiten des Kommandos')
65-
66-
6765
async def on_error(update, context: telegram.ext.ContextTypes.DEFAULT_TYPE):
6866
global application
69-
global custom_command
70-
67+
7168
logging.exception('Error in telegram bot', exc_info = context.error)
72-
69+
7370
# If the bot is idle for several hours it might happen that it looses
7471
# connection after a NetworkError. We can fix that with a simple reconnect
7572
if isinstance(context.error, telegram.error.NetworkError):
@@ -88,20 +85,20 @@ async def bot_stop():
8885
async def bot_send(text, chat_id = None):
8986
global application
9087
global default_chat_id
91-
88+
9289
if chat_id is None:
9390
chat_id = default_chat_id
94-
91+
9592
message = await application.bot.send_message(chat_id, text)
96-
93+
9794
return message.message_id
9895

9996
async def bot_delete(message_id, chat_id = None):
10097
global application
10198
global default_chat_id
102-
99+
103100
if chat_id is None:
104101
chat_id = default_chat_id
105-
102+
106103
await application.bot.delete_message(chat_id, message_id)
107-
104+

0 commit comments

Comments
 (0)