From 0eddbac150e426c8497a6c22543fd2117c87723c Mon Sep 17 00:00:00 2001 From: proddy Date: Thu, 18 Dec 2025 21:40:41 +0100 Subject: [PATCH] remove force in HA MQTT --- src/web/WebCustomEntityService.cpp | 21 +++++++-------------- src/web/WebCustomEntityService.h | 6 +++--- src/web/WebSchedulerService.cpp | 23 ++++++++--------------- src/web/WebSchedulerService.h | 6 +++--- 4 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/web/WebCustomEntityService.cpp b/src/web/WebCustomEntityService.cpp index d04d40fff..9f6fce797 100644 --- a/src/web/WebCustomEntityService.cpp +++ b/src/web/WebCustomEntityService.cpp @@ -378,19 +378,12 @@ void WebCustomEntityService::publish_single(CustomEntityItem & entity) { } // publish to Mqtt -void WebCustomEntityService::publish(const bool force) { - if (force) { - ha_registered_ = false; - } - - if (!Mqtt::enabled()) { +void WebCustomEntityService::publish() { + if (!Mqtt::enabled() || customEntityItems_->empty()) { return; } - if (customEntityItems_->empty()) { - return; - } - if (Mqtt::publish_single() && force) { + if (Mqtt::publish_single()) { for (CustomEntityItem & entityItem : *customEntityItems_) { publish_single(entityItem); } @@ -398,7 +391,7 @@ void WebCustomEntityService::publish(const bool force) { JsonDocument doc; JsonObject output = doc.to(); - bool ha_created = ha_registered_; + bool ha_created = ha_configdone_; for (CustomEntityItem & entityItem : *customEntityItems_) { if (entityItem.hide) { @@ -406,7 +399,7 @@ void WebCustomEntityService::publish(const bool force) { } render_value(output, entityItem); // create HA config - if (Mqtt::ha_enabled() && !ha_registered_) { + if (!ha_configdone_) { JsonDocument config; config["~"] = Mqtt::base(); @@ -464,7 +457,6 @@ void WebCustomEntityService::publish(const bool force) { config["def_ent_id"] = topic_str.substr(0, topic_str.find("/")) + "." + uniq_s; Mqtt::add_ha_classes(config.as(), EMSdevice::DeviceType::SYSTEM, entityItem.value_type, entityItem.uom); - Mqtt::add_ha_dev_section(config.as(), "Custom Entities", nullptr, "EMS-ESP", EMSESP_APP_VERSION, !ha_created); Mqtt::add_ha_avty_section(config.as(), stat_t, val_cond); @@ -472,7 +464,8 @@ void WebCustomEntityService::publish(const bool force) { } } - ha_registered_ = ha_created; + ha_configdone_ = ha_created; + if (output.size() > 0) { char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; snprintf(topic, sizeof(topic), "%s_data", F_(custom)); diff --git a/src/web/WebCustomEntityService.h b/src/web/WebCustomEntityService.h index d6149d31e..517d19c88 100644 --- a/src/web/WebCustomEntityService.h +++ b/src/web/WebCustomEntityService.h @@ -58,7 +58,7 @@ class WebCustomEntityService : public StatefulService { void begin(); void publish_single(CustomEntityItem & entity); - void publish(const bool force = false); + void publish(); bool command_setvalue(const char * value, const int8_t id, const char * name); bool get_value_info(JsonObject output, const char * cmd); void get_value_json(JsonObject output, CustomEntityItem const & entity); @@ -70,7 +70,7 @@ class WebCustomEntityService : public StatefulService { uint8_t count_entities(); void ha_reset() { - ha_registered_ = false; + ha_configdone_ = false; } #if defined(EMSESP_TEST) @@ -85,7 +85,7 @@ class WebCustomEntityService : public StatefulService { std::list> * customEntityItems_; // pointer to the list of entity items - bool ha_registered_ = false; + bool ha_configdone_ = false; }; } // namespace emsesp diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index b72b60556..fcc313bbe 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -106,7 +106,8 @@ StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webSchedu } } - EMSESP::webSchedulerService.publish(true); + EMSESP::webSchedulerService.ha_reset(); + EMSESP::webSchedulerService.publish(); return StateUpdateResult::CHANGED; } @@ -215,20 +216,12 @@ void WebSchedulerService::publish_single(const char * name, const bool state) { } // publish to Mqtt -void WebSchedulerService::publish(const bool force) { - if (force) { - ha_registered_ = false; - } - - if (!Mqtt::enabled()) { +void WebSchedulerService::publish() { + if (!Mqtt::enabled() || scheduleItems_->empty()) { return; } - if (scheduleItems_->empty()) { - return; - } - - if (Mqtt::publish_single() && force) { + if (Mqtt::publish_single()) { for (const ScheduleItem & scheduleItem : *scheduleItems_) { publish_single(scheduleItem.name, scheduleItem.active); } @@ -236,13 +229,13 @@ void WebSchedulerService::publish(const bool force) { JsonDocument doc; JsonObject output = doc.to(); - bool ha_created = ha_registered_; + bool ha_created = ha_configdone_; for (const ScheduleItem & scheduleItem : *scheduleItems_) { if (scheduleItem.name[0] != '\0' && !output[scheduleItem.name].is()) { Mqtt::add_value_bool(output, (const char *)scheduleItem.name, scheduleItem.active); // create HA config - if (Mqtt::ha_enabled() && !ha_registered_) { + if (!ha_configdone_) { JsonDocument config; config["~"] = Mqtt::base(); @@ -284,7 +277,7 @@ void WebSchedulerService::publish(const bool force) { } } - ha_registered_ = ha_created; + ha_configdone_ = ha_created; if (!doc.isNull()) { char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; diff --git a/src/web/WebSchedulerService.h b/src/web/WebSchedulerService.h index 4a485e35d..25b561bcc 100644 --- a/src/web/WebSchedulerService.h +++ b/src/web/WebSchedulerService.h @@ -78,12 +78,12 @@ class WebSchedulerService : public StatefulService { void begin(); void loop(); void publish_single(const char * name, const bool state); - void publish(const bool force = false); + void publish(); bool command_setvalue(const char * value, const int8_t id, const char * name); bool get_value_info(JsonObject output, const char * cmd); void get_value_json(JsonObject output, const ScheduleItem & scheduleItem); void ha_reset() { - ha_registered_ = false; + ha_configdone_ = false; } uint8_t count_entities(bool cmd_only = false); bool onChange(const char * cmd); @@ -106,7 +106,7 @@ class WebSchedulerService : public StatefulService { HttpEndpoint _httpEndpoint; FSPersistence _fsPersistence; - bool ha_registered_ = false; + bool ha_configdone_ = false; std::list> * scheduleItems_; // pointer to the list of schedule events std::list> cmd_changed_; // pointer to commands in list that are triggered by change