Skip to content

Commit

Permalink
Merge pull request #613 from maxwroc/KStringInName
Browse files Browse the repository at this point in the history
KString support in entity name
  • Loading branch information
maxwroc authored Dec 20, 2023
2 parents bdd7a59 + 20c0ddd commit ed04eee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/entity-fields/get-name.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HomeAssistant } from "custom-card-helpers";
import { getRegexFromString, safeGetArray } from "../utils";
import { RichStringProcessor } from "../rich-string-processor";


/**
Expand All @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions test/other/entity-fields/get-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});

0 comments on commit ed04eee

Please sign in to comment.