Skip to content

Commit

Permalink
Merge pull request #622 from maxwroc/RichProcAllDomains
Browse files Browse the repository at this point in the history
Allow to use entities from all domains in KString
  • Loading branch information
maxwroc authored Dec 29, 2023
2 parents 2c3497e + 57df8ea commit eb640e6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
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

0 comments on commit eb640e6

Please sign in to comment.