diff --git a/src/components/entity/ha-entity-name-picker.ts b/src/components/entity/ha-entity-name-picker.ts index 5c8ce4b761..4899d130c8 100644 --- a/src/components/entity/ha-entity-name-picker.ts +++ b/src/components/entity/ha-entity-name-picker.ts @@ -86,8 +86,8 @@ export class HaEntityNamePicker extends LitElement { private _editIndex?: number; - private _validOptions = memoizeOne((entityId?: string) => { - const options = new Set(); + private _validTypes = memoizeOne((entityId?: string) => { + const options = new Set(["text"]); if (!entityId) { return options; } @@ -119,22 +119,22 @@ export class HaEntityNamePicker extends LitElement { return []; } - const options = this._validOptions(entityId); + const types = this._validTypes(entityId); const items = ( ["entity", "device", "area", "floor"] as const ).map((name) => { const stateObj = this.hass.states[entityId]; - const isValid = options.has(name); + const isValid = types.has(name); const primary = this.hass.localize( `ui.components.entity.entity-name-picker.types.${name}` ); const secondary = - stateObj && isValid + (stateObj && isValid ? this.hass.formatEntityName(stateObj, { type: name }) : this.hass.localize( `ui.components.entity.entity-name-picker.types.${name}_missing` as LocalizeKeys - ) || "-"; + )) || "-"; return { primary, @@ -169,9 +169,9 @@ export class HaEntityNamePicker extends LitElement { }; protected render() { - const value = this._value; + const value = this._items; const options = this._getOptions(this.entityId); - const validOptions = this._validOptions(this.entityId); + const validTypes = this._validTypes(this.entityId); return html` ${this.label ? html`` : nothing} @@ -185,12 +185,11 @@ export class HaEntityNamePicker extends LitElement { > ${repeat( - this._value, + this._items, (item) => item, (item: EntityNameItem, idx) => { const label = this._formatItem(item); - const isValid = - item.type === "text" || validOptions.has(item.type); + const isValid = validTypes.has(item.type); return html` { - const value = this._value; + const items = this._items; - const types = value.map((item) => item.type) as string[]; + const excludedValues = new Set( + items + .filter((item) => UNIQUE_TYPES.has(item.type)) + .map((item) => formatOptionValue(item)) + ); const filteredOptions = options.filter( - (option) => - !UNIQUE_TYPES.has(option.value) || - !types.includes(option.value) || - option.value === current + (option) => !excludedValues.has(option.value) || option.value === current ); return filteredOptions; }; @@ -357,16 +359,14 @@ export class HaEntityNamePicker extends LitElement { const options = this._comboBox.items || []; const currentItem = - this._editIndex != null ? this._value[this._editIndex] : undefined; + this._editIndex != null ? this._items[this._editIndex] : undefined; const currentValue = currentItem ? formatOptionValue(currentItem) : ""; - this._comboBox.filteredItems = this._filterSelectedOptions( - options, - currentValue - ); + let filteredItems = this._filterSelectedOptions(options, currentValue); if (!filter) { + this._comboBox.filteredItems = filteredItems; return; } @@ -378,9 +378,8 @@ export class HaEntityNamePicker extends LitElement { ignoreDiacritics: true, }; - const fuse = new Fuse(this._comboBox.filteredItems, fuseOptions); - const filteredItems = fuse.search(filter).map((result) => result.item); - + const fuse = new Fuse(filteredItems, fuseOptions); + filteredItems = fuse.search(filter).map((result) => result.item); filteredItems.push(this._customNameOption(input)); this._comboBox.filteredItems = filteredItems; } @@ -388,7 +387,7 @@ export class HaEntityNamePicker extends LitElement { private async _moveItem(ev: CustomEvent) { ev.stopPropagation(); const { oldIndex, newIndex } = ev.detail; - const value = this._value; + const value = this._items; const newValue = value.concat(); const element = newValue.splice(oldIndex, 1)[0]; newValue.splice(newIndex, 0, element); @@ -399,7 +398,7 @@ export class HaEntityNamePicker extends LitElement { private async _removeItem(ev) { ev.stopPropagation(); - const value = [...this._value]; + const value = [...this._items]; const idx = parseInt(ev.target.dataset.idx, 10); value.splice(idx, 1); this._setValue(value); @@ -417,7 +416,7 @@ export class HaEntityNamePicker extends LitElement { const item: EntityNameItem = parseOptionValue(value); - const newValue = [...this._value]; + const newValue = [...this._items]; if (this._editIndex != null) { newValue[this._editIndex] = item;