temperaturesensor -> F_(temperaturesensor), analog/temperature mqtt-base()

This commit is contained in:
MichaelDvP
2023-11-08 14:51:07 +01:00
parent 555bf8cb2f
commit 110ee59cd1
2 changed files with 15 additions and 11 deletions

View File

@@ -515,7 +515,7 @@ void AnalogSensor::publish_values(const bool force) {
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/%s_data", Mqtt::basename().c_str(), F_(analogsensor)); // use base path snprintf(stat_t, sizeof(stat_t), "%s/%s_data", Mqtt::base().c_str(), F_(analogsensor)); // use base path
config["stat_t"] = stat_t; config["stat_t"] = stat_t;
char val_obj[50]; char val_obj[50];
@@ -561,7 +561,7 @@ void AnalogSensor::publish_values(const bool force) {
if (sensor.type() == AnalogType::DIGITAL_OUT) { if (sensor.type() == AnalogType::DIGITAL_OUT) {
#endif #endif
snprintf(topic, sizeof(topic), "switch/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio()); snprintf(topic, sizeof(topic), "switch/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::basename().c_str(), F_(analogsensor), sensor.name().c_str()); snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
config["cmd_t"] = command_topic; config["cmd_t"] = command_topic;
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) { if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
config["pl_on"] = true; config["pl_on"] = true;
@@ -576,7 +576,7 @@ void AnalogSensor::publish_values(const bool force) {
} }
} else if (sensor.type() == AnalogType::DIGITAL_OUT) { // DAC } else if (sensor.type() == AnalogType::DIGITAL_OUT) { // DAC
snprintf(topic, sizeof(topic), "number/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio()); snprintf(topic, sizeof(topic), "number/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::basename().c_str(), F_(analogsensor), sensor.name().c_str()); snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
config["cmd_t"] = command_topic; config["cmd_t"] = command_topic;
config["min"] = 0; config["min"] = 0;
config["max"] = 255; config["max"] = 255;
@@ -584,7 +584,7 @@ void AnalogSensor::publish_values(const bool force) {
config["step"] = 1; config["step"] = 1;
} else if (sensor.type() >= AnalogType::PWM_0 && sensor.type() <= AnalogType::PWM_2) { } else if (sensor.type() >= AnalogType::PWM_0 && sensor.type() <= AnalogType::PWM_2) {
snprintf(topic, sizeof(topic), "number/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio()); snprintf(topic, sizeof(topic), "number/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::basename().c_str(), F_(analogsensor), sensor.name().c_str()); snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
config["cmd_t"] = command_topic; config["cmd_t"] = command_topic;
config["min"] = 0; config["min"] = 0;
config["max"] = 100; config["max"] = 100;
@@ -592,7 +592,7 @@ void AnalogSensor::publish_values(const bool force) {
config["step"] = 0.1; config["step"] = 0.1;
} else if (sensor.type() == AnalogType::COUNTER) { } else if (sensor.type() == AnalogType::COUNTER) {
snprintf(topic, sizeof(topic), "sensor/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio()); snprintf(topic, sizeof(topic), "sensor/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::basename().c_str(), F_(analogsensor), sensor.name().c_str()); snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
config["cmd_t"] = command_topic; config["cmd_t"] = command_topic;
config["stat_cla"] = "total_increasing"; config["stat_cla"] = "total_increasing";
// config["mode"] = "box"; // auto, slider or box // config["mode"] = "box"; // auto, slider or box

View File

@@ -63,7 +63,9 @@ void TemperatureSensor::start() {
[&](const char * value, const int8_t id, JsonObject & output) { return command_commands(value, id, output); }, [&](const char * value, const int8_t id, JsonObject & output) { return command_commands(value, id, output); },
FL_(commands_cmd)); FL_(commands_cmd));
Mqtt::subscribe(EMSdevice::DeviceType::TEMPERATURESENSOR, "temperaturesensor/#", nullptr); // use empty function callback char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
snprintf(topic, sizeof(topic), "%s/#", F_(temperaturesensor));
Mqtt::subscribe(EMSdevice::DeviceType::TEMPERATURESENSOR, topic, nullptr); // use empty function callback
} }
// load settings // load settings
@@ -511,7 +513,7 @@ void TemperatureSensor::publish_values(const bool force) {
config["dev_cla"] = "temperature"; config["dev_cla"] = "temperature";
char stat_t[50]; char stat_t[50];
snprintf(stat_t, sizeof(stat_t), "%s/temperaturesensor_data", Mqtt::basename().c_str()); snprintf(stat_t, sizeof(stat_t), "%s/%s_data", Mqtt::base().c_str(), F_(temperaturesensor)); // use base path
config["stat_t"] = stat_t; config["stat_t"] = stat_t;
config["unit_of_meas"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES); config["unit_of_meas"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);
@@ -529,9 +531,9 @@ void TemperatureSensor::publish_values(const bool force) {
char uniq_s[70]; char uniq_s[70];
if (Mqtt::entity_format() == Mqtt::entityFormat::MULTI_SHORT) { if (Mqtt::entity_format() == Mqtt::entityFormat::MULTI_SHORT) {
snprintf(uniq_s, sizeof(uniq_s), "%s_temperaturesensor_%s", Mqtt::basename().c_str(), sensor.id().c_str()); snprintf(uniq_s, sizeof(uniq_s), "%s_%s_%s", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id().c_str());
} else { } else {
snprintf(uniq_s, sizeof(uniq_s), "temperaturesensor_%s", sensor.id().c_str()); snprintf(uniq_s, sizeof(uniq_s), "%s_%s", F_(temperaturesensor), sensor.id().c_str());
} }
config["obj_id"] = uniq_s; config["obj_id"] = uniq_s;
@@ -553,14 +555,16 @@ void TemperatureSensor::publish_values(const bool force) {
std::string sensorid = sensor.id(); std::string sensorid = sensor.id();
std::replace(sensorid.begin(), sensorid.end(), '-', '_'); std::replace(sensorid.begin(), sensorid.end(), '-', '_');
snprintf(topic, sizeof(topic), "sensor/%s/temperaturesensor_%s/config", Mqtt::basename().c_str(), sensorid.c_str()); snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(temperaturesensor), sensorid.c_str());
sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>()); sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>());
} }
} }
} }
Mqtt::queue_publish("temperaturesensor_data", doc.as<JsonObject>()); char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
snprintf(topic, sizeof(topic), "%s_data", F_(temperaturesensor));
Mqtt::queue_publish(topic, doc.as<JsonObject>());
} }