Add WS endpoint to fetch the details of a backup (#128430)

* Add WS endpoint to fetch the details of a backup

* Shorten

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Adjust

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Joakim Sørensen 2024-10-15 13:00:34 +02:00 committed by GitHub
parent 84b2c74057
commit fb7bed2ea0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 108 additions and 0 deletions

View file

@ -18,6 +18,7 @@ def async_register_websocket_handlers(hass: HomeAssistant, with_hassio: bool) ->
websocket_api.async_register_command(hass, handle_backup_start)
return
websocket_api.async_register_command(hass, handle_details)
websocket_api.async_register_command(hass, handle_info)
websocket_api.async_register_command(hass, handle_create)
websocket_api.async_register_command(hass, handle_remove)
@ -43,6 +44,29 @@ async def handle_info(
)
@websocket_api.require_admin
@websocket_api.websocket_command(
{
vol.Required("type"): "backup/details",
vol.Required("slug"): str,
}
)
@websocket_api.async_response
async def handle_details(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Get backup details for a specific slug."""
backup = await hass.data[DATA_MANAGER].async_get_backup(slug=msg["slug"])
connection.send_result(
msg["id"],
{
"backup": backup,
},
)
@websocket_api.require_admin
@websocket_api.websocket_command(
{

View file

@ -147,6 +147,54 @@
'type': 'result',
})
# ---
# name: test_details[with_hassio-with_backup_content]
dict({
'error': dict({
'code': 'unknown_command',
'message': 'Unknown command.',
}),
'id': 1,
'success': False,
'type': 'result',
})
# ---
# name: test_details[with_hassio-without_backup_content]
dict({
'error': dict({
'code': 'unknown_command',
'message': 'Unknown command.',
}),
'id': 1,
'success': False,
'type': 'result',
})
# ---
# name: test_details[without_hassio-with_backup_content]
dict({
'id': 1,
'result': dict({
'backup': dict({
'date': '1970-01-01T00:00:00.000Z',
'name': 'Test',
'path': 'abc123.tar',
'size': 0.0,
'slug': 'abc123',
}),
}),
'success': True,
'type': 'result',
})
# ---
# name: test_details[without_hassio-without_backup_content]
dict({
'id': 1,
'result': dict({
'backup': None,
}),
'success': True,
'type': 'result',
})
# ---
# name: test_generate[with_hassio]
dict({
'error': dict({

View file

@ -5,6 +5,7 @@ from unittest.mock import patch
import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.backup.manager import Backup
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
@ -52,6 +53,41 @@ async def test_info(
assert snapshot == await client.receive_json()
@pytest.mark.parametrize(
"backup_content",
[
pytest.param(TEST_BACKUP, id="with_backup_content"),
pytest.param(None, id="without_backup_content"),
],
)
@pytest.mark.parametrize(
"with_hassio",
[
pytest.param(True, id="with_hassio"),
pytest.param(False, id="without_hassio"),
],
)
async def test_details(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
snapshot: SnapshotAssertion,
with_hassio: bool,
backup_content: Backup | None,
) -> None:
"""Test getting backup info."""
await setup_backup_integration(hass, with_hassio=with_hassio)
client = await hass_ws_client(hass)
await hass.async_block_till_done()
with patch(
"homeassistant.components.backup.manager.BackupManager.async_get_backup",
return_value=backup_content,
):
await client.send_json_auto_id({"type": "backup/details", "slug": "abc123"})
assert await client.receive_json() == snapshot
@pytest.mark.parametrize(
"with_hassio",
[