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

@@ -178,18 +178,17 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
MqttSettings newSettings = {}; MqttSettings newSettings = {};
bool changed = false; bool changed = false;
newSettings.enabled = root["enabled"] | FACTORY_MQTT_ENABLED; newSettings.enabled = root["enabled"] | FACTORY_MQTT_ENABLED;
newSettings.host = root["host"] | FACTORY_MQTT_HOST; newSettings.host = root["host"] | FACTORY_MQTT_HOST;
newSettings.port = root["port"] | FACTORY_MQTT_PORT; newSettings.port = root["port"] | FACTORY_MQTT_PORT;
newSettings.base = root["base"] | FACTORY_MQTT_BASE; newSettings.base = root["base"] | FACTORY_MQTT_BASE;
newSettings.username = root["username"] | FACTORY_MQTT_USERNAME; newSettings.username = root["username"] | FACTORY_MQTT_USERNAME;
newSettings.password = root["password"] | FACTORY_MQTT_PASSWORD; newSettings.password = root["password"] | FACTORY_MQTT_PASSWORD;
newSettings.clientId = root["client_id"] | FACTORY_MQTT_CLIENT_ID; newSettings.clientId = root["client_id"] | FACTORY_MQTT_CLIENT_ID;
newSettings.keepAlive = root["keep_alive"] | FACTORY_MQTT_KEEP_ALIVE; newSettings.keepAlive = root["keep_alive"] | FACTORY_MQTT_KEEP_ALIVE;
newSettings.cleanSession = root["clean_session"] | FACTORY_MQTT_CLEAN_SESSION; newSettings.cleanSession = root["clean_session"] | FACTORY_MQTT_CLEAN_SESSION;
newSettings.entity_format = root["entity_format"] | FACTORY_MQTT_ENTITY_FORMAT; newSettings.mqtt_qos = root["mqtt_qos"] | EMSESP_DEFAULT_MQTT_QOS;
newSettings.mqtt_qos = root["mqtt_qos"] | EMSESP_DEFAULT_MQTT_QOS; newSettings.mqtt_retain = root["mqtt_retain"] | EMSESP_DEFAULT_MQTT_RETAIN;
newSettings.mqtt_retain = root["mqtt_retain"] | EMSESP_DEFAULT_MQTT_RETAIN;
newSettings.publish_time_boiler = root["publish_time_boiler"] | EMSESP_DEFAULT_PUBLISH_TIME; newSettings.publish_time_boiler = root["publish_time_boiler"] | EMSESP_DEFAULT_PUBLISH_TIME;
newSettings.publish_time_thermostat = root["publish_time_thermostat"] | EMSESP_DEFAULT_PUBLISH_TIME; newSettings.publish_time_thermostat = root["publish_time_thermostat"] | EMSESP_DEFAULT_PUBLISH_TIME;
@@ -205,6 +204,7 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
newSettings.publish_single = root["publish_single"] | EMSESP_DEFAULT_PUBLISH_SINGLE; newSettings.publish_single = root["publish_single"] | EMSESP_DEFAULT_PUBLISH_SINGLE;
newSettings.publish_single2cmd = root["publish_single2cmd"] | EMSESP_DEFAULT_PUBLISH_SINGLE2CMD; newSettings.publish_single2cmd = root["publish_single2cmd"] | EMSESP_DEFAULT_PUBLISH_SINGLE2CMD;
newSettings.send_response = root["send_response"] | EMSESP_DEFAULT_SEND_RESPONSE; newSettings.send_response = root["send_response"] | EMSESP_DEFAULT_SEND_RESPONSE;
newSettings.entity_format = root["entity_format"] | EMSESP_DEFAULT_ENTITY_FORMAT;
if (newSettings.enabled != settings.enabled) { if (newSettings.enabled != settings.enabled) {
changed = true; changed = true;

View File

@@ -57,10 +57,6 @@ static String generateClientId() {
#define FACTORY_MQTT_MAX_TOPIC_LENGTH 128 #define FACTORY_MQTT_MAX_TOPIC_LENGTH 128
#endif #endif
#ifndef FACTORY_MQTT_ENTITY_FORMAT
#define FACTORY_MQTT_ENTITY_FORMAT 1 // use shortnames
#endif
class MqttSettings { class MqttSettings {
public: public:
// host and port - if enabled // host and port - if enabled
@@ -79,9 +75,6 @@ class MqttSettings {
uint16_t keepAlive; uint16_t keepAlive;
bool cleanSession; bool cleanSession;
// multiple instances
uint8_t entity_format;
// proddy EMS-ESP specific // proddy EMS-ESP specific
String base; String base;
uint16_t publish_time_boiler; uint16_t publish_time_boiler;
@@ -99,6 +92,7 @@ class MqttSettings {
bool publish_single; bool publish_single;
bool publish_single2cmd; bool publish_single2cmd;
bool send_response; bool send_response;
uint8_t entity_format;
static void read(MqttSettings & settings, JsonObject & root); static void read(MqttSettings & settings, JsonObject & root);
static StateUpdateResult update(JsonObject & root, MqttSettings & settings); static StateUpdateResult update(JsonObject & root, MqttSettings & settings);

View File

@@ -1,5 +1,6 @@
# use like # strips out lines between two markers
# make clean; make ARGS=-DEMSESP_STANDALONE_DUMP; echo "test dump" | ./emsesp | python3 ./scripts/dump_entities.py # pipe a file into, for example:
# make clean; make ARGS=-DEMSESP_STANDALONE_DUMP; echo "test dump" | ./emsesp | python3 ./scripts/dump_entities.py
import fileinput import fileinput
with fileinput.input() as f_input: with fileinput.input() as f_input:

View File

@@ -2,4 +2,8 @@
# creates an CSV file called "dump_entities.cvs" with all devices and their entities # creates an CSV file called "dump_entities.cvs" with all devices and their entities
# run from top folder like `sh ./scripts/dump_entities.sh` # run from top folder like `sh ./scripts/dump_entities.sh`
make clean; make ARGS=-DEMSESP_STANDALONE_DUMP; echo "test dump" | ./emsesp | python3 ./scripts/dump_entities.py > dump_entities.csv rm -f dump_entities.csv
make clean;
make ARGS=-DEMSESP_STANDALONE_DUMP;
echo "test dump" | ./emsesp | python3 ./scripts/dump_entities.py > dump_entities.csv
cat dump_entities.csv

View File

@@ -209,6 +209,10 @@
#define EMSESP_DEFAULT_WEBLOG_COMPACT true #define EMSESP_DEFAULT_WEBLOG_COMPACT true
#endif #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 // matches Web UI settings
enum { enum {

View File

@@ -1063,7 +1063,8 @@ void EMSdevice::getCustomEntities(std::vector<std::string> & entity_ids) {
} }
#if defined(EMSESP_STANDALONE_DUMP) #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() { void EMSdevice::dump_value_info() {
for (auto & dv : devicevalues_) { for (auto & dv : devicevalues_) {
Serial.print(name_); Serial.print(name_);
@@ -1071,9 +1072,13 @@ void EMSdevice::dump_value_info() {
Serial.print(device_type_name().c_str()); Serial.print(device_type_name().c_str());
Serial.print(','); Serial.print(',');
Serial.print(product_id_);
Serial.print(',');
Serial.print(dv.short_name); Serial.print(dv.short_name);
Serial.print(','); Serial.print(',');
Serial.print(dv.get_fullname().c_str());
Serial.print(dv.fullname[0]);
Serial.print(','); Serial.print(',');
// type and optional enum values and min/max // type and optional enum values and min/max
@@ -1161,12 +1166,54 @@ void EMSdevice::dump_value_info() {
} }
Serial.print(","); Serial.print(",");
// readable, writeable, visible flags // writeable flag
Serial.print(!dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE) ? "true" : "false"); Serial.print(dv.has_cmd ? "true" : "false");
Serial.print(","); Serial.print(",");
Serial.print((dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY)) ? "true" : "false");
Serial.print(","); // MQTT Discovery entity name, assuming we're using the default v3.5 option
Serial.print(!dv.has_state(DeviceValueState::DV_WEB_EXCLUDE) ? "true" : "false"); 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(); Serial.println();
} }

View File

@@ -201,7 +201,9 @@ class EMSdevice {
bool get_value_info(JsonObject & root, const char * cmd, const int8_t id); bool get_value_info(JsonObject & root, const char * cmd, const int8_t id);
void get_dv_info(JsonObject & json); void get_dv_info(JsonObject & json);
#if defined(EMSESP_STANDALONE_DUMP)
void dump_value_info(); void dump_value_info();
#endif
enum OUTPUT_TARGET : uint8_t { API_VERBOSE, API_SHORTNAMES, MQTT, CONSOLE }; 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); 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) { void EMSESP::dump_all_values(uuid::console::Shell & shell) {
Serial.println("---- CSV START ----"); // marker use by py script Serial.println("---- CSV START ----"); // marker use by py script
// add header for CSV // 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(); Serial.println();
for (const auto & device_class : EMSFactory::device_handlers()) { 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 // 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); snprintf(uniq_id, sizeof(uniq_id), "%s_%s_%s", mqtt_basename_.c_str(), device_name, entity_with_tag);
} else if (Mqtt::entity_format() == 1) { } 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); snprintf(uniq_id, sizeof(uniq_id), "%s_%s", device_name, entity_with_tag);
} else { } else {
// entity_format is 0 // entity_format is 0, the old v3.4 style
// old v3.4 style // take en_name and replace all spaces
// take en_name and replace all spaces and lowercase it
char uniq_s[60]; char uniq_s[60];
strlcpy(uniq_s, en_name, sizeof(uniq_s)); strlcpy(uniq_s, en_name, sizeof(uniq_s));
Helpers::replace_char(uniq_s, ' ', '_'); Helpers::replace_char(uniq_s, ' ', '_');