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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stable entity registry id when a deleted entity is restored #77710

Merged
merged 4 commits into from Jun 26, 2023

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Sep 2, 2022

Proposed change

Ensure entity registry id when a deleted entity is restored.

A deleted entity will be stored in the device registry in a deleted_entities list.
When adding a new entity, a lookup will be done in the deleted_entities before issuing a new entity registry id.

  • Entity registry data is not restored, only entity registry id is preserved
    • This means even the entity_id is not guaranteed to be stable
  • Entities which don't belong to a config entry will be marked for permanent purge when removed
  • When removing a config entry, matching deleted_entities will be marked for permanent purge.
  • Deleted entities marked for purge will be permanently deleted after 30 days

Background

We're currently migrating to use the entity registry id to reference entities in device automations instead of the entity_id.
It's a fairly common pattern that users delete and then re-add a config entry when things don't work. Without the changes in this PR, any device automation referencing entities belonging to the re-added config entry will break.

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:

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
  • The code has been formatted using Black (black --fast 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.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@emontnemery emontnemery requested a review from a team as a code owner September 2, 2022 11:38
@project-bot project-bot bot added this to Needs review in Dev Sep 2, 2022
@emontnemery emontnemery marked this pull request as draft September 2, 2022 12:19
@emontnemery
Copy link
Contributor Author

Set to draft until restoring of area_id, which is not wanted, is removed

@emontnemery emontnemery marked this pull request as ready for review September 2, 2022 12:26
@emontnemery emontnemery changed the title Stable entity_id and registry id when a deleted entity is restored Stable entity registry id when a deleted entity is restored Sep 2, 2022
@github-actions
Copy link

github-actions bot commented Oct 2, 2022

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Oct 2, 2022
@github-actions github-actions bot closed this Oct 9, 2022
Dev automation moved this from Needs review to Cancelled Oct 9, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Oct 10, 2022
@frenck frenck deleted the restore_entity_registry_entries branch January 3, 2023 23:02
@emontnemery emontnemery restored the restore_entity_registry_entries branch June 26, 2023 08:02
@emontnemery emontnemery reopened this Jun 26, 2023
Dev automation moved this from Cancelled to Incoming Jun 26, 2023
@emontnemery emontnemery force-pushed the restore_entity_registry_entries branch from cc9eb60 to b660a6e Compare June 26, 2023 08:30
homeassistant/helpers/entity_registry.py Outdated Show resolved Hide resolved
homeassistant/helpers/entity_registry.py Outdated Show resolved Hide resolved
homeassistant/helpers/entity_registry.py Outdated Show resolved Hide resolved
homeassistant/helpers/entity_registry.py Outdated Show resolved Hide resolved
self.deleted_entities.pop((domain, platform, unique_id))
# Restore id
entity_registry_id = deleted_entity.id

entity_id = self.async_generate_entity_id(
Copy link
Member

Choose a reason for hiding this comment

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

How is the entity_id restored when using an entity entry id of a deleted entity?

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 entity_id is not restored. We could add that in a follow-up if it's wanted. We can't guarantee restoring an entity_id though, or it would mean an entity_id can only be reused 30 days after it is no longer used.

Copy link
Member

Choose a reason for hiding this comment

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

Ok.

@home-assistant home-assistant unlocked this conversation Jun 26, 2023
@emontnemery emontnemery added this to the 2023.7.0b0 milestone Jun 26, 2023
Dev automation moved this from Incoming to Reviewer approved Jun 26, 2023
@emontnemery emontnemery merged commit 8fda56d into dev Jun 26, 2023
51 checks passed
Dev automation moved this from Reviewer approved to Done Jun 26, 2023
@emontnemery emontnemery deleted the restore_entity_registry_entries branch June 26, 2023 13:54
jbouwh pushed a commit to jbouwh/core that referenced this pull request Jun 26, 2023
…istant#77710)

* Stable entity_id and registry id when a deleted entity is restored

* Don't restore area_id

* Don't restore entity_id

* Address review comments
@github-actions github-actions bot locked and limited conversation to collaborators Jun 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants