remove force in HA MQTT

This commit is contained in:
proddy
2025-12-18 21:40:41 +01:00
parent 99afeb221a
commit 0eddbac150
4 changed files with 21 additions and 35 deletions

View File

@@ -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<JsonObject>();
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<JsonObject>(), EMSdevice::DeviceType::SYSTEM, entityItem.value_type, entityItem.uom);
Mqtt::add_ha_dev_section(config.as<JsonObject>(), "Custom Entities", nullptr, "EMS-ESP", EMSESP_APP_VERSION, !ha_created);
Mqtt::add_ha_avty_section(config.as<JsonObject>(), 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));

View File

@@ -58,7 +58,7 @@ class WebCustomEntityService : public StatefulService<WebCustomEntity> {
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<WebCustomEntity> {
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<WebCustomEntity> {
std::list<CustomEntityItem, AllocatorPSRAM<CustomEntityItem>> * customEntityItems_; // pointer to the list of entity items
bool ha_registered_ = false;
bool ha_configdone_ = false;
};
} // namespace emsesp

View File

@@ -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<JsonObject>();
bool ha_created = ha_registered_;
bool ha_created = ha_configdone_;
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
if (scheduleItem.name[0] != '\0' && !output[scheduleItem.name].is<JsonVariantConst>()) {
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];

View File

@@ -78,12 +78,12 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
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<WebScheduler> {
HttpEndpoint<WebScheduler> _httpEndpoint;
FSPersistence<WebScheduler> _fsPersistence;
bool ha_registered_ = false;
bool ha_configdone_ = false;
std::list<ScheduleItem, AllocatorPSRAM<ScheduleItem>> * scheduleItems_; // pointer to the list of schedule events
std::list<ScheduleItem *, AllocatorPSRAM<ScheduleItem *>> cmd_changed_; // pointer to commands in list that are triggered by change