From 4db97bbbef4d5d501757a3ee4b193d15bb3f9387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=81nis=20Amsters?= Date: Thu, 16 Nov 2023 15:58:12 +0200 Subject: [PATCH] Enable custom colors for all numeric values (also outside of 0-100 range) --- src/colors.ts | 2 +- test/other/colors.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/colors.ts b/src/colors.ts index d2b98fdb..eba00902 100644 --- a/src/colors.ts +++ b/src/colors.ts @@ -13,7 +13,7 @@ import { log, safeGetConfigArrayOfObjects } from "./utils"; return config.charging_state.color; } - if (batteryLevel === undefined || isNaN(batteryLevel) || batteryLevel > 100 || batteryLevel < 0) { + if (batteryLevel === undefined || isNaN(batteryLevel)) { return defaultColor; } diff --git a/test/other/colors.test.ts b/test/other/colors.test.ts index 8f857617..7bab7f6f 100644 --- a/test/other/colors.test.ts +++ b/test/other/colors.test.ts @@ -37,6 +37,28 @@ describe("Colors", () => { expect(result).toBe(expectedColor); }) + test.each([ + [-220, "red"], + [-80, "red"], + [-79.9999, "yellow"], + [-65, "yellow"], + [-50, "green"], + [0, "green"], + [100, "green"], + ])("custom steps config", (batteryLevel: number, expectedColor: string) => { + + const colorsConfig: IColorSettings = { + steps: [ + { value: -80, color: "red" }, + { value: -65, color: "yellow" }, + { value: 1000, color: "green" } + ] + } + const result = getColorForBatteryLevel({ entity: "", colors: colorsConfig }, batteryLevel, false); + + expect(result).toBe(expectedColor); + }) + test.each([ [0, "#ff0000"], [25, "#ff7f00"],