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", 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);