Shorten "friendly names" in Home Assistant

see #555
This commit is contained in:
pswid
2022-06-21 20:28:22 +02:00
parent 1fab47acd5
commit 1c48d12167
3 changed files with 28 additions and 10 deletions

View File

@@ -403,7 +403,10 @@ void AnalogSensor::publish_values(const bool force) {
snprintf(str, sizeof(str), "{{value_json['%d'].value}}", sensor.gpio()); snprintf(str, sizeof(str), "{{value_json['%d'].value}}", sensor.gpio());
config["val_tpl"] = str; config["val_tpl"] = str;
snprintf(str, sizeof(str), "Analog Sensor %s", sensor.name().c_str()); snprintf(str, sizeof(str), "analog_sensor_%s", sensor.name().c_str());
config["object_id"] = str;
snprintf(str, sizeof(str), "%s", sensor.name().c_str());
config["name"] = str; config["name"] = str;
snprintf(str, sizeof(str), "analogsensor_%d", sensor.gpio()); snprintf(str, sizeof(str), "analogsensor_%d", sensor.gpio());

View File

@@ -497,7 +497,10 @@ void DallasSensor::publish_values(const bool force) {
snprintf(str, sizeof(str), "{{value_json['%s'].temp}}", sensor.id().c_str()); snprintf(str, sizeof(str), "{{value_json['%s'].temp}}", sensor.id().c_str());
config["val_tpl"] = str; config["val_tpl"] = str;
snprintf(str, sizeof(str), "Temperature Sensor %s", sensor.name().c_str()); snprintf(str, sizeof(str), "temperature_sensor_%s", sensor.name().c_str());
config["object_id"] = str;
snprintf(str, sizeof(str), "%s", sensor.name().c_str());
config["name"] = str; config["name"] = str;
snprintf(str, sizeof(str), "dallasensor_%s", sensor.id().c_str()); snprintf(str, sizeof(str), "dallasensor_%s", sensor.id().c_str());

View File

@@ -629,6 +629,7 @@ void Mqtt::ha_status() {
// doc["avty_t"] = FJSON("~/status"); // commented out, as it causes errors in HA sometimes // doc["avty_t"] = FJSON("~/status"); // commented out, as it causes errors in HA sometimes
// doc["json_attr_t"] = FJSON("~/heartbeat"); // store also as HA attributes // doc["json_attr_t"] = FJSON("~/heartbeat"); // store also as HA attributes
doc["stat_t"] = FJSON("~/heartbeat"); doc["stat_t"] = FJSON("~/heartbeat");
doc["object_id"] = FJSON("ems_esp_status");
doc["name"] = FJSON("EMS-ESP status"); doc["name"] = FJSON("EMS-ESP status");
doc["ic"] = F_(icondevice); doc["ic"] = F_(icondevice);
doc["val_tpl"] = FJSON("{{value_json['bus_status']}}"); doc["val_tpl"] = FJSON("{{value_json['bus_status']}}");
@@ -1104,15 +1105,23 @@ void Mqtt::publish_ha_sensor_config(uint8_t type,
snprintf(stat_t, sizeof(stat_t), "~/%s", tag_to_topic(device_type, tag).c_str()); snprintf(stat_t, sizeof(stat_t), "~/%s", tag_to_topic(device_type, tag).c_str());
doc["stat_t"] = stat_t; doc["stat_t"] = stat_t;
// name = <device> <tag> <name> // friendly name = <tag> <name>
char new_name[80]; char short_name[70];
if (have_tag) { if (have_tag) {
snprintf(new_name, sizeof(new_name), "%s %s %s", device_name, EMSdevice::tag_to_string(tag).c_str(), read_flash_string(name).c_str()); snprintf(short_name, sizeof(short_name), "%s %s", EMSdevice::tag_to_string(tag).c_str(), read_flash_string(name).c_str());
} else { } else {
snprintf(new_name, sizeof(new_name), "%s %s", device_name, read_flash_string(name).c_str()); snprintf(short_name, sizeof(short_name), "%s", read_flash_string(name).c_str());
} }
new_name[0] = toupper(new_name[0]); // capitalize first letter
doc["name"] = new_name; // entity id = emsesp_<device>_<tag>_<name>
char long_name[130];
snprintf(long_name, sizeof(long_name), "%s_%s", device_name, short_name);
// snprintf(long_name, sizeof(long_name), "emsesp_%s_%s", device_name, short_name); //wouldn't it be better?
doc["object_id"] = long_name;
// name (friendly name) = <tag> <name>
short_name[0] = toupper(short_name[0]); // capitalize first letter
doc["name"] = short_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
@@ -1240,7 +1249,8 @@ void Mqtt::publish_ha_climate_config(uint8_t tag, bool has_roomtemp, bool remove
char seltemp_s[30]; char seltemp_s[30];
char currtemp_s[30]; char currtemp_s[30];
char mode_str_tpl[400]; char mode_str_tpl[400];
char name_s[30]; char name_s[10];
char id_s[20];
char uniq_id_s[30]; char uniq_id_s[30];
char temp_cmd_s[30]; char temp_cmd_s[30];
char mode_cmd_s[30]; char mode_cmd_s[30];
@@ -1279,7 +1289,8 @@ void Mqtt::publish_ha_climate_config(uint8_t tag, bool has_roomtemp, bool remove
hc_mode_s, hc_mode_s,
hc_mode_s); hc_mode_s);
snprintf(name_s, sizeof(name_s), "Thermostat hc%d", hc_num); snprintf(id_s, sizeof(id_s), "thermostat_hc%d", hc_num);
snprintf(name_s, sizeof(name_s), "Hc%d", hc_num);
snprintf(uniq_id_s, sizeof(uniq_id_s), "thermostat_hc%d", hc_num); snprintf(uniq_id_s, sizeof(uniq_id_s), "thermostat_hc%d", hc_num);
snprintf(temp_cmd_s, sizeof(temp_cmd_s), "~/thermostat/hc%d/seltemp", hc_num); snprintf(temp_cmd_s, sizeof(temp_cmd_s), "~/thermostat/hc%d/seltemp", hc_num);
snprintf(mode_cmd_s, sizeof(temp_cmd_s), "~/thermostat/hc%d/mode", hc_num); snprintf(mode_cmd_s, sizeof(temp_cmd_s), "~/thermostat/hc%d/mode", hc_num);
@@ -1287,6 +1298,7 @@ void Mqtt::publish_ha_climate_config(uint8_t tag, bool has_roomtemp, bool remove
StaticJsonDocument<EMSESP_JSON_SIZE_HA_CONFIG> doc; StaticJsonDocument<EMSESP_JSON_SIZE_HA_CONFIG> doc;
doc["~"] = base(); doc["~"] = base();
doc["object_id"] = id_s;
doc["name"] = name_s; doc["name"] = name_s;
doc["uniq_id"] = uniq_id_s; doc["uniq_id"] = uniq_id_s;
doc["mode_stat_t"] = topic_t; doc["mode_stat_t"] = topic_t;