fix: mqtt async calls
- Removed await from publish() and subscribe() calls - Updated publish_state to handle state conversion and qos - Kept await for connect() and disconnect()
This commit is contained in:
parent
925f612ede
commit
a065964bd1
|
|
@ -103,7 +103,7 @@ class HomeAssistantMQTT:
|
|||
self._connected = True
|
||||
try:
|
||||
# Subscribe to command topic
|
||||
await self.client.subscribe(self.command_topic, qos=1)
|
||||
self.client.subscribe(self.command_topic, qos=1)
|
||||
logger.info(f"Subscribed to command topic: {self.command_topic}")
|
||||
|
||||
# Publish discovery config
|
||||
|
|
@ -120,7 +120,7 @@ class HomeAssistantMQTT:
|
|||
"state_closed": "closed"
|
||||
}
|
||||
|
||||
await self.client.publish(
|
||||
self.client.publish(
|
||||
self.config_topic,
|
||||
json.dumps(config),
|
||||
qos=1,
|
||||
|
|
@ -129,7 +129,7 @@ class HomeAssistantMQTT:
|
|||
logger.info("Published Home Assistant discovery config")
|
||||
|
||||
# Publish initial availability and state
|
||||
await self.client.publish(
|
||||
self.client.publish(
|
||||
self.availability_topic,
|
||||
"online",
|
||||
qos=1,
|
||||
|
|
@ -184,7 +184,7 @@ class HomeAssistantMQTT:
|
|||
"""Publish a message to a topic"""
|
||||
if self.client and self._connected:
|
||||
try:
|
||||
await self.client.publish(topic, payload, retain=retain)
|
||||
self.client.publish(topic, payload, retain=retain)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to publish message: {e}")
|
||||
|
||||
|
|
@ -192,15 +192,35 @@ class HomeAssistantMQTT:
|
|||
"""Subscribe to a topic"""
|
||||
if self.client and self._connected:
|
||||
try:
|
||||
await self.client.subscribe(topic, qos=1)
|
||||
self.client.subscribe(topic, qos=1)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to subscribe to topic: {e}")
|
||||
|
||||
async def publish_state(self, state: Union[bool, str]):
|
||||
"""Publish gate state"""
|
||||
"""Publish state to MQTT"""
|
||||
if not self._connected:
|
||||
logger.warning("Cannot publish state - not connected to MQTT broker")
|
||||
return
|
||||
|
||||
try:
|
||||
if isinstance(state, bool):
|
||||
state = "open" if state else "closed"
|
||||
await self.publish(self.state_topic, state, retain=True)
|
||||
state_str = "open" if state else "closed"
|
||||
else:
|
||||
state_str = state.lower()
|
||||
|
||||
self.client.publish(
|
||||
self.state_topic,
|
||||
state_str,
|
||||
qos=1,
|
||||
retain=True
|
||||
)
|
||||
logger.debug(f"Published state: {state_str}")
|
||||
|
||||
if self._event_callback:
|
||||
self._event_callback(f"Published state: {state_str}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to publish state: {e}")
|
||||
|
||||
def set_command_callback(self, callback: Callable):
|
||||
"""Set callback for handling commands"""
|
||||
|
|
|
|||
Loading…
Reference in New Issue