do not rebuild all HA configs on a mqtt reconnect, limit by heap

This commit is contained in:
MichaelDvP
2026-08-17 14:11:42 +02:00
parent d397a14b41
commit c363e0e2c8
7 changed files with 33 additions and 19 deletions
+6
View File
@@ -111,6 +111,12 @@ void AnalogSensor::start(const bool factory_settings) {
Mqtt::subscribe(EMSdevice::DeviceType::ANALOGSENSOR, topic, nullptr); // use empty function callback 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 // load settings from the customization file, sorts them and initializes the GPIOs
void AnalogSensor::reload(bool get_nvs) { void AnalogSensor::reload(bool get_nvs) {
exclude_types_.clear(); exclude_types_.clear();
+1
View File
@@ -144,6 +144,7 @@ class AnalogSensor {
void publish_values(const bool force); void publish_values(const bool force);
void reload(bool get_nvs = false); void reload(bool get_nvs = false);
bool updated_values(); bool updated_values();
void ha_reset();
// return back reference to the sensor list, used by other classes // return back reference to the sensor list, used by other classes
const std::vector<Sensor, AllocatorPSRAM<Sensor>> & sensors() const { const std::vector<Sensor, AllocatorPSRAM<Sensor>> & sensors() const {
+1 -1
View File
@@ -2207,7 +2207,7 @@ void EMSdevice::mqtt_ha_entity_config_create() {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
// always create minimum one config // 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; break;
} }
#endif #endif
+2 -2
View File
@@ -657,8 +657,8 @@ void EMSESP::reset_mqtt_ha() {
} }
// force the re-creating of the temperature and analog sensor topics (for HA) // force the re-creating of the temperature and analog sensor topics (for HA)
temperaturesensor_.reload(); temperaturesensor_.ha_reset();
analogsensor_.reload(); analogsensor_.ha_reset();
// rebuild MQTT HA config topics for shower, custom entities and scheduler // rebuild MQTT HA config topics for shower, custom entities and scheduler
shower_.ha_reset(); shower_.ha_reset();
+14 -11
View File
@@ -352,6 +352,7 @@ void Mqtt::reset_mqtt() {
mqttClient_->disconnect(true); // force a disconnect mqttClient_->disconnect(true); // force a disconnect
} }
load_settings(); // reload MQTT settings load_settings(); // reload MQTT settings
connectcount_ = 0;
} }
// load the settings from service // load the settings from service
@@ -512,17 +513,19 @@ void Mqtt::on_connect() {
connecting_ = true; connecting_ = true;
queuecount_ = mqttClient_->queueSize(); queuecount_ = mqttClient_->queueSize();
if (ha_enabled_) { if (connectcount_ == 0) { // only on first connect and after reconfigure, HA messages are reain
queue_unsubscribe_message(discovery_prefix_ + "/+/" + Mqtt::basename() + "/#"); if (ha_enabled_) {
EMSESP::reset_mqtt_ha(); // re-create all HA devices if there are any queue_unsubscribe_message(discovery_prefix_ + "/+/" + Mqtt::basename() + "/#");
ha_status(); // create the EMS-ESP device in HA, which is MQTT retained EMSESP::reset_mqtt_ha(); // re-create all HA devices if there are any
} else { ha_status(); // create the EMS-ESP device in HA, which is MQTT retained
// with disabled HA we subscribe and the broker sends all stored HA-emsesp-configs. } else {
// Around line 272 they are removed (search for "// remove HA topics if we don't use discover") // with disabled HA we subscribe and the broker sends all stored HA-emsesp-configs.
// If HA is enabled the subscriptions are removed. // Around line 272 they are removed (search for "// remove HA topics if we don't use discover")
// As described in the doc (https://emsesp.org/Troubleshooting?id=home-assistant): // If HA is enabled the subscriptions are removed.
// disable HA, wait 5 minutes (to allow the broker to send all), than reenable HA again. // As described in the doc (https://emsesp.org/Troubleshooting?id=home-assistant):
queue_subscribe_message(discovery_prefix_ + "/+/" + Mqtt::basename() + "/#"); // 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 // re-subscribe to all custom registered MQTT topics
+8 -5
View File
@@ -45,17 +45,20 @@ void TemperatureSensor::start(const bool factory_settings) {
Mqtt::subscribe(EMSdevice::DeviceType::TEMPERATURESENSOR, topic, nullptr); // use empty function callback 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 // load settings
void TemperatureSensor::reload() { void TemperatureSensor::reload() {
EMSESP::webSettingsService.read([&](WebSettings const & settings) { EMSESP::webSettingsService.read([&](WebSettings const & settings) {
dallas_gpio_ = settings.dallas_gpio; dallas_gpio_ = settings.dallas_gpio;
parasite_ = settings.dallas_parasite; parasite_ = settings.dallas_parasite;
}); });
ha_reset();
for (auto & sensor : sensors_) {
remove_ha_topic(sensor.id());
sensor.ha_registered = false; // force HA configs to be re-created
}
} }
void TemperatureSensor::loop() { void TemperatureSensor::loop() {
+1
View File
@@ -93,6 +93,7 @@ class TemperatureSensor {
void publish_sensor(const Sensor & sensor); void publish_sensor(const Sensor & sensor);
void publish_values(const bool force); void publish_values(const bool force);
void reload(); void reload();
void ha_reset();
bool updated_values(); bool updated_values();
bool get_value_info(JsonObject output, const char * cmd, const int8_t id = -1); bool get_value_info(JsonObject output, const char * cmd, const int8_t id = -1);