From 2174117782ecc9e949556bec1021266560780027 Mon Sep 17 00:00:00 2001 From: Zack Arnett Date: Fri, 9 Oct 2020 17:15:38 -0500 Subject: [PATCH 1/6] Add Friendly Name to the Entity Picker --- src/components/entity/ha-entity-picker.ts | 25 ++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/components/entity/ha-entity-picker.ts b/src/components/entity/ha-entity-picker.ts index 1fc89bb235f3..8d8afde1c3ff 100644 --- a/src/components/entity/ha-entity-picker.ts +++ b/src/components/entity/ha-entity-picker.ts @@ -174,7 +174,7 @@ export class HaEntityPicker extends LitElement { this.entityFilter, this.includeDeviceClasses ); - (this._comboBox as any).items = states; + (this._comboBox as any).filteredItems = states; this._initedStates = true; } } @@ -192,6 +192,7 @@ export class HaEntityPicker extends LitElement { .renderer=${rowRenderer} @opened-changed=${this._openedChanged} @value-changed=${this._valueChanged} + @filter-changed=${this._filterChanged} > + state.entity_id.toLowerCase().includes(filterString) || + computeStateName(state).toLowerCase().includes(filterString) + ); + console.log(filteredStates); + + (this._comboBox as any).filteredItems = filteredStates; + } + private _setValue(value: string) { this.value = value; setTimeout(() => { From 4d8b9a8e14f203a3b8ad27c2c5dca80d5d0c7f8f Mon Sep 17 00:00:00 2001 From: Zack Arnett Date: Fri, 9 Oct 2020 17:25:43 -0500 Subject: [PATCH 2/6] console.bye --- src/components/entity/ha-entity-picker.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/entity/ha-entity-picker.ts b/src/components/entity/ha-entity-picker.ts index 8d8afde1c3ff..a7713a4dbc23 100644 --- a/src/components/entity/ha-entity-picker.ts +++ b/src/components/entity/ha-entity-picker.ts @@ -278,7 +278,6 @@ export class HaEntityPicker extends LitElement { state.entity_id.toLowerCase().includes(filterString) || computeStateName(state).toLowerCase().includes(filterString) ); - console.log(filteredStates); (this._comboBox as any).filteredItems = filteredStates; } From 15487558ecb7073871b5f9afdbd6e94928955bf1 Mon Sep 17 00:00:00 2001 From: Zack Arnett Date: Tue, 13 Oct 2020 16:28:12 -0500 Subject: [PATCH 3/6] add in fuzzy --- src/components/entity/ha-entity-picker.ts | 25 ++++++++++------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/components/entity/ha-entity-picker.ts b/src/components/entity/ha-entity-picker.ts index a7713a4dbc23..3cffcd3e6952 100644 --- a/src/components/entity/ha-entity-picker.ts +++ b/src/components/entity/ha-entity-picker.ts @@ -18,6 +18,7 @@ import memoizeOne from "memoize-one"; import { fireEvent } from "../../common/dom/fire_event"; import { computeDomain } from "../../common/entity/compute_domain"; import { computeStateName } from "../../common/entity/compute_state_name"; +import { fuzzySequentialMatch } from "../../common/string/sequence_matching"; import { PolymerChangedEvent } from "../../polymer-types"; import { HomeAssistant } from "../../types"; import "../ha-icon-button"; @@ -101,6 +102,8 @@ export class HaEntityPicker extends LitElement { private _initedStates = false; + private _states: HassEntity[] = []; + private _getStates = memoizeOne( ( _opened: boolean, @@ -166,7 +169,7 @@ export class HaEntityPicker extends LitElement { protected updated(changedProps: PropertyValues) { if (!this._initedStates || (changedProps.has("_opened") && this._opened)) { - const states = this._getStates( + this._states = this._getStates( this._opened, this.hass, this.includeDomains, @@ -174,7 +177,7 @@ export class HaEntityPicker extends LitElement { this.entityFilter, this.includeDeviceClasses ); - (this._comboBox as any).filteredItems = states; + (this._comboBox as any).filteredItems = this._states; this._initedStates = true; } } @@ -264,19 +267,13 @@ export class HaEntityPicker extends LitElement { private _filterChanged(ev): void { const filterString = ev.detail.value.toLowerCase(); - const states = this._getStates( - this._opened, - this.hass, - this.includeDomains, - this.excludeDomains, - this.entityFilter, - this.includeDeviceClasses - ); - - const filteredStates = states.filter( + const filteredStates = this._states.filter( (state) => - state.entity_id.toLowerCase().includes(filterString) || - computeStateName(state).toLowerCase().includes(filterString) + fuzzySequentialMatch(filterString, state.entity_id.toLowerCase()) || + fuzzySequentialMatch( + filterString, + computeStateName(state).toLowerCase() + ) ); (this._comboBox as any).filteredItems = filteredStates; From 05fe2a8225b36a4efae007b6fe990048d2de398e Mon Sep 17 00:00:00 2001 From: Zack Arnett Date: Tue, 13 Oct 2020 17:43:02 -0500 Subject: [PATCH 4/6] fuzseq --- src/components/entity/ha-entity-picker.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/components/entity/ha-entity-picker.ts b/src/components/entity/ha-entity-picker.ts index 3cffcd3e6952..81fd2b99ba3e 100644 --- a/src/components/entity/ha-entity-picker.ts +++ b/src/components/entity/ha-entity-picker.ts @@ -267,13 +267,11 @@ export class HaEntityPicker extends LitElement { private _filterChanged(ev): void { const filterString = ev.detail.value.toLowerCase(); - const filteredStates = this._states.filter( - (state) => - fuzzySequentialMatch(filterString, state.entity_id.toLowerCase()) || - fuzzySequentialMatch( - filterString, - computeStateName(state).toLowerCase() - ) + const filteredStates = this._states.filter((state) => + fuzzySequentialMatch(filterString, [ + state.entity_id.toLowerCase(), + computeStateName(state).toLowerCase(), + ]) ); (this._comboBox as any).filteredItems = filteredStates; From d44cc339597f50eb33aac0bc6aa7074eb7ba52b5 Mon Sep 17 00:00:00 2001 From: Zack Arnett Date: Wed, 14 Oct 2020 19:51:45 -0500 Subject: [PATCH 5/6] lower case in fuzzy --- src/common/string/sequence_matching.ts | 2 +- src/components/entity/ha-entity-picker.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/common/string/sequence_matching.ts b/src/common/string/sequence_matching.ts index 402b35baee5e..921b2471df60 100644 --- a/src/common/string/sequence_matching.ts +++ b/src/common/string/sequence_matching.ts @@ -9,7 +9,7 @@ */ export const fuzzySequentialMatch = (filter: string, words: string[]) => { for (const word of words) { - if (_fuzzySequentialMatch(filter, word)) { + if (_fuzzySequentialMatch(filter.toLowerCase(), word.toLowerCase())) { return true; } } diff --git a/src/components/entity/ha-entity-picker.ts b/src/components/entity/ha-entity-picker.ts index eee240dff0a1..e40ac1288fad 100644 --- a/src/components/entity/ha-entity-picker.ts +++ b/src/components/entity/ha-entity-picker.ts @@ -1,3 +1,4 @@ +import "@material/mwc-icon-button/mwc-icon-button"; import { mdiClose, mdiMenuDown, mdiMenuUp } from "@mdi/js"; import "@polymer/paper-input/paper-input"; import "@polymer/paper-item/paper-icon-item"; @@ -24,7 +25,6 @@ import { PolymerChangedEvent } from "../../polymer-types"; import { HomeAssistant } from "../../types"; import "../ha-svg-icon"; import "./state-badge"; -import "@material/mwc-icon-button/mwc-icon-button"; export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean; @@ -267,12 +267,12 @@ export class HaEntityPicker extends LitElement { } private _filterChanged(ev): void { - const filterString = ev.detail.value.toLowerCase(); + const filterString = ev.detail.value; const filteredStates = this._states.filter((state) => fuzzySequentialMatch(filterString, [ - state.entity_id.toLowerCase(), - computeStateName(state).toLowerCase(), + state.entity_id, + computeStateName(state), ]) ); From ed60e6a0096e9d0edfa803db50fa9794752cf277 Mon Sep 17 00:00:00 2001 From: Zack Arnett Date: Wed, 14 Oct 2020 19:53:30 -0500 Subject: [PATCH 6/6] less code --- src/components/entity/ha-entity-picker.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/components/entity/ha-entity-picker.ts b/src/components/entity/ha-entity-picker.ts index e40ac1288fad..437bd7ec4651 100644 --- a/src/components/entity/ha-entity-picker.ts +++ b/src/components/entity/ha-entity-picker.ts @@ -267,16 +267,12 @@ export class HaEntityPicker extends LitElement { } private _filterChanged(ev): void { - const filterString = ev.detail.value; - - const filteredStates = this._states.filter((state) => - fuzzySequentialMatch(filterString, [ + (this._comboBox as any).filteredItems = this._states.filter((state) => + fuzzySequentialMatch(ev.detail.value, [ state.entity_id, computeStateName(state), ]) ); - - (this._comboBox as any).filteredItems = filteredStates; } private _setValue(value: string) {