diff --git a/brewblox_hass/mqtt.py b/brewblox_hass/mqtt.py index 4057269..9ea488d 100644 --- a/brewblox_hass/mqtt.py +++ b/brewblox_hass/mqtt.py @@ -41,6 +41,8 @@ async def mqtt_lifespan(fmqtt: FastMQTT): @asynccontextmanager async def lifespan(): async with AsyncExitStack() as stack: - await stack.enter_async_context(mqtt_lifespan(CV_LOCAL.get())) + # Order matters here: we want to be able to publish + # before we start receiving messages await stack.enter_async_context(mqtt_lifespan(CV_HASS.get())) + await stack.enter_async_context(mqtt_lifespan(CV_LOCAL.get())) yield diff --git a/brewblox_hass/relay.py b/brewblox_hass/relay.py index 47b5f30..6163785 100644 --- a/brewblox_hass/relay.py +++ b/brewblox_hass/relay.py @@ -3,7 +3,6 @@ """ -import asyncio import json import logging import re @@ -48,7 +47,7 @@ def binary_sensor_state(value: bool): return 'ON' if value else 'OFF' -async def handle_spark_state(message: dict): +def handle_spark_state(message: dict): known = CV_KNOWN.get() publisher = mqtt.CV_HASS.get() service = message['key'] @@ -143,7 +142,7 @@ async def handle_spark_state(message: dict): publisher.publish(state_topic, published_state) -async def handle_tilt_state(message: dict): +def handle_tilt_state(message: dict): known = CV_KNOWN.get() publisher = mqtt.CV_HASS.get() service = message['key'] @@ -207,9 +206,9 @@ async def on_state_message(client, topic, payload, qos, properties): message = json.loads(payload) if message['type'] == 'Spark.state': - asyncio.create_task(handle_spark_state(message)) + handle_spark_state(message) return if message['type'] == 'Tilt.state': - asyncio.create_task(handle_tilt_state(message)) + handle_tilt_state(message) return