Skip to content

Commit

Permalink
Add WS endpoint to fetch the details of a backup (#128430)
Browse files Browse the repository at this point in the history
* Add WS endpoint to fetch the details of a backup

* Shorten

Co-authored-by: epenet <[email protected]>

* Adjust

---------

Co-authored-by: Martin Hjelmare <[email protected]>
Co-authored-by: epenet <[email protected]>
  • Loading branch information
3 people authored Oct 15, 2024
1 parent 84b2c74 commit fb7bed2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
24 changes: 24 additions & 0 deletions homeassistant/components/backup/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
{
Expand Down
48 changes: 48 additions & 0 deletions tests/components/backup/snapshots/test_websocket.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
36 changes: 36 additions & 0 deletions tests/components/backup/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from syrupy import SnapshotAssertion

from homeassistant.components.backup.manager import Backup
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError

Expand Down Expand Up @@ -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",
[
Expand Down

0 comments on commit fb7bed2

Please sign in to comment.