Skip to content

Improve search in add automation element dialog #25626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/panels/config/automation/add-automation-element-dialog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { mdiClose, mdiContentPaste, mdiPlus } from "@mdi/js";
import type { IFuseOptions } from "fuse.js";
import Fuse from "fuse.js";
import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit";
import { LitElement, css, html, nothing } from "lit";
Expand Down Expand Up @@ -46,6 +45,7 @@ import { haStyle, haStyleDialog } from "../../../resources/styles";
import type { HomeAssistant } from "../../../types";
import type { AddAutomationElementDialogParams } from "./show-add-automation-element-dialog";
import { PASTE_VALUE } from "./show-add-automation-element-dialog";
import { HaFuse } from "../../../resources/fuse";

const TYPES = {
trigger: { groups: TRIGGER_GROUPS, icons: TRIGGER_ICONS },
Expand Down Expand Up @@ -175,6 +175,40 @@ class DialogAddAutomationElement extends LitElement implements HassDialog {
type: AddAutomationElementDialogParams["type"],
group: string | undefined,
filter: string,
domains: Set<string> | undefined,
localize: LocalizeFunc,
services: HomeAssistant["services"],
manifests?: DomainManifestLookup
): ListItem[] => {
const items = this._items(type, group, localize, services, manifests);

const index = this._fuseIndex(items);

const fuse = new HaFuse(
items,
{ ignoreLocation: true, includeScore: true },
index
);

const results = fuse.multiTermsSearch(filter);
if (results) {
return results.map((result) => result.item);
}
return this._getGroupItems(
type,
group,
domains,
localize,
services,
manifests
);
}
);

private _items = memoizeOne(
(
type: AddAutomationElementDialogParams["type"],
group: string | undefined,
localize: LocalizeFunc,
services: HomeAssistant["services"],
manifests?: DomainManifestLookup
Expand All @@ -189,24 +223,17 @@ class DialogAddAutomationElement extends LitElement implements HassDialog {
);

const items = flattenGroups(groups).flat();

if (type === "action") {
items.push(...this._services(localize, services, manifests, group));
}

const options: IFuseOptions<ListItem> = {
keys: ["key", "name", "description"],
isCaseSensitive: false,
ignoreLocation: true,
minMatchCharLength: Math.min(filter.length, 2),
threshold: 0.2,
ignoreDiacritics: true,
};
const fuse = new Fuse(items, options);
return fuse.search(filter).map((result) => result.item);
return items;
}
);

private _fuseIndex = memoizeOne((items: ListItem[]) =>
Fuse.createIndex(["key", "name", "description"], items)
);

private _getGroupItems = memoizeOne(
(
type: AddAutomationElementDialogParams["type"],
Expand Down Expand Up @@ -449,6 +476,7 @@ class DialogAddAutomationElement extends LitElement implements HassDialog {
this._params.type,
this._group,
this._filter,
this._domains,
this.hass.localize,
this.hass.services,
this._manifests
Expand Down
Loading