From 608934defa0ff2c6f42e29d57b422387e8b4137f Mon Sep 17 00:00:00 2001 From: Ricardo Hermida Ruiz Date: Tue, 7 Jan 2025 21:26:54 -0300 Subject: [PATCH] Use device area if available for entities that have no area directly set --- .../matcher/matches-entity-filter.test.ts | 59 +++++++++++++++++++ .../bridge/matcher/matches-entity-filter.ts | 5 +- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/matter/bridge/matcher/matches-entity-filter.test.ts b/packages/backend/src/matter/bridge/matcher/matches-entity-filter.test.ts index 55b9ee7..60badfa 100644 --- a/packages/backend/src/matter/bridge/matcher/matches-entity-filter.test.ts +++ b/packages/backend/src/matter/bridge/matcher/matches-entity-filter.test.ts @@ -5,6 +5,7 @@ import { HomeAssistantEntityRegistry, HomeAssistantEntityState, HomeAssistantMatcherType, + HomeAssistantDeviceRegistry, } from "@home-assistant-matter-hub/common"; const registry: HomeAssistantEntityRegistry = { @@ -19,6 +20,8 @@ const registry: HomeAssistantEntityRegistry = { labels: ["test_label"], }; +const registryWithArea = { ...registry, area_id: "area_id" }; + const state: HomeAssistantEntityState = { entity_id: "light.my_entity", state: "on", @@ -28,12 +31,20 @@ const state: HomeAssistantEntityState = { attributes: {}, }; +const deviceRegistry: HomeAssistantDeviceRegistry = { + area_id: "area_id", +}; + const entity: HomeAssistantEntityInformation = { entity_id: "light.my_entity", registry, state, }; +const entityWithArea = { ...entity, registry: registryWithArea }; + +const entityWithDevice = { ...entity, deviceRegistry }; + describe("matchEntityFilter.testMatcher", () => { it("should match the domain", () => { expect( @@ -86,6 +97,54 @@ describe("matchEntityFilter.testMatcher", () => { ).toBeFalsy(); }); + it("should match the area", () => { + expect( + testMatcher(entityWithArea, { + type: HomeAssistantMatcherType.Area, + value: "area_id", + }), + ).toBeTruthy(); + }); + it("should not match the area", () => { + expect( + testMatcher(entityWithArea, { + type: HomeAssistantMatcherType.Area, + value: "another_area_id", + }), + ).toBeFalsy(); + }); + it("should match the device area when entity has no area", () => { + expect( + testMatcher(entityWithDevice, { + type: HomeAssistantMatcherType.Area, + value: "area_id", + }), + ).toBeTruthy(); + }); + it("should not match the device area when entity has no area", () => { + expect( + testMatcher(entityWithDevice, { + type: HomeAssistantMatcherType.Area, + value: "another_area_id", + }), + ).toBeFalsy(); + }); + it("should match when entity and device are in different areas", () => { + expect( + testMatcher(entityWithArea, { + type: HomeAssistantMatcherType.Area, + value: "area_id", + }), + ).toBeTruthy(); + }); + it("should not match when entity and device are in different areas", () => { + expect( + testMatcher(entityWithArea, { + type: HomeAssistantMatcherType.Area, + value: "another_area_id", + }), + ).toBeFalsy(); + }); it("should match the entity category", () => { expect( testMatcher(entity, { diff --git a/packages/backend/src/matter/bridge/matcher/matches-entity-filter.ts b/packages/backend/src/matter/bridge/matcher/matches-entity-filter.ts index c600df8..c9a9110 100644 --- a/packages/backend/src/matter/bridge/matcher/matches-entity-filter.ts +++ b/packages/backend/src/matter/bridge/matcher/matches-entity-filter.ts @@ -36,7 +36,10 @@ export function testMatcher( case "pattern": return patternToRegex(matcher.value).test(entity.entity_id); case "area": - return entity.registry?.area_id === matcher.value; + return ( + (entity.registry?.area_id ?? entity.deviceRegistry?.area_id) === + matcher.value + ); } return false; }