-
Notifications
You must be signed in to change notification settings - Fork 111
/
filter.ts
202 lines (191 loc) 路 6.69 KB
/
filter.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { HAState, HassObject } from "./types";
import { getAreas, getDevices, getEntities } from "./helpers";
const ago_suffix_regex = /([mhd])\s+ago\s*$/i;
const default_ago_suffix = "m ago";
function match(pattern: any, value: any) {
if (typeof pattern === "string" && pattern.startsWith("$$")) {
pattern = pattern.substring(2);
value = JSON.stringify(value);
}
if (typeof value === "string" && typeof pattern === "string") {
if (
(pattern.startsWith("/") && pattern.endsWith("/")) ||
pattern.indexOf("*") !== -1
) {
if (!pattern.startsWith("/")) {
// Convert globs to regex
pattern = pattern.replace(/\./g, ".").replace(/\*/g, ".*");
pattern = `/^${pattern}$/`;
}
let regex = new RegExp(pattern.slice(1, -1));
return regex.test(value);
}
}
if (typeof pattern === "string") {
const match = ago_suffix_regex.exec(pattern);
if (match) {
pattern = pattern.replace(match[0], "");
const now = new Date().getTime();
const updated = new Date(value).getTime();
value = (now - updated) / 60000;
const period = match[1];
if (period === "h") {
value = value / 60;
} else if (period === "d") {
value = value / 60 / 24;
}
}
}
if (typeof pattern === "string") {
// Comparisons assume numerical values
if (pattern.startsWith("<="))
return parseFloat(value) <= parseFloat(pattern.substring(2));
if (pattern.startsWith(">="))
return parseFloat(value) >= parseFloat(pattern.substring(2));
if (pattern.startsWith("<"))
return parseFloat(value) < parseFloat(pattern.substring(1));
if (pattern.startsWith(">"))
return parseFloat(value) > parseFloat(pattern.substring(1));
if (pattern.startsWith("!"))
return parseFloat(value) != parseFloat(pattern.substring(1));
if (pattern.startsWith("="))
return parseFloat(value) == parseFloat(pattern.substring(1));
}
return pattern === value;
}
const FILTERS: Record<
string,
(hass: HassObject, value: any, entity: HAState) => Promise<boolean>
> = {
options: async () => true,
sort: async () => true,
domain: async (hass, value, entity) => {
return match(value, entity.entity_id.split(".")[0]);
},
entity_id: async (hass, value, entity) => {
return match(value, entity.entity_id);
},
state: async (hass, value, entity) => {
return match(value, entity.state);
},
name: async (hass, value, entity) => {
return match(value, entity.attributes?.friendly_name);
},
group: async (hass, value, entity) => {
return hass.states[value]?.attributes?.entity_id?.includes(
entity.entity_id
);
},
attributes: async (hass, value, entity) => {
for (const [k, v] of Object.entries(value as Record<string, any>)) {
let attr = k.split(" ")[0]; // Remove any suffixes
let obj = entity.attributes;
for (const step of attr.split(":")) {
obj = obj ? obj[step] : undefined;
}
if (obj === undefined || !match(v, obj)) return false;
}
return true;
},
not: async (hass, value, entity) => {
return !(await filter_entity(hass, value, entity.entity_id));
},
and: async (hass, value, entity) => {
for (const v of value) {
if (!(await filter_entity(hass, v, entity.entity_id))) return false;
}
return true;
},
or: async (hass, value, entity) => {
for (const v of value) {
if (await filter_entity(hass, v, entity.entity_id)) return true;
}
return false;
},
device: async (hass, value, entity) => {
const ent = (await getEntities(hass)).find(
(e) => e.entity_id === entity.entity_id
);
if (!ent) return false;
const device = (await getDevices(hass)).find((d) => d.id === ent.device_id);
if (!device) return false;
return match(value, device.name_by_user) || match(value, device.name);
},
device_manufacturer: async (hass, value, entity) => {
const ent = (await getEntities(hass)).find(
(e) => e.entity_id === entity.entity_id
);
if (!ent) return false;
const device = (await getDevices(hass)).find((d) => d.id === ent.device_id);
if (!device) return false;
return match(value, device.manufacturer);
},
device_model: async (hass, value, entity) => {
const ent = (await getEntities(hass)).find(
(e) => e.entity_id === entity.entity_id
);
if (!ent) return false;
const device = (await getDevices(hass)).find((d) => d.id === ent.device_id);
if (!device) return false;
return match(value, device.model);
},
area: async (hass, value, entity) => {
const ent = (await getEntities(hass)).find(
(e) => e.entity_id === entity.entity_id
);
if (!ent) return false;
let area = (await getAreas(hass)).find((a) => a.area_id === ent.area_id);
if (area) return match(value, area.name) || match(value, area.area_id);
const device = (await getDevices(hass)).find((d) => d.id === ent.device_id);
if (!device) return false;
area = (await getAreas(hass)).find((a) => a.area_id === device.area_id);
if (!area) return false;
return match(value, area.name) || match(value, area.area_id);
},
entity_category: async (hass, value, entity) => {
const ent = (await getEntities(hass)).find(
(e) => e.entity_id === entity.entity_id
);
if (!ent) return false;
return match(value, ent.entity_category);
},
last_changed: async (hass, value, entity) => {
if (!ago_suffix_regex.test(value)) value = value + default_ago_suffix;
return match(value, entity.last_changed);
},
last_updated: async (hass, value, entity) => {
if (!ago_suffix_regex.test(value)) value = value + default_ago_suffix;
return match(value, entity.last_updated);
},
last_triggered: async (hass, value, entity) => {
if (entity.attributes.last_triggered == null) return false;
if (!ago_suffix_regex.test(value)) value = value + default_ago_suffix;
return match(value, entity.attributes.last_triggered);
},
integration: async (hass, value, entity) => {
const ent = (await getEntities(hass)).find(
(e) => e.entity_id === entity.entity_id
);
if (!ent) return false;
return match(value, ent.platform);
},
hidden_by: async (hass, value, entity) => {
const ent = (await getEntities(hass)).find(
(e) => e.entity_id === entity.entity_id
);
if (!ent) return false;
return match(value, ent.hidden_by);
},
};
export async function filter_entity(
hass: HassObject,
filter: Record<string, any>,
entity_id: string
): Promise<boolean> {
if (!hass.states[entity_id]) return false;
for (let [k, v] of Object.entries(filter)) {
k = k.trim().split(" ")[0].trim();
if (!(await FILTERS[k]?.(hass, v, hass.states[entity_id]))) return false;
}
return true;
}