From 20c0dddb7f544e6b4c2bda1c8a57d8f5246c7962 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Wed, 20 Dec 2023 16:55:36 +0000 Subject: [PATCH] KString support in entity name --- package.json | 2 +- src/entity-fields/get-name.ts | 4 +++- test/other/entity-fields/get-name.test.ts | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2b26d35b..dba488cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "battery-state-card", - "version": "3.0.2", + "version": "3.1.0", "description": "Battery State card for Home Assistant", "main": "dist/battery-state-card.js", "author": "Max Chodorowski", diff --git a/src/entity-fields/get-name.ts b/src/entity-fields/get-name.ts index 1c520ae4..d8a4050b 100644 --- a/src/entity-fields/get-name.ts +++ b/src/entity-fields/get-name.ts @@ -1,5 +1,6 @@ import { HomeAssistant } from "custom-card-helpers"; import { getRegexFromString, safeGetArray } from "../utils"; +import { RichStringProcessor } from "../rich-string-processor"; /** @@ -10,7 +11,8 @@ import { getRegexFromString, safeGetArray } from "../utils"; */ export const getName = (config: IBatteryEntityConfig, hass: HomeAssistant | undefined): string => { if (config.name) { - return config.name; + const proc = new RichStringProcessor(hass, config.entity); + return proc.process(config.name); } if (!hass) { diff --git a/test/other/entity-fields/get-name.test.ts b/test/other/entity-fields/get-name.test.ts index 3f70f6f5..b4e186b8 100644 --- a/test/other/entity-fields/get-name.test.ts +++ b/test/other/entity-fields/get-name.test.ts @@ -69,4 +69,18 @@ describe("Get name", () => { expect(name).toBe(expectedResult); }); + + test.each([ + ["State in the name {state}%", "State in the name 45%"], + ["KString func {state|multiply(2)}%", "KString func 90%"], + ["KString other entity {sensor.other_entity.state}", "KString other entity CR2032"], + ])("KString in the name", (name: string, expectedResult: string) => { + const hassMock = new HomeAssistantMock(true); + hassMock.addEntity("My entity", "45"); + + hassMock.addEntity("Other entity", "CR2032", undefined, "sensor"); + + let result = getName({entity: "my_entity", name}, hassMock.hass); + expect(result).toBe(expectedResult); + }) }); \ No newline at end of file