Fix binary sensor color when off (#14735)

This commit is contained in:
Paul Bottein 2022-12-13 12:11:09 +01:00 committed by GitHub
parent 5cabf1d041
commit d64bb98bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 8 deletions

View File

@ -1,7 +1,7 @@
export const batteryStateColor = (state: string) => { export const batteryStateColor = (state: string) => {
const value = Number(state); const value = Number(state);
if (isNaN(value)) { if (isNaN(value)) {
return "sensor-battery-unknown"; return undefined;
} }
if (value >= 70) { if (value >= 70) {
return "sensor-battery-high"; return "sensor-battery-high";

View File

@ -1,4 +1,5 @@
import { HassEntity } from "home-assistant-js-websocket"; import { HassEntity } from "home-assistant-js-websocket";
import { stateActive } from "../state_active";
const ALERTING_DEVICE_CLASSES = new Set([ const ALERTING_DEVICE_CLASSES = new Set([
"battery", "battery",
@ -12,9 +13,15 @@ const ALERTING_DEVICE_CLASSES = new Set([
"tamper", "tamper",
]); ]);
export const binarySensorColor = (stateObj: HassEntity): string | undefined => { export const binarySensorColor = (
stateObj: HassEntity,
state: string
): string | undefined => {
const deviceClass = stateObj?.attributes.device_class; const deviceClass = stateObj?.attributes.device_class;
if (!stateActive(stateObj, state)) {
return undefined;
}
return deviceClass && ALERTING_DEVICE_CLASSES.has(deviceClass) return deviceClass && ALERTING_DEVICE_CLASSES.has(deviceClass)
? "binary-sensor-alerting" ? "binary-sensor-alerting"
: "binary-sensor"; : "binary-sensor";

View File

@ -0,0 +1,15 @@
import { HassEntity } from "home-assistant-js-websocket";
import { UpdateEntity, updateIsInstalling } from "../../../data/update";
import { stateActive } from "../state_active";
export const updateColor = (
stateObj: HassEntity,
state: string
): string | undefined => {
if (!stateActive(stateObj, state)) {
return undefined;
}
return updateIsInstalling(stateObj as UpdateEntity)
? "update-installing"
: "update";
};

View File

@ -1,13 +1,13 @@
/** Return an color representing a state. */ /** Return an color representing a state. */
import { HassEntity } from "home-assistant-js-websocket"; import { HassEntity } from "home-assistant-js-websocket";
import { UNAVAILABLE } from "../../data/entity"; import { UNAVAILABLE } from "../../data/entity";
import { UpdateEntity, updateIsInstalling } from "../../data/update";
import { alarmControlPanelColor } from "./color/alarm_control_panel_color"; import { alarmControlPanelColor } from "./color/alarm_control_panel_color";
import { binarySensorColor } from "./color/binary_sensor_color"; import { binarySensorColor } from "./color/binary_sensor_color";
import { climateColor } from "./color/climate_color"; import { climateColor } from "./color/climate_color";
import { lockColor } from "./color/lock_color"; import { lockColor } from "./color/lock_color";
import { personColor } from "./color/person_color"; import { personColor } from "./color/person_color";
import { sensorColor } from "./color/sensor_color"; import { sensorColor } from "./color/sensor_color";
import { updateColor } from "./color/update_color";
import { computeDomain } from "./compute_domain"; import { computeDomain } from "./compute_domain";
import { stateActive } from "./state_active"; import { stateActive } from "./state_active";
@ -66,7 +66,7 @@ export const stateColor = (stateObj: HassEntity, state?: string) => {
return alarmControlPanelColor(compareState); return alarmControlPanelColor(compareState);
case "binary_sensor": case "binary_sensor":
return binarySensorColor(stateObj); return binarySensorColor(stateObj, compareState);
case "climate": case "climate":
return climateColor(compareState); return climateColor(compareState);
@ -85,9 +85,7 @@ export const stateColor = (stateObj: HassEntity, state?: string) => {
return compareState === "above_horizon" ? "sun-day" : "sun-night"; return compareState === "above_horizon" ? "sun-day" : "sun-night";
case "update": case "update":
return updateIsInstalling(stateObj as UpdateEntity) return updateColor(stateObj, compareState);
? "update-installing"
: "update";
} }
return undefined; return undefined;

View File

@ -176,7 +176,6 @@ documentContainer.innerHTML = `<custom-style>
--rgb-state-sensor-battery-high-color: var(--rgb-green-color); --rgb-state-sensor-battery-high-color: var(--rgb-green-color);
--rgb-state-sensor-battery-low-color: var(--rgb-red-color); --rgb-state-sensor-battery-low-color: var(--rgb-red-color);
--rgb-state-sensor-battery-medium-color: var(--rgb-orange-color); --rgb-state-sensor-battery-medium-color: var(--rgb-orange-color);
--rgb-state-sensor-battery-unknown-color: var(--rgb-off-color);
--rgb-state-siren-color: var(--rgb-red-color); --rgb-state-siren-color: var(--rgb-red-color);
--rgb-state-sun-day-color: var(--rgb-amber-color); --rgb-state-sun-day-color: var(--rgb-amber-color);
--rgb-state-sun-night-color: var(--rgb-deep-purple-color); --rgb-state-sun-night-color: var(--rgb-deep-purple-color);