Move backup/* WS commands to the backup integration (#111636)

Revert "Revert "Move backup/* WS commands to the backup integration" (#111136)"

This reverts commit 8f83426895.
This commit is contained in:
Joakim Sørensen 2024-02-27 17:56:07 +01:00 committed by GitHub
parent 890e651bdd
commit f622ddef47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 467 additions and 158 deletions

View file

@ -14,23 +14,27 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Backup integration."""
if is_hassio(hass):
LOGGER.error(
"The backup integration is not supported on this installation method, "
"please remove it from your configuration"
)
return False
backup_manager = BackupManager(hass)
hass.data[DOMAIN] = backup_manager
with_hassio = is_hassio(hass)
async_register_websocket_handlers(hass, with_hassio)
if with_hassio:
if DOMAIN in config:
LOGGER.error(
"The backup integration is not supported on this installation method, "
"please remove it from your configuration"
)
return True
async def async_handle_create_service(call: ServiceCall) -> None:
"""Service handler for creating backups."""
await backup_manager.generate_backup()
hass.services.async_register(DOMAIN, "create", async_handle_create_service)
async_register_websocket_handlers(hass)
async_register_http_views(hass)
return True

View file

@ -6,13 +6,18 @@ import voluptuous as vol
from homeassistant.components import websocket_api
from homeassistant.core import HomeAssistant, callback
from .const import DOMAIN
from .const import DOMAIN, LOGGER
from .manager import BackupManager
@callback
def async_register_websocket_handlers(hass: HomeAssistant) -> None:
def async_register_websocket_handlers(hass: HomeAssistant, with_hassio: bool) -> None:
"""Register websocket commands."""
if with_hassio:
websocket_api.async_register_command(hass, handle_backup_end)
websocket_api.async_register_command(hass, handle_backup_start)
return
websocket_api.async_register_command(hass, handle_info)
websocket_api.async_register_command(hass, handle_create)
websocket_api.async_register_command(hass, handle_remove)
@ -69,3 +74,47 @@ async def handle_create(
manager: BackupManager = hass.data[DOMAIN]
backup = await manager.generate_backup()
connection.send_result(msg["id"], backup)
@websocket_api.ws_require_user(only_supervisor=True)
@websocket_api.websocket_command({vol.Required("type"): "backup/start"})
@websocket_api.async_response
async def handle_backup_start(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Backup start notification."""
manager: BackupManager = hass.data[DOMAIN]
manager.backing_up = True
LOGGER.debug("Backup start notification")
try:
await manager.pre_backup_actions()
except Exception as err: # pylint: disable=broad-except
connection.send_error(msg["id"], "pre_backup_actions_failed", str(err))
return
connection.send_result(msg["id"])
@websocket_api.ws_require_user(only_supervisor=True)
@websocket_api.websocket_command({vol.Required("type"): "backup/end"})
@websocket_api.async_response
async def handle_backup_end(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Backup end notification."""
manager: BackupManager = hass.data[DOMAIN]
manager.backing_up = False
LOGGER.debug("Backup end notification")
try:
await manager.post_backup_actions()
except Exception as err: # pylint: disable=broad-except
connection.send_error(msg["id"], "post_backup_actions_failed", str(err))
return
connection.send_result(msg["id"])