Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make WS command backup/generate send events #130524

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions homeassistant/components/backup/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import abc
import asyncio
from collections.abc import Callable
from dataclasses import asdict, dataclass
import hashlib
import io
Expand Down Expand Up @@ -34,6 +35,13 @@
BUF_SIZE = 2**20 * 4 # 4MB


@dataclass(slots=True)
class NewBackup:
"""New backup class."""

slug: str


@dataclass(slots=True)
class Backup:
"""Backup class."""
Expand All @@ -49,6 +57,15 @@ def as_dict(self) -> dict:
return {**asdict(self), "path": self.path.as_posix()}


@dataclass(slots=True)
class BackupProgress:
"""Backup progress class."""

done: bool
stage: str | None
success: bool | None
Comment on lines +60 to +66
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inspired by backup job progress fired by supervisor:

event_type: supervisor_event
data:
  event: job
  data:
    name: backup_manager_partial_backup
    reference: a945ac38
    uuid: 6a6ab1807ed64ca18ab1d03fb2fd2282
    progress: 0
    stage: finishing_file
    done: true
    parent_id: null
    errors: []



class BackupPlatformProtocol(Protocol):
"""Define the format that backup platforms can have."""

Expand All @@ -65,7 +82,7 @@ class BaseBackupManager(abc.ABC):
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the backup manager."""
self.hass = hass
self.backing_up = False
self.backup_task: asyncio.Task | None = None
self.backups: dict[str, Backup] = {}
self.loaded_platforms = False
self.platforms: dict[str, BackupPlatformProtocol] = {}
Expand Down Expand Up @@ -133,7 +150,12 @@ async def async_restore_backup(self, slug: str, **kwargs: Any) -> None:
"""Restore a backup."""

@abc.abstractmethod
async def async_create_backup(self, **kwargs: Any) -> Backup:
async def async_create_backup(
self,
*,
on_progress: Callable[[BackupProgress], None] | None,
**kwargs: Any,
) -> NewBackup:
"""Generate a backup."""

@abc.abstractmethod
Expand Down Expand Up @@ -292,17 +314,36 @@ def _move_and_cleanup() -> None:
await self.hass.async_add_executor_job(_move_and_cleanup)
await self.load_backups()

async def async_create_backup(self, **kwargs: Any) -> Backup:
async def async_create_backup(
self,
*,
on_progress: Callable[[BackupProgress], None] | None,
**kwargs: Any,
) -> NewBackup:
"""Generate a backup."""
if self.backing_up:
if self.backup_task:
raise HomeAssistantError("Backup already in progress")
backup_name = f"Core {HAVERSION}"
date_str = dt_util.now().isoformat()
slug = _generate_slug(date_str, backup_name)
self.backup_task = self.hass.async_create_task(
self._async_create_backup(backup_name, date_str, slug, on_progress),
name="backup_manager_create_backup",
eager_start=False, # To ensure the task is not started before we return
)
return NewBackup(slug=slug)

async def _async_create_backup(
self,
backup_name: str,
date_str: str,
slug: str,
on_progress: Callable[[BackupProgress], None] | None,
) -> Backup:
"""Generate a backup."""
success = False
try:
self.backing_up = True
await self.async_pre_backup_actions()
backup_name = f"Core {HAVERSION}"
date_str = dt_util.now().isoformat()
slug = _generate_slug(date_str, backup_name)

backup_data = {
"slug": slug,
Expand All @@ -329,9 +370,12 @@ async def async_create_backup(self, **kwargs: Any) -> Backup:
if self.loaded_backups:
self.backups[slug] = backup
LOGGER.debug("Generated new backup with slug %s", slug)
success = True
return backup
finally:
self.backing_up = False
if on_progress:
on_progress(BackupProgress(done=True, stage=None, success=success))
self.backup_task = None
await self.async_post_backup_actions()

def _mkdir_and_generate_backup_contents(
Expand Down
11 changes: 7 additions & 4 deletions homeassistant/components/backup/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from homeassistant.core import HomeAssistant, callback

from .const import DATA_MANAGER, LOGGER
from .manager import BackupProgress


@callback
Expand Down Expand Up @@ -40,7 +41,7 @@ async def handle_info(
msg["id"],
{
"backups": list(backups.values()),
"backing_up": manager.backing_up,
"backing_up": manager.backup_task is not None,
},
)

Expand Down Expand Up @@ -113,7 +114,11 @@ async def handle_create(
msg: dict[str, Any],
) -> None:
"""Generate a backup."""
backup = await hass.data[DATA_MANAGER].async_create_backup()

def on_progress(progress: BackupProgress) -> None:
connection.send_message(websocket_api.event_message(msg["id"], progress))

backup = await hass.data[DATA_MANAGER].async_create_backup(on_progress=on_progress)
connection.send_result(msg["id"], backup)


Expand All @@ -127,7 +132,6 @@ async def handle_backup_start(
) -> None:
"""Backup start notification."""
manager = hass.data[DATA_MANAGER]
manager.backing_up = True
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This did not seem to be needed.

LOGGER.debug("Backup start notification")

try:
Expand All @@ -149,7 +153,6 @@ async def handle_backup_end(
) -> None:
"""Backup end notification."""
manager = hass.data[DATA_MANAGER]
manager.backing_up = False
LOGGER.debug("Backup end notification")

try:
Expand Down
73 changes: 73 additions & 0 deletions tests/components/backup/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Test fixtures for the Backup integration."""

