-#!/usr/bin/env python
+#!/usr/bin/env python3
# Iris -- the Burrow's goddess of rainbow
# Controls LEDs on the Rainbow according to the state of the house
# (c) 2022 Martin Mareš <mj@ucw.cz>
import argparse
import asyncio
-import asyncio_mqtt
+import aiomqtt
from datetime import datetime, timedelta
import logging
from logging.handlers import SysLogHandler
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')
- will = asyncio_mqtt.Will(topic='status/iris', payload='dead', qos=1, retain=True)
+ will = aiomqtt.Will(topic='status/iris', payload='dead', qos=1, retain=True)
- async with asyncio_mqtt.Client(client_id='iris', hostname="burrow-mqtt", port=8883, tls_context=sctx, will=will) as mqtt:
- global st
- st = State(mqtt)
- async with mqtt.unfiltered_messages() as messages:
- await mqtt.subscribe("burrow/air/ac-on")
- await mqtt.subscribe("burrow/heating/#")
- await mqtt.subscribe("burrow/temp/#")
- await mqtt.publish("status/iris", "ok", retain=True)
- async for msg in messages:
- await mqtt_process_msg(msg.topic, msg.payload.decode())
+ mqtt = aiomqtt.Client(client_id='iris', hostname="burrow-mqtt", port=8883, tls_context=sctx, will=will)
+ await mqtt.connect()
+ global st
+ st = State(mqtt)
+ async with mqtt.messages() as messages:
+ await mqtt.subscribe("burrow/air/ac-on")
+ await mqtt.subscribe("burrow/heating/#")
+ await mqtt.subscribe("burrow/temp/#")
+ await mqtt.publish("status/iris", "ok", retain=True)
+ async for msg in messages:
+ await mqtt_process_msg(msg.topic.value, msg.payload.decode())
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)