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
|
self._connected = True
|
||||||
try:
|
try:
|
||||||
# Subscribe to command topic
|
# 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}")
|
logger.info(f"Subscribed to command topic: {self.command_topic}")
|
||||||
|
|
||||||
# Publish discovery config
|
# Publish discovery config
|
||||||
|
|
@ -120,7 +120,7 @@ class HomeAssistantMQTT:
|
||||||
"state_closed": "closed"
|
"state_closed": "closed"
|
||||||
}
|
}
|
||||||
|
|
||||||
await self.client.publish(
|
self.client.publish(
|
||||||
self.config_topic,
|
self.config_topic,
|
||||||
json.dumps(config),
|
json.dumps(config),
|
||||||
qos=1,
|
qos=1,
|
||||||
|
|
@ -129,7 +129,7 @@ class HomeAssistantMQTT:
|
||||||
logger.info("Published Home Assistant discovery config")
|
logger.info("Published Home Assistant discovery config")
|
||||||
|
|
||||||
# Publish initial availability and state
|
# Publish initial availability and state
|
||||||
await self.client.publish(
|
self.client.publish(
|
||||||
self.availability_topic,
|
self.availability_topic,
|
||||||
"online",
|
"online",
|
||||||
qos=1,
|
qos=1,
|
||||||
|
|
@ -184,7 +184,7 @@ class HomeAssistantMQTT:
|
||||||
"""Publish a message to a topic"""
|
"""Publish a message to a topic"""
|
||||||
if self.client and self._connected:
|
if self.client and self._connected:
|
||||||
try:
|
try:
|
||||||
await self.client.publish(topic, payload, retain=retain)
|
self.client.publish(topic, payload, retain=retain)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to publish message: {e}")
|
logger.error(f"Failed to publish message: {e}")
|
||||||
|
|
||||||
|
|
@ -192,15 +192,35 @@ class HomeAssistantMQTT:
|
||||||
"""Subscribe to a topic"""
|
"""Subscribe to a topic"""
|
||||||
if self.client and self._connected:
|
if self.client and self._connected:
|
||||||
try:
|
try:
|
||||||
await self.client.subscribe(topic, qos=1)
|
self.client.subscribe(topic, qos=1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to subscribe to topic: {e}")
|
logger.error(f"Failed to subscribe to topic: {e}")
|
||||||
|
|
||||||
async def publish_state(self, state: Union[bool, str]):
|
async def publish_state(self, state: Union[bool, str]):
|
||||||
"""Publish gate state"""
|
"""Publish state to MQTT"""
|
||||||
if isinstance(state, bool):
|
if not self._connected:
|
||||||
state = "open" if state else "closed"
|
logger.warning("Cannot publish state - not connected to MQTT broker")
|
||||||
await self.publish(self.state_topic, state, retain=True)
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
if isinstance(state, bool):
|
||||||
|
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):
|
def set_command_callback(self, callback: Callable):
|
||||||
"""Set callback for handling commands"""
|
"""Set callback for handling commands"""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue