X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=telegram%2Fburrow-telegram.py;h=22fa145eda4f97ef4cacb5cb859b5c4891f18d69;hb=ef81a4d74227ac8fb82b0d153ae85b25cfc7b18b;hp=f763af00b803b95832dc5f3de2b0b120ed17a1ec;hpb=df772cedd84204136f203409b655f35444837f3f;p=home-hw.git diff --git a/telegram/burrow-telegram.py b/telegram/burrow-telegram.py index f763af0..22fa145 100755 --- a/telegram/burrow-telegram.py +++ b/telegram/burrow-telegram.py @@ -4,7 +4,7 @@ from aiogram import Bot, Dispatcher, executor, types import asyncio -import asyncio_mqtt +import aiomqtt from configparser import ConfigParser from datetime import datetime, timedelta import logging @@ -132,17 +132,19 @@ async def mqtt_process_msg(topic, val): async def mqtt_loop(): - sctx = ssl.SSLContext(ssl.PROTOCOL_TLS) + sctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) sctx.verify_mode = ssl.CERT_REQUIRED sctx.load_cert_chain('/etc/burrow-mqtt/client.crt', '/etc/burrow-mqtt/client.key') sctx.load_verify_locations(cafile='/etc/burrow-mqtt/ca.crt') - async with asyncio_mqtt.Client(hostname="burrow-mqtt", port=8883, tls_context=sctx) as mqtt: - async with mqtt.unfiltered_messages() as messages: - await mqtt.subscribe("burrow/heating/#") - await mqtt.subscribe("burrow/temp/#") - async for msg in messages: - await mqtt_process_msg(msg.topic, msg.payload.decode()) + mqtt = aiomqtt.Client(client_id='telegram', hostname="burrow-mqtt", port=8883, tls_context=sctx) + await mqtt.connect() + + async with mqtt.messages() as messages: + await mqtt.subscribe("burrow/heating/#") + await mqtt.subscribe("burrow/temp/#") + async for msg in messages: + await mqtt_process_msg(msg.topic.value, msg.payload.decode()) async def mqtt_watcher(): @@ -150,7 +152,7 @@ async def mqtt_watcher(): try: logger.info("Starting MQTT") await mqtt_loop() - except asyncio_mqtt.MqttError as error: + except aiomqtt.MqttError as error: logger.error(f"MQTT error: {error}") await asyncio.sleep(10)