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 device integration filter for entityless devices #21136

Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion src/components/ha-selector/ha-selector-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
fetchEntitySourcesWithCache,
} from "../../data/entity_sources";
import type { AreaSelector } from "../../data/selector";
import { ConfigEntry, getConfigEntries } from "../../data/config_entries";
import {
filterSelectorDevices,
filterSelectorEntities,
Expand All @@ -37,6 +38,8 @@ export class HaAreaSelector extends LitElement {

@state() private _entitySources?: EntitySources;

@state() private _configEntries?: ConfigEntry[];

private _deviceIntegrationLookup = memoizeOne(getDeviceIntegrationLookup);

private _hasIntegration(selector: AreaSelector) {
Expand Down Expand Up @@ -72,6 +75,12 @@ export class HaAreaSelector extends LitElement {
this._entitySources = sources;
});
}
if (!this._configEntries && this._hasIntegration(this.selector)) {
this._configEntries = [];
getConfigEntries(this.hass).then((entries) => {
this._configEntries = entries;
});
}
}

protected render() {
Expand Down Expand Up @@ -136,7 +145,9 @@ export class HaAreaSelector extends LitElement {
const deviceIntegrations = this._entitySources
? this._deviceIntegrationLookup(
this._entitySources,
Object.values(this.hass.entities)
Object.values(this.hass.entities),
Object.values(this.hass.devices),
this._configEntries
)
: undefined;

Expand Down
13 changes: 12 additions & 1 deletion src/components/ha-selector/ha-selector-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
fetchEntitySourcesWithCache,
} from "../../data/entity_sources";
import type { DeviceSelector } from "../../data/selector";
import { ConfigEntry, getConfigEntries } from "../../data/config_entries";
import {
filterSelectorDevices,
filterSelectorEntities,
Expand All @@ -27,6 +28,8 @@ export class HaDeviceSelector extends LitElement {

@state() private _entitySources?: EntitySources;

@state() private _configEntries?: ConfigEntry[];

@property() public value?: any;
karwosts marked this conversation as resolved.
Show resolved Hide resolved

@property() public label?: string;
Expand Down Expand Up @@ -75,6 +78,12 @@ export class HaDeviceSelector extends LitElement {
this._entitySources = sources;
});
}
if (!this._configEntries && this._hasIntegration(this.selector)) {
this._configEntries = [];
getConfigEntries(this.hass).then((entries) => {
this._configEntries = entries;
});
}
}

