add HA classes to Numbers - Missing attributes in MQTT Discovery Message #2149

This commit is contained in:
proddy
2024-10-28 11:56:58 +01:00
parent eed676f521
commit 7598290c5f

View File

@@ -934,7 +934,7 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
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);
bool readonly_sensors = true; bool add_ha_classes = true; // default we'll add the "unit_of_meas", "stat_cla" and "dev_cla" attributes
// create the topic // create the topic
// depending on the type and whether the device entity is writable (a command) // depending on the type and whether the device entity is writable (a command)
@@ -953,7 +953,6 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
// older Domoticz does not support number, use sensor // older Domoticz does not support number, use sensor
if (discovery_type() == discoveryType::HOMEASSISTANT || discovery_type() == discoveryType::DOMOTICZ_LATEST) { if (discovery_type() == discoveryType::HOMEASSISTANT || discovery_type() == discoveryType::DOMOTICZ_LATEST) {
snprintf(topic, sizeof(topic), "number/%s", config_topic); snprintf(topic, sizeof(topic), "number/%s", config_topic);
readonly_sensors = false;
} else { } else {
snprintf(topic, sizeof(topic), "sensor/%s", config_topic); snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
} }
@@ -961,11 +960,11 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
case DeviceValueType::BOOL: case DeviceValueType::BOOL:
// switch - https://www.home-assistant.io/integrations/switch.mqtt // switch - https://www.home-assistant.io/integrations/switch.mqtt
snprintf(topic, sizeof(topic), "switch/%s", config_topic); snprintf(topic, sizeof(topic), "switch/%s", config_topic);
readonly_sensors = false; add_ha_classes = false;
break; break;
case DeviceValueType::ENUM: case DeviceValueType::ENUM:
snprintf(topic, sizeof(topic), "select/%s", config_topic); snprintf(topic, sizeof(topic), "select/%s", config_topic);
readonly_sensors = false; add_ha_classes = false;
break; break;
case DeviceValueType::CMD: // hardcoded commands are always ENUMS case DeviceValueType::CMD: // hardcoded commands are always ENUMS
// select - https://www.home-assistant.io/integrations/select.mqtt // select - https://www.home-assistant.io/integrations/select.mqtt
@@ -976,22 +975,19 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
} else { } else {
snprintf(topic, sizeof(topic), "sensor/%s", config_topic); snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
} }
readonly_sensors = false; add_ha_classes = false;
break; break;
case DeviceValueType::STRING: case DeviceValueType::STRING:
// text - https://www.home-assistant.io/integrations/text.mqtt // text - https://www.home-assistant.io/integrations/text.mqtt
snprintf(topic, sizeof(topic), "text/%s", config_topic); // e.g. set_datetime, set_holiday, set_wwswitchtime snprintf(topic, sizeof(topic), "text/%s", config_topic); // e.g. set_datetime, set_holiday, set_wwswitchtime
readonly_sensors = false; add_ha_classes = false;
break; break;
default: default:
// plain old sensor, and make it read-only // plain old sensor, and make it read-only
break; break;
} }
} } else {
// it is not a command and a read-only sensor. Use then either sensor or binary_sensor
// For read-only sensors there are either sensor or binary_sensor
// for both we also set the device class and state class
if (readonly_sensors) {
snprintf(topic, sizeof(topic), (type == DeviceValueType::BOOL) ? "binary_sensor/%s" : "sensor/%s", config_topic); // binary sensor (for booleans) snprintf(topic, sizeof(topic), (type == DeviceValueType::BOOL) ? "binary_sensor/%s" : "sensor/%s", config_topic); // binary sensor (for booleans)
} }
@@ -1127,8 +1123,9 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
add_ha_sections_to_doc(nullptr, stat_t, doc, false, val_cond); // no name, since the "dev" has already been adde add_ha_sections_to_doc(nullptr, stat_t, doc, false, val_cond); // no name, since the "dev" has already been adde
} }
// Add the state class, device class and sometimes the icon. Used only for read-only sensors like Sensor and Binary Sensor // Add the state class, device class and sometimes the icon.
if (readonly_sensors) { // Used only for read-only sensors like Sensor and Binary Sensor but also Numbers
if (add_ha_classes) {
// first set the catagory for System entities // first set the catagory for System entities
// https://github.com/emsesp/EMS-ESP32/discussions/1459#discussioncomment-7694873 // https://github.com/emsesp/EMS-ESP32/discussions/1459#discussioncomment-7694873
if (device_type == EMSdevice::DeviceType::SYSTEM) { if (device_type == EMSdevice::DeviceType::SYSTEM) {