This commit is contained in:
Proddy
2022-12-28 15:39:34 +01:00
parent 5d45064c2d
commit 43b4adc618
9 changed files with 85 additions and 34 deletions

View File

@@ -209,6 +209,10 @@
#define EMSESP_DEFAULT_WEBLOG_COMPACT true
#endif
#ifndef EMSESP_DEFAULT_ENTITY_FORMAT
#define EMSESP_DEFAULT_ENTITY_FORMAT 1 // in MQTT discovery, use shortnames and not multiple (prefixed with base)
#endif
// matches Web UI settings
enum {

View File

@@ -1063,7 +1063,8 @@ void EMSdevice::getCustomEntities(std::vector<std::string> & entity_ids) {
}
#if defined(EMSESP_STANDALONE_DUMP)
// device name, device type, shortname, fullname, type [(enum values) | (min/max)], uom, readable, writeable, visible
// dumps all entity values in native English
// device name,device type,product_id,shortname,fullname,type [(enum values) | (min/max)],uom,writeable,discovery_entityid
void EMSdevice::dump_value_info() {
for (auto & dv : devicevalues_) {
Serial.print(name_);
@@ -1071,9 +1072,13 @@ void EMSdevice::dump_value_info() {
Serial.print(device_type_name().c_str());
Serial.print(',');
Serial.print(product_id_);
Serial.print(',');
Serial.print(dv.short_name);
Serial.print(',');
Serial.print(dv.get_fullname().c_str());
Serial.print(dv.fullname[0]);
Serial.print(',');
// type and optional enum values and min/max
@@ -1161,12 +1166,54 @@ void EMSdevice::dump_value_info() {
}
Serial.print(",");
// readable, writeable, visible flags
Serial.print(!dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE) ? "true" : "false");
// writeable flag
Serial.print(dv.has_cmd ? "true" : "false");
Serial.print(",");
Serial.print((dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY)) ? "true" : "false");
Serial.print(",");
Serial.print(!dv.has_state(DeviceValueState::DV_WEB_EXCLUDE) ? "true" : "false");
// MQTT Discovery entity name, assuming we're using the default v3.5 option
char entity_with_tag[50];
if (dv.tag >= DeviceValueTAG::TAG_HC1) {
snprintf(entity_with_tag,
sizeof(entity_with_tag),
"%s_%s_%s",
device_type_2_device_name(device_type_),
EMSdevice::tag_to_mqtt(dv.tag).c_str(),
dv.short_name);
} else {
// should really test for which device types have tags (like hc, wwc etc)
// snprintf(entity_with_tag, sizeof(entity_with_tag), "%s_[<tag>_]%s", device_type_2_device_name(device_type_), dv.short_name);
snprintf(entity_with_tag, sizeof(entity_with_tag), "%s_%s", device_type_2_device_name(device_type_), dv.short_name);
}
char entityid[150];
if (dv.has_cmd) {
switch (dv.type) {
case DeviceValueType::INT:
case DeviceValueType::UINT:
case DeviceValueType::SHORT:
case DeviceValueType::USHORT:
case DeviceValueType::ULONG:
snprintf(entityid, sizeof(entityid), "number.%s", entity_with_tag);
break;
case DeviceValueType::BOOL:
snprintf(entityid, sizeof(entityid), "switch.%s", entity_with_tag);
break;
case DeviceValueType::ENUM:
snprintf(entityid, sizeof(entityid), "select.%s", entity_with_tag);
break;
default:
snprintf(entityid, sizeof(entityid), "sensor.%s", entity_with_tag);
break;
}
} else {
if (dv.type == DeviceValueType::BOOL) {
snprintf(entityid, sizeof(entityid), "binary_sensor.%s", entity_with_tag); // binary sensor (for booleans)
} else {
snprintf(entityid, sizeof(entityid), "sensor.%s", entity_with_tag); // normal HA sensor
}
}
Serial.print(entityid);
Serial.println();
}

View File

@@ -201,7 +201,9 @@ class EMSdevice {
bool get_value_info(JsonObject & root, const char * cmd, const int8_t id);
void get_dv_info(JsonObject & json);
#if defined(EMSESP_STANDALONE_DUMP)
void dump_value_info();
#endif
enum OUTPUT_TARGET : uint8_t { API_VERBOSE, API_SHORTNAMES, MQTT, CONSOLE };
bool generate_values(JsonObject & output, const uint8_t tag_filter, const bool nested, const uint8_t output_target);

View File

@@ -313,7 +313,7 @@ void EMSESP::show_ems(uuid::console::Shell & shell) {
void EMSESP::dump_all_values(uuid::console::Shell & shell) {
Serial.println("---- CSV START ----"); // marker use by py script
// add header for CSV
Serial.print("device name,device type,shortname,fullname,type [(enum values) | (min/max)],uom,readable,writeable,visible");
Serial.print("device name,device type,product_id,shortname,fullname,type [(enum values) | (min/max)],uom,writeable,discovery_entityid");
Serial.println();
for (const auto & device_class : EMSFactory::device_handlers()) {

View File

@@ -992,12 +992,11 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
// prefix base name to each uniq_id and use the shortname
snprintf(uniq_id, sizeof(uniq_id), "%s_%s_%s", mqtt_basename_.c_str(), device_name, entity_with_tag);
} else if (Mqtt::entity_format() == 1) {
// shortname, no mqtt base
// shortname, no mqtt base. This is the default version.
snprintf(uniq_id, sizeof(uniq_id), "%s_%s", device_name, entity_with_tag);
} else {
// entity_format is 0
// old v3.4 style
// take en_name and replace all spaces and lowercase it
// entity_format is 0, the old v3.4 style
// take en_name and replace all spaces
char uniq_s[60];
strlcpy(uniq_s, en_name, sizeof(uniq_s));
Helpers::replace_char(uniq_s, ' ', '_');