Skip to content

Commit

Permalink
changes to #828
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Dec 28, 2022
1 parent 5d45064 commit 43b4adc
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 34 deletions.
24 changes: 12 additions & 12 deletions lib/framework/MqttSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,17 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
MqttSettings newSettings = {};
bool changed = false;

newSettings.enabled = root["enabled"] | FACTORY_MQTT_ENABLED;
newSettings.host = root["host"] | FACTORY_MQTT_HOST;
newSettings.port = root["port"] | FACTORY_MQTT_PORT;
newSettings.base = root["base"] | FACTORY_MQTT_BASE;
newSettings.username = root["username"] | FACTORY_MQTT_USERNAME;
newSettings.password = root["password"] | FACTORY_MQTT_PASSWORD;
newSettings.clientId = root["client_id"] | FACTORY_MQTT_CLIENT_ID;
newSettings.keepAlive = root["keep_alive"] | FACTORY_MQTT_KEEP_ALIVE;
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_retain = root["mqtt_retain"] | EMSESP_DEFAULT_MQTT_RETAIN;
newSettings.enabled = root["enabled"] | FACTORY_MQTT_ENABLED;
newSettings.host = root["host"] | FACTORY_MQTT_HOST;
newSettings.port = root["port"] | FACTORY_MQTT_PORT;
newSettings.base = root["base"] | FACTORY_MQTT_BASE;
newSettings.username = root["username"] | FACTORY_MQTT_USERNAME;
newSettings.password = root["password"] | FACTORY_MQTT_PASSWORD;
newSettings.clientId = root["client_id"] | FACTORY_MQTT_CLIENT_ID;
newSettings.keepAlive = root["keep_alive"] | FACTORY_MQTT_KEEP_ALIVE;
newSettings.cleanSession = root["clean_session"] | FACTORY_MQTT_CLEAN_SESSION;
newSettings.mqtt_qos = root["mqtt_qos"] | EMSESP_DEFAULT_MQTT_QOS;
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_thermostat = root["publish_time_thermostat"] | EMSESP_DEFAULT_PUBLISH_TIME;
Expand All @@ -205,6 +204,7 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
newSettings.publish_single = root["publish_single"] | EMSESP_DEFAULT_PUBLISH_SINGLE;
newSettings.publish_single2cmd = root["publish_single2cmd"] | EMSESP_DEFAULT_PUBLISH_SINGLE2CMD;
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) {
changed = true;
Expand Down
8 changes: 1 addition & 7 deletions lib/framework/MqttSettingsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ static String generateClientId() {
#define FACTORY_MQTT_MAX_TOPIC_LENGTH 128
#endif

#ifndef FACTORY_MQTT_ENTITY_FORMAT
#define FACTORY_MQTT_ENTITY_FORMAT 1 // use shortnames
#endif

class MqttSettings {
public:
// host and port - if enabled
Expand All @@ -79,9 +75,6 @@ class MqttSettings {
uint16_t keepAlive;
bool cleanSession;

// multiple instances
uint8_t entity_format;

// proddy EMS-ESP specific
String base;
uint16_t publish_time_boiler;
Expand All @@ -99,6 +92,7 @@ class MqttSettings {
bool publish_single;
bool publish_single2cmd;
bool send_response;
uint8_t entity_format;

static void read(MqttSettings & settings, JsonObject & root);
static StateUpdateResult update(JsonObject & root, MqttSettings & settings);
Expand Down
5 changes: 3 additions & 2 deletions scripts/dump_entities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# use like
# make clean; make ARGS=-DEMSESP_STANDALONE_DUMP; echo "test dump" | ./emsesp | python3 ./scripts/dump_entities.py
# strips out lines between two markers
# pipe a file into, for example:
# make clean; make ARGS=-DEMSESP_STANDALONE_DUMP; echo "test dump" | ./emsesp | python3 ./scripts/dump_entities.py

import fileinput
with fileinput.input() as f_input:
Expand Down
6 changes: 5 additions & 1 deletion scripts/dump_entities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

# creates an CSV file called "dump_entities.cvs" with all devices and their entities
# 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
4 changes: 4 additions & 0 deletions src/default_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
61 changes: 54 additions & 7 deletions src/emsdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,17 +1063,22 @@ 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_);
Serial.print(',');
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
Expand Down Expand Up @@ -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");
Serial.print(",");
Serial.print((dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY)) ? "true" : "false");
// writeable flag
Serial.print(dv.has_cmd ? "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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/emsdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/emsesp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
7 changes: 3 additions & 4 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ' ', '_');
Expand Down

0 comments on commit 43b4adc

Please sign in to comment.