updates for #759

This commit is contained in:
Proddy
2022-11-30 21:21:32 +01:00
parent e8d6c4d451
commit e3c94cc1f7

View File

@@ -280,6 +280,7 @@ void Mqtt::on_message(const char * topic, const char * payload, size_t len) cons
} }
return; return;
} }
// for misconfigured mqtt servers and publish2command ignore echos // for misconfigured mqtt servers and publish2command ignore echos
if (publish_single_ && publish_single2cmd_ && lasttopic_ == topic && lastpayload_ == message) { if (publish_single_ && publish_single2cmd_ && lasttopic_ == topic && lastpayload_ == message) {
LOG_DEBUG("Received echo message %s: %s", topic, message); LOG_DEBUG("Received echo message %s: %s", topic, message);
@@ -291,11 +292,12 @@ void Mqtt::on_message(const char * topic, const char * payload, size_t len) cons
// 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)) {
LOG_ERROR("error: invalid payload %s for this topic %s", message, topic); LOG_ERROR("error: invalid payload %s for this topic %s", message, topic);
if (send_response_) { if (send_response_) {
Mqtt::publish(F_(response), "error: invalid data"); Mqtt::publish("response", "error: invalid data");
} }
} }
return; return;
@@ -330,12 +332,12 @@ void Mqtt::on_message(const char * topic, const char * payload, size_t len) cons
} }
LOG_ERROR(error); LOG_ERROR(error);
if (send_response_) { if (send_response_) {
Mqtt::publish(F_(response), error); Mqtt::publish("response", error);
} }
} else { } else {
// all good, send back json output from call // all good, send back json output from call
if (send_response_) { if (send_response_) {
Mqtt::publish(F_(response), output); Mqtt::publish("response", output);
} }
} }
} }
@@ -904,7 +906,6 @@ void Mqtt::publish_ha_sensor_config(DeviceValue & dv, const std::string & model,
publish_ha_sensor_config(dv.type, publish_ha_sensor_config(dv.type,
dv.tag, dv.tag,
dv.get_fullname().c_str(), dv.get_fullname().c_str(),
(dv.fullname ? dv.fullname[0] : nullptr), // EN name
dv.device_type, dv.device_type,
dv.short_name, dv.short_name,
dv.uom, dv.uom,
@@ -925,7 +926,7 @@ void Mqtt::publish_system_ha_sensor_config(uint8_t type, const char * name, cons
JsonArray ids = dev_json.createNestedArray("ids"); JsonArray ids = dev_json.createNestedArray("ids");
ids.add("ems-esp"); ids.add("ems-esp");
publish_ha_sensor_config(type, DeviceValueTAG::TAG_HEARTBEAT, name, name, EMSdevice::DeviceType::SYSTEM, entity, uom, false, false, nullptr, 0, 0, 0, dev_json); publish_ha_sensor_config(type, DeviceValueTAG::TAG_HEARTBEAT, name, EMSdevice::DeviceType::SYSTEM, entity, uom, false, false, nullptr, 0, 0, 0, dev_json);
} }
// MQTT discovery configs // MQTT discovery configs
@@ -934,7 +935,6 @@ void Mqtt::publish_system_ha_sensor_config(uint8_t type, const char * name, cons
void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdevice::DeviceValueType void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdevice::DeviceValueType
uint8_t tag, // EMSdevice::DeviceValueTAG uint8_t tag, // EMSdevice::DeviceValueTAG
const char * const fullname, // fullname, already translated const char * const fullname, // fullname, already translated
const char * const en_name, // original name
const uint8_t device_type, // EMSdevice::DeviceType const uint8_t device_type, // EMSdevice::DeviceType
const char * const entity, // same as shortname const char * const entity, // same as shortname
const uint8_t uom, // EMSdevice::DeviceValueUOM (0=NONE) const uint8_t uom, // EMSdevice::DeviceValueUOM (0=NONE)
@@ -946,19 +946,17 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
const int16_t dv_set_max, const int16_t dv_set_max,
const JsonObject & dev_json) { const JsonObject & dev_json) {
// ignore if name (fullname) is empty // ignore if name (fullname) is empty
if (fullname == nullptr || en_name == nullptr) { if (!fullname) {
return; return;
} }
// create the device name // create the device name
auto device_name = EMSdevice::device_type_2_device_name(device_type); auto device_name = EMSdevice::device_type_2_device_name(device_type);
// create entity by add the hc/wwc tag if present, separating with a . // create entity by add the hc/wwc tag if present, separating with a _
char entity_with_tag[50]; char entity_with_tag[50];
if (tag >= DeviceValueTAG::TAG_HC1) { if (tag >= DeviceValueTAG::TAG_HC1) {
snprintf(entity_with_tag, sizeof(entity_with_tag), "%s.%s", EMSdevice::tag_to_mqtt(tag).c_str(), entity); snprintf(entity_with_tag, sizeof(entity_with_tag), "%s_%s", EMSdevice::tag_to_mqtt(tag).c_str(), entity);
} else { } else {
snprintf(entity_with_tag, sizeof(entity_with_tag), "%s", entity); snprintf(entity_with_tag, sizeof(entity_with_tag), "%s", entity);
} }
@@ -966,13 +964,11 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
// build unique identifier which will be used in the topic, also used as object_id // build unique identifier which will be used in the topic, also used as object_id
char uniq_id[70]; char uniq_id[70];
snprintf(uniq_id, sizeof(uniq_id), "%s_%s_%s", mqtt_basename_.c_str(), device_name, entity_with_tag); snprintf(uniq_id, sizeof(uniq_id), "%s_%s_%s", mqtt_basename_.c_str(), device_name, entity_with_tag);
Helpers::replace_char(uniq_id, '.', '_'); // replacing all . with _ as not to break HA
// build a config topic that will be prefix onto a HA type (e.g. number, switch) // build a config topic that will be prefix onto a HA type (e.g. number, switch)
// e.g. homeassistant/number/ems-esp/thermostat_hc1_manualtemp // e.g. homeassistant/number/ems-esp/thermostat_hc1_manualtemp
char config_topic[70]; char config_topic[70];
snprintf(config_topic, sizeof(config_topic), "%s/%s_%s/config", mqtt_basename_.c_str(), device_name, entity_with_tag); snprintf(config_topic, sizeof(config_topic), "%s/%s_%s/config", mqtt_basename_.c_str(), device_name, entity_with_tag);
Helpers::replace_char(config_topic, '.', '_'); // replacing all . with _ as not to break HA
bool set_ha_classes = false; // set to true if we want to set the state class and device class bool set_ha_classes = false; // set to true if we want to set the state class and device class
@@ -1005,8 +1001,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
break; break;
} }
} else { } else {
// these are Read only sensors. We can set the device class and state class set_ha_classes = true; // these are Read only sensors. We can set the device class and state class
set_ha_classes = true;
// plain old read only device entity // plain old read only device entity
if (type == DeviceValueType::BOOL) { if (type == DeviceValueType::BOOL) {
snprintf(topic, sizeof(topic), "binary_sensor/%s", config_topic); // binary sensor (for booleans) snprintf(topic, sizeof(topic), "binary_sensor/%s", config_topic); // binary sensor (for booleans)
@@ -1025,7 +1020,6 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
// build the payload // build the payload
DynamicJsonDocument doc(EMSESP_JSON_SIZE_HA_CONFIG); DynamicJsonDocument doc(EMSESP_JSON_SIZE_HA_CONFIG);
doc["~"] = mqtt_base_;
doc["uniq_id"] = uniq_id; doc["uniq_id"] = uniq_id;
doc["object_id"] = uniq_id; // same as unique_id doc["object_id"] = uniq_id; // same as unique_id
@@ -1039,8 +1033,11 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
if (has_cmd) { if (has_cmd) {
// command topic back to EMS-ESP // command topic back to EMS-ESP
char command_topic[MQTT_TOPIC_MAX_SIZE]; char command_topic[MQTT_TOPIC_MAX_SIZE];
snprintf(command_topic, sizeof(command_topic), "~/%s", uniq_id); if (tag >= DeviceValueTAG::TAG_HC1) {
Helpers::replace_char(command_topic, '_', '/'); snprintf(command_topic, sizeof(command_topic), "%s/%s/%s/%s", mqtt_basename_.c_str(), device_name, EMSdevice::tag_to_mqtt(tag).c_str(), entity);
} else {
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", mqtt_basename_.c_str(), device_name, entity);
}
doc["command_topic"] = command_topic; doc["command_topic"] = command_topic;
// for enums, add options // for enums, add options
@@ -1082,7 +1079,7 @@ void 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", tag_to_topic(device_type, tag).c_str()); snprintf(stat_t, sizeof(stat_t), "%s/%s", mqtt_basename_.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>
@@ -1090,7 +1087,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
char * F_name = strdup(fullname); char * F_name = strdup(fullname);
F_name[0] = toupper(F_name[0]); // capitalize first letter F_name[0] = toupper(F_name[0]); // capitalize first letter
if (EMSdevice::tag_to_string(tag).empty()) { if (EMSdevice::tag_to_string(tag).empty()) {
snprintf(ha_name, sizeof(ha_name), "%s", F_name); snprintf(ha_name, sizeof(ha_name), "%s", F_name); // no tag
} else { } else {
snprintf(ha_name, sizeof(ha_name), "%s %s", EMSdevice::tag_to_string(tag).c_str(), F_name); snprintf(ha_name, sizeof(ha_name), "%s %s", EMSdevice::tag_to_string(tag).c_str(), F_name);
} }
@@ -1098,10 +1095,14 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
doc["name"] = ha_name; doc["name"] = ha_name;
// value template // value template
// if its nested mqtt format then use the appended entity name, otherwise take the original // if its nested mqtt format then use the appended entity name, otherwise take the original name
char val_tpl[75]; char val_tpl[75];
if (is_nested()) { if (is_nested()) {
snprintf(val_tpl, sizeof(val_tpl), "{{value_json.%s}}", entity_with_tag); if (tag >= DeviceValueTAG::TAG_HC1) {
snprintf(val_tpl, sizeof(val_tpl), "{{value_json.%s.%s}}", EMSdevice::tag_to_mqtt(tag).c_str(), entity);
} else {
snprintf(val_tpl, sizeof(val_tpl), "{{value_json.%s}}", entity);
}
} else { } else {
snprintf(val_tpl, sizeof(val_tpl), "{{value_json.%s}}", entity); snprintf(val_tpl, sizeof(val_tpl), "{{value_json.%s}}", entity);
} }