From be3d678f23bdea5db1286d9ce92401ff4d6ab026 Mon Sep 17 00:00:00 2001
From: Erik Montnemery <erik@montnemery.com>
Date: Tue, 4 Mar 2025 20:20:49 +0100
Subject: [PATCH] Align hassio with changes in BackupAgent (#139780)

---
 homeassistant/components/hassio/backup.py | 36 +++++++++++++----------
 1 file changed, 20 insertions(+), 16 deletions(-)

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