Add Friendly Name to the Entity Picker + FuzzySeq Algo (#7291)

This commit is contained in:
Zack Barett 2020-10-14 20:20:21 -05:00 committed by GitHub
parent c2741638b2
commit 12d73fe90d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -9,7 +9,7 @@
*/ */
export const fuzzySequentialMatch = (filter: string, words: string[]) => { export const fuzzySequentialMatch = (filter: string, words: string[]) => {
for (const word of words) { for (const word of words) {
if (_fuzzySequentialMatch(filter, word)) { if (_fuzzySequentialMatch(filter.toLowerCase(), word.toLowerCase())) {
return true; return true;
} }
} }

View File

@ -1,3 +1,4 @@
import "@material/mwc-icon-button/mwc-icon-button";
import { mdiClose, mdiMenuDown, mdiMenuUp } from "@mdi/js"; import { mdiClose, mdiMenuDown, mdiMenuUp } from "@mdi/js";
import "@polymer/paper-input/paper-input"; import "@polymer/paper-input/paper-input";
import "@polymer/paper-item/paper-icon-item"; import "@polymer/paper-item/paper-icon-item";
@ -19,11 +20,11 @@ import memoizeOne from "memoize-one";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import { computeDomain } from "../../common/entity/compute_domain"; import { computeDomain } from "../../common/entity/compute_domain";
import { computeStateName } from "../../common/entity/compute_state_name"; import { computeStateName } from "../../common/entity/compute_state_name";
import { fuzzySequentialMatch } from "../../common/string/sequence_matching";
import { PolymerChangedEvent } from "../../polymer-types"; import { PolymerChangedEvent } from "../../polymer-types";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import "../ha-svg-icon"; import "../ha-svg-icon";
import "./state-badge"; import "./state-badge";
import "@material/mwc-icon-button/mwc-icon-button";
export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean; export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
@ -103,6 +104,8 @@ export class HaEntityPicker extends LitElement {
private _initedStates = false; private _initedStates = false;
private _states: HassEntity[] = [];
private _getStates = memoizeOne( private _getStates = memoizeOne(
( (
_opened: boolean, _opened: boolean,
@ -168,7 +171,7 @@ export class HaEntityPicker extends LitElement {
protected updated(changedProps: PropertyValues) { protected updated(changedProps: PropertyValues) {
if (!this._initedStates || (changedProps.has("_opened") && this._opened)) { if (!this._initedStates || (changedProps.has("_opened") && this._opened)) {
const states = this._getStates( this._states = this._getStates(
this._opened, this._opened,
this.hass, this.hass,
this.includeDomains, this.includeDomains,
@ -176,7 +179,7 @@ export class HaEntityPicker extends LitElement {
this.entityFilter, this.entityFilter,
this.includeDeviceClasses this.includeDeviceClasses
); );
(this._comboBox as any).items = states; (this._comboBox as any).filteredItems = this._states;
this._initedStates = true; this._initedStates = true;
} }
} }
@ -194,6 +197,7 @@ export class HaEntityPicker extends LitElement {
.renderer=${rowRenderer} .renderer=${rowRenderer}
@opened-changed=${this._openedChanged} @opened-changed=${this._openedChanged}
@value-changed=${this._valueChanged} @value-changed=${this._valueChanged}
@filter-changed=${this._filterChanged}
> >
<paper-input <paper-input
.autofocus=${this.autofocus} .autofocus=${this.autofocus}
@ -262,6 +266,15 @@ export class HaEntityPicker extends LitElement {
} }
} }
private _filterChanged(ev): void {
(this._comboBox as any).filteredItems = this._states.filter((state) =>
fuzzySequentialMatch(ev.detail.value, [
state.entity_id,
computeStateName(state),
])
);
}
private _setValue(value: string) { private _setValue(value: string) {
this.value = value; this.value = value;
setTimeout(() => { setTimeout(() => {