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.
This commit is contained in:
2024-10-20 18:52:00 +11:00
parent 998ad12e61
commit 96bf583eb7
+7 -5
View File
@@ -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: