diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index 9520499fc..2cc94fd22 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1767,6 +1767,11 @@ const char * EMSdevice::telegram_type_name(std::shared_ptr teleg bool EMSdevice::handle_telegram(std::shared_ptr telegram) { for (auto & tf : telegram_functions_) { if (tf.telegram_type_id_ == telegram->type_id) { + // for telegram desitnation only read telegram + if (telegram->dest == device_id_ && telegram->message_length > 0) { + tf.process_function_(telegram); + return true; + } // if the data block is empty and we have not received data before, assume that this telegram // is not recognized by the bus master. So remove it from the automatic fetch list if (telegram->message_length == 0 && telegram->offset == 0 && !tf.received_) { diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 314a94599..67a591d11 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -939,6 +939,8 @@ bool EMSESP::process_telegram(std::shared_ptr telegram) { emsdevice->add_handlers_ignored(telegram->type_id); } break; + } else if (emsdevice->is_device_id(telegram->dest)) { + emsdevice->handle_telegram(telegram); } } diff --git a/src/web/WebEntityService.cpp b/src/web/WebEntityService.cpp index 15708c8a1..1919c5eb3 100644 --- a/src/web/WebEntityService.cpp +++ b/src/web/WebEntityService.cpp @@ -61,6 +61,7 @@ StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) { Command::erase_command(EMSdevice::DeviceType::CUSTOM, entityItem.name.c_str()); } webEntity.entityItems.clear(); + EMSESP::webEntityService.ha_reset(); if (root["entities"].is()) { for (const JsonObject ei : root["entities"].as()) { @@ -319,7 +320,8 @@ void WebEntityService::publish(const bool force) { } DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE); - JsonObject output = doc.to(); + JsonObject output = doc.to(); + bool ha_created = ha_registered_; for (const EntityItem & entityItem : *entityItems) { render_value(output, entityItem); // create HA config @@ -383,11 +385,10 @@ void WebEntityService::publish(const bool force) { // add "availability" section Mqtt::add_avty_to_doc(stat_t, config.as(), val_cond); - if (Mqtt::queue_ha(topic, config.as())) { - ha_registered_ = true; - } + ha_created |= Mqtt::queue_ha(topic, config.as()); } } + ha_registered_ = ha_created; if (output.size() > 0) { Mqtt::queue_publish("custom_data", output); } diff --git a/src/web/WebEntityService.h b/src/web/WebEntityService.h index e58a7cb39..7df9c4243 100644 --- a/src/web/WebEntityService.h +++ b/src/web/WebEntityService.h @@ -63,6 +63,9 @@ class WebEntityService : public StatefulService { uint8_t count_entities(); uint8_t has_commands(); void generate_value_web(JsonObject & output); + void ha_reset() { + ha_registered_ = false; + } private: diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index 2402281b1..b7b166a26 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -71,6 +71,7 @@ StateUpdateResult WebScheduler::update(JsonObject & root, WebScheduler & webSche Command::erase_command(EMSdevice::DeviceType::SCHEDULER, scheduleItem.name.c_str()); } webScheduler.scheduleItems.clear(); + EMSESP::webSchedulerService.ha_reset(); if (root["schedule"].is()) { for (const JsonObject schedule : root["schedule"].as()) { @@ -219,6 +220,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()) { + return; + } EMSESP::webSchedulerService.read([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems; }); if (scheduleItems->size() == 0) { return; @@ -230,6 +237,7 @@ void WebSchedulerService::publish(const bool force) { } DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE); + bool ha_created = ha_registered_; for (const ScheduleItem & scheduleItem : *scheduleItems) { if (!scheduleItem.name.empty() && !doc.containsKey(scheduleItem.name)) { if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) { @@ -242,7 +250,7 @@ void WebSchedulerService::publish(const bool force) { } // create HA config - if (Mqtt::ha_enabled() && force) { + if (Mqtt::ha_enabled() && !ha_registered_) { StaticJsonDocument config; char stat_t[50]; snprintf(stat_t, sizeof(stat_t), "%s/scheduler_data", Mqtt::basename().c_str()); @@ -284,10 +292,11 @@ void WebSchedulerService::publish(const bool force) { // add "availability" section Mqtt::add_avty_to_doc(stat_t, config.as(), val_cond); - Mqtt::queue_ha(topic, config.as()); + ha_created |= Mqtt::queue_ha(topic, config.as()); } } } + ha_registered_ = ha_created; if (doc.size() > 0) { Mqtt::queue_publish("scheduler_data", doc.as()); } diff --git a/src/web/WebSchedulerService.h b/src/web/WebSchedulerService.h index 43501d66b..ef012fc5a 100644 --- a/src/web/WebSchedulerService.h +++ b/src/web/WebSchedulerService.h @@ -58,6 +58,9 @@ class WebSchedulerService : public StatefulService { bool has_commands(); bool command_setvalue(const char * value, const std::string name); bool get_value_info(JsonObject & output, const char * cmd); + void ha_reset() { + ha_registered_ = false; + } // make all functions public so we can test in the debug and standalone mode #ifndef EMSESP_STANDALONE @@ -69,6 +72,7 @@ class WebSchedulerService : public StatefulService { FSPersistence _fsPersistence; std::list * scheduleItems; // pointer to the list of schedule events + bool ha_registered_ = false; }; } // namespace emsesp