-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathsearch.ts
66 lines (60 loc) · 1.47 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { HassEntity } from "home-assistant-js-websocket";
import type { HomeAssistant } from "../types";
import { caseInsensitiveStringCompare } from "../common/string/compare";
export interface RelatedResult {
area?: string[];
automation?: string[];
automation_blueprint?: string[];
config_entry?: string[];
device?: string[];
entity?: string[];
group?: string[];
integration?: string[];
scene?: string[];
script?: string[];
script_blueprint?: string[];
}
export const SearchableDomains = new Set([
"automation",
"script",
"scene",
"group",
]);
export type ItemType =
| "area"
| "automation"
| "config_entry"
| "device"
| "entity"
| "floor"
| "group"
| "label"
| "scene"
| "script"
| "automation_blueprint"
| "script_blueprint";
export const findRelated = async (
hass: HomeAssistant,
itemType: ItemType,
itemId: string
): Promise<RelatedResult> => {
const related = await hass.callWS<RelatedResult>({
type: "search/related",
item_type: itemType,
item_id: itemId,
});
Object.keys(related).forEach((key) => {
related[key] = related[key]
.map((id: string) => hass.states[id])
.filter(Boolean)
.sort((a: HassEntity, b: HassEntity) =>
caseInsensitiveStringCompare(
a.attributes.friendly_name ?? a.entity_id,
b.attributes.friendly_name ?? b.entity_id,
hass.language
)
)
.map((entity: HassEntity) => entity.entity_id);
});
return related;
};