Fix entity active states (#14341)

* fix media player state

* Fix active states

* Consider idle media player as active

* improve custom cases
This commit is contained in:
Paul Bottein 2022-11-17 15:49:33 +01:00 committed by GitHub
parent 185d2f1d52
commit c6386284d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,33 +1,30 @@
import { HassEntity } from "home-assistant-js-websocket";
import { OFF_STATES } from "../../data/entity";
import { OFF_STATES, UNAVAILABLE } from "../../data/entity";
import { computeDomain } from "./compute_domain";
const NORMAL_UNKNOWN_DOMAIN = ["button", "input_button", "scene"];
const NORMAL_OFF_DOMAIN = ["script"];
export function stateActive(stateObj: HassEntity, state?: string): boolean {
const domain = computeDomain(stateObj.entity_id);
const compareState = state !== undefined ? state : stateObj?.state;
if (
OFF_STATES.includes(compareState) &&
!(NORMAL_UNKNOWN_DOMAIN.includes(domain) && compareState === "unknown") &&
!(NORMAL_OFF_DOMAIN.includes(domain) && compareState === "script")
) {
if (["button", "input_button", "scene"].includes(domain)) {
return compareState !== UNAVAILABLE;
}
if (OFF_STATES.includes(compareState)) {
return false;
}
// Custom cases
switch (domain) {
case "cover":
return compareState === "open" || compareState === "opening";
return !["close", "closing"].includes(compareState);
case "device_tracker":
case "person":
return compareState !== "not_home";
case "media-player":
return compareState !== "idle" && compareState !== "standby";
case "media_player":
return compareState !== "standby";
case "vacuum":
return compareState === "on" || compareState === "cleaning";
return !["idle", "docked", "paused"].includes(compareState);
case "plant":
return compareState === "problem";
default: