Skip to content

Commit

Permalink
Fix area registry indexing when there is a name collision (#125050)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturpragacz authored Sep 2, 2024
1 parent 3e350bd commit fb27297
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions homeassistant/helpers/area_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,23 @@ def __init__(self) -> None:

def _index_entry(self, key: str, entry: AreaEntry) -> None:
"""Index an entry."""
super()._index_entry(key, entry)
if entry.floor_id is not None:
self._floors_index[entry.floor_id][key] = True
for label in entry.labels:
self._labels_index[label][key] = True
super()._index_entry(key, entry)

def _unindex_entry(
self, key: str, replacement_entry: AreaEntry | None = None
) -> None:
# always call base class before other indices
super()._unindex_entry(key, replacement_entry)
entry = self.data[key]
if labels := entry.labels:
for label in labels:
self._unindex_entry_value(key, label, self._labels_index)
if floor_id := entry.floor_id:
self._unindex_entry_value(key, floor_id, self._floors_index)
return super()._unindex_entry(key, replacement_entry)

def get_areas_for_label(self, label: str) -> list[AreaEntry]:
"""Get areas for label."""
Expand Down
7 changes: 6 additions & 1 deletion tests/helpers/test_area_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,12 @@ async def test_update_area_with_same_name_change_case(

async def test_update_area_with_name_already_in_use(
area_registry: ar.AreaRegistry,
floor_registry: fr.FloorRegistry,
) -> None:
"""Make sure that we can't update an area with a name already in use."""
area1 = area_registry.async_create("mock1")
floor = floor_registry.async_create("mock")
floor_id = floor.floor_id
area1 = area_registry.async_create("mock1", floor_id=floor_id)
area2 = area_registry.async_create("mock2")

with pytest.raises(ValueError) as e_info:
Expand All @@ -255,6 +258,8 @@ async def test_update_area_with_name_already_in_use(
assert area2.name == "mock2"
assert len(area_registry.areas) == 2

assert area_registry.areas.get_areas_for_floor(floor_id) == [area1]


async def test_update_area_with_normalized_name_already_in_use(
area_registry: ar.AreaRegistry,
Expand Down

0 comments on commit fb27297

Please sign in to comment.