Use HassKey in backup (#127546)

* Use HassKey in backup

* Use DATA_MANAGER
This commit is contained in:
epenet 2024-10-04 16:01:50 +02:00 committed by GitHub
parent 79de27544c
commit 8bbbaae290
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 26 additions and 20 deletions

View File

@ -5,7 +5,7 @@ from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN, LOGGER from .const import DATA_MANAGER, DOMAIN, LOGGER
from .http import async_register_http_views from .http import async_register_http_views
from .manager import BackupManager from .manager import BackupManager
from .websocket import async_register_websocket_handlers from .websocket import async_register_websocket_handlers
@ -16,7 +16,7 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Backup integration.""" """Set up the Backup integration."""
backup_manager = BackupManager(hass) backup_manager = BackupManager(hass)
hass.data[DOMAIN] = backup_manager hass.data[DATA_MANAGER] = backup_manager
with_hassio = is_hassio(hass) with_hassio = is_hassio(hass)

View File

@ -1,8 +1,17 @@
"""Constants for the Backup integration.""" """Constants for the Backup integration."""
from __future__ import annotations
from logging import getLogger from logging import getLogger
from typing import TYPE_CHECKING
from homeassistant.util.hass_dict import HassKey
if TYPE_CHECKING:
from .manager import BackupManager
DOMAIN = "backup" DOMAIN = "backup"
DATA_MANAGER: HassKey[BackupManager] = HassKey(DOMAIN)
LOGGER = getLogger(__package__) LOGGER = getLogger(__package__)
EXCLUDE_FROM_BACKUP = [ EXCLUDE_FROM_BACKUP = [

View File

@ -7,8 +7,7 @@ import voluptuous as vol
from homeassistant.components import websocket_api from homeassistant.components import websocket_api
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from .const import DOMAIN, LOGGER from .const import DATA_MANAGER, LOGGER
from .manager import BackupManager
@callback @callback
@ -33,7 +32,7 @@ async def handle_info(
msg: dict[str, Any], msg: dict[str, Any],
) -> None: ) -> None:
"""List all stored backups.""" """List all stored backups."""
manager: BackupManager = hass.data[DOMAIN] manager = hass.data[DATA_MANAGER]
backups = await manager.get_backups() backups = await manager.get_backups()
connection.send_result( connection.send_result(
msg["id"], msg["id"],
@ -58,8 +57,7 @@ async def handle_remove(
msg: dict[str, Any], msg: dict[str, Any],
) -> None: ) -> None:
"""Remove a backup.""" """Remove a backup."""
manager: BackupManager = hass.data[DOMAIN] await hass.data[DATA_MANAGER].remove_backup(msg["slug"])
await manager.remove_backup(msg["slug"])
connection.send_result(msg["id"]) connection.send_result(msg["id"])
@ -72,8 +70,7 @@ async def handle_create(
msg: dict[str, Any], msg: dict[str, Any],
) -> None: ) -> None:
"""Generate a backup.""" """Generate a backup."""
manager: BackupManager = hass.data[DOMAIN] backup = await hass.data[DATA_MANAGER].generate_backup()
backup = await manager.generate_backup()
connection.send_result(msg["id"], backup) connection.send_result(msg["id"], backup)
@ -86,7 +83,7 @@ async def handle_backup_start(
msg: dict[str, Any], msg: dict[str, Any],
) -> None: ) -> None:
"""Backup start notification.""" """Backup start notification."""
manager: BackupManager = hass.data[DOMAIN] manager = hass.data[DATA_MANAGER]
manager.backing_up = True manager.backing_up = True
LOGGER.debug("Backup start notification") LOGGER.debug("Backup start notification")
@ -108,7 +105,7 @@ async def handle_backup_end(
msg: dict[str, Any], msg: dict[str, Any],
) -> None: ) -> None:
"""Backup end notification.""" """Backup end notification."""
manager: BackupManager = hass.data[DOMAIN] manager = hass.data[DATA_MANAGER]
manager.backing_up = False manager.backing_up = False
LOGGER.debug("Backup end notification") LOGGER.debug("Backup end notification")

View File

@ -23,7 +23,7 @@ async def test_downloading_backup(
with ( with (
patch( patch(
"homeassistant.components.backup.http.BackupManager.get_backup", "homeassistant.components.backup.manager.BackupManager.get_backup",
return_value=TEST_BACKUP, return_value=TEST_BACKUP,
), ),
patch("pathlib.Path.exists", return_value=True), patch("pathlib.Path.exists", return_value=True),

View File

@ -33,7 +33,7 @@ async def test_create_service(
await setup_backup_integration(hass) await setup_backup_integration(hass)
with patch( with patch(
"homeassistant.components.backup.websocket.BackupManager.generate_backup", "homeassistant.components.backup.manager.BackupManager.generate_backup",
) as generate_backup: ) as generate_backup:
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,

View File

@ -45,7 +45,7 @@ async def test_info(
await hass.async_block_till_done() await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.backup.websocket.BackupManager.get_backups", "homeassistant.components.backup.manager.BackupManager.get_backups",
return_value={TEST_BACKUP.slug: TEST_BACKUP}, return_value={TEST_BACKUP.slug: TEST_BACKUP},
): ):
await client.send_json_auto_id({"type": "backup/info"}) await client.send_json_auto_id({"type": "backup/info"})
@ -72,7 +72,7 @@ async def test_remove(
await hass.async_block_till_done() await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.backup.websocket.BackupManager.remove_backup", "homeassistant.components.backup.manager.BackupManager.remove_backup",
): ):
await client.send_json_auto_id({"type": "backup/remove", "slug": "abc123"}) await client.send_json_auto_id({"type": "backup/remove", "slug": "abc123"})
assert snapshot == await client.receive_json() assert snapshot == await client.receive_json()
@ -98,7 +98,7 @@ async def test_generate(
await hass.async_block_till_done() await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.backup.websocket.BackupManager.generate_backup", "homeassistant.components.backup.manager.BackupManager.generate_backup",
return_value=TEST_BACKUP, return_value=TEST_BACKUP,
): ):
await client.send_json_auto_id({"type": "backup/generate"}) await client.send_json_auto_id({"type": "backup/generate"})
@ -132,7 +132,7 @@ async def test_backup_end(
await hass.async_block_till_done() await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.backup.websocket.BackupManager.post_backup_actions", "homeassistant.components.backup.manager.BackupManager.post_backup_actions",
): ):
await client.send_json_auto_id({"type": "backup/end"}) await client.send_json_auto_id({"type": "backup/end"})
assert snapshot == await client.receive_json() assert snapshot == await client.receive_json()
@ -165,7 +165,7 @@ async def test_backup_start(
await hass.async_block_till_done() await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.backup.websocket.BackupManager.pre_backup_actions", "homeassistant.components.backup.manager.BackupManager.pre_backup_actions",
): ):
await client.send_json_auto_id({"type": "backup/start"}) await client.send_json_auto_id({"type": "backup/start"})
assert snapshot == await client.receive_json() assert snapshot == await client.receive_json()
@ -193,7 +193,7 @@ async def test_backup_end_excepion(
await hass.async_block_till_done() await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.backup.websocket.BackupManager.post_backup_actions", "homeassistant.components.backup.manager.BackupManager.post_backup_actions",
side_effect=exception, side_effect=exception,
): ):
await client.send_json_auto_id({"type": "backup/end"}) await client.send_json_auto_id({"type": "backup/end"})
@ -222,7 +222,7 @@ async def test_backup_start_excepion(
await hass.async_block_till_done() await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.backup.websocket.BackupManager.pre_backup_actions", "homeassistant.components.backup.manager.BackupManager.pre_backup_actions",
side_effect=exception, side_effect=exception,
): ):
await client.send_json_auto_id({"type": "backup/start"}) await client.send_json_auto_id({"type": "backup/start"})