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:
@@ -33,20 +33,22 @@ initial_snapshot_processed = False
|
|||||||
connected_once = False
|
connected_once = False
|
||||||
|
|
||||||
class DaygloDetectorMQTTClient(MQTTClient):
|
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
|
global connected_once
|
||||||
if rc == 0 and not connected_once:
|
if rc == 0 and not connected_once:
|
||||||
print("Connected successfully to MQTT broker")
|
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}")
|
print(f"Subscribed to topic: {MQTT_SNAPSHOT_TOPIC}")
|
||||||
connected_once = True
|
connected_once = True
|
||||||
await publish_discovery_configurations(self)
|
await publish_discovery_configurations(client)
|
||||||
# Publish initial rating of 0
|
# Publish initial rating of 0
|
||||||
await publish_rating(self, 0)
|
await publish_rating(client, 0)
|
||||||
else:
|
else:
|
||||||
print(f"Failed to connect, return code {rc}")
|
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:
|
if topic == MQTT_SNAPSHOT_TOPIC:
|
||||||
print("Snapshot received")
|
print("Snapshot received")
|
||||||
if len(payload) == 0:
|
if len(payload) == 0:
|
||||||
|
|||||||
Reference in New Issue
Block a user