publish mqtt emsesp on-change messages on connect

This commit is contained in:
MichaelDvP
2025-12-19 17:14:50 +01:00
parent b4affbff6d
commit 1b8b72c443
6 changed files with 42 additions and 21 deletions

View File

@@ -664,13 +664,18 @@ void AnalogSensor::remove_ha_topic(const int8_t type, const uint8_t gpio) const
void AnalogSensor::publish_values(const bool force) { void AnalogSensor::publish_values(const bool force) {
uint8_t num_sensors = sensors_.size(); uint8_t num_sensors = sensors_.size();
if (num_sensors == 0) { if (!Mqtt::connected() || num_sensors == 0) {
return; return;
} }
if (force && Mqtt::publish_single()) { if (force) {
for (const auto & sensor : sensors_) { if (Mqtt::publish_single()) {
publish_sensor(sensor); for (const auto & sensor : sensors_) {
publish_sensor(sensor);
}
return;
} else if (!EMSESP::mqtt_.get_publish_onchange(0)) {
return; // wait for first time periode
} }
} }

View File

@@ -511,6 +511,11 @@ void Mqtt::on_connect() {
// send initial MQTT messages for some of our services // send initial MQTT messages for some of our services
EMSESP::system_.send_heartbeat(); // send heartbeat EMSESP::system_.send_heartbeat(); // send heartbeat
// for publish on change publish the initial complete list
EMSESP::webCustomEntityService.publish(true);
EMSESP::webSchedulerService.publish(true);
EMSESP::analogsensor_.publish_values(true);
EMSESP::temperaturesensor_.publish_values(true);
} }
// Home Assistant Discovery - the main master Device called EMS-ESP // Home Assistant Discovery - the main master Device called EMS-ESP

View File

@@ -473,10 +473,15 @@ void TemperatureSensor::publish_values(const bool force) {
return; return;
} }
if (force && Mqtt::publish_single()) { if (force) {
if (Mqtt::publish_single()) {
for (const auto & sensor : sensors_) { for (const auto & sensor : sensors_) {
publish_sensor(sensor); publish_sensor(sensor);
} }
return;
} else if (!EMSESP::mqtt_.get_publish_onchange(0)) {
return; // wait for first time periode
}
} }
JsonDocument doc; JsonDocument doc;

View File

@@ -378,10 +378,20 @@ void WebCustomEntityService::publish_single(CustomEntityItem & entity) {
} }
// publish to Mqtt // publish to Mqtt
void WebCustomEntityService::publish() { void WebCustomEntityService::publish(const bool force) {
if (!Mqtt::enabled() || customEntityItems_->empty()) { if (!Mqtt::connected() || customEntityItems_->empty()) {
return; return;
} }
if (force) {
if (Mqtt::publish_single()) {
for (CustomEntityItem & entityItem : *customEntityItems_) {
publish_single(entityItem);
}
return;
} else if (!EMSESP::mqtt_.get_publish_onchange(0)) {
return; // wait for first time periode
}
}
JsonDocument doc; JsonDocument doc;
JsonObject output = doc.to<JsonObject>(); JsonObject output = doc.to<JsonObject>();

View File

@@ -58,7 +58,7 @@ class WebCustomEntityService : public StatefulService<WebCustomEntity> {
void begin(); void begin();
void publish_single(CustomEntityItem & entity); void publish_single(CustomEntityItem & entity);
void publish(); void publish(const bool force = false);
bool command_setvalue(const char * value, const int8_t id, const char * name); bool command_setvalue(const char * value, const int8_t id, const char * name);
bool get_value_info(JsonObject output, const char * cmd); bool get_value_info(JsonObject output, const char * cmd);
void get_value_json(JsonObject output, CustomEntityItem const & entity); void get_value_json(JsonObject output, CustomEntityItem const & entity);

View File

@@ -105,10 +105,6 @@ StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webSchedu
CommandFlag::ADMIN_ONLY); CommandFlag::ADMIN_ONLY);
} }
} }
EMSESP::webSchedulerService.ha_reset();
EMSESP::webSchedulerService.publish();
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
} }
@@ -217,17 +213,17 @@ void WebSchedulerService::publish_single(const char * name, const bool state) {
// publish to Mqtt // publish to Mqtt
void WebSchedulerService::publish(const bool force) { void WebSchedulerService::publish(const bool force) {
if (force) { if (!Mqtt::connected() || scheduleItems_->empty()) {
ha_configdone_ = false;
}
if (!Mqtt::enabled() || scheduleItems_->empty()) {
return; return;
} }
if (force) {
if (Mqtt::publish_single() && force) { if (Mqtt::publish_single()) {
for (const ScheduleItem & scheduleItem : *scheduleItems_) { for (const ScheduleItem & scheduleItem : *scheduleItems_) {
publish_single(scheduleItem.name, scheduleItem.active); publish_single(scheduleItem.name, scheduleItem.active);
}
return;
} else if (!EMSESP::mqtt_.get_publish_onchange(0)) {
return; // wait for first time periode
} }
} }