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. |
MartinHjelmare
left a comment
There was a problem hiding this comment.
I don't have a better suggestion at the moment.
|
|
||
| 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.
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.
Is super.bla correct syntax? I thought it should be super().bla.
There was a problem hiding this comment.
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
bachya
left a comment
There was a problem hiding this comment.
@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_updatedwill causeasync_update_ha_stateto 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:
REQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.If the code does not interact with devices: