]> mj.ucw.cz Git - home-hw.git/commitdiff
Iris: Two adjustments for new aiomqtt
authorMartin Mares <mj@ucw.cz>
Sat, 26 Aug 2023 14:26:07 +0000 (16:26 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 26 Aug 2023 14:26:07 +0000 (16:26 +0200)
(1) Topic is now an object, we need to extract its value.

(2) Run connect() explicitly instead of relying on automatic connection
establishment using "with". In the latter way, clean disconnect is attempted
upon exceptions, so the will is not sent.

rainbow/iris/burrow-iris.py

index dbf14e7b90c887e511d2cbbbba3ef781908b0a32..7af1fd5c9920bb5a906a8bf68f1581e26eb9d601 100755 (executable)
@@ -1,4 +1,4 @@
-#!/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>
@@ -148,16 +148,17 @@ async def mqtt_loop():
 
     will = aiomqtt.Will(topic='status/iris', payload='dead', qos=1, retain=True)
 
-    async with aiomqtt.Client(client_id='iris', hostname="burrow-mqtt", port=8883, tls_context=sctx, will=will) as mqtt:
-        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, 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():