From dcd4e366e0f0d640f5b04c282c02cdae041c92b9 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Mon, 22 Jul 2024 16:06:51 +0200 Subject: [PATCH] Add retain on config, so that when broker restarts it can still publish --- poo.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/poo.py b/poo.py index 1af5dac..6a9b6b2 100644 --- a/poo.py +++ b/poo.py @@ -4,7 +4,7 @@ from fastapi_mqtt import FastMQTT, MQTTConfig class PooRecorder: - mqtt_config = MQTTConfig(username="mqtt", password="mqtt") # noqa: S106 + mqtt_config = MQTTConfig(username="mqtt", password="mqtt", reconnect_retries=-1) # noqa: S106 mqtt = FastMQTT(config=mqtt_config) CONFIG_TOPIC = "homeassistant/text/poo_recorder/config" AVAILABILITY_TOPIC = "studiotj/poo_recorder/status" @@ -35,18 +35,18 @@ class PooRecorder: def on_connect(client, flags, rc, properties) -> None: # noqa: ANN001, ARG004 print("Connected") config = PooRecorder.compose_config() - PooRecorder.mqtt.publish(PooRecorder.CONFIG_TOPIC, config) - PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE) + PooRecorder.mqtt.publish(PooRecorder.CONFIG_TOPIC, config, retain=True) + PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True) @staticmethod def publish_text(new_text: str) -> None: - PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE) + PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True) PooRecorder.mqtt.publish(PooRecorder.STATE_TOPIC, new_text, retain=True) @staticmethod def publish_time(time: datetime) -> None: formatted_time = time.strftime("%a | %Y-%m-%d | %H:%M") - PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE) + PooRecorder.mqtt.publish(PooRecorder.AVAILABILITY_TOPIC, PooRecorder.ONLINE, retain=True) json_string = {"last_poo": formatted_time} PooRecorder.mqtt.publish(PooRecorder.JSON_TOPIC, json_string, retain=True)