Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix entity category of Home Assistant exposed enum sensors #19474

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand All @@ -867,17 +867,17 @@ 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'},
motion_sensitivity: {entity_category: 'config', icon: 'mdi:tune'},
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'},
Expand All @@ -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}],
Expand All @@ -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') {
Copy link
Owner

@Koenkk Koenkk Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frenck if I understand correctly the logic is: only if type == sensor and entity_category == config, then entity_category should be set to diagnostic. If true, then I propose to remove this and the other similar if and instead change the end of the function to:

        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;

In this way, any future added sensors to other types are also covered.

discoveryEntry.discovery_payload.entity_category = 'diagnostic';
}

discoveryEntries.push(discoveryEntry);
}

/**
Expand Down