@@ -934,14 +934,12 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
934
934
char config_topic[70 ];
935
935
snprintf (config_topic, sizeof (config_topic), " %s/%s_%s/config" , mqtt_basename_.c_str (), device_name, entity_with_tag);
936
936
937
- bool add_ha_classes = true ; // default we'll add the "unit_of_meas", "stat_cla" and "dev_cla" attributes
938
-
939
937
// create the topic
940
- // depending on the type and whether the device entity is writable (a command)
938
+ // depending on the type and whether the device entity is writable (i.e. a command)
941
939
// https://developers.home-assistant.io/docs/core/entity
942
- char topic[MQTT_TOPIC_MAX_SIZE];
943
- // if it's a command then we can use Number, Switch, Select or Text. Otherwise stick to Sensor
940
+ char topic[MQTT_TOPIC_MAX_SIZE] = {0 };
944
941
if (has_cmd) {
942
+ // if it's a command then we can use Number, Switch, Select or Text. Otherwise stick to Sensor
945
943
switch (type) {
946
944
case DeviceValueType::INT8:
947
945
case DeviceValueType::UINT8:
@@ -950,45 +948,40 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
950
948
case DeviceValueType::UINT24:
951
949
case DeviceValueType::UINT32:
952
950
// number - https://www.home-assistant.io/integrations/number.mqtt
953
- // older Domoticz does not support number, use sensor
951
+ // older Domoticz does not support number, will default to Sensor
954
952
if (discovery_type () == discoveryType::HOMEASSISTANT || discovery_type () == discoveryType::DOMOTICZ_LATEST) {
955
953
snprintf (topic, sizeof (topic), " number/%s" , config_topic);
956
- } else {
957
- snprintf (topic, sizeof (topic), " sensor/%s" , config_topic);
958
954
}
959
955
break ;
960
956
case DeviceValueType::BOOL:
961
957
// switch - https://www.home-assistant.io/integrations/switch.mqtt
962
958
snprintf (topic, sizeof (topic), " switch/%s" , config_topic);
963
- add_ha_classes = false ;
964
959
break ;
965
960
case DeviceValueType::ENUM:
961
+ // select - https://www.home-assistant.io/integrations/select.mqtt
966
962
snprintf (topic, sizeof (topic), " select/%s" , config_topic);
967
- add_ha_classes = false ;
968
963
break ;
969
- case DeviceValueType::CMD: // hardcoded commands are always ENUMS
970
- // select - https://www.home-assistant.io/integrations/select.mqtt
964
+ case DeviceValueType::CMD:
971
965
if (uom == DeviceValueUOM::NONE) {
972
- snprintf (topic, sizeof (topic), " select/%s" , config_topic);
966
+ snprintf (topic, sizeof (topic), " select/%s" , config_topic); // hardcoded commands are always ENUMS
973
967
} else if (discovery_type () == discoveryType::HOMEASSISTANT || discovery_type () == discoveryType::DOMOTICZ_LATEST) {
974
968
snprintf (topic, sizeof (topic), " number/%s" , config_topic);
975
- } else {
976
- snprintf (topic, sizeof (topic), " sensor/%s" , config_topic);
977
969
}
978
- add_ha_classes = false ;
979
970
break ;
980
971
case DeviceValueType::STRING:
981
972
// text - https://www.home-assistant.io/integrations/text.mqtt
982
- snprintf (topic, sizeof (topic), " text/%s" , config_topic); // e.g. set_datetime, set_holiday, set_wwswitchtime
983
- add_ha_classes = false ;
973
+ // Domoticz does not support text, will default to Sensor
974
+ if (discovery_type () == discoveryType::HOMEASSISTANT) {
975
+ snprintf (topic, sizeof (topic), " text/%s" , config_topic); // e.g. set_datetime, set_holiday, set_wwswitchtime
976
+ }
984
977
break ;
985
978
default :
986
- // plain old sensor
987
- snprintf (topic, sizeof (topic), " sensor/%s" , config_topic);
988
979
break ;
989
980
}
990
- } else {
991
- // it is not a command and a read-only sensor. Use then either sensor or binary_sensor
981
+ }
982
+
983
+ // if at this point we don't have a topic created yet, create a default sensor one. We always need a topic.
984
+ if (strlen (topic) == 0 ) {
992
985
snprintf (topic, sizeof (topic), (type == DeviceValueType::BOOL) ? " binary_sensor/%s" : " sensor/%s" , config_topic); // binary sensor (for booleans)
993
986
}
994
987
@@ -1054,21 +1047,6 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
1054
1047
doc[" max" ] = dv_set_max;
1055
1048
snprintf (sample_val, sizeof (sample_val), " %i" , dv_set_min);
1056
1049
}
1057
-
1058
- // set icons
1059
- // since these don't have a device class we need to add the icon ourselves
1060
- switch (uom) {
1061
- case DeviceValueUOM::DEGREES:
1062
- case DeviceValueUOM::DEGREES_R:
1063
- case DeviceValueUOM::K:
1064
- doc[" ic" ] = F_ (icondegrees);
1065
- break ;
1066
- case DeviceValueUOM::PERCENT:
1067
- doc[" ic" ] = F_ (iconpercent);
1068
- break ;
1069
- default :
1070
- break ;
1071
- }
1072
1050
}
1073
1051
1074
1052
// friendly name = <tag> <name>
@@ -1129,26 +1107,23 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
1129
1107
// Domoticz doesn't support value templates, so we just use the value directly
1130
1108
// Also omit the uom and other state classes
1131
1109
doc[" val_tpl" ] = (std::string) " {{" + val_obj + " }}" ;
1132
- // add_ha_classes = false; // don't add the classes, categories of uom (dev_cla, stat_cla)
1133
1110
}
1134
1111
}
1135
1112
1136
- // Add the state class, device class and sometimes the icon.
1137
- // Used only for read-only sensors like Sensor and Binary Sensor but also Numbers
1138
- if (add_ha_classes) {
1139
- // first set the catagory for System entities
1140
- // https://github.com/emsesp/EMS-ESP32/discussions/1459#discussioncomment-7694873
1141
- if (device_type == EMSdevice::DeviceType::SYSTEM) {
1142
- doc[" ent_cat" ] = " diagnostic" ;
1143
- }
1144
- add_ha_uom (doc.as <JsonObject>(), type, uom, entity); // add the UoM, device and state class
1113
+ // Add the state class, device class and an optional icon based on the uom
1114
+ // first set the catagory for System entities
1115
+ // https://github.com/emsesp/EMS-ESP32/discussions/1459#discussioncomment-7694873
1116
+ if (device_type == EMSdevice::DeviceType::SYSTEM) {
1117
+ doc[" ent_cat" ] = " diagnostic" ; // instead of config
1145
1118
}
1119
+ add_ha_uom (doc.as <JsonObject>(), type, uom, entity);
1146
1120
1147
1121
doc[" dev" ] = dev_json;
1148
1122
1149
1123
return queue_ha (topic, doc.as <JsonObject>());
1150
1124
}
1151
1125
1126
+ // Add the state class, device class and an optional icon based on the uom
1152
1127
void Mqtt::add_ha_uom (JsonObject doc, const uint8_t type, const uint8_t uom, const char * entity) {
1153
1128
const char * dc_ha = " dev_cla" ; // device class
1154
1129
const char * sc_ha = " stat_cla" ; // state class
@@ -1169,16 +1144,19 @@ void Mqtt::add_ha_uom(JsonObject doc, const uint8_t type, const uint8_t uom, con
1169
1144
}
1170
1145
1171
1146
// set state and device class
1147
+ // also icon, when there is no device class that sets one
1172
1148
switch (uom) {
1173
1149
case DeviceValueUOM::DEGREES:
1174
1150
case DeviceValueUOM::DEGREES_R:
1175
1151
case DeviceValueUOM::K:
1176
1152
doc[sc_ha] = F_ (measurement);
1177
1153
doc[dc_ha] = " temperature" ;
1154
+ doc[" ic" ] = F_ (icondegrees); // icon
1178
1155
break ;
1179
1156
case DeviceValueUOM::PERCENT:
1180
1157
doc[sc_ha] = F_ (measurement);
1181
1158
doc[dc_ha] = " power_factor" ;
1159
+ doc[" ic" ] = F_ (iconpercent); // icon
1182
1160
break ;
1183
1161
case DeviceValueUOM::SECONDS:
1184
1162
case DeviceValueUOM::MINUTES:
@@ -1256,8 +1234,6 @@ void Mqtt::add_ha_uom(JsonObject doc, const uint8_t type, const uint8_t uom, con
1256
1234
}
1257
1235
1258
1236
bool Mqtt::publish_ha_climate_config (const int8_t tag, const bool has_roomtemp, const bool remove, const int16_t min, const uint32_t max) {
1259
- // TODO: check if Domoticz supports climate via MQTT discovery, otherwise exit this function if (discovery_type() != discoveryType::HOMEASSISTANT
1260
-
1261
1237
uint8_t hc_num = tag;
1262
1238
1263
1239
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
0 commit comments