From c363e0e2c8add2cb377ecdce5f7ccaecd4a7798e Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Mon, 17 Aug 2026 14:11:42 +0200 Subject: [PATCH] do not rebuild all HA configs on a mqtt reconnect, limit by heap --- src/core/analogsensor.cpp | 6 ++++++ src/core/analogsensor.h | 1 + src/core/emsdevice.cpp | 2 +- src/core/emsesp.cpp | 4 ++-- src/core/mqtt.cpp | 25 ++++++++++++++----------- src/core/temperaturesensor.cpp | 13 ++++++++----- src/core/temperaturesensor.h | 1 + 7 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/core/analogsensor.cpp b/src/core/analogsensor.cpp index 5a9e5ee06..4a03cd388 100644 --- a/src/core/analogsensor.cpp +++ b/src/core/analogsensor.cpp @@ -111,6 +111,12 @@ void AnalogSensor::start(const bool factory_settings) { Mqtt::subscribe(EMSdevice::DeviceType::ANALOGSENSOR, topic, nullptr); // use empty function callback } +void AnalogSensor::ha_reset() { + for (const auto & sensor : sensors_) { + remove_ha_topic(sensor.type(), sensor.gpio()); + } +} + // load settings from the customization file, sorts them and initializes the GPIOs void AnalogSensor::reload(bool get_nvs) { exclude_types_.clear(); diff --git a/src/core/analogsensor.h b/src/core/analogsensor.h index 94da0653b..e44e48b7d 100644 --- a/src/core/analogsensor.h +++ b/src/core/analogsensor.h @@ -144,6 +144,7 @@ class AnalogSensor { void publish_values(const bool force); void reload(bool get_nvs = false); bool updated_values(); + void ha_reset(); // return back reference to the sensor list, used by other classes const std::vector> & sensors() const { diff --git a/src/core/emsdevice.cpp b/src/core/emsdevice.cpp index 5fa829408..6e149dabc 100644 --- a/src/core/emsdevice.cpp +++ b/src/core/emsdevice.cpp @@ -2207,7 +2207,7 @@ void EMSdevice::mqtt_ha_entity_config_create() { #ifndef EMSESP_STANDALONE // always create minimum one config - if (count && (heap_caps_get_free_size(MALLOC_CAP_8BIT) < 65 * 1024)) { // checks free Heap+PSRAM + if (count && (heap_caps_get_free_size(MALLOC_CAP_INTERNAL) < 65 * 1024)) { // checks free Heap break; } #endif diff --git a/src/core/emsesp.cpp b/src/core/emsesp.cpp index a3efbcf5b..f43ce861d 100644 --- a/src/core/emsesp.cpp +++ b/src/core/emsesp.cpp @@ -657,8 +657,8 @@ void EMSESP::reset_mqtt_ha() { } // force the re-creating of the temperature and analog sensor topics (for HA) - temperaturesensor_.reload(); - analogsensor_.reload(); + temperaturesensor_.ha_reset(); + analogsensor_.ha_reset(); // rebuild MQTT HA config topics for shower, custom entities and scheduler shower_.ha_reset(); diff --git a/src/core/mqtt.cpp b/src/core/mqtt.cpp index 401018e51..9ca3405db 100644 --- a/src/core/mqtt.cpp +++ b/src/core/mqtt.cpp @@ -352,6 +352,7 @@ void Mqtt::reset_mqtt() { mqttClient_->disconnect(true); // force a disconnect } load_settings(); // reload MQTT settings + connectcount_ = 0; } // load the settings from service @@ -512,17 +513,19 @@ void Mqtt::on_connect() { connecting_ = true; queuecount_ = mqttClient_->queueSize(); - if (ha_enabled_) { - queue_unsubscribe_message(discovery_prefix_ + "/+/" + Mqtt::basename() + "/#"); - EMSESP::reset_mqtt_ha(); // re-create all HA devices if there are any - ha_status(); // create the EMS-ESP device in HA, which is MQTT retained - } else { - // with disabled HA we subscribe and the broker sends all stored HA-emsesp-configs. - // Around line 272 they are removed (search for "// remove HA topics if we don't use discover") - // If HA is enabled the subscriptions are removed. - // As described in the doc (https://emsesp.org/Troubleshooting?id=home-assistant): - // disable HA, wait 5 minutes (to allow the broker to send all), than reenable HA again. - queue_subscribe_message(discovery_prefix_ + "/+/" + Mqtt::basename() + "/#"); + if (connectcount_ == 0) { // only on first connect and after reconfigure, HA messages are reain + if (ha_enabled_) { + queue_unsubscribe_message(discovery_prefix_ + "/+/" + Mqtt::basename() + "/#"); + EMSESP::reset_mqtt_ha(); // re-create all HA devices if there are any + ha_status(); // create the EMS-ESP device in HA, which is MQTT retained + } else { + // with disabled HA we subscribe and the broker sends all stored HA-emsesp-configs. + // Around line 272 they are removed (search for "// remove HA topics if we don't use discover") + // If HA is enabled the subscriptions are removed. + // As described in the doc (https://emsesp.org/Troubleshooting?id=home-assistant): + // disable HA, wait 5 minutes (to allow the broker to send all), than reenable HA again. + queue_subscribe_message(discovery_prefix_ + "/+/" + Mqtt::basename() + "/#"); + } } // re-subscribe to all custom registered MQTT topics diff --git a/src/core/temperaturesensor.cpp b/src/core/temperaturesensor.cpp index 868567318..b92ecda58 100644 --- a/src/core/temperaturesensor.cpp +++ b/src/core/temperaturesensor.cpp @@ -45,17 +45,20 @@ void TemperatureSensor::start(const bool factory_settings) { Mqtt::subscribe(EMSdevice::DeviceType::TEMPERATURESENSOR, topic, nullptr); // use empty function callback } +void TemperatureSensor::ha_reset() { + for (auto & sensor : sensors_) { + remove_ha_topic(sensor.id()); + sensor.ha_registered = false; // force HA configs to be re-created + } +} + // load settings void TemperatureSensor::reload() { EMSESP::webSettingsService.read([&](WebSettings const & settings) { dallas_gpio_ = settings.dallas_gpio; parasite_ = settings.dallas_parasite; }); - - for (auto & sensor : sensors_) { - remove_ha_topic(sensor.id()); - sensor.ha_registered = false; // force HA configs to be re-created - } + ha_reset(); } void TemperatureSensor::loop() { diff --git a/src/core/temperaturesensor.h b/src/core/temperaturesensor.h index ee141d353..1dd60f0d2 100644 --- a/src/core/temperaturesensor.h +++ b/src/core/temperaturesensor.h @@ -93,6 +93,7 @@ class TemperatureSensor { void publish_sensor(const Sensor & sensor); void publish_values(const bool force); void reload(); + void ha_reset(); bool updated_values(); bool get_value_info(JsonObject output, const char * cmd, const int8_t id = -1);