Add filter options to entity and device selectors (#15302)

This commit is contained in:
Paul Bottein
2023-02-20 14:30:05 +01:00
committed by GitHub
parent cf377558ae
commit c4160e8368
11 changed files with 331 additions and 188 deletions

View File

@@ -14,7 +14,6 @@ import {
DeviceRegistryEntry,
getDeviceIntegrationLookup,
} from "../../data/device_registry";
import { EntityRegistryEntry } from "../../data/entity_registry";
import {
EntitySources,
fetchEntitySourcesWithCache,
@@ -45,12 +44,24 @@ export class HaTargetSelector extends LitElement {
private _deviceIntegrationLookup = memoizeOne(getDeviceIntegrationLookup);
private _hasIntegration(selector: TargetSelector) {
return (
(selector.target?.entity &&
ensureArray(selector.target.entity).some(
(filter) => filter.integration
)) ||
(selector.target?.device &&
ensureArray(selector.target.device).some(
(device) => device.integration
))
);
}
protected updated(changedProperties: PropertyValues): void {
super.updated(changedProperties);
if (
changedProperties.has("selector") &&
(this.selector.target?.device?.integration ||
this.selector.target?.entity?.integration) &&
this._hasIntegration(this.selector) &&
!this._entitySources
) {
fetchEntitySourcesWithCache(this.hass).then((sources) => {
@@ -60,11 +71,7 @@ export class HaTargetSelector extends LitElement {
}
protected render(): TemplateResult {
if (
(this.selector.target?.device?.integration ||
this.selector.target?.entity?.integration) &&
!this._entitySources
) {
if (this._hasIntegration(this.selector) && !this._entitySources) {
return html``;
}
@@ -73,39 +80,21 @@ export class HaTargetSelector extends LitElement {
.value=${this.value}
.helper=${this.helper}
.deviceFilter=${this._filterDevices}
.entityFilter=${this._filterStates}
.entityRegFilter=${this._filterRegEntities}
.includeDeviceClasses=${this.selector.target?.entity?.device_class
? [this.selector.target?.entity.device_class]
: undefined}
.includeDomains=${this.selector.target?.entity?.domain
? ensureArray(this.selector.target.entity.domain as string | string[])
: undefined}
.entityFilter=${this._filterEntities}
.disabled=${this.disabled}
></ha-target-picker>`;
}
private _filterStates = (entity: HassEntity): boolean => {
private _filterEntities = (entity: HassEntity): boolean => {
if (!this.selector.target?.entity) {
return true;
}
return filterSelectorEntities(
this.selector.target.entity,
entity,
this._entitySources
return ensureArray(this.selector.target.entity).some((filter) =>
filterSelectorEntities(filter, entity, this._entitySources)
);
};
private _filterRegEntities = (entity: EntityRegistryEntry): boolean => {
if (this.selector.target?.entity?.integration) {
if (entity.platform !== this.selector.target.entity.integration) {
return false;
}
}
return true;
};
private _filterDevices = (device: DeviceRegistryEntry): boolean => {
if (!this.selector.target?.device) {
return true;
@@ -118,10 +107,8 @@ export class HaTargetSelector extends LitElement {
)
: undefined;
return filterSelectorDevices(
this.selector.target.device,
device,
deviceIntegrations
return ensureArray(this.selector.target.device).some((filter) =>
filterSelectorDevices(filter, device, deviceIntegrations)
);
};