From 96bf583eb7b6c43a7f2fdebee5d7614a70d75739 Mon Sep 17 00:00:00 2001 From: Marcus Brown Date: Sun, 20 Oct 2024 18:52:00 +1100 Subject: [PATCH] Fix coroutine warning by properly awaiting async MQTT methods - Wrapped `on_connect` and `on_message` methods with `asyncio.create_task()` to ensure they are properly awaited. - Created separate handler functions for connection and message handling. --- dayglo_detector/dayglo_detector.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dayglo_detector/dayglo_detector.py b/dayglo_detector/dayglo_detector.py index 788b618..210708e 100644 --- a/dayglo_detector/dayglo_detector.py +++ b/dayglo_detector/dayglo_detector.py @@ -33,20 +33,22 @@ initial_snapshot_processed = False connected_once = False class DaygloDetectorMQTTClient(MQTTClient): - async def on_connect(self, client, flags, rc, properties): + async def handle_on_connect(self, client, flags, rc, properties): + asyncio.create_task(self.handle_on_connect(client, flags, rc, properties)) global connected_once if rc == 0 and not connected_once: print("Connected successfully to MQTT broker") - await self.subscribe(MQTT_SNAPSHOT_TOPIC, qos=1) + await client.subscribe(MQTT_SNAPSHOT_TOPIC, qos=1) print(f"Subscribed to topic: {MQTT_SNAPSHOT_TOPIC}") connected_once = True - await publish_discovery_configurations(self) + await publish_discovery_configurations(client) # Publish initial rating of 0 - await publish_rating(self, 0) + await publish_rating(client, 0) else: print(f"Failed to connect, return code {rc}") - async def on_message(self, client, topic, payload, qos, properties): + async def handle_on_message(self, client, topic, payload, qos, properties): + asyncio.create_task(self.handle_on_message(client, topic, payload, qos, properties)) if topic == MQTT_SNAPSHOT_TOPIC: print("Snapshot received") if len(payload) == 0: