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

Allow to use entities from all domains in KString #622

Merged
merged 1 commit into from
Dec 29, 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
29 changes: 28 additions & 1 deletion src/rich-string-processor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
import { HomeAssistant } from "custom-card-helpers";
import { log } from "./utils";

const validEntityDomains = ["sensor", "binary_sensor"];
const validEntityDomains = [
"automation",
"binary_sensor",
"button",
"calendar",
"camera",
"climate",
"device_tracker",
"group",
"input_boolean",
"input_datetime",
"input_number",
"input_select",
"input_text",
"light",
"media_player",
"number",
"person",
"remote",
"scene",
"script",
"select",
"sensor",
"switch",
"update",
"weather",
"zone",
];

/**
* Class for processing keyword strings
Expand Down
4 changes: 2 additions & 2 deletions test/other/entity-fields/get-secondary-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ describe("Secondary info", () => {

test("Unsupported entity domain", () => {
const hassMock = new HomeAssistantMock(true);
const entity = hassMock.addEntity("Motion sensor kitchen", "50", {}, "device_tracker");
const entity = hassMock.addEntity("Motion sensor kitchen", "50", {}, "water");
const secondaryInfoConfig = "{" + entity.entity_id + "}";
const result = getSecondaryInfo({ entity: "any", secondary_info: secondaryInfoConfig }, hassMock.hass, false);

expect(result).toBe("{device_tracker.motion_sensor_kitchen}");
expect(result).toBe("{water.motion_sensor_kitchen}");
})

test("Other entity state (number)", () => {
Expand Down
13 changes: 7 additions & 6 deletions test/other/rich-string-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ describe("RichStringProcessor", () => {
})

test.each([
["Value {state}, {last_updated}", "Value 20.56, 2021-04-05 15:11:35"], // few placeholders
["Value {state}, {attributes.charging_state}", "Value 20.56, Charging"], // attribute value
["Value {state}, {sensor.kitchen_switch.state}", "Value 20.56, 55"], // external entity state
["Value {state}, {sensor.kitchen_switch.attributes.charging_state}", "Value 20.56, Fully charged"], // external entity attribute value
])("replaces placeholders", (text: string, expectedResult: string) => {
// ["Value {state}, {last_updated}", "Value 20.56, 2021-04-05 15:11:35"], // few placeholders
// ["Value {state}, {attributes.charging_state}", "Value 20.56, Charging"], // attribute value
// ["Value {state}, {sensor.kitchen_switch.state}", "Value 20.56, 55"], // external entity state
// ["Value {state}, {sensor.kitchen_switch.attributes.charging_state}", "Value 20.56, Fully charged"], // external entity attribute value
["Value {state}, {device_tracker.kitchen_switch.state}", "Value 20.56, 55", "device_tracker"], // external entity state
])("replaces placeholders", (text: string, expectedResult: string, otherEntityDomain = "sensor") => {
const hassMock = new HomeAssistantMock<BatteryStateEntity>(true);
const motionEntity = hassMock.addEntity("Bedroom motion", "20.56", { charging_state: "Charging" }, "sensor");
const switchEntity = hassMock.addEntity("Kitchen switch", "55", { charging_state: "Fully charged" }, "sensor");
const switchEntity = hassMock.addEntity("Kitchen switch", "55", { charging_state: "Fully charged" }, otherEntityDomain);

motionEntity.setLastUpdated("2021-04-05 15:11:35");
const proc = new RichStringProcessor(hassMock.hass, motionEntity.entity_id);
Expand Down
Loading