Skip to content

Commit 70d4bcf

Browse files
committed
support String entities in Home Assistant #1373
1 parent 879df33 commit 70d4bcf

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/mqtt.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,11 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
848848

849849
bool set_ha_classes = false; // set to true if we want to set the state class and device class
850850

851-
// create the topic, depending on the type and whether the device entity is writable (a command)
851+
// create the topic
852+
// depending on the type and whether the device entity is writable (a command)
852853
// https://developers.home-assistant.io/docs/core/entity
853854
char topic[MQTT_TOPIC_MAX_SIZE];
854-
// if it's a command then we can use Number, Switch, Select. Otherwise stick to Sensor
855+
// if it's a command then we can use Number, Switch, Select or Text. Otherwise stick to Sensor
855856
if (has_cmd) {
856857
switch (type) {
857858
case DeviceValueType::INT:
@@ -878,6 +879,10 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
878879
snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
879880
set_ha_classes = true;
880881
break;
882+
case DeviceValueType::STRING:
883+
snprintf(topic, sizeof(topic), "text/%s", config_topic); // e.g. set_datetime, set_holiday, set_wwswitchtime
884+
set_ha_classes = true;
885+
break;
881886
default:
882887
// plain old sensor
883888
snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
@@ -911,20 +916,18 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
911916

912917
char sample_val[30] = "0"; // sample, correct(!) entity value, used only to prevent warning/error in HA if real value is not published yet
913918

914-
// handle commands, which are device entities that are writable
915-
// we add the command topic parameter
916-
// note: there is no way to handle strings in HA so datetimes (e.g. set_datetime, set_holiday, set_wwswitchtime etc) are excluded
919+
// we add the command topic parameter for commands
917920
if (has_cmd) {
918-
// command topic back to EMS-ESP
919921
char command_topic[MQTT_TOPIC_MAX_SIZE];
922+
// add command topic
920923
if (tag >= DeviceValueTAG::TAG_HC1) {
921924
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s/%s", mqtt_basename_.c_str(), device_name, EMSdevice::tag_to_mqtt(tag), entity);
922925
} else {
923926
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", mqtt_basename_.c_str(), device_name, entity);
924927
}
925928
doc["cmd_t"] = command_topic;
926929

927-
// for enums, add options
930+
// extend for enums, add options
928931
if (type == DeviceValueType::ENUM) {
929932
JsonArray option_list = doc.createNestedArray("ops"); // options
930933
if (EMSESP::system_.enum_format() == ENUM_FORMAT_INDEX) {
@@ -942,7 +945,7 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
942945
}
943946

944947
} else if (type != DeviceValueType::STRING && type != DeviceValueType::BOOL) {
945-
// Must be Numeric....
948+
// For numeric's add the range
946949
doc["mode"] = "box"; // auto, slider or box
947950
if (num_op > 0) {
948951
doc["step"] = 1.0 / num_op;

0 commit comments

Comments
 (0)