Skip to content

Commit

Permalink
Use JSON as format for .HA_RESTORE (home-assistant#129792)
Browse files Browse the repository at this point in the history
* Use JSON as format for .HA_RESTORE

* Adjust bakup manager test
  • Loading branch information
ludeeus authored Nov 4, 2024
1 parent ae06f73 commit 3cadc17
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
6 changes: 3 additions & 3 deletions homeassistant/backup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def restore_backup_file_content(config_dir: Path) -> RestoreBackupFileContent |
"""Return the contents of the restore backup file."""
instruction_path = config_dir.joinpath(RESTORE_BACKUP_FILE)
try:
instruction_content = instruction_path.read_text(encoding="utf-8")
instruction_content = json.loads(instruction_path.read_text(encoding="utf-8"))
return RestoreBackupFileContent(
backup_file_path=Path(instruction_content.split(";")[0])
backup_file_path=Path(instruction_content["path"])
)
except FileNotFoundError:
except (FileNotFoundError, json.JSONDecodeError):
return None


Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/backup/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ async def async_restore_backup(self, slug: str, **kwargs: Any) -> None:
def _write_restore_file() -> None:
"""Write the restore file."""
Path(self.hass.config.path(RESTORE_BACKUP_FILE)).write_text(
f"{backup.path.as_posix()};",
json.dumps({"path": backup.path.as_posix()}),
encoding="utf-8",
)

Expand Down
2 changes: 1 addition & 1 deletion tests/components/backup/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ async def test_async_trigger_restore(
patch("homeassistant.core.ServiceRegistry.async_call") as mocked_service_call,
):
await manager.async_restore_backup(TEST_BACKUP.slug)
assert mocked_write_text.call_args[0][0] == "abc123.tar;"
assert mocked_write_text.call_args[0][0] == '{"path": "abc123.tar"}'
assert mocked_service_call.called


Expand Down
9 changes: 2 additions & 7 deletions tests/test_backup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@
("side_effect", "content", "expected"),
[
(FileNotFoundError, "", None),
(None, "", backup_restore.RestoreBackupFileContent(backup_file_path=Path(""))),
(None, "", None),
(
None,
"test;",
backup_restore.RestoreBackupFileContent(backup_file_path=Path("test")),
),
(
None,
"test;;;;",
'{"path": "test"}',
backup_restore.RestoreBackupFileContent(backup_file_path=Path("test")),
),
],
Expand Down

0 comments on commit 3cadc17

Please sign in to comment.