Skip to content

Commit

Permalink
HA register all values from custom and scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Sep 9, 2023
1 parent 83211e0 commit 4bec32e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/web/WebEntityService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonArray>()) {
for (const JsonObject ei : root["entities"].as<JsonArray>()) {
Expand Down Expand Up @@ -319,7 +320,8 @@ void WebEntityService::publish(const bool force) {
}

DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE);
JsonObject output = doc.to<JsonObject>();
JsonObject output = doc.to<JsonObject>();
bool ha_created = ha_registered_;
for (const EntityItem & entityItem : *entityItems) {
render_value(output, entityItem);
// create HA config
Expand Down Expand Up @@ -383,11 +385,10 @@ void WebEntityService::publish(const bool force) {

// add "availability" section
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
if (Mqtt::queue_ha(topic, config.as<JsonObject>())) {
ha_registered_ = true;
}
ha_created |= Mqtt::queue_ha(topic, config.as<JsonObject>());
}
}
ha_registered_ = ha_created;
if (output.size() > 0) {
Mqtt::queue_publish("custom_data", output);
}
Expand Down
3 changes: 3 additions & 0 deletions src/web/WebEntityService.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class WebEntityService : public StatefulService<WebEntity> {
uint8_t count_entities();
uint8_t has_commands();
void generate_value_web(JsonObject & output);
void ha_reset() {
ha_registered_ = false;
}


private:
Expand Down
13 changes: 11 additions & 2 deletions src/web/WebSchedulerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonArray>()) {
for (const JsonObject schedule : root["schedule"].as<JsonArray>()) {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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<EMSESP_JSON_SIZE_MEDIUM> config;
char stat_t[50];
snprintf(stat_t, sizeof(stat_t), "%s/scheduler_data", Mqtt::basename().c_str());
Expand Down Expand Up @@ -284,10 +292,11 @@ void WebSchedulerService::publish(const bool force) {

// add "availability" section
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
Mqtt::queue_ha(topic, config.as<JsonObject>());
ha_created |= Mqtt::queue_ha(topic, config.as<JsonObject>());
}
}
}
ha_registered_ = ha_created;
if (doc.size() > 0) {
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
}
Expand Down
4 changes: 4 additions & 0 deletions src/web/WebSchedulerService.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
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
Expand All @@ -69,6 +72,7 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
FSPersistence<WebScheduler> _fsPersistence;

std::list<ScheduleItem> * scheduleItems; // pointer to the list of schedule events
bool ha_registered_ = false;
};

} // namespace emsesp
Expand Down

0 comments on commit 4bec32e

Please sign in to comment.