Align hassio with changes in BackupAgent (#139780)

pull/136603/merge
Erik Montnemery 2025-03-04 20:20:49 +01:00 committed by GitHub
parent e8099fd3b2
commit be3d678f23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 16 deletions

View File

@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio import asyncio
from collections.abc import AsyncIterator, Callable, Coroutine, Mapping from collections.abc import AsyncIterator, Callable, Coroutine, Mapping
from contextlib import suppress
import logging import logging
import os import os
from pathlib import Path, PurePath from pathlib import Path, PurePath
@ -173,7 +174,7 @@ class SupervisorBackupAgent(BackupAgent):
), ),
) )
except SupervisorNotFoundError as err: except SupervisorNotFoundError as err:
raise BackupNotFound from err raise BackupNotFound(f"Backup {backup_id} not found") from err
async def async_upload_backup( async def async_upload_backup(
self, self,
@ -186,13 +187,14 @@ class SupervisorBackupAgent(BackupAgent):
The upload will be skipped if the backup already exists in the agent's location. The upload will be skipped if the backup already exists in the agent's location.
""" """
if await self.async_get_backup(backup.backup_id): with suppress(BackupNotFound):
_LOGGER.debug( if await self.async_get_backup(backup.backup_id):
"Backup %s already exists in location %s", _LOGGER.debug(
backup.backup_id, "Backup %s already exists in location %s",
self.location, backup.backup_id,
) self.location,
return )
return
stream = await open_stream() stream = await open_stream()
upload_options = supervisor_backups.UploadBackupOptions( upload_options = supervisor_backups.UploadBackupOptions(
location={self.location}, location={self.location},
@ -218,14 +220,14 @@ class SupervisorBackupAgent(BackupAgent):
self, self,
backup_id: str, backup_id: str,
**kwargs: Any, **kwargs: Any,
) -> AgentBackup | None: ) -> AgentBackup:
"""Return a backup.""" """Return a backup."""
try: try:
details = await self._client.backups.backup_info(backup_id) details = await self._client.backups.backup_info(backup_id)
except SupervisorNotFoundError: except SupervisorNotFoundError as err:
return None raise BackupNotFound(f"Backup {backup_id} not found") from err
if self.location not in details.location_attributes: if self.location not in details.location_attributes:
return None raise BackupNotFound(f"Backup {backup_id} not found")
return _backup_details_to_agent_backup(details, self.location) return _backup_details_to_agent_backup(details, self.location)
async def async_delete_backup(self, backup_id: str, **kwargs: Any) -> None: async def async_delete_backup(self, backup_id: str, **kwargs: Any) -> None:
@ -237,8 +239,8 @@ class SupervisorBackupAgent(BackupAgent):
location={self.location} location={self.location}
), ),
) )
except SupervisorNotFoundError: except SupervisorNotFoundError as err:
_LOGGER.debug("Backup %s does not exist", backup_id) raise BackupNotFound(f"Backup {backup_id} not found") from err
class SupervisorBackupReaderWriter(BackupReaderWriter): class SupervisorBackupReaderWriter(BackupReaderWriter):
@ -492,10 +494,12 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
) -> None: ) -> None:
"""Restore a backup.""" """Restore a backup."""
manager = self._hass.data[DATA_MANAGER] manager = self._hass.data[DATA_MANAGER]
# The backup manager has already checked that the backup exists so we don't need to # The backup manager has already checked that the backup exists so we don't
# check that here. # need to catch BackupNotFound here.
backup = await manager.backup_agents[agent_id].async_get_backup(backup_id) backup = await manager.backup_agents[agent_id].async_get_backup(backup_id)
if ( if (
# Check for None to be backwards compatible with the old BackupAgent API,
# this can be removed in HA Core 2025.10
backup backup
and restore_homeassistant and restore_homeassistant
and restore_database != backup.database_included and restore_database != backup.database_included