mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Filter selected entities in entities picker using includeEntities property (#22059)
* Filter selected entities in entities picker using includeEntities property * Don't ignore other property when using include entities
This commit is contained in:
parent
be02a8869f
commit
f7f37c24e2
@ -1,10 +1,9 @@
|
|||||||
import type { HassEntity } from "home-assistant-js-websocket";
|
|
||||||
import { css, html, LitElement, nothing } from "lit";
|
import { css, html, LitElement, nothing } from "lit";
|
||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import { isValidEntityId } from "../../common/entity/valid_entity_id";
|
import { isValidEntityId } from "../../common/entity/valid_entity_id";
|
||||||
import type { ValueChangedEvent, HomeAssistant } from "../../types";
|
import type { HomeAssistant, ValueChangedEvent } from "../../types";
|
||||||
import "./ha-entity-picker";
|
import "./ha-entity-picker";
|
||||||
import type { HaEntityPickerEntityFilterFunc } from "./ha-entity-picker";
|
import type { HaEntityPickerEntityFilterFunc } from "./ha-entity-picker";
|
||||||
|
|
||||||
@ -98,10 +97,7 @@ class HaEntitiesPickerLight extends LitElement {
|
|||||||
.excludeEntities=${this.excludeEntities}
|
.excludeEntities=${this.excludeEntities}
|
||||||
.includeDeviceClasses=${this.includeDeviceClasses}
|
.includeDeviceClasses=${this.includeDeviceClasses}
|
||||||
.includeUnitOfMeasurement=${this.includeUnitOfMeasurement}
|
.includeUnitOfMeasurement=${this.includeUnitOfMeasurement}
|
||||||
.entityFilter=${this._getEntityFilter(
|
.entityFilter=${this.entityFilter}
|
||||||
this.value,
|
|
||||||
this.entityFilter
|
|
||||||
)}
|
|
||||||
.value=${entityId}
|
.value=${entityId}
|
||||||
.label=${this.pickedEntityLabel}
|
.label=${this.pickedEntityLabel}
|
||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
@ -118,10 +114,13 @@ class HaEntitiesPickerLight extends LitElement {
|
|||||||
.includeDomains=${this.includeDomains}
|
.includeDomains=${this.includeDomains}
|
||||||
.excludeDomains=${this.excludeDomains}
|
.excludeDomains=${this.excludeDomains}
|
||||||
.includeEntities=${this.includeEntities}
|
.includeEntities=${this.includeEntities}
|
||||||
.excludeEntities=${this.excludeEntities}
|
.excludeEntities=${this._excludeEntities(
|
||||||
|
this.value,
|
||||||
|
this.excludeEntities
|
||||||
|
)}
|
||||||
.includeDeviceClasses=${this.includeDeviceClasses}
|
.includeDeviceClasses=${this.includeDeviceClasses}
|
||||||
.includeUnitOfMeasurement=${this.includeUnitOfMeasurement}
|
.includeUnitOfMeasurement=${this.includeUnitOfMeasurement}
|
||||||
.entityFilter=${this._getEntityFilter(this.value, this.entityFilter)}
|
.entityFilter=${this.entityFilter}
|
||||||
.label=${this.pickEntityLabel}
|
.label=${this.pickEntityLabel}
|
||||||
.helper=${this.helper}
|
.helper=${this.helper}
|
||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
@ -133,14 +132,16 @@ class HaEntitiesPickerLight extends LitElement {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getEntityFilter = memoizeOne(
|
private _excludeEntities = memoizeOne(
|
||||||
(
|
(
|
||||||
value: string[] | undefined,
|
value: string[] | undefined,
|
||||||
entityFilter: HaEntityPickerEntityFilterFunc | undefined
|
excludeEntities: string[] | undefined
|
||||||
): HaEntityPickerEntityFilterFunc =>
|
): string[] | undefined => {
|
||||||
(stateObj: HassEntity) =>
|
if (value === undefined) {
|
||||||
(!value || !value.includes(stateObj.entity_id)) &&
|
return excludeEntities;
|
||||||
(!entityFilter || entityFilter(stateObj))
|
}
|
||||||
|
return [...(excludeEntities || []), ...value];
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
private get _currentEntities() {
|
private get _currentEntities() {
|
||||||
|
@ -87,7 +87,7 @@ export class HaEntityPicker extends LitElement {
|
|||||||
public includeUnitOfMeasurement?: string[];
|
public includeUnitOfMeasurement?: string[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of allowed entities to show. Will ignore all other filters.
|
* List of allowed entities to show.
|
||||||
* @type {Array}
|
* @type {Array}
|
||||||
* @attr include-entities
|
* @attr include-entities
|
||||||
*/
|
*/
|
||||||
@ -220,30 +220,13 @@ export class HaEntityPicker extends LitElement {
|
|||||||
|
|
||||||
if (includeEntities) {
|
if (includeEntities) {
|
||||||
entityIds = entityIds.filter((entityId) =>
|
entityIds = entityIds.filter((entityId) =>
|
||||||
this.includeEntities!.includes(entityId)
|
includeEntities.includes(entityId)
|
||||||
);
|
);
|
||||||
|
|
||||||
return entityIds
|
|
||||||
.map((key) => {
|
|
||||||
const friendly_name = computeStateName(hass!.states[key]) || key;
|
|
||||||
return {
|
|
||||||
...hass!.states[key],
|
|
||||||
friendly_name,
|
|
||||||
strings: [key, friendly_name],
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.sort((entityA, entityB) =>
|
|
||||||
caseInsensitiveStringCompare(
|
|
||||||
entityA.friendly_name,
|
|
||||||
entityB.friendly_name,
|
|
||||||
this.hass.locale.language
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (excludeEntities) {
|
if (excludeEntities) {
|
||||||
entityIds = entityIds.filter(
|
entityIds = entityIds.filter(
|
||||||
(entityId) => !excludeEntities!.includes(entityId)
|
(entityId) => !excludeEntities.includes(entityId)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user