From 31913c035c73e282c5aca0a2019f7d1d7eab0ac2 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 29 Oct 2023 15:15:16 +0100 Subject: [PATCH 1/2] Fix entity category of Home Assistant exposed enum sensors --- lib/extension/homeassistant.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/extension/homeassistant.ts b/lib/extension/homeassistant.ts index 9a651beb67..d6974222d8 100644 --- a/lib/extension/homeassistant.ts +++ b/lib/extension/homeassistant.ts @@ -855,8 +855,8 @@ export default class HomeAssistant extends Extension { } else if (firstExpose.type === 'enum') { const lookup: {[s: string]: KeyValue} = { action: {icon: 'mdi:gesture-double-tap'}, - alarm_humidity: {icon: 'mdi:water-percent-alert'}, - alarm_temperature: {icon: 'mdi:thermometer-alert'}, + alarm_humidity: {entity_category: 'config', icon: 'mdi:water-percent-alert'}, + alarm_temperature: {entity_category: 'config', icon: 'mdi:thermometer-alert'}, backlight_auto_dim: {entity_category: 'config', icon: 'mdi:brightness-auto'}, backlight_mode: {entity_category: 'config', icon: 'mdi:lightbulb'}, color_power_on_behavior: {entity_category: 'config', icon: 'mdi:palette'}, @@ -867,8 +867,8 @@ export default class HomeAssistant extends Extension { keep_time: {entity_category: 'config', icon: 'mdi:av-timer'}, keypad_lockout: {entity_category: 'config', icon: 'mdi:lock'}, load_detection_mode: {entity_category: 'config', icon: 'mdi:tune'}, - load_dimmable: {entity_category: 'diagnostic', icon: 'mdi:chart-bell-curve'}, - load_type: {entity_category: 'diagnostic', icon: 'mdi:led-on'}, + load_dimmable: {entity_category: 'config', icon: 'mdi:chart-bell-curve'}, + load_type: {entity_category: 'config', icon: 'mdi:led-on'}, melody: {entity_category: 'config', icon: 'mdi:music-note'}, mode_phase_control: {entity_category: 'config', icon: 'mdi:tune'}, mode: {entity_category: 'config', icon: 'mdi:tune'}, @@ -876,8 +876,8 @@ export default class HomeAssistant extends Extension { operation_mode: {entity_category: 'config', icon: 'mdi:tune'}, power_on_behavior: {entity_category: 'config', icon: 'mdi:power-settings'}, power_outage_memory: {entity_category: 'config', icon: 'mdi:power-settings'}, - power_supply_mode: {entity_category: 'diagnostic', icon: 'mdi:power-settings'}, - power_type: {entity_category: 'diagnostic', icon: 'mdi:lightning-bolt-circle'}, + power_supply_mode: {entity_category: 'config', icon: 'mdi:power-settings'}, + power_type: {entity_category: 'config', icon: 'mdi:lightning-bolt-circle'}, sensitivity: {entity_category: 'config', icon: 'mdi:tune'}, sensors_type: {entity_category: 'config', icon: 'mdi:tune'}, sound_volume: {entity_category: 'config', icon: 'mdi:volume-high'}, @@ -893,7 +893,7 @@ export default class HomeAssistant extends Extension { `{{ value_json.${firstExpose.property} }}` : undefined; if (firstExpose.access & ACCESS_STATE) { - discoveryEntries.push({ + const discoveryEntry: DiscoveryEntry = { type: 'sensor', object_id: firstExpose.property, mockProperties: [{property: firstExpose.property, value: null}], @@ -903,7 +903,15 @@ export default class HomeAssistant extends Extension { enabled_by_default: !(firstExpose.access & ACCESS_SET), ...lookup[firstExpose.name], }, - }); + }; + + // If it has an entity category of config, but exposed as sensor, then change + // it to diagnostic. Sensors have no input, so can't be configured. + if (discoveryEntry.discovery_payload.entity_category === 'config') { + discoveryEntry.discovery_payload.entity_category = 'diagnostic'; + } + + discoveryEntries.push(discoveryEntry); } /** From abc7f7d02fa1863899155e40eb23c1ba0adec493 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 30 Oct 2023 21:18:51 +0100 Subject: [PATCH 2/2] Process review comment --- lib/extension/homeassistant.ts | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/extension/homeassistant.ts b/lib/extension/homeassistant.ts index d6974222d8..3cd74d18bd 100644 --- a/lib/extension/homeassistant.ts +++ b/lib/extension/homeassistant.ts @@ -792,12 +792,6 @@ export default class HomeAssistant extends Extension { }, }; - // If it has an entity category of config, but exposed as sensor, then change - // it to diagnostic. Sensors have no input, so can't be configured. - if (discoveryEntry.discovery_payload.entity_category === 'config') { - discoveryEntry.discovery_payload.entity_category = 'diagnostic'; - } - // When a device_class is set, unit_of_measurement must be set, otherwise warnings are generated. // https://github.com/Koenkk/zigbee2mqtt/issues/15958#issuecomment-1377483202 if (discoveryEntry.discovery_payload.device_class && @@ -893,7 +887,7 @@ export default class HomeAssistant extends Extension { `{{ value_json.${firstExpose.property} }}` : undefined; if (firstExpose.access & ACCESS_STATE) { - const discoveryEntry: DiscoveryEntry = { + discoveryEntries.push({ type: 'sensor', object_id: firstExpose.property, mockProperties: [{property: firstExpose.property, value: null}], @@ -903,15 +897,7 @@ export default class HomeAssistant extends Extension { enabled_by_default: !(firstExpose.access & ACCESS_SET), ...lookup[firstExpose.name], }, - }; - - // If it has an entity category of config, but exposed as sensor, then change - // it to diagnostic. Sensors have no input, so can't be configured. - if (discoveryEntry.discovery_payload.entity_category === 'config') { - discoveryEntry.discovery_payload.entity_category = 'diagnostic'; - } - - discoveryEntries.push(discoveryEntry); + }); } /** @@ -979,6 +965,15 @@ export default class HomeAssistant extends Extension { throw new Error(`Unsupported exposes type: '${firstExpose.type}'`); } + discoveryEntries.forEach((d) => { + // If a sensor has entity category `config`, then change + // it to `diagnostic`. Sensors have no input, so can't be configured. + // https://github.com/Koenkk/zigbee2mqtt/pull/19474 + if (d.type === 'sensor' && d.discovery_payload.entity_category === 'config') { + d.discovery_payload.entity_category = 'diagnostic'; + } + }); + return discoveryEntries; }