@@ -71,6 +71,7 @@ StateUpdateResult WebScheduler::update(JsonObject & root, WebScheduler & webSche
71
71
Command::erase_command (EMSdevice::DeviceType::SCHEDULER, scheduleItem.name .c_str ());
72
72
}
73
73
webScheduler.scheduleItems .clear ();
74
+ EMSESP::webSchedulerService.ha_reset ();
74
75
75
76
if (root[" schedule" ].is <JsonArray>()) {
76
77
for (const JsonObject schedule : root[" schedule" ].as <JsonArray>()) {
@@ -219,6 +220,12 @@ void WebSchedulerService::publish_single(const char * name, const bool state) {
219
220
220
221
// publish to Mqtt
221
222
void WebSchedulerService::publish (const bool force) {
223
+ if (force) {
224
+ ha_registered_ = false ;
225
+ }
226
+ if (!Mqtt::enabled ()) {
227
+ return ;
228
+ }
222
229
EMSESP::webSchedulerService.read ([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems ; });
223
230
if (scheduleItems->size () == 0 ) {
224
231
return ;
@@ -230,6 +237,7 @@ void WebSchedulerService::publish(const bool force) {
230
237
}
231
238
232
239
DynamicJsonDocument doc (EMSESP_JSON_SIZE_XLARGE);
240
+ bool ha_created = ha_registered_;
233
241
for (const ScheduleItem & scheduleItem : *scheduleItems) {
234
242
if (!scheduleItem.name .empty () && !doc.containsKey (scheduleItem.name )) {
235
243
if (EMSESP::system_.bool_format () == BOOL_FORMAT_TRUEFALSE) {
@@ -242,7 +250,7 @@ void WebSchedulerService::publish(const bool force) {
242
250
}
243
251
244
252
// create HA config
245
- if (Mqtt::ha_enabled () && force ) {
253
+ if (Mqtt::ha_enabled () && !ha_registered_ ) {
246
254
StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
247
255
char stat_t [50 ];
248
256
snprintf (stat_t , sizeof (stat_t ), " %s/scheduler_data" , Mqtt::basename ().c_str ());
@@ -284,10 +292,11 @@ void WebSchedulerService::publish(const bool force) {
284
292
285
293
// add "availability" section
286
294
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>());
288
296
}
289
297
}
290
298
}
299
+ ha_registered_ = ha_created;
291
300
if (doc.size () > 0 ) {
292
301
Mqtt::queue_publish (" scheduler_data" , doc.as <JsonObject>());
293
302
}
0 commit comments