Skip to content

Commit

Permalink
fix init order of local and hass mqtt client
Browse files Browse the repository at this point in the history
  • Loading branch information
steersbob committed Feb 15, 2024
1 parent 998d1b5 commit b1fd0bc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 3 additions & 1 deletion brewblox_hass/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 4 additions & 5 deletions brewblox_hass/relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""


import asyncio
import json
import logging
import re
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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

0 comments on commit b1fd0bc

Please sign in to comment.