Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release publish_lock on error #168

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading