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