Skip to content

Commit

Permalink
Merge pull request #599 from amsterjanis/Enable-custom-colors-for-all…
Browse files Browse the repository at this point in the history
…-numeric-values

Enable custom colors for all numeric values (also outside of 0-100 range)
  • Loading branch information
maxwroc authored Nov 16, 2023
2 parents b27f606 + 4db97bb commit 447fd45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
22 changes: 22 additions & 0 deletions test/other/colors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 447fd45

Please sign in to comment.