From d5e784c61d6561b7ebd77855262a129e26993ed0 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Fri, 9 Feb 2024 15:50:24 +0000 Subject: [PATCH 1/3] Fixed HA formatted state parsing --- src/entity-fields/battery-level.ts | 14 +++++++++++--- .../other/entity-fields/battery-level.test.ts | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/entity-fields/battery-level.ts b/src/entity-fields/battery-level.ts index 693621e..be93f20 100644 --- a/src/entity-fields/battery-level.ts +++ b/src/entity-fields/battery-level.ts @@ -7,6 +7,12 @@ import { isNumber, log, toNumber } from "../utils"; */ const stringValuePattern = /\b([0-9]{1,3})\s?%/; + +/** + * HA formatted state pattern + */ +const formattedStatePattern = /(-?[0-9,.]+)\s?(.*)/; + /** * Getts battery level/state * @param config Entity config @@ -100,9 +106,11 @@ export const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssista if (config.default_state_formatting !== false && !displayValue && state === entityData.state) { const formattedState = hass.formatEntityState(entityData); - // assuming it is a number followed by unit - [state, unit] = formattedState.split(" ", 2); - unit = unit; + const matches = formattedState.match(formattedStatePattern); + if (matches != null) { + state = matches[1]; + unit = matches[2] || unit; + } } return { diff --git a/test/other/entity-fields/battery-level.test.ts b/test/other/entity-fields/battery-level.test.ts index 013968b..1c84152 100644 --- a/test/other/entity-fields/battery-level.test.ts +++ b/test/other/entity-fields/battery-level.test.ts @@ -261,4 +261,23 @@ describe("Battery level", () => { expect(state).toBe("2"); }) + + test.each([ + ["55 %", { state: "55", level: 55, unit: "%" }], + ["66%", { state: "66", level: 66, unit: "%" }], + ["-77lqi", { state: "-77", level: -77, unit: "lqi" }], + ["-88.8lqi", { state: "-88.8", level: -88.8, unit: "lqi" }], + ]) + ("default HA formatting - various formatted states", (formattedResult: string, expected: { state: string, level: number, unit?: string }) => { + + const hassMock = new HomeAssistantMock(true); + hassMock.addEntity("Mocked entity", "45"); + hassMock.mockFunc("formatEntityState", () => formattedResult); + + const { state, level, unit } = getBatteryLevel({ entity: "mocked_entity" }, hassMock.hass); + + expect(level).toBe(expected.level); + expect(state).toBe(expected.state); + expect(unit).toBe(expected.unit); + }); }); \ No newline at end of file From d5b33b5572ca83698ecd377fa4063db248044668 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Fri, 9 Feb 2024 15:53:17 +0000 Subject: [PATCH 2/3] Bumped ver --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 55c49b5..d562ae2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "battery-state-card", - "version": "3.1.5", + "version": "3.1.6", "description": "Battery State card for Home Assistant", "main": "dist/battery-state-card.js", "author": "Max Chodorowski", From 3ccc244b06480ed7d8f65dc0093c19515fd0756f Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Fri, 9 Feb 2024 16:02:11 +0000 Subject: [PATCH 3/3] One more test --- test/other/entity-fields/battery-level.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/other/entity-fields/battery-level.test.ts b/test/other/entity-fields/battery-level.test.ts index 1c84152..8694a6e 100644 --- a/test/other/entity-fields/battery-level.test.ts +++ b/test/other/entity-fields/battery-level.test.ts @@ -267,6 +267,7 @@ describe("Battery level", () => { ["66%", { state: "66", level: 66, unit: "%" }], ["-77lqi", { state: "-77", level: -77, unit: "lqi" }], ["-88.8lqi", { state: "-88.8", level: -88.8, unit: "lqi" }], + ["99", { state: "99", level: 99, unit: "%" }], ]) ("default HA formatting - various formatted states", (formattedResult: string, expected: { state: string, level: number, unit?: string }) => {