Use JSON as format for .HA_RESTORE (#129792)

* Use JSON as format for .HA_RESTORE

* Adjust bakup manager test
This commit is contained in:
Joakim Sørensen 2024-11-04 13:07:11 +01:00 committed by GitHub
parent ae06f734ce
commit 3cadc1796f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 7 additions and 12 deletions

View file

@ -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

View file

@ -308,7 +308,7 @@ class BackupManager(BaseBackupManager):
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",
)

View file

@ -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

View file

@ -15,15 +15,10 @@ from .common import get_test_config_dir
("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")),
),
],