-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
state_active.ts
58 lines (52 loc) · 1.93 KB
/
state_active.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
import type { HassEntity } from "home-assistant-js-websocket";
import { isUnavailableState, OFF, UNAVAILABLE } from "../../data/entity";
import { computeDomain } from "./compute_domain";
export function stateActive(stateObj: HassEntity, state?: string): boolean {
const domain = computeDomain(stateObj.entity_id);
const compareState = state !== undefined ? state : stateObj?.state;
if (["button", "event", "input_button", "scene"].includes(domain)) {
return compareState !== UNAVAILABLE;
}
if (isUnavailableState(compareState)) {
return false;
}
// The "off" check is relevant for most domains, but there are exceptions
// such as "alert" where "off" is still a somewhat active state and
// therefore gets a custom color and "idle" is instead the state that
// matches what most other domains consider inactive.
if (compareState === OFF && domain !== "alert") {
return false;
}
// Custom cases
switch (domain) {
case "alarm_control_panel":
return compareState !== "disarmed";
case "alert":
// "on" and "off" are active, as "off" just means alert was acknowledged but is still active
return compareState !== "idle";
case "cover":
return compareState !== "closed";
case "device_tracker":
case "person":
return compareState !== "not_home";
case "lawn_mower":
return ["mowing", "error"].includes(compareState);
case "lock":
return compareState !== "locked";
case "media_player":
return compareState !== "standby";
case "vacuum":
return !["idle", "docked", "paused"].includes(compareState);
case "valve":
return compareState !== "closed";
case "plant":
return compareState === "problem";
case "group":
return ["on", "home", "open", "locked", "problem"].includes(compareState);
case "timer":
return compareState === "active";
case "camera":
return compareState === "streaming";
}
return true;
}