Skip to content

Commit

Permalink
Merge pull request #168 from kbialek/feature/reconnect-fix
Browse files Browse the repository at this point in the history
Release publish_lock on error
  • Loading branch information
kbialek authored Apr 25, 2024
2 parents 1da6405 + b8e3c54 commit c228ec8
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/deye_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c228ec8

Please sign in to comment.