mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
Merge branch 'dev' of https://github.com/proddy/EMS-ESP32 into dev2
This commit is contained in:
@@ -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
|
||||||
|
|||||||
30
src/mqtt.cpp
30
src/mqtt.cpp
@@ -199,7 +199,7 @@ void Mqtt::show_mqtt(uuid::console::Shell & shell) {
|
|||||||
// show subscriptions
|
// show subscriptions
|
||||||
shell.printfln("MQTT topic subscriptions:");
|
shell.printfln("MQTT topic subscriptions:");
|
||||||
for (const auto & mqtt_subfunction : mqtt_subfunctions_) {
|
for (const auto & mqtt_subfunction : mqtt_subfunctions_) {
|
||||||
shell.printfln(" %s/%s", mqtt_base_.c_str(), mqtt_subfunction.topic_.c_str());
|
shell.printfln(" %s/%s", Mqtt::base().c_str(), mqtt_subfunction.topic_.c_str());
|
||||||
}
|
}
|
||||||
shell.println();
|
shell.println();
|
||||||
|
|
||||||
@@ -252,7 +252,7 @@ void Mqtt::on_message(const char * topic, const uint8_t * payload, size_t len) c
|
|||||||
for (const auto & mf : mqtt_subfunctions_) {
|
for (const auto & mf : mqtt_subfunctions_) {
|
||||||
// add the base back
|
// add the base back
|
||||||
char full_topic[MQTT_TOPIC_MAX_SIZE];
|
char full_topic[MQTT_TOPIC_MAX_SIZE];
|
||||||
snprintf(full_topic, sizeof(full_topic), "%s/%s", mqtt_base_.c_str(), mf.topic_.c_str());
|
snprintf(full_topic, sizeof(full_topic), "%s/%s", Mqtt::base().c_str(), mf.topic_.c_str());
|
||||||
|
|
||||||
if ((!strcmp(topic, full_topic)) && (mf.mqtt_subfunction_)) {
|
if ((!strcmp(topic, full_topic)) && (mf.mqtt_subfunction_)) {
|
||||||
if (!(mf.mqtt_subfunction_)(message)) {
|
if (!(mf.mqtt_subfunction_)(message)) {
|
||||||
@@ -362,8 +362,8 @@ void Mqtt::load_settings() {
|
|||||||
publish_time_heartbeat_ = mqttSettings.publish_time_heartbeat * 1000;
|
publish_time_heartbeat_ = mqttSettings.publish_time_heartbeat * 1000;
|
||||||
});
|
});
|
||||||
|
|
||||||
// create basename from base
|
// create basename from the mqtt base
|
||||||
// by taking the MQTT base path and replacing all / with underscores
|
// and replacing all / with underscores, in case it's a path
|
||||||
mqtt_basename_ = mqtt_base_;
|
mqtt_basename_ = mqtt_base_;
|
||||||
std::replace(mqtt_basename_.begin(), mqtt_basename_.end(), '/', '_');
|
std::replace(mqtt_basename_.begin(), mqtt_basename_.end(), '/', '_');
|
||||||
}
|
}
|
||||||
@@ -390,8 +390,8 @@ void Mqtt::start() {
|
|||||||
|
|
||||||
// create last will topic with the base prefixed. It has to be static because asyncmqttclient destroys the reference
|
// create last will topic with the base prefixed. It has to be static because asyncmqttclient destroys the reference
|
||||||
static char will_topic[MQTT_TOPIC_MAX_SIZE];
|
static char will_topic[MQTT_TOPIC_MAX_SIZE];
|
||||||
if (!mqtt_base_.empty()) {
|
if (!Mqtt::base().empty()) {
|
||||||
snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "%s/status", mqtt_base_.c_str());
|
snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "%s/status", Mqtt::base().c_str());
|
||||||
} else {
|
} else {
|
||||||
snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "status");
|
snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "status");
|
||||||
}
|
}
|
||||||
@@ -540,7 +540,7 @@ void Mqtt::ha_status() {
|
|||||||
|
|
||||||
char uniq[70];
|
char uniq[70];
|
||||||
if (Mqtt::entity_format() == entityFormat::MULTI_SHORT) {
|
if (Mqtt::entity_format() == entityFormat::MULTI_SHORT) {
|
||||||
snprintf(uniq, sizeof(uniq), "%s_system_status", mqtt_basename_.c_str());
|
snprintf(uniq, sizeof(uniq), "%s_system_status", Mqtt::basename().c_str());
|
||||||
} else {
|
} else {
|
||||||
strcpy(uniq, "system_status");
|
strcpy(uniq, "system_status");
|
||||||
}
|
}
|
||||||
@@ -548,7 +548,7 @@ void Mqtt::ha_status() {
|
|||||||
doc["uniq_id"] = uniq;
|
doc["uniq_id"] = uniq;
|
||||||
doc["obj_id"] = uniq;
|
doc["obj_id"] = uniq;
|
||||||
|
|
||||||
doc["stat_t"] = mqtt_basename_ + "/status";
|
doc["stat_t"] = Mqtt::base() + "/status";
|
||||||
doc["name"] = "System status";
|
doc["name"] = "System status";
|
||||||
doc["pl_on"] = "online";
|
doc["pl_on"] = "online";
|
||||||
doc["pl_off"] = "offline";
|
doc["pl_off"] = "offline";
|
||||||
@@ -560,7 +560,7 @@ void Mqtt::ha_status() {
|
|||||||
// doc["json_attr_t"] = "~/heartbeat"; // store also as HA attributes
|
// doc["json_attr_t"] = "~/heartbeat"; // store also as HA attributes
|
||||||
|
|
||||||
JsonObject dev = doc.createNestedObject("dev");
|
JsonObject dev = doc.createNestedObject("dev");
|
||||||
dev["name"] = Mqtt::basename(); // take basename
|
dev["name"] = Mqtt::basename();
|
||||||
dev["sw"] = "v" + std::string(EMSESP_APP_VERSION);
|
dev["sw"] = "v" + std::string(EMSESP_APP_VERSION);
|
||||||
dev["mf"] = "proddy";
|
dev["mf"] = "proddy";
|
||||||
dev["mdl"] = "EMS-ESP";
|
dev["mdl"] = "EMS-ESP";
|
||||||
@@ -636,7 +636,7 @@ bool Mqtt::queue_message(const uint8_t operation, const std::string & topic, con
|
|||||||
strlcpy(fulltopic, topic.c_str(), sizeof(fulltopic)); // leave discovery topic as it is
|
strlcpy(fulltopic, topic.c_str(), sizeof(fulltopic)); // leave discovery topic as it is
|
||||||
} else {
|
} else {
|
||||||
// it's not a discovery topic, added the mqtt base to the topic path
|
// it's not a discovery topic, added the mqtt base to the topic path
|
||||||
snprintf(fulltopic, sizeof(fulltopic), "%s/%s", mqtt_base_.c_str(), topic.c_str()); // uses base
|
snprintf(fulltopic, sizeof(fulltopic), "%s/%s", Mqtt::base().c_str(), topic.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operation == Operation::PUBLISH) {
|
if (operation == Operation::PUBLISH) {
|
||||||
@@ -998,7 +998,7 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
|||||||
|
|
||||||
// state topic
|
// state topic
|
||||||
char stat_t[MQTT_TOPIC_MAX_SIZE];
|
char stat_t[MQTT_TOPIC_MAX_SIZE];
|
||||||
snprintf(stat_t, sizeof(stat_t), "%s/%s", mqtt_basename_.c_str(), tag_to_topic(device_type, tag).c_str());
|
snprintf(stat_t, sizeof(stat_t), "%s/%s", Mqtt::base().c_str(), tag_to_topic(device_type, tag).c_str());
|
||||||
doc["stat_t"] = stat_t;
|
doc["stat_t"] = stat_t;
|
||||||
|
|
||||||
// friendly name = <tag> <name>
|
// friendly name = <tag> <name>
|
||||||
@@ -1174,7 +1174,7 @@ bool Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp,
|
|||||||
char min_s[10];
|
char min_s[10];
|
||||||
char max_s[10];
|
char max_s[10];
|
||||||
|
|
||||||
snprintf(topic, sizeof(topic), "climate/%s/thermostat_hc%d/config", mqtt_basename_.c_str(), hc_num);
|
snprintf(topic, sizeof(topic), "climate/%s/thermostat_hc%d/config", Mqtt::basename().c_str(), hc_num);
|
||||||
if (remove) {
|
if (remove) {
|
||||||
return queue_remove_topic(topic); // publish empty payload with retain flag
|
return queue_remove_topic(topic); // publish empty payload with retain flag
|
||||||
}
|
}
|
||||||
@@ -1217,7 +1217,7 @@ bool Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp,
|
|||||||
snprintf(name_s, sizeof(name_s), "Hc%d", hc_num);
|
snprintf(name_s, sizeof(name_s), "Hc%d", hc_num);
|
||||||
|
|
||||||
if (Mqtt::entity_format() == entityFormat::MULTI_SHORT) {
|
if (Mqtt::entity_format() == entityFormat::MULTI_SHORT) {
|
||||||
snprintf(uniq_id_s, sizeof(uniq_id_s), "%s_thermostat_hc%d", mqtt_basename_.c_str(), hc_num); // add basename
|
snprintf(uniq_id_s, sizeof(uniq_id_s), "%s_thermostat_hc%d", Mqtt::basename().c_str(), hc_num); // add basename
|
||||||
} else {
|
} else {
|
||||||
snprintf(uniq_id_s, sizeof(uniq_id_s), "thermostat_hc%d", hc_num); // backward compatible with v3.4
|
snprintf(uniq_id_s, sizeof(uniq_id_s), "thermostat_hc%d", hc_num); // backward compatible with v3.4
|
||||||
}
|
}
|
||||||
@@ -1227,7 +1227,7 @@ bool Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp,
|
|||||||
|
|
||||||
StaticJsonDocument<EMSESP_JSON_SIZE_XLARGE> doc; // 1024 is not enough
|
StaticJsonDocument<EMSESP_JSON_SIZE_XLARGE> doc; // 1024 is not enough
|
||||||
|
|
||||||
doc["~"] = mqtt_base_;
|
doc["~"] = Mqtt::base();
|
||||||
doc["uniq_id"] = uniq_id_s;
|
doc["uniq_id"] = uniq_id_s;
|
||||||
doc["obj_id"] = uniq_id_s; // same as uniq_id
|
doc["obj_id"] = uniq_id_s; // same as uniq_id
|
||||||
doc["name"] = name_s;
|
doc["name"] = name_s;
|
||||||
@@ -1294,7 +1294,7 @@ void Mqtt::add_avty_to_doc(const char * state_t, const JsonObject & doc, const c
|
|||||||
|
|
||||||
StaticJsonDocument<512> avty_json;
|
StaticJsonDocument<512> avty_json;
|
||||||
|
|
||||||
snprintf(tpl, sizeof(tpl), "%s/status", mqtt_base_.c_str());
|
snprintf(tpl, sizeof(tpl), "%s/status", Mqtt::base().c_str());
|
||||||
avty_json["t"] = tpl;
|
avty_json["t"] = tpl;
|
||||||
snprintf(tpl, sizeof(tpl), tpl_draft, "value == 'online'");
|
snprintf(tpl, sizeof(tpl), tpl_draft, "value == 'online'");
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ void Shower::set_shower_state(bool state, bool force) {
|
|||||||
doc["uniq_id"] = str;
|
doc["uniq_id"] = str;
|
||||||
doc["object_id"] = str;
|
doc["object_id"] = str;
|
||||||
|
|
||||||
snprintf(stat_t, sizeof(stat_t), "%s/shower_active", Mqtt::basename().c_str());
|
snprintf(stat_t, sizeof(stat_t), "%s/shower_active", Mqtt::base().c_str());
|
||||||
doc["stat_t"] = stat_t;
|
doc["stat_t"] = stat_t;
|
||||||
|
|
||||||
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||||
@@ -229,7 +229,7 @@ void Shower::set_shower_state(bool state, bool force) {
|
|||||||
doc["uniq_id"] = str;
|
doc["uniq_id"] = str;
|
||||||
doc["object_id"] = str;
|
doc["object_id"] = str;
|
||||||
|
|
||||||
snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::basename().c_str());
|
snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::base().c_str());
|
||||||
doc["stat_t"] = stat_t;
|
doc["stat_t"] = stat_t;
|
||||||
|
|
||||||
doc["name"] = "Shower Duration";
|
doc["name"] = "Shower Duration";
|
||||||
@@ -258,7 +258,7 @@ void Shower::set_shower_state(bool state, bool force) {
|
|||||||
doc["uniq_id"] = str;
|
doc["uniq_id"] = str;
|
||||||
doc["object_id"] = str;
|
doc["object_id"] = str;
|
||||||
|
|
||||||
snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::basename().c_str());
|
snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::base().c_str());
|
||||||
doc["stat_t"] = stat_t;
|
doc["stat_t"] = stat_t;
|
||||||
|
|
||||||
doc["name"] = "Shower Timestamp";
|
doc["name"] = "Shower Timestamp";
|
||||||
|
|||||||
@@ -358,7 +358,7 @@ void WebCustomEntityService::publish(const bool force) {
|
|||||||
if (Mqtt::ha_enabled() && !ha_registered_) {
|
if (Mqtt::ha_enabled() && !ha_registered_) {
|
||||||
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/custom_data", Mqtt::basename().c_str());
|
snprintf(stat_t, sizeof(stat_t), "%s/custom_data", Mqtt::base().c_str());
|
||||||
config["stat_t"] = stat_t;
|
config["stat_t"] = stat_t;
|
||||||
|
|
||||||
char val_obj[50];
|
char val_obj[50];
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ void WebSchedulerService::publish(const bool force) {
|
|||||||
if (Mqtt::ha_enabled() && !ha_registered_) {
|
if (Mqtt::ha_enabled() && !ha_registered_) {
|
||||||
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/scheduler_data", Mqtt::basename().c_str());
|
snprintf(stat_t, sizeof(stat_t), "%s/scheduler_data", Mqtt::base().c_str());
|
||||||
config["stat_t"] = stat_t;
|
config["stat_t"] = stat_t;
|
||||||
|
|
||||||
char val_obj[50];
|
char val_obj[50];
|
||||||
@@ -273,7 +273,7 @@ void WebSchedulerService::publish(const bool force) {
|
|||||||
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||||
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||||
snprintf(topic, sizeof(topic), "switch/%s/scheduler_%s/config", Mqtt::basename().c_str(), scheduleItem.name.c_str());
|
snprintf(topic, sizeof(topic), "switch/%s/scheduler_%s/config", Mqtt::basename().c_str(), scheduleItem.name.c_str());
|
||||||
snprintf(command_topic, sizeof(command_topic), "%s/scheduler/%s", Mqtt::basename().c_str(), scheduleItem.name.c_str());
|
snprintf(command_topic, sizeof(command_topic), "%s/scheduler/%s", Mqtt::base().c_str(), scheduleItem.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;
|
||||||
|
|||||||
Reference in New Issue
Block a user