Skip to content

Commit 4bec32e

Browse files
committed
HA register all values from custom and scheduler
1 parent 83211e0 commit 4bec32e

4 files changed

+23
-6
lines changed

src/web/WebEntityService.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) {
6161
Command::erase_command(EMSdevice::DeviceType::CUSTOM, entityItem.name.c_str());
6262
}
6363
webEntity.entityItems.clear();
64+
EMSESP::webEntityService.ha_reset();
6465

6566
if (root["entities"].is<JsonArray>()) {
6667
for (const JsonObject ei : root["entities"].as<JsonArray>()) {
@@ -319,7 +320,8 @@ void WebEntityService::publish(const bool force) {
319320
}
320321

321322
DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE);
322-
JsonObject output = doc.to<JsonObject>();
323+
JsonObject output = doc.to<JsonObject>();
324+
bool ha_created = ha_registered_;
323325
for (const EntityItem & entityItem : *entityItems) {
324326
render_value(output, entityItem);
325327
// create HA config
@@ -383,11 +385,10 @@ void WebEntityService::publish(const bool force) {
383385

384386
// add "availability" section
385387
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
386-
if (Mqtt::queue_ha(topic, config.as<JsonObject>())) {
387-
ha_registered_ = true;
388-
}
388+
ha_created |= Mqtt::queue_ha(topic, config.as<JsonObject>());
389389
}
390390
}
391+
ha_registered_ = ha_created;
391392
if (output.size() > 0) {
392393
Mqtt::queue_publish("custom_data", output);
393394
}

src/web/WebEntityService.h

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class WebEntityService : public StatefulService<WebEntity> {
6363
uint8_t count_entities();
6464
uint8_t has_commands();
6565
void generate_value_web(JsonObject & output);
66+
void ha_reset() {
67+
ha_registered_ = false;
68+
}
6669

6770

6871
private:

src/web/WebSchedulerService.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ StateUpdateResult WebScheduler::update(JsonObject & root, WebScheduler & webSche
7171
Command::erase_command(EMSdevice::DeviceType::SCHEDULER, scheduleItem.name.c_str());
7272
}
7373
webScheduler.scheduleItems.clear();
74+
EMSESP::webSchedulerService.ha_reset();
7475

7576
if (root["schedule"].is<JsonArray>()) {
7677
for (const JsonObject schedule : root["schedule"].as<JsonArray>()) {
@@ -219,6 +220,12 @@ void WebSchedulerService::publish_single(const char * name, const bool state) {
219220

220221
// publish to Mqtt
221222
void WebSchedulerService::publish(const bool force) {
223+
if (force) {
224+
ha_registered_ = false;
225+
}
226+
if (!Mqtt::enabled()) {
227+
return;
228+
}
222229
EMSESP::webSchedulerService.read([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems; });
223230
if (scheduleItems->size() == 0) {
224231
return;
@@ -230,6 +237,7 @@ void WebSchedulerService::publish(const bool force) {
230237
}
231238

232239
DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE);
240+
bool ha_created = ha_registered_;
233241
for (const ScheduleItem & scheduleItem : *scheduleItems) {
234242
if (!scheduleItem.name.empty() && !doc.containsKey(scheduleItem.name)) {
235243
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
@@ -242,7 +250,7 @@ void WebSchedulerService::publish(const bool force) {
242250
}
243251

244252
// create HA config
245-
if (Mqtt::ha_enabled() && force) {
253+
if (Mqtt::ha_enabled() && !ha_registered_) {
246254
StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
247255
char stat_t[50];
248256
snprintf(stat_t, sizeof(stat_t), "%s/scheduler_data", Mqtt::basename().c_str());
@@ -284,10 +292,11 @@ void WebSchedulerService::publish(const bool force) {
284292

285293
// add "availability" section
286294
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
287-
Mqtt::queue_ha(topic, config.as<JsonObject>());
295+
ha_created |= Mqtt::queue_ha(topic, config.as<JsonObject>());
288296
}
289297
}
290298
}
299+
ha_registered_ = ha_created;
291300
if (doc.size() > 0) {
292301
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
293302
}

src/web/WebSchedulerService.h

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
5858
bool has_commands();
5959
bool command_setvalue(const char * value, const std::string name);
6060
bool get_value_info(JsonObject & output, const char * cmd);
61+
void ha_reset() {
62+
ha_registered_ = false;
63+
}
6164

6265
// make all functions public so we can test in the debug and standalone mode
6366
#ifndef EMSESP_STANDALONE
@@ -69,6 +72,7 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
6972
FSPersistence<WebScheduler> _fsPersistence;
7073

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

7478
} // namespace emsesp

0 commit comments

Comments
 (0)