Skip to content

Update async_update_entity_platform to not allow loaded entities#171773

Draft
gjohansson-ST wants to merge 1 commit into
devfrom
gj-20260521-03
Draft

Update async_update_entity_platform to not allow loaded entities#171773
gjohansson-ST wants to merge 1 commit into
devfrom
gj-20260521-03

Conversation

@gjohansson-ST
Copy link
Copy Markdown
Member

Breaking change

Proposed change

Updates the async_update_entity_platform method in entity registry to not only look at entities with state unknown.
It's not sufficient to know if an entity is loaded or not

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

  • I understand the code I am submitting and can explain how it works.
  • 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][dev-checklist]
  • I have followed the [perfect PR recommendations][perfect-pr]
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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

  • Documentation added/updated for [www.home-assistant.io][docs-repository]

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

  • The [manifest file][manifest-docs] 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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

  • I have reviewed two other [open pull requests][prs] in this repository.

Copy link
Copy Markdown
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 changes entity platform migration in the entity registry to reject migrations for entities that are actually loaded in Home Assistant (rather than inferring “loaded” from the entity’s current state value).

Changes:

  • Update EntityRegistry.async_update_entity_platform to block migration when the entity is present in entity_sources() (i.e., loaded).
  • Adjust entity registry migration tests to exercise the new “loaded entity” behavior using MockEntityPlatform/MockEntity.

Reviewed changes

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

File Description
homeassistant/helpers/entity_registry.py Switches the migration guard from a state-based heuristic to checking loaded entities via entity_sources().
tests/helpers/test_entity_registry.py Updates migration error-handling coverage to create a genuinely loaded entity and assert migration is blocked.

Comment on lines +3831 to +3838
with pytest.raises(
ValueError,
match="Only entities that haven't been loaded can be migrated",
):
entity_registry.async_update_entity_platform(
"light.light",
"hue3",
)
# import here to avoid circular import
from .entity import entity_sources # noqa: PLC0415

if (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (
if entity_id in entity_sources(self.hass):

Comment on lines +1957 to +1958
loaded_entities := entity_sources(self.hass)
) and entity_id in loaded_entities:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
loaded_entities := entity_sources(self.hass)
) and entity_id in loaded_entities:

original_icon="initial-original_icon",
original_name="initial-original_name",
platform.config_entry = orig_config_entry
entity_entry = MockEntity(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
entity_entry = MockEntity(
entity = MockEntity(

platform = MockEntityPlatform(hass, domain="light", platform_name="hue")
orig_config_entry = MockConfigEntry(
domain="light",
domain="test",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this changed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Should actually be hue but I'll fix that. Was missed flipping around with the tests (has anyway no real impact)

Comment on lines +3831 to +3838
with pytest.raises(
ValueError,
match="Only entities that haven't been loaded can be migrated",
):
entity_registry.async_update_entity_platform(
"light.light",
"hue3",
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems superfluous to me given the next lines.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I put it there to reflect it tests if an entity is loaded or not before any other checks (but it could probably be removed)

@home-assistant home-assistant Bot marked this pull request as draft May 21, 2026 20:42
@home-assistant
Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants