Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make local backup a backup agent #130623

Merged
merged 25 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
400f792
Make local backup a backup agent
emontnemery Nov 14, 2024
0ce1fd7
Adjust
emontnemery Nov 14, 2024
f103835
Adjust
emontnemery Nov 14, 2024
a91b4db
Merge remote-tracking branch 'upstream/allthebackupchanges' into loca…
emontnemery Nov 15, 2024
0e128d2
Adjust
emontnemery Nov 15, 2024
8bdc819
Adjust tests
emontnemery Nov 15, 2024
8f6ae45
Adjust
emontnemery Nov 16, 2024
1858092
Adjust
emontnemery Nov 18, 2024
8edf464
Adjust docstring
emontnemery Nov 18, 2024
068e2d4
Adjust
emontnemery Nov 18, 2024
19c8ebb
Protect members of CoreLocalBackupAgent
emontnemery Nov 18, 2024
f9b3cb7
Remove redundant check for file
emontnemery Nov 18, 2024
2c1e58f
Make the backup.create service use the first local agent
emontnemery Nov 18, 2024
b06e8d6
Add BackupAgent.async_get_backup
emontnemery Nov 18, 2024
10d0fec
Fix some TODOs
emontnemery Nov 18, 2024
e788d00
Add support for downloading backup from a remote agent
emontnemery Nov 18, 2024
8a9cdfa
Fix restore
emontnemery Nov 18, 2024
244416b
Fix test
emontnemery Nov 18, 2024
6ab3e5d
Adjust kitchen_sink test
emontnemery Nov 18, 2024
46b085e
Remove unused method BackupManager.async_get_backup_path
emontnemery Nov 18, 2024
9609b55
Merge remote-tracking branch 'upstream/allthebackupchanges' into loca…
emontnemery Nov 18, 2024
7f45aa9
Re-enable kitchen sink test
emontnemery Nov 18, 2024
a251813
Remove BaseBackupManager.async_upload_backup
emontnemery Nov 18, 2024
4edf64c
Support restore from remote agent
emontnemery Nov 18, 2024
5baa39e
Fix review comments
emontnemery Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Protect members of CoreLocalBackupAgent
  • Loading branch information
