Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support KString in entity name #613

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
})
});
Loading