diff --git a/homeassistant/components/google_drive/backup.py b/homeassistant/components/google_drive/backup.py index 73e5902f8f5..a4b7fc956ce 100644 --- a/homeassistant/components/google_drive/backup.py +++ b/homeassistant/components/google_drive/backup.py @@ -8,7 +8,12 @@ from typing import Any from google_drive_api.exceptions import GoogleDriveApiError -from homeassistant.components.backup import AgentBackup, BackupAgent, BackupAgentError +from homeassistant.components.backup import ( + AgentBackup, + BackupAgent, + BackupAgentError, + BackupNotFound, +) from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.aiohttp_client import ChunkAsyncStreamIterator @@ -93,13 +98,13 @@ class GoogleDriveBackupAgent(BackupAgent): self, backup_id: str, **kwargs: Any, - ) -> AgentBackup | None: + ) -> AgentBackup: """Return a backup.""" backups = await self.async_list_backups() for backup in backups: if backup.backup_id == backup_id: return backup - return None + raise BackupNotFound(f"Backup {backup_id} not found") async def async_download_backup( self, @@ -120,7 +125,7 @@ class GoogleDriveBackupAgent(BackupAgent): return ChunkAsyncStreamIterator(stream) except (GoogleDriveApiError, HomeAssistantError, TimeoutError) as err: raise BackupAgentError(f"Failed to download backup: {err}") from err - raise BackupAgentError("Backup not found") + raise BackupNotFound(f"Backup {backup_id} not found") async def async_delete_backup( self, @@ -138,5 +143,7 @@ class GoogleDriveBackupAgent(BackupAgent): _LOGGER.debug("Deleting file_id: %s", file_id) await self._client.async_delete(file_id) _LOGGER.debug("Deleted backup_id: %s", backup_id) + return except (GoogleDriveApiError, HomeAssistantError, TimeoutError) as err: raise BackupAgentError(f"Failed to delete backup: {err}") from err + raise BackupNotFound(f"Backup {backup_id} not found") diff --git a/tests/components/google_drive/test_backup.py b/tests/components/google_drive/test_backup.py index 2da397def5b..9cf86a280bd 100644 --- a/tests/components/google_drive/test_backup.py +++ b/tests/components/google_drive/test_backup.py @@ -247,9 +247,9 @@ async def test_agents_download_file_not_found( resp = await client.get( f"/api/backup/download/{TEST_AGENT_BACKUP.backup_id}?agent_id={TEST_AGENT_ID}" ) - assert resp.status == 500 + assert resp.status == 404 content = await resp.content.read() - assert "Backup not found" in content.decode() + assert content == b"" async def test_agents_download_metadata_not_found(