protected render() {
Expand Down Expand Up @@ -123,7 +132,9 @@ export class HaDeviceSelector extends LitElement {
const deviceIntegrations = this._entitySources
? this._deviceIntegrationLookup(
this._entitySources,
Object.values(this.hass.entities)
Object.values(this.hass.entities),
Object.values(this.hass.devices),
this._configEntries
)
: undefined;

Expand Down
13 changes: 12 additions & 1 deletion src/components/ha-selector/ha-selector-floor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
fetchEntitySourcesWithCache,
} from "../../data/entity_sources";
import type { FloorSelector } from "../../data/selector";
import { ConfigEntry, getConfigEntries } from "../../data/config_entries";
import {
filterSelectorDevices,
filterSelectorEntities,
Expand All @@ -37,6 +38,8 @@ export class HaFloorSelector extends LitElement {

@state() private _entitySources?: EntitySources;

@state() private _configEntries?: ConfigEntry[];

private _deviceIntegrationLookup = memoizeOne(getDeviceIntegrationLookup);

private _hasIntegration(selector: FloorSelector) {
Expand Down Expand Up @@ -72,6 +75,12 @@ export class HaFloorSelector extends LitElement {
this._entitySources = sources;
});
}
if (!this._configEntries && this._hasIntegration(this.selector)) {
this._configEntries = [];
getConfigEntries(this.hass).then((entries) => {
this._configEntries = entries;
});
}
}

protected render() {
Expand Down Expand Up @@ -136,7 +145,9 @@ export class HaFloorSelector extends LitElement {
const deviceIntegrations = this._entitySources
? this._deviceIntegrationLookup(
this._entitySources,
Object.values(this.hass.entities)
Object.values(this.hass.entities),
Object.values(this.hass.devices),
this._configEntries
)
: undefined;

Expand Down
27 changes: 21 additions & 6 deletions src/data/device_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
EntityRegistryDisplayEntry,
EntityRegistryEntry,
} from "./entity_registry";
import { ConfigEntry } from "./config_entries";
import type { EntitySources } from "./entity_sources";

export {
Expand Down Expand Up @@ -142,20 +143,34 @@ export const getDeviceEntityDisplayLookup = (

export const getDeviceIntegrationLookup = (
entitySources: EntitySources,
entities: EntityRegistryDisplayEntry[] | EntityRegistryEntry[]
): Record<string, string[]> => {
const deviceIntegrations: Record<string, string[]> = {};
entities: EntityRegistryDisplayEntry[] | EntityRegistryEntry[],
devices?: DeviceRegistryEntry[],
configEntries?: ConfigEntry[]
): Record<string, Set<string>> => {
const deviceIntegrations: Record<string, Set<string>> = {};

for (const entity of entities) {
const source = entitySources[entity.entity_id];
if (!source?.domain || entity.device_id === null) {
continue;
}

if (!deviceIntegrations[entity.device_id!]) {
deviceIntegrations[entity.device_id!] = [];
deviceIntegrations[entity.device_id!] =
deviceIntegrations[entity.device_id!] || new Set<string>();
deviceIntegrations[entity.device_id!].add(source.domain);
Comment on lines +158 to +160
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid non-null assertions.

The non-null assertion operator ! is used here, which can lead to runtime errors if the value is actually null. Consider using optional chaining or proper checks before accessing properties.

- deviceIntegrations[entity.device_id!] = deviceIntegrations[entity.device_id!] || new Set<string>();
- deviceIntegrations[entity.device_id!].add(source.domain);
+ deviceIntegrations[entity.device_id] = deviceIntegrations[entity.device_id] || new Set<string>();
+ if (deviceIntegrations[entity.device_id]) {
+   deviceIntegrations[entity.device_id].add(source.domain);
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
deviceIntegrations[entity.device_id!] =
deviceIntegrations[entity.device_id!] || new Set<string>();
deviceIntegrations[entity.device_id!].add(source.domain);
deviceIntegrations[entity.device_id] =
deviceIntegrations[entity.device_id] || new Set<string>();
if (deviceIntegrations[entity.device_id]) {
deviceIntegrations[entity.device_id].add(source.domain);
}
Tools
Biome

[error] 158-158: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 159-159: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 160-160: Forbidden non-null assertion. (lint/style/noNonNullAssertion)

}
// Lookup devices that have no entities
if (devices && configEntries) {
for (const device of devices) {
for (const config_entry_id of device.config_entries) {
const entry = configEntries.find((e) => e.entry_id === config_entry_id);
if (entry?.domain) {
deviceIntegrations[device.id] =
deviceIntegrations[device.id] || new Set<string>();
deviceIntegrations[device.id].add(entry.domain);
}
}
Comment on lines +149 to +172
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider refactoring to reduce complexity.

The getDeviceIntegrationLookup function has been flagged for excessive complexity. Breaking down this function into smaller, more manageable parts could enhance readability and maintainability.

- export const getDeviceIntegrationLookup = (
+ function addEntityIntegrations(deviceIntegrations, entitySources, entities) {
+   for (const entity of entities) {
+     const source = entitySources[entity.entity_id];
+     if (!source?.domain || entity.device_id === null) {
+       continue;
+     }
+     deviceIntegrations[entity.device_id] = deviceIntegrations[entity.device_id] || new Set<string>();
+     deviceIntegrations[entity.device_id].add(source.domain);
+   }
+ }
+
+ function addDeviceConfigIntegrations(deviceIntegrations, devices, configEntries) {
+   for (const device of devices) {
+     for (const config_entry_id of device.config_entries) {
+       const entry = configEntries.find((e) => e.entry_id === config_entry_id);
+       if (entry?.domain) {
+         deviceIntegrations[device.id] = deviceIntegrations[device.id] || new Set<string>();
+         deviceIntegrations[device.id].add(entry.domain);
+       }
+     }
+   }
+ }
+
+ export const getDeviceIntegrationLookup = (
    entitySources: EntitySources,
    entities: EntityRegistryDisplayEntry[] | EntityRegistryEntry[],
    devices?: DeviceRegistryEntry[],
    configEntries?: ConfigEntry[]
  ): Record<string, Set<string>> => {
    const deviceIntegrations: Record<string, Set<string>> = {};
    addEntityIntegrations(deviceIntegrations, entitySources, entities);
    if (devices && configEntries) {
      addDeviceConfigIntegrations(deviceIntegrations, devices, configEntries);
    }
    return deviceIntegrations;
  };
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
): Record<string, Set<string>> => {
const deviceIntegrations: Record<string, Set<string>> = {};
for (const entity of entities) {
const source = entitySources[entity.entity_id];
if (!source?.domain || entity.device_id === null) {
continue;
}
if (!deviceIntegrations[entity.device_id!]) {
deviceIntegrations[entity.device_id!] = [];
deviceIntegrations[entity.device_id!] =
deviceIntegrations[entity.device_id!] || new Set<string>();
deviceIntegrations[entity.device_id!].add(source.domain);
}
// Lookup devices that have no entities
if (devices && configEntries) {
for (const device of devices) {
for (const config_entry_id of device.config_entries) {
const entry = configEntries.find((e) => e.entry_id === config_entry_id);
if (entry?.domain) {
deviceIntegrations[device.id] =
deviceIntegrations[device.id] || new Set<string>();
deviceIntegrations[device.id].add(entry.domain);
}
}
function addEntityIntegrations(deviceIntegrations, entitySources, entities) {
for (const entity of entities) {
const source = entitySources[entity.entity_id];
if (!source?.domain || entity.device_id === null) {
continue;
}
deviceIntegrations[entity.device_id] = deviceIntegrations[entity.device_id] || new Set<string>();
deviceIntegrations[entity.device_id].add(source.domain);
}
}
function addDeviceConfigIntegrations(deviceIntegrations, devices, configEntries) {
for (const device of devices) {
for (const config_entry_id of device.config_entries) {
const entry = configEntries.find((e) => e.entry_id === config_entry_id);
if (entry?.domain) {
deviceIntegrations[device.id] = deviceIntegrations[device.id] || new Set<string>();
deviceIntegrations[device.id].add(entry.domain);
}
}
}
}
export const getDeviceIntegrationLookup = (
entitySources: EntitySources,
entities: EntityRegistryDisplayEntry[] | EntityRegistryEntry[],
devices?: DeviceRegistryEntry[],
configEntries?: ConfigEntry[]
): Record<string, Set<string>> => {
const deviceIntegrations: Record<string, Set<string>> = {};
addEntityIntegrations(deviceIntegrations, entitySources, entities);
if (devices && configEntries) {
addDeviceConfigIntegrations(deviceIntegrations, devices, configEntries);
}
return deviceIntegrations;
};
Tools
Biome

[error] 158-158: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 159-159: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 160-160: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 149-149: Excessive complexity detected. (lint/complexity/noExcessiveCognitiveComplexity)

Please refactor this function to reduce its complexity score from 17 to the max allowed complexity 15.

}
deviceIntegrations[entity.device_id!].push(source.domain);
}
return deviceIntegrations;
};
4 changes: 2 additions & 2 deletions src/data/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ export const entityMeetsTargetSelector = (
export const filterSelectorDevices = (
filterDevice: DeviceSelectorFilter,
device: DeviceRegistryEntry,
deviceIntegrationLookup?: Record<string, string[]> | undefined
deviceIntegrationLookup?: Record<string, Set<string>> | undefined
): boolean => {
const {
manufacturer: filterManufacturer,
Expand All @@ -713,7 +713,7 @@ export const filterSelectorDevices = (
}

if (filterIntegration && deviceIntegrationLookup) {
if (!deviceIntegrationLookup?.[device.id]?.includes(filterIntegration)) {
if (!deviceIntegrationLookup?.[device.id]?.has(filterIntegration)) {
return false;
}
}
Expand Down
Loading