Clarify config entry unload quality scale rule (#2566)

* Clarify config entry unload quality scale rule

* Apply suggestions from code review

---------

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2025-02-16 17:53:14 +01:00 committed by GitHub
parent bd80269a3d
commit e1b99a12d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -16,7 +16,6 @@ This improves the user experience, since the user can do more actions without ha
In the `async_unload_entry` interface function, the integration should clean up any subscriptions and close any connections opened during the setup of the integration.
The method that has to be added to `__init__.py` looks very similar to the `async_setup_entry` method.
In this example we have a listener, stored in the `runtime_data` of the config entry, which we want to clean up to avoid memory leaks.
`__init__.py`:
@ -29,8 +28,13 @@ async def async_unload_entry(hass: HomeAssistant, entry: MyConfigEntry) -> bool:
```
:::info
You can also use `entry.async_on_unload` to register a callback that will be called when the config entry is unloaded.
Integrations can use `entry.async_on_unload` to register callbacks which will be called when the config entry is unloaded or if it fails to set up.
This can be useful to clean up resources without having to keep track of the removal methods yourself.
The registered callbacks will be called if :
- `async_setup_entry` raises either `ConfigEntryError`, `ConfigEntryAuthFailed`, or `ConfigEntryNotReady`
- `async_unload_entry` succeeds, i.e., it returns True and does not raise.
Note that integrations always need to implement `async_unload_entry` to support config entry unloading, just calling `entry.async_on_unload` is not enough.
:::
## Additional resources