Skip to content

Commit bce7d8b

Browse files
Extract logic to config function
1 parent 16f174f commit bce7d8b

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

homeassistant/components/backup/config.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,8 @@ def update(
185185
if automatic_backups_configured is not UNDEFINED:
186186
self.data.automatic_backups_configured = automatic_backups_configured
187187
if create_backup is not UNDEFINED:
188-
if (new_agent_ids := create_backup.get("agent_ids", [])) and (
189-
discarded_agent_ids := set(self.data.create_backup.agent_ids)
190-
- set(new_agent_ids)
191-
):
192-
for agent_id in discarded_agent_ids:
193-
delete_automatic_backup_agents_unavailable_issue(
194-
self._hass, agent_id
195-
)
188+
if new_agent_ids := create_backup.get("agent_ids"):
189+
check_unavailable_agents(self._hass, self._manager, new_agent_ids)
196190
self.data.create_backup = replace(self.data.create_backup, **create_backup)
197191
if retention is not UNDEFINED:
198192
new_retention = RetentionConfig(**retention)
@@ -576,6 +570,35 @@ def _delete_filter(
576570
)
577571

578572

573+
@callback
574+
def check_unavailable_agents(
575+
hass: HomeAssistant, manager: BackupManager, new_agent_ids: list[str] | None = None
576+
) -> None:
577+
"""Check for unavailable agents."""
578+
if missing_agent_ids := set(manager.config.data.create_backup.agent_ids) - set(
579+
manager.backup_agents
580+
):
581+
LOGGER.debug(
582+
"Agents %s are configured for automatic backup but are not loaded",
583+
missing_agent_ids,
584+
)
585+
for agent_id in missing_agent_ids:
586+
create_automatic_backup_agents_unavailable_issue(hass, agent_id)
587+
588+
# Remove any issues for agents that are now loaded
589+
for agent_id in manager.backup_agents:
590+
delete_automatic_backup_agents_unavailable_issue(hass, agent_id)
591+
592+
if new_agent_ids is None:
593+
return
594+
595+
# Remove issues for agents that are no longer configured
596+
for agent_id in set(manager.config.data.create_backup.agent_ids) - set(
597+
new_agent_ids
598+
):
599+
delete_automatic_backup_agents_unavailable_issue(hass, agent_id)
600+
601+
579602
@callback
580603
def create_automatic_backup_agents_unavailable_issue(
581604
hass: HomeAssistant, agent_id: str

homeassistant/components/backup/manager.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
instance_id,
3333
integration_platform,
3434
issue_registry as ir,
35+
start,
3536
)
3637
from homeassistant.helpers.backup import DATA_BACKUP
3738
from homeassistant.helpers.json import json_bytes
@@ -47,8 +48,7 @@
4748
from .config import (
4849
BackupConfig,
4950
CreateBackupParametersDict,
50-
create_automatic_backup_agents_unavailable_issue,
51-
delete_automatic_backup_agents_unavailable_issue,
51+
check_unavailable_agents,
5252
delete_backups_exceeding_configured_count,
5353
)
5454
from .const import (
@@ -418,19 +418,13 @@ async def _async_reload_backup_agents(self, domain: str) -> None:
418418
if isinstance(agent, LocalBackupAgent)
419419
}
420420
)
421-
if missing_agent_ids := set(self.config.data.create_backup.agent_ids) - set(
422-
self.backup_agents
423-
):
424-
LOGGER.debug(
425-
"Agents %s are configured for automatic backup but are not loaded",
426-
missing_agent_ids,
427-
)
428-
for agent_id in missing_agent_ids:
429-
create_automatic_backup_agents_unavailable_issue(self.hass, agent_id)
430421

431-
# Remove any issues for agents that are now loaded
432-
for agent_id in self.backup_agents:
433-
delete_automatic_backup_agents_unavailable_issue(self.hass, agent_id)
422+
@callback
423+
def check_unavailable_agents_after_start(hass: HomeAssistant) -> None:
424+
"""Check unavailable agents after start."""
425+
check_unavailable_agents(hass, self)
426+
427+
start.async_at_started(self.hass, check_unavailable_agents_after_start)
434428

435429
async def _add_platform(
436430
self,

0 commit comments

Comments
 (0)