Update async_update_entity_platform to not allow loaded entities#171773
Update async_update_entity_platform to not allow loaded entities#171773gjohansson-ST wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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_platformto block migration when the entity is present inentity_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. |
| 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 ( |
There was a problem hiding this comment.
| if ( | |
| if entity_id in entity_sources(self.hass): |
| loaded_entities := entity_sources(self.hass) | ||
| ) and entity_id in loaded_entities: |
There was a problem hiding this comment.
| 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( |
There was a problem hiding this comment.
| entity_entry = MockEntity( | |
| entity = MockEntity( |
| platform = MockEntityPlatform(hass, domain="light", platform_name="hue") | ||
| orig_config_entry = MockConfigEntry( | ||
| domain="light", | ||
| domain="test", |
There was a problem hiding this comment.
Should actually be hue but I'll fix that. Was missed flipping around with the tests (has anyway no real impact)
| with pytest.raises( | ||
| ValueError, | ||
| match="Only entities that haven't been loaded can be migrated", | ||
| ): | ||
| entity_registry.async_update_entity_platform( | ||
| "light.light", | ||
| "hue3", | ||
| ) |
There was a problem hiding this comment.
Seems superfluous to me given the next lines.
There was a problem hiding this comment.
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)
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Breaking change
Proposed change
Updates the
async_update_entity_platformmethod in entity registry to not only look at entities with stateunknown.It's not sufficient to know if an entity is loaded or not
Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: