From b8e3c5453b0499e588495f4372066ae1cfb326bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Bia=C5=82ek?= Date: Sun, 21 Apr 2024 10:52:52 +0200 Subject: [PATCH] Release publish_lock on error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Krzysztof BiaƂek --- src/deye_mqtt.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/deye_mqtt.py b/src/deye_mqtt.py index 49d8693..697e729 100644 --- a/src/deye_mqtt.py +++ b/src/deye_mqtt.py @@ -60,7 +60,7 @@ def __init__(self, config: DeyeConfig): self.__mqtt_client.will_set(self.__status_topic, "offline", retain=True, qos=1) self.__config = config.mqtt self.__mqtt_timeout = 3 # seconds - self.__publish_lock = threading.Lock() + self.__publish_lock = threading.RLock() def subscribe(self, topic: str, callback): self.connect() @@ -100,13 +100,14 @@ def __do_publish(self, mqtt_topic: str, value: str): self.connect() info = self.__mqtt_client.publish(mqtt_topic, value, qos=1) info.wait_for_publish(self.__mqtt_timeout) - self.__publish_lock.release() except ValueError as e: raise DeyeMqttPublishError(f"MQTT outgoing queue is full: {str(e)}") except RuntimeError as e: raise DeyeMqttPublishError(f"Unknown MQTT publishing error: {str(e)}") except OSError as e: raise DeyeMqttPublishError(f"MQTT connection error: {str(e)}") + finally: + self.__publish_lock.release() def __build_topic_name(self, logger_topic_prefix: str, topic_suffix: str) -> str: if logger_topic_prefix: