mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
HA register all values from custom and scheduler
This commit is contained in:
@@ -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>()) {
|
||||
@@ -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<EMSESP_JSON_SIZE_MEDIUM> 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<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>());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user