From 564c963af109b7cb5f55dc898bbfbbd1d9fd0532 Mon Sep 17 00:00:00 2001 From: Marcus Brown Date: Sun, 20 Oct 2024 18:48:18 +1100 Subject: [PATCH] Fix coroutine warning and replace MQTT client loop handling - Properly awaited the `on_connect` method to resolve RuntimeWarning. - Replaced `loop_forever()` with an async event loop using `asyncio.get_event_loop().create_future()`. --- dayglo_detector/dayglo_detector.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dayglo_detector/dayglo_detector.py b/dayglo_detector/dayglo_detector.py index e29e813..3295e8d 100644 --- a/dayglo_detector/dayglo_detector.py +++ b/dayglo_detector/dayglo_detector.py @@ -33,7 +33,7 @@ initial_snapshot_processed = False connected_once = False class DaygloDetectorMQTTClient(MQTTClient): - async def on_connect(self, client, flags, rc, properties): + async async def on_connect(self, client, flags, rc, properties): global connected_once if rc == 0 and not connected_once: print("Connected successfully to MQTT broker") @@ -134,7 +134,8 @@ async def main(): client.set_auth_credentials(MQTT_USERNAME, MQTT_PASSWORD) await client.connect(MQTT_BROKER, MQTT_PORT, keepalive=60) - client.loop_forever() + await client.connect(MQTT_BROKER, MQTT_PORT, keepalive=60) + await asyncio.get_event_loop().create_future() if __name__ == "__main__": asyncio.run(main())