-
-
Notifications
You must be signed in to change notification settings - Fork 36.6k
Add backup helper #139199
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
Merged
Merged
Add backup helper #139199
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5aa579d
Add backup helper
emontnemery 446e6f3
Add hassio to stage 1
emontnemery fb332c2
Merge branch 'dev' into backup_helper
bdraco e9f1f1c
Apply same changes to newly merged `webdav` and `azure_storage` to fi…
bdraco 14e8ca3
Address comments, add tests
emontnemery File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| """Websocket commands for the Backup integration.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components import websocket_api | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.helpers.backup import async_subscribe_events | ||
|
|
||
| from .const import DATA_MANAGER | ||
| from .manager import ManagerStateEvent | ||
|
|
||
|
|
||
| @callback | ||
| def async_register_websocket_handlers(hass: HomeAssistant) -> None: | ||
| """Register websocket commands.""" | ||
| websocket_api.async_register_command(hass, handle_subscribe_events) | ||
|
|
||
|
|
||
| @websocket_api.require_admin | ||
| @websocket_api.websocket_command({vol.Required("type"): "backup/subscribe_events"}) | ||
| @websocket_api.async_response | ||
| async def handle_subscribe_events( | ||
| hass: HomeAssistant, | ||
| connection: websocket_api.ActiveConnection, | ||
| msg: dict[str, Any], | ||
| ) -> None: | ||
| """Subscribe to backup events.""" | ||
|
|
||
| def on_event(event: ManagerStateEvent) -> None: | ||
| connection.send_message(websocket_api.event_message(msg["id"], event)) | ||
|
|
||
| if DATA_MANAGER in hass.data: | ||
| manager = hass.data[DATA_MANAGER] | ||
| on_event(manager.last_event) | ||
| connection.subscriptions[msg["id"]] = async_subscribe_events(hass, on_event) | ||
| connection.send_result(msg["id"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """Helpers for the backup integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from collections.abc import Callable | ||
| from dataclasses import dataclass, field | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.exceptions import HomeAssistantError | ||
| from homeassistant.util.hass_dict import HassKey | ||
|
|
||
| if TYPE_CHECKING: | ||
| from homeassistant.components.backup import BackupManager, ManagerStateEvent | ||
|
|
||
| DATA_BACKUP: HassKey[BackupData] = HassKey("backup_data") | ||
| DATA_MANAGER: HassKey[BackupManager] = HassKey("backup") | ||
|
|
||
|
|
||
| @dataclass(slots=True) | ||
| class BackupData: | ||
| """Backup data stored in hass.data.""" | ||
|
|
||
| backup_event_subscriptions: list[Callable[[ManagerStateEvent], None]] = field( | ||
| default_factory=list | ||
| ) | ||
| manager_ready: asyncio.Future[None] = field(default_factory=asyncio.Future) | ||
|
|
||
|
|
||
| @callback | ||
| def async_initialize_backup(hass: HomeAssistant) -> None: | ||
| """Initialize backup data. | ||
|
|
||
| This creates the BackupData instance stored in hass.data[DATA_BACKUP] and | ||
| registers the basic backup websocket API which is used by frontend to subscribe | ||
| to backup events. | ||
| """ | ||
| # pylint: disable-next=import-outside-toplevel | ||
| from homeassistant.components.backup import basic_websocket | ||
|
|
||
| hass.data[DATA_BACKUP] = BackupData() | ||
| basic_websocket.async_register_websocket_handlers(hass) | ||
|
|
||
|
|
||
| async def async_get_manager(hass: HomeAssistant) -> BackupManager: | ||
| """Get the backup manager instance. | ||
|
|
||
| Raises HomeAssistantError if the backup integration is not available. | ||
| """ | ||
| if DATA_BACKUP not in hass.data: | ||
| raise HomeAssistantError("Backup integration is not available") | ||
|
|
||
| await hass.data[DATA_BACKUP].manager_ready | ||
| return hass.data[DATA_MANAGER] | ||
|
|
||
|
|
||
| @callback | ||
| def async_subscribe_events( | ||
| hass: HomeAssistant, | ||
| on_event: Callable[[ManagerStateEvent], None], | ||
| ) -> Callable[[], None]: | ||
| """Subscribe to backup events.""" | ||
| backup_event_subscriptions = hass.data[DATA_BACKUP].backup_event_subscriptions | ||
|
|
||
| def remove_subscription() -> None: | ||
| backup_event_subscriptions.remove(on_event) | ||
|
|
||
| backup_event_subscriptions.append(on_event) | ||
| return remove_subscription | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.