Download backup if restore fails in Z-Wave migration (#145434)

* ZWaveJS migration: Download backup if restore fails

* update test

* PR comment
This commit is contained in:
Petar Petrov 2025-05-26 14:56:37 +03:00 committed by GitHub
parent 13a6c13b89
commit 87c3e2c7ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 5 deletions

View File

@ -3,6 +3,7 @@
from __future__ import annotations
import asyncio
import base64
from contextlib import suppress
from datetime import datetime
import logging
@ -192,7 +193,7 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN):
self.backup_task: asyncio.Task | None = None
self.restore_backup_task: asyncio.Task | None = None
self.backup_data: bytes | None = None
self.backup_filepath: str | None = None
self.backup_filepath: Path | None = None
self.use_addon = False
self._migrating = False
self._reconfigure_config_entry: ConfigEntry | None = None
@ -1241,11 +1242,15 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN):
"""Restore failed."""
if user_input is not None:
return await self.async_step_restore_nvm()
assert self.backup_filepath is not None
assert self.backup_data is not None
return self.async_show_form(
step_id="restore_failed",
description_placeholders={
"file_path": str(self.backup_filepath),
"file_url": f"data:application/octet-stream;base64,{base64.b64encode(self.backup_data).decode('ascii')}",
"file_name": self.backup_filepath.name,
},
)
@ -1383,12 +1388,14 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN):
unsub()
# save the backup to a file just in case
self.backup_filepath = self.hass.config.path(
f"zwavejs_nvm_backup_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.bin"
self.backup_filepath = Path(
self.hass.config.path(
f"zwavejs_nvm_backup_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.bin"
)
)
try:
await self.hass.async_add_executor_job(
Path(self.backup_filepath).write_bytes,
self.backup_filepath.write_bytes,
self.backup_data,
)
except OSError as err:

View File

@ -123,7 +123,7 @@
},
"restore_failed": {
"title": "Restoring unsuccessful",
"description": "Your Z-Wave network could not be restored to the new controller. This means that your Z-Wave devices are not connected to Home Assistant.\n\nThe backup is saved to ”{file_path}”",
"description": "Your Z-Wave network could not be restored to the new controller. This means that your Z-Wave devices are not connected to Home Assistant.\n\nThe backup is saved to ”{file_path}”\n\n'<'a href=\"{file_url}\" download=\"{file_name}\"'>'Download backup file'<'/a'>'",
"submit": "Try again"
},
"choose_serial_port": {

View File

@ -4231,6 +4231,8 @@ async def test_reconfigure_migrate_restore_failure(
description_placeholders = result["description_placeholders"]
assert description_placeholders is not None
assert description_placeholders["file_path"]
assert description_placeholders["file_url"]
assert description_placeholders["file_name"]
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})