This commit is contained in:
MichaelDvP
2025-12-17 18:11:53 +01:00
3 changed files with 10 additions and 7 deletions

View File

@@ -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"] = (const char *)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;
}
}
}