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:
@@ -61,6 +61,7 @@ StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) {
|
|||||||
Command::erase_command(EMSdevice::DeviceType::CUSTOM, entityItem.name.c_str());
|
Command::erase_command(EMSdevice::DeviceType::CUSTOM, entityItem.name.c_str());
|
||||||
}
|
}
|
||||||
webEntity.entityItems.clear();
|
webEntity.entityItems.clear();
|
||||||
|
EMSESP::webEntityService.ha_reset();
|
||||||
|
|
||||||
if (root["entities"].is<JsonArray>()) {
|
if (root["entities"].is<JsonArray>()) {
|
||||||
for (const JsonObject ei : root["entities"].as<JsonArray>()) {
|
for (const JsonObject ei : root["entities"].as<JsonArray>()) {
|
||||||
@@ -319,7 +320,8 @@ void WebEntityService::publish(const bool force) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE);
|
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) {
|
for (const EntityItem & entityItem : *entityItems) {
|
||||||
render_value(output, entityItem);
|
render_value(output, entityItem);
|
||||||
// create HA config
|
// create HA config
|
||||||
@@ -383,11 +385,10 @@ void WebEntityService::publish(const bool force) {
|
|||||||
|
|
||||||
// add "availability" section
|
// add "availability" section
|
||||||
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
|
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
|
||||||
if (Mqtt::queue_ha(topic, config.as<JsonObject>())) {
|
ha_created |= Mqtt::queue_ha(topic, config.as<JsonObject>());
|
||||||
ha_registered_ = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ha_registered_ = ha_created;
|
||||||
if (output.size() > 0) {
|
if (output.size() > 0) {
|
||||||
Mqtt::queue_publish("custom_data", output);
|
Mqtt::queue_publish("custom_data", output);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,9 @@ class WebEntityService : public StatefulService<WebEntity> {
|
|||||||
uint8_t count_entities();
|
uint8_t count_entities();
|
||||||
uint8_t has_commands();
|
uint8_t has_commands();
|
||||||
void generate_value_web(JsonObject & output);
|
void generate_value_web(JsonObject & output);
|
||||||
|
void ha_reset() {
|
||||||
|
ha_registered_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ StateUpdateResult WebScheduler::update(JsonObject & root, WebScheduler & webSche
|
|||||||
Command::erase_command(EMSdevice::DeviceType::SCHEDULER, scheduleItem.name.c_str());
|
Command::erase_command(EMSdevice::DeviceType::SCHEDULER, scheduleItem.name.c_str());
|
||||||
}
|
}
|
||||||
webScheduler.scheduleItems.clear();
|
webScheduler.scheduleItems.clear();
|
||||||
|
EMSESP::webSchedulerService.ha_reset();
|
||||||
|
|
||||||
if (root["schedule"].is<JsonArray>()) {
|
if (root["schedule"].is<JsonArray>()) {
|
||||||
for (const JsonObject schedule : root["schedule"].as<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
|
// publish to Mqtt
|
||||||
void WebSchedulerService::publish(const bool force) {
|
void WebSchedulerService::publish(const bool force) {
|
||||||
|
if (force) {
|
||||||
|
ha_registered_ = false;
|
||||||
|
}
|
||||||
|
if (!Mqtt::enabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
EMSESP::webSchedulerService.read([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems; });
|
EMSESP::webSchedulerService.read([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems; });
|
||||||
if (scheduleItems->size() == 0) {
|
if (scheduleItems->size() == 0) {
|
||||||
return;
|
return;
|
||||||
@@ -230,6 +237,7 @@ void WebSchedulerService::publish(const bool force) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE);
|
DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE);
|
||||||
|
bool ha_created = ha_registered_;
|
||||||
for (const ScheduleItem & scheduleItem : *scheduleItems) {
|
for (const ScheduleItem & scheduleItem : *scheduleItems) {
|
||||||
if (!scheduleItem.name.empty() && !doc.containsKey(scheduleItem.name)) {
|
if (!scheduleItem.name.empty() && !doc.containsKey(scheduleItem.name)) {
|
||||||
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||||
@@ -242,7 +250,7 @@ void WebSchedulerService::publish(const bool force) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create HA config
|
// create HA config
|
||||||
if (Mqtt::ha_enabled() && force) {
|
if (Mqtt::ha_enabled() && !ha_registered_) {
|
||||||
StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
|
StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> config;
|
||||||
char stat_t[50];
|
char stat_t[50];
|
||||||
snprintf(stat_t, sizeof(stat_t), "%s/scheduler_data", Mqtt::basename().c_str());
|
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
|
// add "availability" section
|
||||||
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
|
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) {
|
if (doc.size() > 0) {
|
||||||
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
|
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
|
|||||||
bool has_commands();
|
bool has_commands();
|
||||||
bool command_setvalue(const char * value, const std::string name);
|
bool command_setvalue(const char * value, const std::string name);
|
||||||
bool get_value_info(JsonObject & output, const char * cmd);
|
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
|
// make all functions public so we can test in the debug and standalone mode
|
||||||
#ifndef EMSESP_STANDALONE
|
#ifndef EMSESP_STANDALONE
|
||||||
@@ -69,6 +72,7 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
|
|||||||
FSPersistence<WebScheduler> _fsPersistence;
|
FSPersistence<WebScheduler> _fsPersistence;
|
||||||
|
|
||||||
std::list<ScheduleItem> * scheduleItems; // pointer to the list of schedule events
|
std::list<ScheduleItem> * scheduleItems; // pointer to the list of schedule events
|
||||||
|
bool ha_registered_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace emsesp
|
} // namespace emsesp
|
||||||
|
|||||||
Reference in New Issue
Block a user