Skip to content

Commit cf27d0e

Browse files
1technophileclaude
andauthored
[BT] Omit state_class/unit_of_measurement for enum sensors in HA discovery (#2316)
Home Assistant rejects an MQTT discovery payload that carries `options` (an enum sensor) together with `state_class` or `unit_of_measurement`: these fields are mutually exclusive. createDiscovery() was emitting all three for Mi Body Composition Scale (XMTZC0x) weighing_mode/unit sensors, so HA discarded the discovery message. Gate the stat_cla and unit_of_meas writes on enum_options == nullptr so enum sensors no longer carry the conflicting fields. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6d64dbc commit cf27d0e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

main/mqttDiscovery.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,10 @@ void createDiscovery(const char* sensor_type,
504504
}
505505
}
506506

507-
if (unit_of_meas && unit_of_meas[0]) {
507+
// HA rejects a discovery payload that carries `options` (enum sensor) together
508+
// with `unit_of_measurement` or `state_class` - they are mutually exclusive.
509+
// Skip unit_of_measurement (and state_class below) when this is an enum sensor.
510+
if (enum_options == nullptr && unit_of_meas && unit_of_meas[0]) {
508511
// We check if the class belongs to HAAS units list
509512
int num_units = sizeof(availableHASSUnits) / sizeof(availableHASSUnits[0]);
510513
for (int i = 0; i < num_units; i++) { // see units list and size into config_mqttDiscovery.h
@@ -578,8 +581,8 @@ void createDiscovery(const char* sensor_type,
578581
sensor["pl_avail"] = payload_available; // payload_on
579582
if (payload_not_available[0])
580583
sensor["pl_not_avail"] = payload_not_available; //payload_off
581-
if (state_class && state_class[0])
582-
sensor["stat_cla"] = state_class; //add the state class on the sensors ( https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes )
584+
if (enum_options == nullptr && state_class && state_class[0])
585+
sensor["stat_cla"] = state_class; //add the state class on the sensors ( https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes ) - omitted for enum sensors (mutually exclusive with `options`)
583586
if (state_on != nullptr)
584587
if (strcmp(state_on, "true") == 0) {
585588
sensor["stat_on"] = true;

0 commit comments

Comments
 (0)