from __future__ import annotations

from collections.abc import Generator
from pathlib import Path
from unittest.mock import MagicMock, Mock, patch

import pytest

from homeassistant.core import HomeAssistant


@pytest.fixture(name="mocked_json_bytes")
def mocked_json_bytes_fixture() -> Generator[Mock]:
"""Mock json_bytes."""
with patch(
"homeassistant.components.backup.manager.json_bytes",
return_value=b"{}", # Empty JSON
) as mocked_json_bytes:
yield mocked_json_bytes


@pytest.fixture(name="mocked_tarfile")
def mocked_tarfile_fixture() -> Generator[Mock]:
"""Mock tarfile."""
with patch(
"homeassistant.components.backup.manager.SecureTarFile"
) as mocked_tarfile:
yield mocked_tarfile


@pytest.fixture(name="mock_backup_generation")
def mock_backup_generation_fixture(
hass: HomeAssistant, mocked_json_bytes: Mock, mocked_tarfile: Mock
) -> Generator[None]:
"""Mock backup generator."""

def _mock_iterdir(path: Path) -> list[Path]:
if not path.name.endswith("testing_config"):
return []
return [
Path("test.txt"),
Path(".DS_Store"),
Path(".storage"),
]

with (
patch("pathlib.Path.iterdir", _mock_iterdir),
patch("pathlib.Path.stat", MagicMock(st_size=123)),
patch("pathlib.Path.is_file", lambda x: x.name != ".storage"),
patch(
"pathlib.Path.is_dir",
lambda x: x.name == ".storage",
),
patch(
"pathlib.Path.exists",
lambda x: x != Path(hass.config.path("backups")),
),
patch(
"pathlib.Path.is_symlink",
lambda _: False,
),
patch(
"pathlib.Path.mkdir",
MagicMock(),
),
patch(
"homeassistant.components.backup.manager.HAVERSION",
"2025.1.0",
),
):
yield
17 changes: 12 additions & 5 deletions tests/components/backup/snapshots/test_websocket.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,23 @@
dict({
'id': 1,
'result': dict({
'date': '1970-01-01T00:00:00.000Z',
'name': 'Test',
'path': 'abc123.tar',
'size': 0.0,
'slug': 'abc123',
'slug': '27f5c632',
}),
'success': True,
'type': 'result',
})
# ---
# name: test_generate[without_hassio].1
dict({
'event': dict({
'done': True,
'stage': None,
'success': True,
}),
'id': 1,
'type': 'event',
})
# ---
# name: test_info[with_hassio]
dict({
'error': dict({
Expand Down
Loading
Loading