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

@@ -676,6 +676,7 @@ void AnalogSensor::publish_values(const bool force) {
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
bool ha_dev_created = false;
for (auto & sensor : sensors_) {
if (Mqtt::is_nested()) {
@@ -807,9 +808,12 @@ void AnalogSensor::publish_values(const bool force) {
std::string topic_str(topic);
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);
sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>());
ha_dev_created = sensor.ha_registered;
}
}

View File

@@ -1468,7 +1468,7 @@ void Mqtt::add_ha_dev_section(JsonObject doc, const char * name, const char * mo
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) {
dev_json["mf"] = brand != nullptr ? brand : "EMS-ESP";
if (model != nullptr) {

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;
}
}
}