emontnemery committed Nov 18, 2024
commit 19c8ebb20ce412db3ab997a8295a4da0f41a4c81
28 changes: 14 additions & 14 deletions homeassistant/components/backup/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the backup agent."""
super().__init__()
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
self._hass = hass
self.backup_dir = Path(hass.config.path("backups"))
self.backups: dict[str, LocalBackup] = {}
self.loaded_backups = False
self._backup_dir = Path(hass.config.path("backups"))
self._backups: dict[str, LocalBackup] = {}
self._loaded_backups = False

async def load_backups(self) -> None:
"""Load data of stored backup files."""
backups = await self._hass.async_add_executor_job(self._read_backups)
LOGGER.debug("Loaded %s local backups", len(backups))
self.backups = backups
self.loaded_backups = True
self._backups = backups
self._loaded_backups = True

def _read_backups(self) -> dict[str, LocalBackup]:
"""Read backups from disk."""
backups: dict[str, LocalBackup] = {}
for backup_path in self.backup_dir.glob("*.tar"):
for backup_path in self._backup_dir.glob("*.tar"):
try:
base_backup = read_backup(backup_path)
backup = LocalBackup(
Expand Down Expand Up @@ -93,7 +93,7 @@ async def async_upload_backup(
**kwargs: Any,
) -> None:
"""Upload a backup."""
self.backups[metadata.slug] = LocalBackup(
self._backups[metadata.slug] = LocalBackup(
id=metadata.slug, # Do we need another ID?
Copy link
Contributor Author

@emontnemery emontnemery Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above about the ID

slug=metadata.slug,
name=metadata.name,
Expand All @@ -105,18 +105,18 @@ async def async_upload_backup(

async def async_list_backups(self, **kwargs: Any) -> list[UploadedBackup]:
"""List backups."""
if not self.loaded_backups:
if not self._loaded_backups:
await self.load_backups()
return list(self.backups.values())
return list(self._backups.values())

async def async_get_backup(
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
self, *, slug: str, **kwargs: Any
) -> UploadedBackup | None:
"""Return a backup."""
if not self.loaded_backups:
if not self._loaded_backups:
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():
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -128,14 +128,14 @@ async def async_get_backup(
backup.slug,
backup.path,
)
self.backups.pop(slug)
self._backups.pop(slug)
return None

return backup

def get_backup_path(self, slug: str) -> Path:
"""Return the local path to a backup."""
return self.backup_dir / f"{slug}.tar"
return self._backup_dir / f"{slug}.tar"

async def async_remove_backup(self, *, slug: str, **kwargs: Any) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't part of the agent interface yet. I'm working on adding async_delete_backup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep it for now. I'll replace it later.

"""Remove a backup."""
Expand All @@ -144,4 +144,4 @@ async def async_remove_backup(self, *, slug: str, **kwargs: Any) -> None:

await self._hass.async_add_executor_job(backup.path.unlink, True) # type: ignore[attr-defined]
LOGGER.debug("Removed backup located at %s", backup.path) # type: ignore[attr-defined]
self.backups.pop(slug)
self._backups.pop(slug)
4 changes: 2 additions & 2 deletions tests/components/backup/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def setup_backup_integration(
return result

local_agent = hass.data[DATA_MANAGER].backup_agents[LOCAL_AGENT_ID]
local_agent.backups = {backups.slug: backups for backups in backups}
local_agent.loaded_backups = True
local_agent._backups = {backups.slug: backups for backups in backups}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local_agent._backups = {backups.slug: backups for backups in backups}
local_agent._backups = {backup.slug: backup for backup in backups}

local_agent._loaded_backups = True

return result
16 changes: 8 additions & 8 deletions tests/components/backup/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def on_progress(_progress: BackupProgress) -> None:
"type": "partial",
}
local_agent = manager.backup_agents[LOCAL_AGENT_ID]
assert local_agent.backup_dir.as_posix() in str(
assert local_agent._backup_dir.as_posix() in str(
mocked_tarfile.call_args_list[0][0][0]
)
outer_tar = mocked_tarfile.return_value
Expand Down Expand Up @@ -180,8 +180,8 @@ async def test_removing_backup(
await manager.load_platforms()

local_agent = manager.backup_agents[LOCAL_AGENT_ID]
local_agent.backups = {TEST_LOCAL_BACKUP.slug: TEST_LOCAL_BACKUP}
local_agent.loaded_backups = True
local_agent._backups = {TEST_LOCAL_BACKUP.slug: TEST_LOCAL_BACKUP}
local_agent._loaded_backups = True

with patch("pathlib.Path.exists", return_value=True):
await manager.async_remove_backup(slug=TEST_LOCAL_BACKUP.slug)
Expand Down Expand Up @@ -214,8 +214,8 @@ async def test_getting_backup_that_does_not_exist(
await manager.load_platforms()

local_agent = manager.backup_agents[LOCAL_AGENT_ID]
local_agent.backups = {TEST_LOCAL_BACKUP.slug: TEST_LOCAL_BACKUP}
local_agent.loaded_backups = True
local_agent._backups = {TEST_LOCAL_BACKUP.slug: TEST_LOCAL_BACKUP}
local_agent._loaded_backups = True

with patch("pathlib.Path.exists", return_value=False):
backup = await manager.async_get_backup(slug=TEST_LOCAL_BACKUP.slug)
Expand Down Expand Up @@ -269,7 +269,7 @@ async def test_async_create_backup(
await manager.load_platforms()

local_agent = manager.backup_agents[LOCAL_AGENT_ID]
local_agent.loaded_backups = True
local_agent._loaded_backups = True

await _mock_backup_generation(
hass, manager, mocked_json_bytes, mocked_tarfile, **params
Expand All @@ -280,8 +280,8 @@ async def test_async_create_backup(
assert "Loaded 0 platforms" in caplog.text
assert "Loaded 1 agents" in caplog.text

assert len(local_agent.backups) == 1
backup = list(local_agent.backups.values())[0]
assert len(local_agent._backups) == 1
backup = list(local_agent._backups.values())[0]
assert backup.protected is bool(params.get("password"))


Expand Down
Loading