Skip to content

Commit

Permalink
Clarify config entry unload quality scale rule (#2566)
Browse files Browse the repository at this point in the history
* Clarify config entry unload quality scale rule

* Apply suggestions from code review

---------

Co-authored-by: Franck Nijhof <git@frenck.dev>
  • Loading branch information
emontnemery and frenck authored Feb 16, 2025
1 parent bd80269 commit e1b99a1
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand All @@ -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
Expand Down

0 comments on commit e1b99a1

Please sign in to comment.