-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
Fix unloading an entry can leave states around #17786
Conversation
CC @amelchio @bachya - could you guys try to repro the bug with the fix from this PR applied ? I was unable to repro the bug with the extended test case (see diff), but I do have a solution. The problem was that we were not calling the on remove listeners for an entity when it was removed via a platform, because there was some duplicate code. I have fixed that by making the |
The cleanup will make sure that the entity will no longer be registered for updates on the entity reigstry when the config entry ID is cleared inside the entity registry. Still bugs me that I was unable to repro it with a test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a better suggestion at the moment.
@@ -96,6 +96,8 @@ def update(): | |||
|
|||
async def async_will_remove_from_hass(self) -> None: | |||
"""Disconnect dispatcher listener when removed.""" | |||
await super.async_will_remove_from_hass() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this change. It puts a burden on each platform to make sure the core operates correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is super.bla
correct syntax? I thought it should be super().bla
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point. will fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about c4a52dd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as one opinion: calling super() on methods that subclasses override is just part of Python that needs to be implemented correctly. Just like we have to trust that async platforms aren't doing IO on the main thread, it's OK to trust that subclasses need to be implemented correctly, or they won't work properly.
689da70
to
c4a52dd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@balloob I tested both of my config entry-enabled components (OpenUV and SimpliSafe) against this PR: in both cases, the entities were correctly removed when the config entry was deleted.
From my vantage point, this PR is good to go. 👍
This works with my work-in-progress LIFX unloading as well. The entities are not removed from |
Your test never reaches |
Awesome detective work guys 👍 Good catch on the test too @amelchio ❤️ |
* Add test that tests unloading on remove * Add more test things * Untangle entity remove code from entity platform * Don't add default implementation of async_will_remove * Keep entity weakref alive
Description:
Fix for #17370.
The issue is caused by the following (analysis by @amelchio):
Entity.async_registry_updated
.Entity.async_registry_updated
will causeasync_update_ha_state
to be called, writing the state of a removed entity to the state machine.Related issue (if applicable): fixes #17370
Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#<home-assistant.io PR number goes here>
Example entry for
configuration.yaml
(if applicable):Checklist:
tox
. Your PR cannot be merged unless tests passIf user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
REQUIREMENTS
variable (example).requirements_all.txt
by runningscript/gen_requirements_all.py
..coveragerc
.If the code does not interact with devices: