Skip to content

Conversation

@emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Jul 31, 2025

Proposed change

Make device suggested_area only influence new devices

This means passing a suggested_area to DeviceRegistry.async_update_device no longer has any meaning, and that parameter has been deprecated and will be removed in HA Core 2026.9.

Rationale:
We should create new areas and use that for the device only for newly created devices

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI review requested due to automatic review settings July 31, 2025 15:06
@emontnemery emontnemery requested a review from a team as a code owner July 31, 2025 15:06
@home-assistant home-assistant bot added cla-signed core new-feature small-pr PRs with less than 30 lines. labels Jul 31, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modifies the device registry behavior to limit the automatic area creation and assignment based on suggested_area to only apply to newly created devices, rather than existing devices being updated.

Key Changes

  • Updated device registry logic to check if a device is new before automatically creating areas from suggested_area
  • Modified test cases to verify that existing devices do not get areas auto-created when suggested_area is updated
  • Added parameterized test cases to cover both new devices (with no initial area) and existing devices (with existing areas)

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
homeassistant/helpers/device_registry.py Added old.is_new condition to restrict automatic area creation to new devices only
tests/helpers/test_device_registry.py Enhanced test coverage with parameterized cases and updated assertions to verify new behavior


if (
suggested_area is not None
old.is_new
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition old.is_new appears to be checking a property that may not exist on device objects. Device objects typically don't have an is_new attribute. This should likely be checking if the device was just created in the current operation, or use a different approach to determine if this is a new device versus an existing one being updated.

Suggested change
old.is_new
old.created_at is not None
and (utcnow() - old.created_at).total_seconds() < 300 # 5 minutes threshold

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fuck?

and suggested_area is not None
and suggested_area is not UNDEFINED
and suggested_area != ""
and area_id is UNDEFINED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and area_id is UNDEFINED

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's correct. It's possible to specify an area_id directly, and that should have priority over suggested_area.

Copy link
Contributor

@arturpragacz arturpragacz Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to specify an area_id directly

I don't think it's possible for new devices, which are the only ones relevant now.

In other words if old.is_new, then area_id is always UNDEFINED.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, check 👍

Copy link
Contributor Author

@emontnemery emontnemery Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also don't need to check old.area_id for the same reason?
Edit: we need to check that when restoring a deleted device

@arturpragacz
Copy link
Contributor

arturpragacz commented Aug 1, 2025

Could probably also wait until #149730 is merged, and instead move this whole code from async_update_device into async_get_or_create.

if (
old.is_new
and suggested_area is not None
and suggested_area is not UNDEFINED
and suggested_area != ""
and area_id is UNDEFINED
and old.area_id is None
):
# Circular dep
from . import area_registry as ar # noqa: PLC0415
area = ar.async_get(self.hass).async_get_or_create(suggested_area)
area_id = area.id

@emontnemery
Copy link
Contributor Author

Could probably also wait until #149730 is merged, and instead move this whole code from async_update_device into async_get_or_create instead.

if (
old.is_new
and suggested_area is not None
and suggested_area is not UNDEFINED
and suggested_area != ""
and area_id is UNDEFINED
and old.area_id is None
):
# Circular dep
from . import area_registry as ar # noqa: PLC0415
area = ar.async_get(self.hass).async_get_or_create(suggested_area)
area_id = area.id

OK, we then also need to deprecate passing a suggested_area to DeviceRegistry.async_update_device

@emontnemery emontnemery added this to the 2025.8.0 milestone Aug 1, 2025
@home-assistant
Copy link

home-assistant bot commented Aug 1, 2025

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft August 1, 2025 10:24
@emontnemery emontnemery marked this pull request as ready for review August 1, 2025 10:49
@home-assistant home-assistant bot requested a review from abmantis August 1, 2025 10:49
Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>
@emontnemery emontnemery merged commit f538807 into dev Aug 1, 2025
48 checks passed
@emontnemery emontnemery deleted the suggested_area_new_devices_only branch August 1, 2025 12:55
@github-actions github-actions bot locked and limited conversation to collaborators Aug 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants