From fe5552a0b16c4433b72cc4ea3952b02ee14c49ce Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 30 Oct 2023 17:09:26 +0100 Subject: [PATCH] read initial state, after restart, from MQTT simply by using retain, the client will get the latest state upon subscribe --- include/ha.h | 6 ++++++ include/mqtt.h | 3 --- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/ha.h b/include/ha.h index 0afc07f..7667155 100644 --- a/include/ha.h +++ b/include/ha.h @@ -91,6 +91,11 @@ namespace Ha { sprintf(commandTopic, "homeassistant/%s/esp_clock/%s/set", type, id); } + void buildConfig(JsonDocument& jsonDoc) override { + Command::buildConfig(jsonDoc); + jsonDoc["retain"] = true; + } + }; struct Brightness : Command { @@ -102,6 +107,7 @@ namespace Ha { void buildConfig(JsonDocument& jsonDoc) override { Command::buildConfig(jsonDoc); + jsonDoc["retain"] = true; jsonDoc["min"] = BRIGHTNESS_MIN; jsonDoc["max"] = BRIGHTNESS_MAX; jsonDoc["step"] = BRIGHTNESS_STEP; diff --git a/include/mqtt.h b/include/mqtt.h index 85739d0..250cae9 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -116,9 +116,6 @@ namespace Mqtt { for (Ha::Component* cmp : switches) { publishComponentConfig(*cmp); } - brightnessMqtt->publishState(); - hourFormatMqtt->publishState(); - ledMqtt->publishState(); ts.deleteTask(tPublishInit); }