Remove class attributes for backup manager (#67431)
* Remove class attributes for backup manager * remove patchespull/67439/head
parent
7bbde822d2
commit
32adeb8356
|
@ -36,14 +36,13 @@ class Backup:
|
|||
class BackupManager:
|
||||
"""Backup manager for the Backup integration."""
|
||||
|
||||
_backups: dict[str, Backup] = {}
|
||||
_loaded = False
|
||||
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
"""Initialize the backup manager."""
|
||||
self.hass = hass
|
||||
self.backup_dir = Path(hass.config.path("backups"))
|
||||
self.backing_up = False
|
||||
self.backups: dict[str, Backup] = {}
|
||||
self.loaded = False
|
||||
|
||||
async def load_backups(self) -> None:
|
||||
"""Load data of stored backup files."""
|
||||
|
@ -68,22 +67,22 @@ class BackupManager:
|
|||
|
||||
await self.hass.async_add_executor_job(_read_backups)
|
||||
LOGGER.debug("Loaded %s backups", len(backups))
|
||||
self._backups = backups
|
||||
self._loaded = True
|
||||
self.backups = backups
|
||||
self.loaded = True
|
||||
|
||||
async def get_backups(self) -> dict[str, Backup]:
|
||||
"""Return backups."""
|
||||
if not self._loaded:
|
||||
if not self.loaded:
|
||||
await self.load_backups()
|
||||
|
||||
return self._backups
|
||||
return self.backups
|
||||
|
||||
async def get_backup(self, slug: str) -> Backup | None:
|
||||
"""Return a backup."""
|
||||
if not self._loaded:
|
||||
if not self.loaded:
|
||||
await self.load_backups()
|
||||
|
||||
if not (backup := self._backups.get(slug)):
|
||||
if not (backup := self.backups.get(slug)):
|
||||
return None
|
||||
|
||||
if not backup.path.exists():
|
||||
|
@ -92,7 +91,7 @@ class BackupManager:
|
|||
backup.slug,
|
||||
backup.path,
|
||||
)
|
||||
self._backups.pop(slug)
|
||||
self.backups.pop(slug)
|
||||
return None
|
||||
|
||||
return backup
|
||||
|
@ -104,7 +103,7 @@ class BackupManager:
|
|||
|
||||
await self.hass.async_add_executor_job(backup.path.unlink, True)
|
||||
LOGGER.debug("Removed backup located at %s", backup.path)
|
||||
self._backups.pop(slug)
|
||||
self.backups.pop(slug)
|
||||
|
||||
async def generate_backup(self) -> Backup:
|
||||
"""Generate a backup."""
|
||||
|
@ -160,8 +159,8 @@ class BackupManager:
|
|||
path=tar_file_path,
|
||||
size=round(tar_file_path.stat().st_size / 1_048_576, 2),
|
||||
)
|
||||
if self._loaded:
|
||||
self._backups[slug] = backup
|
||||
if self.loaded:
|
||||
self.backups[slug] = backup
|
||||
LOGGER.debug("Generated new backup with slug %s", slug)
|
||||
return backup
|
||||
finally:
|
||||
|
|
|
@ -52,6 +52,20 @@ async def test_load_backups_with_exception(
|
|||
assert backups == {}
|
||||
|
||||
|
||||
async def test_removing_backup(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test removing backup."""
|
||||
manager = BackupManager(hass)
|
||||
manager.backups = {TEST_BACKUP.slug: TEST_BACKUP}
|
||||
manager.loaded = True
|
||||
|
||||
with patch("pathlib.Path.exists", return_value=True):
|
||||
await manager.remove_backup(TEST_BACKUP.slug)
|
||||
assert "Removed backup located at" in caplog.text
|
||||
|
||||
|
||||
async def test_removing_non_existing_backup(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
|
@ -69,16 +83,10 @@ async def test_getting_backup_that_does_not_exist(
|
|||
):
|
||||
"""Test getting backup that does not exist."""
|
||||
manager = BackupManager(hass)
|
||||
manager.backups = {TEST_BACKUP.slug: TEST_BACKUP}
|
||||
manager.loaded = True
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.backup.websocket.BackupManager._backups",
|
||||
{TEST_BACKUP.slug: TEST_BACKUP},
|
||||
), patch(
|
||||
"homeassistant.components.backup.websocket.BackupManager._loaded",
|
||||
True,
|
||||
), patch(
|
||||
"pathlib.Path.exists", return_value=False
|
||||
):
|
||||
with patch("pathlib.Path.exists", return_value=False):
|
||||
backup = await manager.get_backup(TEST_BACKUP.slug)
|
||||
assert backup is None
|
||||
|
||||
|
@ -102,6 +110,7 @@ async def test_generate_backup(
|
|||
) -> None:
|
||||
"""Test generate backup."""
|
||||
manager = BackupManager(hass)
|
||||
manager.loaded = True
|
||||
|
||||
def _mock_iterdir(path: Path) -> list[Path]:
|
||||
if not path.name.endswith("testing_config"):
|
||||
|
@ -133,9 +142,6 @@ async def test_generate_backup(
|
|||
) as mocked_json_util, patch(
|
||||
"homeassistant.components.backup.manager.HAVERSION",
|
||||
"2025.1.0",
|
||||
), patch(
|
||||
"homeassistant.components.backup.websocket.BackupManager._loaded",
|
||||
True,
|
||||
):
|
||||
await manager.generate_backup()
|
||||
|
||||
|
|
|
@ -40,22 +40,13 @@ async def test_remove(
|
|||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.backup.websocket.BackupManager._backups",
|
||||
{TEST_BACKUP.slug: TEST_BACKUP},
|
||||
), patch(
|
||||
"homeassistant.components.backup.websocket.BackupManager._loaded",
|
||||
True,
|
||||
), patch(
|
||||
"pathlib.Path.unlink"
|
||||
), patch(
|
||||
"pathlib.Path.exists", return_value=True
|
||||
"homeassistant.components.backup.websocket.BackupManager.remove_backup",
|
||||
):
|
||||
await client.send_json({"id": 1, "type": "backup/remove", "slug": "abc123"})
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg["id"] == 1
|
||||
assert msg["success"]
|
||||
assert f"Removed backup located at {TEST_BACKUP.path}" in caplog.text
|
||||
|
||||
|
||||
async def test_generate(
|
||||
|
@ -69,9 +60,6 @@ async def test_generate(
|
|||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.backup.websocket.BackupManager._backups",
|
||||
{TEST_BACKUP.slug: TEST_BACKUP},
|
||||
), patch(
|
||||
"homeassistant.components.backup.websocket.BackupManager.generate_backup",
|
||||
return_value=TEST_BACKUP,
|
||||
):
|
||||
|
|
Loading…
Reference in New Issue