From c03a8c991971253dce070eae0548eb7d79f488d4 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Sat, 13 Jan 2024 19:57:10 +0000 Subject: [PATCH 1/2] Fixed icon color for decimal entity values --- src/entity-fields/battery-level.ts | 4 ++-- src/sorting.ts | 6 +++--- test/other/entity-fields/battery-level.test.ts | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/entity-fields/battery-level.ts b/src/entity-fields/battery-level.ts index 942946b..693621e 100644 --- a/src/entity-fields/battery-level.ts +++ b/src/entity-fields/battery-level.ts @@ -24,7 +24,7 @@ export const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssista const processedValue = stringProcessor.process(config.value_override.toString()); return { state: processedValue, - level: isNumber(processedValue) ? Number(processedValue) : undefined, + level: isNumber(processedValue) ? toNumber(processedValue) : undefined, unit: getUnit(processedValue, undefined, undefined, config, hass), } } @@ -107,7 +107,7 @@ export const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssista return { state: displayValue || state, - level: isNumber(state) ? Number(state) : undefined, + level: isNumber(state) ? toNumber(state) : undefined, unit: getUnit(state, displayValue, unit, config, hass), }; } diff --git a/src/sorting.ts b/src/sorting.ts index 9376854..8586aea 100644 --- a/src/sorting.ts +++ b/src/sorting.ts @@ -1,5 +1,5 @@ import { IBatteryCollection } from "./battery-provider"; -import { isNumber, log, safeGetConfigArrayOfObjects } from "./utils"; +import { isNumber, log, safeGetConfigArrayOfObjects, toNumber } from "./utils"; /** * Sorts batteries by given criterias and returns their IDs @@ -76,8 +76,8 @@ import { isNumber, log, safeGetConfigArrayOfObjects } from "./utils"; * @returns Comparison result */ const compareNumbers = (a: string, b: string): number => { - let aNum = Number(a); - let bNum = Number(b); + let aNum = toNumber(a); + let bNum = toNumber(b); aNum = isNaN(aNum) ? -1 : aNum; bNum = isNaN(bNum) ? -1 : bNum; return aNum - bNum; diff --git a/test/other/entity-fields/battery-level.test.ts b/test/other/entity-fields/battery-level.test.ts index af63c75..013968b 100644 --- a/test/other/entity-fields/battery-level.test.ts +++ b/test/other/entity-fields/battery-level.test.ts @@ -194,12 +194,13 @@ describe("Battery level", () => { [false, "45", "dbm", { state: "45", level: 45, unit: "%" }], // test when the setting is turned off [true, "45", "dbm", { state: "56", level: 56, unit: "%" }, [ { from: "45", to: "56" } ]], // test when the state was changed by state_map [true, "45", "dbm", { state: "Low", level: 45, unit: undefined }, [ { from: "45", to: "45", display: "Low" } ]], // test when the display value was changed by state_map + [true, "45.4", "dbm", { state: "45,4", level: 45.4, unit: "[dbm]" }, undefined, ","], // test when default HA formatting returns state with comma as decimal point ]) - ("default HA formatting", (defaultStateFormatting: boolean | undefined, entityState: string, unitOfMeasurement: string, expected: { state: string, level: number, unit?: string }, stateMap: IConvert[] | undefined = undefined) => { + ("default HA formatting", (defaultStateFormatting: boolean | undefined, entityState: string, unitOfMeasurement: string, expected: { state: string, level: number, unit?: string }, stateMap: IConvert[] | undefined = undefined, decimalPoint: string = ".") => { const hassMock = new HomeAssistantMock(true); hassMock.addEntity("Mocked entity", entityState); - hassMock.mockFunc("formatEntityState", (entityData: any) => `${entityData.state} [${unitOfMeasurement}]`); + hassMock.mockFunc("formatEntityState", (entityData: any) => `${entityData.state.replace(".", decimalPoint)} [${unitOfMeasurement}]`); const { state, level, unit } = getBatteryLevel({ entity: "mocked_entity", default_state_formatting: defaultStateFormatting, state_map: stateMap }, hassMock.hass); From aab43ca4f4f8858b296a50a3f855fa985fb510ec Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Sat, 13 Jan 2024 19:58:18 +0000 Subject: [PATCH 2/2] Bumped version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c6251b4..55c49b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "battery-state-card", - "version": "3.1.4", + "version": "3.1.5", "description": "Battery State card for Home Assistant", "main": "dist/battery-state-card.js", "author": "Max Chodorowski",