mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-21 16:26:43 +00:00
Add integration filter to Device Selector (#12680)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
3f1a2526b3
commit
97663aef42
@ -1,18 +1,33 @@
|
|||||||
|
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import { html, LitElement } from "lit";
|
import { html, LitElement } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import { ConfigEntry, getConfigEntries } from "../../data/config_entries";
|
import memoizeOne from "memoize-one";
|
||||||
|
import { ConfigEntry } from "../../data/config_entries";
|
||||||
import type { DeviceRegistryEntry } from "../../data/device_registry";
|
import type { DeviceRegistryEntry } from "../../data/device_registry";
|
||||||
|
import {
|
||||||
|
EntityRegistryEntry,
|
||||||
|
subscribeEntityRegistry,
|
||||||
|
} from "../../data/entity_registry";
|
||||||
|
import {
|
||||||
|
EntitySources,
|
||||||
|
fetchEntitySourcesWithCache,
|
||||||
|
} from "../../data/entity_sources";
|
||||||
import type { DeviceSelector } from "../../data/selector";
|
import type { DeviceSelector } from "../../data/selector";
|
||||||
|
import { SubscribeMixin } from "../../mixins/subscribe-mixin";
|
||||||
import type { HomeAssistant } from "../../types";
|
import type { HomeAssistant } from "../../types";
|
||||||
import "../device/ha-device-picker";
|
import "../device/ha-device-picker";
|
||||||
import "../device/ha-devices-picker";
|
import "../device/ha-devices-picker";
|
||||||
|
|
||||||
@customElement("ha-selector-device")
|
@customElement("ha-selector-device")
|
||||||
export class HaDeviceSelector extends LitElement {
|
export class HaDeviceSelector extends SubscribeMixin(LitElement) {
|
||||||
@property() public hass!: HomeAssistant;
|
@property() public hass!: HomeAssistant;
|
||||||
|
|
||||||
@property() public selector!: DeviceSelector;
|
@property() public selector!: DeviceSelector;
|
||||||
|
|
||||||
|
@state() private _entitySources?: EntitySources;
|
||||||
|
|
||||||
|
@state() private _entities?: EntityRegistryEntry[];
|
||||||
|
|
||||||
@property() public value?: any;
|
@property() public value?: any;
|
||||||
|
|
||||||
@property() public label?: string;
|
@property() public label?: string;
|
||||||
@ -25,20 +40,32 @@ export class HaDeviceSelector extends LitElement {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public required = true;
|
@property({ type: Boolean }) public required = true;
|
||||||
|
|
||||||
protected updated(changedProperties) {
|
public hassSubscribe(): UnsubscribeFunc[] {
|
||||||
if (changedProperties.has("selector")) {
|
return [
|
||||||
const oldSelector = changedProperties.get("selector");
|
subscribeEntityRegistry(this.hass.connection!, (entities) => {
|
||||||
if (oldSelector !== this.selector && this.selector.device?.integration) {
|
this._entities = entities.filter((entity) => entity.device_id !== null);
|
||||||
getConfigEntries(this.hass, {
|
}),
|
||||||
domain: this.selector.device.integration,
|
];
|
||||||
}).then((entries) => {
|
}
|
||||||
this._configEntries = entries;
|
|
||||||
});
|
protected updated(changedProperties): void {
|
||||||
}
|
super.updated(changedProperties);
|
||||||
|
if (
|
||||||
|
changedProperties.has("selector") &&
|
||||||
|
this.selector.device.integration &&
|
||||||
|
!this._entitySources
|
||||||
|
) {
|
||||||
|
fetchEntitySourcesWithCache(this.hass).then((sources) => {
|
||||||
|
this._entitySources = sources;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
|
if (this.selector.device.integration && !this._entitySources) {
|
||||||
|
return html``;
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.selector.device.multiple) {
|
if (!this.selector.device.multiple) {
|
||||||
return html`
|
return html`
|
||||||
<ha-device-picker
|
<ha-device-picker
|
||||||
@ -80,30 +107,48 @@ export class HaDeviceSelector extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _filterDevices = (device: DeviceRegistryEntry): boolean => {
|
private _filterDevices = (device: DeviceRegistryEntry): boolean => {
|
||||||
if (
|
const {
|
||||||
this.selector.device?.manufacturer &&
|
manufacturer: filterManufacturer,
|
||||||
device.manufacturer !== this.selector.device.manufacturer
|
model: filterModel,
|
||||||
) {
|
integration: filterIntegration,
|
||||||
|
} = this.selector.device;
|
||||||
|
|
||||||
|
if (filterManufacturer && device.manufacturer !== filterManufacturer) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (
|
if (filterModel && device.model !== filterModel) {
|
||||||
this.selector.device?.model &&
|
|
||||||
device.model !== this.selector.device.model
|
|
||||||
) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (this.selector.device?.integration) {
|
if (filterIntegration && this._entitySources && this._entities) {
|
||||||
if (
|
const deviceIntegrations = this._deviceIntegrations(
|
||||||
this._configEntries &&
|
this._entitySources,
|
||||||
!this._configEntries.some((entry) =>
|
this._entities
|
||||||
device.config_entries.includes(entry.entry_id)
|
);
|
||||||
)
|
if (!deviceIntegrations?.[device.id]?.includes(filterIntegration)) {
|
||||||
) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private _deviceIntegrations = memoizeOne(
|
||||||
|
(entitySources: EntitySources, entities: EntityRegistryEntry[]) => {
|
||||||
|
const deviceIntegrations: Record<string, string[]> = {};
|
||||||
|
|
||||||
|
for (const entity of entities) {
|
||||||
|
const source = entitySources[entity.entity_id];
|
||||||
|
if (!source?.domain) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!deviceIntegrations[entity.device_id!]) {
|
||||||
|
deviceIntegrations[entity.device_id!] = [];
|
||||||
|
}
|
||||||
|
deviceIntegrations[entity.device_id!].push(source.domain);
|
||||||
|
}
|
||||||
|
return deviceIntegrations;
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user