mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-26 08:39:09 +03:00
fix HA sensor referencing
This commit is contained in:
@@ -675,7 +675,8 @@ void AnalogSensor::publish_values(const bool force) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonObject obj = doc.to<JsonObject>();
|
JsonObject obj = doc.to<JsonObject>();
|
||||||
|
bool ha_dev_created = false;
|
||||||
|
|
||||||
for (auto & sensor : sensors_) {
|
for (auto & sensor : sensors_) {
|
||||||
if (Mqtt::is_nested()) {
|
if (Mqtt::is_nested()) {
|
||||||
@@ -807,9 +808,12 @@ void AnalogSensor::publish_values(const bool force) {
|
|||||||
std::string topic_str(topic);
|
std::string topic_str(topic);
|
||||||
config["def_ent_id"] = topic_str.substr(0, topic_str.find("/")) + "." + uniq_s;
|
config["def_ent_id"] = topic_str.substr(0, topic_str.find("/")) + "." + uniq_s;
|
||||||
|
|
||||||
|
// dev section with model is only created on the 1st sensor
|
||||||
|
Mqtt::add_ha_dev_section(config.as<JsonObject>(), "Analog Sensors", nullptr, "EMS-ESP", EMSESP_APP_VERSION, !ha_dev_created);
|
||||||
Mqtt::add_ha_avty_section(config.as<JsonObject>(), stat_t, val_cond);
|
Mqtt::add_ha_avty_section(config.as<JsonObject>(), stat_t, val_cond);
|
||||||
|
|
||||||
sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>());
|
sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>());
|
||||||
|
ha_dev_created = sensor.ha_registered;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1468,7 +1468,7 @@ void Mqtt::add_ha_dev_section(JsonObject doc, const char * name, const char * mo
|
|||||||
dev_json["name"] = Mqtt::basename();
|
dev_json["name"] = Mqtt::basename();
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is used to only create it once when entities are dynamically added
|
// create the model, manufacturer and version
|
||||||
if (create_model) {
|
if (create_model) {
|
||||||
dev_json["mf"] = brand != nullptr ? brand : "EMS-ESP";
|
dev_json["mf"] = brand != nullptr ? brand : "EMS-ESP";
|
||||||
if (model != nullptr) {
|
if (model != nullptr) {
|
||||||
|
|||||||
@@ -480,6 +480,7 @@ void TemperatureSensor::publish_values(const bool force) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
|
bool ha_dev_created = false;
|
||||||
|
|
||||||
for (auto & sensor : sensors_) {
|
for (auto & sensor : sensors_) {
|
||||||
bool has_value = Helpers::hasValue(sensor.temperature_c);
|
bool has_value = Helpers::hasValue(sensor.temperature_c);
|
||||||
@@ -543,17 +544,15 @@ void TemperatureSensor::publish_values(const bool force) {
|
|||||||
config["def_ent_id"] = (std::string) "sensor." + uniq_s;
|
config["def_ent_id"] = (std::string) "sensor." + uniq_s;
|
||||||
config["name"] = sensor.name();
|
config["name"] = sensor.name();
|
||||||
|
|
||||||
// see if we need to create the [devs] discovery section, as this needs only to be done once for all sensors
|
// dev section with model is only created on the 1st sensor
|
||||||
if (std::none_of(sensors_.begin(), sensors_.end(), [](const auto & sensor) { return sensor.ha_registered; })) {
|
Mqtt::add_ha_dev_section(config.as<JsonObject>(), "Temperature Sensors", nullptr, "EMS-ESP", EMSESP_APP_VERSION, !ha_dev_created);
|
||||||
Mqtt::add_ha_dev_section(config.as<JsonObject>(), "Temperature Sensors", nullptr, "EMS-ESP", EMSESP_APP_VERSION, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Mqtt::add_ha_avty_section(config.as<JsonObject>(), stat_t, val_cond);
|
Mqtt::add_ha_avty_section(config.as<JsonObject>(), stat_t, val_cond);
|
||||||
|
|
||||||
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||||
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id());
|
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id());
|
||||||
|
|
||||||
sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>());
|
sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>());
|
||||||
|
ha_dev_created = sensor.ha_registered;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user