From 1be32a88d40d2b75b491d923e6ff996fa0d022da Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Mon, 24 Feb 2025 15:19:10 +0100 Subject: [PATCH] Update backup agent reload listener docs (#2570) * Remove task from backup listener unloading * Remove async from _notify_backup_listeners function * Update backup listener setup in documentation --- docs/core/platform/backup.md | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/docs/core/platform/backup.md b/docs/core/platform/backup.md index 74a87170..96131912 100644 --- a/docs/core/platform/backup.md +++ b/docs/core/platform/backup.md @@ -43,22 +43,20 @@ def async_register_backup_agents_listener( return remove_listener ``` -The listener stored in `async_register_backup_agents_listener` should be called every time there is the need to reload backup agents, to remove stale agents and add new ones, such as when the integration is reloaded. For example: +The listener stored in `async_register_backup_agents_listener` should be called every time there is the need to reload backup agents, to remove stale agents and add new ones. This can be done by registering the listeners during `async_setup_entry`: ```python -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: - """Unload config entry.""" +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + """Set up config entry.""" + # do things to set up your config entry + # Notify backup listeners - hass.async_create_task(_notify_backup_listeners(hass), eager_start=False) + def notify_backup_listeners() -> None: + for listener in hass.data.get(DATA_BACKUP_AGENT_LISTENERS, []): + listener() + entry.async_on_unload(entry.async_on_state_change(notify_backup_listeners)) - return await hass.config_entries.async_unload_platforms( - entry, PLATFORMS - ) - - -async def _notify_backup_listeners(hass: HomeAssistant) -> None: - for listener in hass.data.get(DATA_BACKUP_AGENT_LISTENERS, []): - listener() + return True ``` A backup agent should implement the abstract interface of the `BackupAgent` base class as shown in this example: