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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only fire device_registry_updated for suggested_area if the suggestion results in an area change #69215

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions homeassistant/helpers/device_registry.py
Expand Up @@ -43,6 +43,8 @@

ORPHANED_DEVICE_KEEP_SECONDS = 86400 * 30

RUNTIME_ONLY_ATTRS = {"suggested_area"}


class _DeviceIndex(NamedTuple):
identifiers: dict[tuple[str, str], str]
Expand Down Expand Up @@ -509,6 +511,15 @@ def async_update_device(

new = attr.evolve(old, **new_values)
self._update_device(old, new)

# If its only run time attributes (suggested_area)
# that do not get saved we do not want to write
# to disk or fire an event as we would end up
# firing events for data we have nothing to compare
# against since its never saved on disk
if RUNTIME_ONLY_ATTRS.issuperset(new_values):
return new

self.async_schedule_save()

data: dict[str, Any] = {
Expand Down
10 changes: 10 additions & 0 deletions tests/helpers/test_device_registry.py
Expand Up @@ -1128,6 +1128,16 @@ async def test_update_suggested_area(hass, registry, area_registry, update_event
assert update_events[1]["device_id"] == entry.id
assert update_events[1]["changes"] == {"area_id": None, "suggested_area": None}

# Do not save or fire the event if the suggested
# area does not result in a change of area
# but still update the actual entry
with patch.object(registry, "async_schedule_save") as mock_save_2:
updated_entry = registry.async_update_device(entry.id, suggested_area="Other")
assert len(update_events) == 2
assert mock_save_2.call_count == 0
assert updated_entry != entry
assert updated_entry.suggested_area == "Other"


async def test_cleanup_device_registry(hass, registry):
"""Test cleanup works."""
Expand Down