M8-R15: fix HA discovery identities and thermal totals
This commit is contained in:
@@ -174,23 +174,33 @@ class MqttManager:
|
||||
*,
|
||||
retain: bool = False,
|
||||
qos: int = 0,
|
||||
) -> None:
|
||||
) -> bool:
|
||||
"""Publish a message to *topic*.
|
||||
|
||||
If the client is not connected the call is silently skipped.
|
||||
Errors are logged but do not raise.
|
||||
Return whether paho accepted the message for publication. If the
|
||||
client is not connected, or paho raises/rejects it, return ``False``;
|
||||
errors still do not escape this best-effort boundary.
|
||||
"""
|
||||
with self._lock:
|
||||
client = self._client
|
||||
|
||||
if client is None or not self._connected:
|
||||
logger.debug("MQTT publish skipped — not connected (topic=%s).", topic)
|
||||
return
|
||||
return False
|
||||
|
||||
try:
|
||||
client.publish(topic, payload=payload, qos=qos, retain=retain)
|
||||
result = client.publish(topic, payload=payload, qos=qos, retain=retain)
|
||||
# paho returns MQTTMessageInfo with an integer rc. Keep fakes and
|
||||
# alternate clients compatible by treating an absent/non-int rc as
|
||||
# accepted after the call itself succeeded.
|
||||
rc = getattr(result, "rc", None)
|
||||
if isinstance(rc, int) and rc != mqtt.MQTT_ERR_SUCCESS:
|
||||
logger.warning("MQTT publish rejected (topic=%s, rc=%s).", topic, rc)
|
||||
return False
|
||||
return True
|
||||
except Exception:
|
||||
logger.exception("MQTT publish error (topic=%s).", topic)
|
||||
return False
|
||||
|
||||
def subscribe(self, topic: str, handler: Callable[[bytes], None]) -> None:
|
||||
"""Register *handler* to be called when a message arrives on *topic*.
|
||||
|
||||
Reference in New Issue
Block a user