From a106de13c1e8395f0281d6d6dc0ba2d75d592c36 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Thu, 6 Jun 2024 10:38:35 -0500 Subject: [PATCH] Check aliases in get_by_name for areas/floors --- homeassistant/helpers/area_registry.py | 20 +++++++++++++++---- homeassistant/helpers/floor_registry.py | 15 ++++++++++++-- tests/components/config/test_area_registry.py | 6 ++++++ .../components/config/test_floor_registry.py | 6 ++++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/homeassistant/helpers/area_registry.py b/homeassistant/helpers/area_registry.py index 975750ebbdd73c..6a9ba379f9aab1 100644 --- a/homeassistant/helpers/area_registry.py +++ b/homeassistant/helpers/area_registry.py @@ -197,8 +197,19 @@ def async_get_area(self, area_id: str) -> AreaEntry | None: @callback def async_get_area_by_name(self, name: str) -> AreaEntry | None: - """Get area by name.""" - return self.areas.get_by_name(name) + """Get area by name or alias.""" + if area := self.areas.get_by_name(name): + return area + + # Check aliases + normalized_name = normalize_name(name) + for area in self.async_list_areas(): + for alias in area.aliases: + normalized_alias = normalize_name(alias) + if normalized_name == normalized_alias: + return area + + return None @callback def async_list_areas(self) -> Iterable[AreaEntry]: @@ -404,8 +415,9 @@ def _async_setup_cleanup(self) -> None: @callback def _removed_from_registry_filter( - event_data: fr.EventFloorRegistryUpdatedData - | lr.EventLabelRegistryUpdatedData, + event_data: ( + fr.EventFloorRegistryUpdatedData | lr.EventLabelRegistryUpdatedData + ), ) -> bool: """Filter all except for the item removed from registry events.""" return event_data["action"] == "remove" diff --git a/homeassistant/helpers/floor_registry.py b/homeassistant/helpers/floor_registry.py index 9bf8a2a5d2611d..5fd8bfaaf690ff 100644 --- a/homeassistant/helpers/floor_registry.py +++ b/homeassistant/helpers/floor_registry.py @@ -93,8 +93,19 @@ def async_get_floor(self, floor_id: str) -> FloorEntry | None: @callback def async_get_floor_by_name(self, name: str) -> FloorEntry | None: - """Get floor by name.""" - return self.floors.get_by_name(name) + """Get floor by name or alias.""" + if floor := self.floors.get_by_name(name): + return floor + + # Check aliases + normalized_name = normalize_name(name) + for floor in self.async_list_floors(): + for alias in floor.aliases: + normalized_alias = normalize_name(alias) + if normalized_name == normalized_alias: + return floor + + return None @callback def async_list_floors(self) -> Iterable[FloorEntry]: diff --git a/tests/components/config/test_area_registry.py b/tests/components/config/test_area_registry.py index fb59725fd29a3b..75725dfae6ae8d 100644 --- a/tests/components/config/test_area_registry.py +++ b/tests/components/config/test_area_registry.py @@ -260,3 +260,9 @@ async def test_update_area_with_name_already_in_use( assert msg["error"]["code"] == "invalid_info" assert msg["error"]["message"] == "The name mock 2 (mock2) is already in use" assert len(area_registry.areas) == 2 + + +async def test_get_area_by_alias(area_registry: ar.AreaRegistry) -> None: + """Test async_get_area_by_name also works with aliases.""" + area = area_registry.async_create("area 1", aliases={"alias 1"}) + assert area_registry.async_get_area_by_name("alias 1") == area diff --git a/tests/components/config/test_floor_registry.py b/tests/components/config/test_floor_registry.py index b4e3907bc4d7d0..770b8807ad971e 100644 --- a/tests/components/config/test_floor_registry.py +++ b/tests/components/config/test_floor_registry.py @@ -239,3 +239,9 @@ async def test_update_with_name_already_in_use( == "The name Second floor (secondfloor) is already in use" ) assert len(floor_registry.floors) == 2 + + +async def test_get_floor_by_alias(floor_registry: fr.FloorRegistry) -> None: + """Test async_get_floor_by_name also works with aliases.""" + floor = floor_registry.async_create("floor 1", aliases={"alias 1"}) + assert floor_registry.async_get_floor_by_name("alias 1") == floor