Skip to content

Commit

Permalink
Revert "Move backup/* WS commands to the backup integration" (home-as…
Browse files Browse the repository at this point in the history
…sistant#111136)

Revert "Move backup/* WS commands to the backup integration (home-assistant#110651)"

This reverts commit ec4e6c3.
  • Loading branch information
ludeeus authored Feb 22, 2024
1 parent 8bf0466 commit 8f83426
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 467 deletions.
7 changes: 6 additions & 1 deletion homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
#
# Integrations providing core functionality:
"application_credentials",
"backup",
"frontend",
"hardware",
"logger",
Expand Down Expand Up @@ -149,6 +148,10 @@
# These integrations are set up if using the Supervisor
"hassio",
}
DEFAULT_INTEGRATIONS_NON_SUPERVISOR = {
# These integrations are set up if not using the Supervisor
"backup",
}
CRITICAL_INTEGRATIONS = {
# Recovery mode is activated if these integrations fail to set up
"frontend",
Expand Down Expand Up @@ -538,6 +541,8 @@ def _get_domains(hass: core.HomeAssistant, config: dict[str, Any]) -> set[str]:
# Add domains depending on if the Supervisor is used or not
if "SUPERVISOR" in os.environ:
domains.update(DEFAULT_INTEGRATIONS_SUPERVISOR)
else:
domains.update(DEFAULT_INTEGRATIONS_NON_SUPERVISOR)

return domains

Expand Down
20 changes: 8 additions & 12 deletions homeassistant/components/backup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,23 @@

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
53 changes: 2 additions & 51 deletions homeassistant/components/backup/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@
from homeassistant.components import websocket_api
from homeassistant.core import HomeAssistant, callback

from .const import DOMAIN, LOGGER
from .const import DOMAIN
from .manager import BackupManager


@callback
def async_register_websocket_handlers(hass: HomeAssistant, with_hassio: bool) -> None:
def async_register_websocket_handlers(hass: HomeAssistant) -> 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)
Expand Down Expand Up @@ -74,47 +69,3 @@ 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"])
40 changes: 40 additions & 0 deletions homeassistant/components/recorder/websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from datetime import datetime as dt
import logging
from typing import Any, Literal, cast

import voluptuous as vol
Expand Down Expand Up @@ -45,6 +46,8 @@
)
from .util import PERIOD_SCHEMA, get_instance, resolve_period

_LOGGER: logging.Logger = logging.getLogger(__package__)

UNIT_SCHEMA = vol.Schema(
{
vol.Optional("data_rate"): vol.In(DataRateConverter.VALID_UNITS),
Expand All @@ -70,6 +73,8 @@
def async_setup(hass: HomeAssistant) -> None:
"""Set up the recorder websocket API."""
websocket_api.async_register_command(hass, ws_adjust_sum_statistics)
websocket_api.async_register_command(hass, ws_backup_end)
websocket_api.async_register_command(hass, ws_backup_start)
websocket_api.async_register_command(hass, ws_change_statistics_unit)
websocket_api.async_register_command(hass, ws_clear_statistics)
websocket_api.async_register_command(hass, ws_get_statistic_during_period)
Expand Down Expand Up @@ -512,3 +517,38 @@ def ws_info(
"thread_running": is_running,
}
connection.send_result(msg["id"], recorder_info)


@websocket_api.ws_require_user(only_supervisor=True)
@websocket_api.websocket_command({vol.Required("type"): "backup/start"})
@websocket_api.async_response
async def ws_backup_start(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None:
"""Backup start notification."""

_LOGGER.info("Backup start notification, locking database for writes")
instance = get_instance(hass)
try:
await instance.lock_database()
except TimeoutError as err:
connection.send_error(msg["id"], "timeout_error", 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 ws_backup_end(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None:
"""Backup end notification."""

instance = get_instance(hass)
_LOGGER.info("Backup end notification, releasing write lock")
if not instance.unlock_database():
connection.send_error(
msg["id"], "database_unlock_failed", "Failed to unlock database."
)
connection.send_result(msg["id"])
Loading

0 comments on commit 8f83426

Please sign in to comment.