diff --git a/homeassistant/components/backup/manager.py b/homeassistant/components/backup/manager.py index 36791a1915269..5513b912bd653 100644 --- a/homeassistant/components/backup/manager.py +++ b/homeassistant/components/backup/manager.py @@ -12,7 +12,6 @@ from pathlib import Path import shutil import tarfile -from tempfile import TemporaryDirectory import time from typing import TYPE_CHECKING, Any, Protocol @@ -389,25 +388,11 @@ async def async_receive_backup( contents: aiohttp.BodyPartReader, ) -> None: """Receive and store a backup file from upload.""" - temp_dir_handler = await self.hass.async_add_executor_job(TemporaryDirectory) - target_temp_file = Path( - temp_dir_handler.name, contents.filename or "backup.tar" - ) + target_temp_file = Path(self.temp_backup_dir, contents.filename or "backup.tar") + await self.hass.async_add_executor_job(make_backup_dir, self.temp_backup_dir) await receive_file(self.hass, contents, target_temp_file) - def _copy_and_cleanup( - local_file_paths: list[Path], backup: AgentBackup - ) -> Path: - if local_file_paths: - tar_file_path = local_file_paths[0] - else: - tar_file_path = self.temp_backup_dir / f"{backup.backup_id}.tar" - for local_path in local_file_paths: - shutil.copy(target_temp_file, local_path) - temp_dir_handler.cleanup() - return tar_file_path - try: backup = await self.hass.async_add_executor_job( read_backup, target_temp_file @@ -421,9 +406,18 @@ def _copy_and_cleanup( for agent_id in agent_ids if agent_id in self.local_backup_agents ] - tar_file_path = await self.hass.async_add_executor_job( - _copy_and_cleanup, local_file_paths, backup - ) + if local_file_paths: + + def _copy_to_local_agents(local_file_paths: list[Path]) -> Path: + for local_path in local_file_paths: + shutil.copy(target_temp_file, local_path) + return local_file_paths[0] + + tar_file_path = await self.hass.async_add_executor_job( + _copy_to_local_agents, local_file_paths + ) + else: + tar_file_path = target_temp_file await self._async_upload_backup( backup=backup, agent_ids=agent_ids, path=tar_file_path ) diff --git a/tests/components/cloud/test_backup.py b/tests/components/cloud/test_backup.py index faeacbe006c87..41414426f5243 100644 --- a/tests/components/cloud/test_backup.py +++ b/tests/components/cloud/test_backup.py @@ -525,6 +525,7 @@ async def test_agents_upload_not_protected( size=0.0, ) with ( + patch("pathlib.Path.open"), patch( "homeassistant.components.backup.manager.read_backup", return_value=test_backup, diff --git a/tests/components/hassio/test_backup.py b/tests/components/hassio/test_backup.py index 426329a622b07..07fbd500752cc 100644 --- a/tests/components/hassio/test_backup.py +++ b/tests/components/hassio/test_backup.py @@ -184,6 +184,8 @@ async def test_agent_upload( supervisor_client.backups.reload.assert_not_called() with ( + patch("pathlib.Path.mkdir"), + patch("pathlib.Path.open"), patch( "homeassistant.components.backup.manager.BackupManager.async_get_backup", ) as fetch_backup, diff --git a/tests/components/kitchen_sink/test_backup.py b/tests/components/kitchen_sink/test_backup.py index d36b287871340..2e53216d43e2c 100644 --- a/tests/components/kitchen_sink/test_backup.py +++ b/tests/components/kitchen_sink/test_backup.py @@ -141,6 +141,7 @@ async def test_agents_upload( ) with ( + patch("pathlib.Path.open"), patch( "homeassistant.components.backup.manager.BackupManager.async_get_backup", ) as fetch_backup,