mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-26 16:49:11 +03:00
fix HA sensor referencing
This commit is contained in:
@@ -480,6 +480,7 @@ void TemperatureSensor::publish_values(const bool force) {
|
||||
}
|
||||
|
||||
JsonDocument doc;
|
||||
bool ha_dev_created = false;
|
||||
|
||||
for (auto & sensor : sensors_) {
|
||||
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["name"] = sensor.name();
|
||||
|
||||
// see if we need to create the [devs] discovery section, as this needs only to be done once for all sensors
|
||||
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, true);
|
||||
}
|
||||
|
||||
// dev section with model is only created on the 1st sensor
|
||||
Mqtt::add_ha_dev_section(config.as<JsonObject>(), "Temperature Sensors", nullptr, "EMS-ESP", EMSESP_APP_VERSION, !ha_dev_created);
|
||||
Mqtt::add_ha_avty_section(config.as<JsonObject>(), stat_t, val_cond);
|
||||
|
||||
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||
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>());
|
||||
ha_dev_created = sensor.ha_registered;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user