core/tests/components/conftest.py

876 lines
28 KiB
Python
Raw Normal View History

"""Fixtures for component testing."""
2024-05-22 07:34:17 +00:00
from __future__ import annotations
import asyncio
from collections.abc import AsyncGenerator, Callable, Generator
from functools import lru_cache
from importlib.util import find_spec
from pathlib import Path
import string
from typing import TYPE_CHECKING, Any
from unittest.mock import AsyncMock, MagicMock, patch
2021-01-01 21:31:56 +00:00
from aiohasupervisor.models import (
Discovery,
Repository,
ResolutionInfo,
StoreAddon,
StoreInfo,
)
import pytest
from homeassistant.components import repairs
from homeassistant.config_entries import (
DISCOVERY_SOURCES,
ConfigEntriesFlowManager,
FlowResult,
OptionsFlowManager,
)
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import Context, HomeAssistant, ServiceRegistry, ServiceResponse
from homeassistant.data_entry_flow import (
FlowContext,
FlowHandler,
FlowManager,
FlowResultType,
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.translation import async_get_translations
from homeassistant.util import yaml
if TYPE_CHECKING:
from homeassistant.components.hassio import AddonManager
2024-05-22 07:34:17 +00:00
from .conversation import MockAgent
from .device_tracker.common import MockScanner
from .light.common import MockLight
from .sensor.common import MockSensor
from .switch.common import MockSwitch
@pytest.fixture(scope="session", autouse=find_spec("zeroconf") is not None)
def patch_zeroconf_multiple_catcher() -> Generator[None]:
"""If installed, patch zeroconf wrapper that detects if multiple instances are used."""
2021-04-25 18:16:38 +00:00
with patch(
"homeassistant.components.zeroconf.install_multiple_zeroconf_catcher",
side_effect=lambda zc: None,
):
yield
@pytest.fixture(scope="session", autouse=True)
def prevent_io() -> Generator[None]:
"""Fixture to prevent certain I/O from happening."""
2019-07-31 19:25:30 +00:00
with patch(
"homeassistant.components.http.ban.load_yaml_config_file",
2019-07-31 19:25:30 +00:00
):
yield
@pytest.fixture
def entity_registry_enabled_by_default() -> Generator[None]:
"""Test fixture that ensures all entities are enabled in the registry."""
with patch(
"homeassistant.helpers.entity.Entity.entity_registry_enabled_default",
return_value=True,
):
yield
2023-05-25 09:59:20 +00:00
# Blueprint test fixtures
@pytest.fixture(name="stub_blueprint_populate")
def stub_blueprint_populate_fixture() -> Generator[None]:
"""Stub copying the blueprints to the config folder."""
# pylint: disable-next=import-outside-toplevel
from .blueprint.common import stub_blueprint_populate_fixture_helper
yield from stub_blueprint_populate_fixture_helper()
# TTS test fixtures
2023-05-25 09:59:20 +00:00
@pytest.fixture(name="mock_tts_get_cache_files")
def mock_tts_get_cache_files_fixture() -> Generator[MagicMock]:
2023-05-25 09:59:20 +00:00
"""Mock the list TTS cache function."""
# pylint: disable-next=import-outside-toplevel
from .tts.common import mock_tts_get_cache_files_fixture_helper
2023-05-25 09:59:20 +00:00
yield from mock_tts_get_cache_files_fixture_helper()
@pytest.fixture(name="mock_tts_init_cache_dir")
def mock_tts_init_cache_dir_fixture(
init_tts_cache_dir_side_effect: Any,
) -> Generator[MagicMock]:
2023-05-25 09:59:20 +00:00
"""Mock the TTS cache dir in memory."""
# pylint: disable-next=import-outside-toplevel
from .tts.common import mock_tts_init_cache_dir_fixture_helper
2023-05-25 09:59:20 +00:00
yield from mock_tts_init_cache_dir_fixture_helper(init_tts_cache_dir_side_effect)
@pytest.fixture(name="init_tts_cache_dir_side_effect")
def init_tts_cache_dir_side_effect_fixture() -> Any:
"""Return the cache dir."""
# pylint: disable-next=import-outside-toplevel
from .tts.common import init_tts_cache_dir_side_effect_fixture_helper
2023-05-25 09:59:20 +00:00
return init_tts_cache_dir_side_effect_fixture_helper()
@pytest.fixture(name="mock_tts_cache_dir")
def mock_tts_cache_dir_fixture(
tmp_path: Path,
mock_tts_init_cache_dir: MagicMock,
mock_tts_get_cache_files: MagicMock,
request: pytest.FixtureRequest,
) -> Generator[Path]:
2023-05-25 09:59:20 +00:00
"""Mock the TTS cache dir with empty dir."""
# pylint: disable-next=import-outside-toplevel
from .tts.common import mock_tts_cache_dir_fixture_helper
2023-05-25 09:59:20 +00:00
yield from mock_tts_cache_dir_fixture_helper(
tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request
)
@pytest.fixture(name="tts_mutagen_mock")
def tts_mutagen_mock_fixture() -> Generator[MagicMock]:
2023-05-25 09:59:20 +00:00
"""Mock writing tags."""
# pylint: disable-next=import-outside-toplevel
from .tts.common import tts_mutagen_mock_fixture_helper
2023-05-25 09:59:20 +00:00
yield from tts_mutagen_mock_fixture_helper()
@pytest.fixture(name="mock_conversation_agent")
def mock_conversation_agent_fixture(hass: HomeAssistant) -> MockAgent:
"""Mock a conversation agent."""
# pylint: disable-next=import-outside-toplevel
from .conversation.common import mock_conversation_agent_fixture_helper
return mock_conversation_agent_fixture_helper(hass)
@pytest.fixture(scope="session", autouse=find_spec("ffmpeg") is not None)
def prevent_ffmpeg_subprocess() -> Generator[None]:
"""If installed, prevent ffmpeg from creating a subprocess."""
with patch(
"homeassistant.components.ffmpeg.FFVersion.get_version", return_value="6.0"
):
yield
@pytest.fixture
2024-05-22 07:34:17 +00:00
def mock_light_entities() -> list[MockLight]:
"""Return mocked light entities."""
# pylint: disable-next=import-outside-toplevel
from .light.common import MockLight
return [
MockLight("Ceiling", STATE_ON),
MockLight("Ceiling", STATE_OFF),
MockLight(None, STATE_OFF),
]
@pytest.fixture
2024-05-22 07:34:17 +00:00
def mock_sensor_entities() -> dict[str, MockSensor]:
"""Return mocked sensor entities."""
# pylint: disable-next=import-outside-toplevel
from .sensor.common import get_mock_sensor_entities
return get_mock_sensor_entities()
@pytest.fixture
2024-05-22 07:34:17 +00:00
def mock_switch_entities() -> list[MockSwitch]:
"""Return mocked toggle entities."""
# pylint: disable-next=import-outside-toplevel
from .switch.common import get_mock_switch_entities
return get_mock_switch_entities()
@pytest.fixture
2024-05-22 07:34:17 +00:00
def mock_legacy_device_scanner() -> MockScanner:
"""Return mocked legacy device scanner entity."""
# pylint: disable-next=import-outside-toplevel
from .device_tracker.common import MockScanner
return MockScanner()
@pytest.fixture
2024-05-22 07:34:17 +00:00
def mock_legacy_device_tracker_setup() -> Callable[[HomeAssistant, MockScanner], None]:
"""Return setup callable for legacy device tracker setup."""
# pylint: disable-next=import-outside-toplevel
from .device_tracker.common import mock_legacy_device_tracker_setup
return mock_legacy_device_tracker_setup
@pytest.fixture(name="addon_manager")
def addon_manager_fixture(
hass: HomeAssistant, supervisor_client: AsyncMock
) -> AddonManager:
"""Return an AddonManager instance."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_addon_manager
return mock_addon_manager(hass)
@pytest.fixture(name="discovery_info")
def discovery_info_fixture() -> list[Discovery]:
"""Return the discovery info from the supervisor."""
return []
@pytest.fixture(name="discovery_info_side_effect")
def discovery_info_side_effect_fixture() -> Any | None:
"""Return the discovery info from the supervisor."""
return None
@pytest.fixture(name="get_addon_discovery_info")
def get_addon_discovery_info_fixture(
supervisor_client: AsyncMock,
discovery_info: list[Discovery],
discovery_info_side_effect: Any | None,
) -> AsyncMock:
"""Mock get add-on discovery info."""
supervisor_client.discovery.list.return_value = discovery_info
supervisor_client.discovery.list.side_effect = discovery_info_side_effect
return supervisor_client.discovery.list
@pytest.fixture(name="get_discovery_message_side_effect")
def get_discovery_message_side_effect_fixture() -> Any | None:
"""Side effect for getting a discovery message by uuid."""
return None
@pytest.fixture(name="get_discovery_message")
def get_discovery_message_fixture(
supervisor_client: AsyncMock, get_discovery_message_side_effect: Any | None
) -> AsyncMock:
"""Mock getting a discovery message by uuid."""
supervisor_client.discovery.get.side_effect = get_discovery_message_side_effect
return supervisor_client.discovery.get
@pytest.fixture(name="addon_store_info_side_effect")
def addon_store_info_side_effect_fixture() -> Any | None:
"""Return the add-on store info side effect."""
return None
@pytest.fixture(name="addon_store_info")
def addon_store_info_fixture(
supervisor_client: AsyncMock,
addon_store_info_side_effect: Any | None,
) -> AsyncMock:
"""Mock Supervisor add-on store info."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_addon_store_info
return mock_addon_store_info(supervisor_client, addon_store_info_side_effect)
@pytest.fixture(name="addon_info_side_effect")
def addon_info_side_effect_fixture() -> Any | None:
"""Return the add-on info side effect."""
return None
@pytest.fixture(name="addon_info")
def addon_info_fixture(
supervisor_client: AsyncMock, addon_info_side_effect: Any | None
) -> AsyncMock:
"""Mock Supervisor add-on info."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_addon_info
return mock_addon_info(supervisor_client, addon_info_side_effect)
@pytest.fixture(name="addon_not_installed")
def addon_not_installed_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on not installed."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_addon_not_installed
return mock_addon_not_installed(addon_store_info, addon_info)
@pytest.fixture(name="addon_installed")
def addon_installed_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on already installed but not running."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_addon_installed
return mock_addon_installed(addon_store_info, addon_info)
@pytest.fixture(name="addon_running")
def addon_running_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on already running."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_addon_running
return mock_addon_running(addon_store_info, addon_info)
@pytest.fixture(name="install_addon_side_effect")
def install_addon_side_effect_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> Any | None:
"""Return the install add-on side effect."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_install_addon_side_effect
return mock_install_addon_side_effect(addon_store_info, addon_info)
@pytest.fixture(name="install_addon")
def install_addon_fixture(
supervisor_client: AsyncMock,
install_addon_side_effect: Any | None,
) -> AsyncMock:
"""Mock install add-on."""
supervisor_client.store.install_addon.side_effect = install_addon_side_effect
return supervisor_client.store.install_addon
@pytest.fixture(name="start_addon_side_effect")
def start_addon_side_effect_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> Any | None:
"""Return the start add-on options side effect."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_start_addon_side_effect
return mock_start_addon_side_effect(addon_store_info, addon_info)
@pytest.fixture(name="start_addon")
def start_addon_fixture(
supervisor_client: AsyncMock, start_addon_side_effect: Any | None
) -> AsyncMock:
"""Mock start add-on."""
supervisor_client.addons.start_addon.side_effect = start_addon_side_effect
return supervisor_client.addons.start_addon
@pytest.fixture(name="restart_addon_side_effect")
def restart_addon_side_effect_fixture() -> Any | None:
"""Return the restart add-on options side effect."""
return None
@pytest.fixture(name="restart_addon")
def restart_addon_fixture(
supervisor_client: AsyncMock,
restart_addon_side_effect: Any | None,
) -> AsyncMock:
"""Mock restart add-on."""
supervisor_client.addons.restart_addon.side_effect = restart_addon_side_effect
return supervisor_client.addons.restart_addon
@pytest.fixture(name="stop_addon")
def stop_addon_fixture(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock stop add-on."""
return supervisor_client.addons.stop_addon
@pytest.fixture(name="addon_options")
def addon_options_fixture(addon_info: AsyncMock) -> dict[str, Any]:
"""Mock add-on options."""
return addon_info.return_value.options
@pytest.fixture(name="set_addon_options_side_effect")
def set_addon_options_side_effect_fixture(
addon_options: dict[str, Any],
) -> Any | None:
"""Return the set add-on options side effect."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_set_addon_options_side_effect
return mock_set_addon_options_side_effect(addon_options)
@pytest.fixture(name="set_addon_options")
def set_addon_options_fixture(
supervisor_client: AsyncMock,
set_addon_options_side_effect: Any | None,
) -> AsyncMock:
"""Mock set add-on options."""
supervisor_client.addons.set_addon_options.side_effect = (
set_addon_options_side_effect
)
return supervisor_client.addons.set_addon_options
@pytest.fixture(name="uninstall_addon")
def uninstall_addon_fixture(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock uninstall add-on."""
return supervisor_client.addons.uninstall_addon
@pytest.fixture(name="create_backup")
def create_backup_fixture() -> Generator[AsyncMock]:
"""Mock create backup."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_create_backup
yield from mock_create_backup()
@pytest.fixture(name="update_addon")
def update_addon_fixture(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock update add-on."""
return supervisor_client.store.update_addon
@pytest.fixture(name="store_addons")
def store_addons_fixture() -> list[StoreAddon]:
"""Mock store addons list."""
return []
@pytest.fixture(name="store_repositories")
def store_repositories_fixture() -> list[Repository]:
"""Mock store repositories list."""
return []
@pytest.fixture(name="store_info")
def store_info_fixture(
supervisor_client: AsyncMock,
store_addons: list[StoreAddon],
store_repositories: list[Repository],
) -> AsyncMock:
"""Mock store info."""
supervisor_client.store.info.return_value = StoreInfo(
addons=store_addons, repositories=store_repositories
)
return supervisor_client.store.info
@pytest.fixture(name="addon_stats")
def addon_stats_fixture(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock addon stats info."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import mock_addon_stats
return mock_addon_stats(supervisor_client)
@pytest.fixture(name="addon_changelog")
def addon_changelog_fixture(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock addon changelog."""
supervisor_client.store.addon_changelog.return_value = ""
return supervisor_client.store.addon_changelog
@pytest.fixture(name="supervisor_is_connected")
def supervisor_is_connected_fixture(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock supervisor is connected."""
supervisor_client.supervisor.ping.return_value = None
return supervisor_client.supervisor.ping
@pytest.fixture(name="resolution_info")
def resolution_info_fixture(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock resolution info from supervisor."""
supervisor_client.resolution.info.return_value = ResolutionInfo(
suggestions=[],
unsupported=[],
unhealthy=[],
issues=[],
checks=[],
)
return supervisor_client.resolution.info
@pytest.fixture(name="resolution_suggestions_for_issue")
def resolution_suggestions_for_issue_fixture(supervisor_client: AsyncMock) -> AsyncMock:
"""Mock suggestions by issue from supervisor resolution."""
supervisor_client.resolution.suggestions_for_issue.return_value = []
return supervisor_client.resolution.suggestions_for_issue
@pytest.fixture(name="supervisor_client")
def supervisor_client() -> Generator[AsyncMock]:
"""Mock the supervisor client."""
supervisor_client = AsyncMock()
supervisor_client.addons = AsyncMock()
supervisor_client.discovery = AsyncMock()
supervisor_client.homeassistant = AsyncMock()
supervisor_client.host = AsyncMock()
supervisor_client.os = AsyncMock()
supervisor_client.resolution = AsyncMock()
supervisor_client.supervisor = AsyncMock()
with (
patch(
"homeassistant.components.hassio.get_supervisor_client",
return_value=supervisor_client,
),
patch(
"homeassistant.components.hassio.handler.get_supervisor_client",
return_value=supervisor_client,
),
patch(
"homeassistant.components.hassio.addon_manager.get_supervisor_client",
return_value=supervisor_client,
),
Merge feature branch with backup changes to dev (#132954) * Reapply "Make WS command backup/generate send events" (#131530) This reverts commit 9b8316df3f78d136ae73c096168bd73ffebc4465. * MVP implementation of Backup sync agents (#126122) * init sync agent * add syncing * root import * rename list to info and add sync state * Add base backup class * Revert unneded change * adjust tests * move to kitchen_sink * split * move * Adjustments * Adjustment * update * Tests * Test unknown agent * adjust * Adjust for different test environments * Change /info WS to contain a dictinary * reorder * Add websocket command to trigger sync from the supervisor * cleanup * Make mypy happier --------- Co-authored-by: Erik <erik@montnemery.com> * Make BackupSyncMetadata model a dataclass (#130555) Make backup BackupSyncMetadata model a dataclass * Rename backup sync agent to backup agent (#130575) * Rename sync agent module to agent * Rename BackupSyncAgent to BackupAgent * Fix test typo * Rename async_get_backup_sync_agents to async_get_backup_agents * Rename and clean up remaining sync things * Update kitchen sink * Apply suggestions from code review * Update test_manager.py --------- Co-authored-by: Erik Montnemery <erik@montnemery.com> * Add additional options to WS command backup/generate (#130530) * Add additional options to WS command backup/generate * Improve test * Improve test * Align parameter names in backup/agents/* WS commands (#130590) * Allow setting password for backups (#110630) * Allow setting password for backups * use is_hassio from helpers * move it * Fix getting psw * Fix restoring with psw * Address review comments * Improve docstring * Adjust kitchen sink * Adjust --------- Co-authored-by: Erik <erik@montnemery.com> * Export relevant names from backup integration (#130596) * Tweak backup agent interface (#130613) * Tweak backup agent interface * Adjust kitchen_sink * Test kitchen sink backup (#130609) * Test agents_list_backups * Test agents_info * Test agents_download * Export Backup from manager * Test agents_upload * Update tests after rebase * Use backup domain * Remove WS command backup/upload (#130588) * Remove WS command backup/upload * Disable failing kitchen_sink test * Make local backup a backup agent (#130623) * Make local backup a backup agent * Adjust * Adjust * Adjust * Adjust tests * Adjust * Adjust * Adjust docstring * Adjust * Protect members of CoreLocalBackupAgent * Remove redundant check for file * Make the backup.create service use the first local agent * Add BackupAgent.async_get_backup * Fix some TODOs * Add support for downloading backup from a remote agent * Fix restore * Fix test * Adjust kitchen_sink test * Remove unused method BackupManager.async_get_backup_path * Re-enable kitchen sink test * Remove BaseBackupManager.async_upload_backup * Support restore from remote agent * Fix review comments * Include backup agent error in response to WS command backup/info (#130884) * Adjust code related to WS command backup/info (#130890) * Include backup agent error in response to WS command backup/details (#130892) * Remove LOCAL_AGENT_ID constant from backup manager (#130895) * Add backup config storage (#130871) * Add base for backup config * Allow updating backup config * Test loading backup config * Add backup config update method * Add temporary check for BackupAgent.async_remove_backup (#130893) * Rename backup slug to backup_id (#130902) * Improve backup websocket API tests (#130912) * Improve backup websocket API tests * Add missing snapshot * Fix tests leaving files behind * Improve backup manager backup creation tests (#130916) * Remove class backup.backup.LocalBackup (#130919) * Add agent delete backup (#130921) * Add backup agent delete backup * Remove agents delete websocket command * Update docstring Co-authored-by: Erik Montnemery <erik@montnemery.com> --------- Co-authored-by: Erik Montnemery <erik@montnemery.com> * Disable core local backup agent in hassio (#130933) * Rename remove backup to delete backup (#130940) * Rename remove backup to delete backup * Revert "backup/delete" * Refactor BackupManager (#130947) * Refactor BackupManager * Adjust * Adjust backup creation * Copy in executor * Fix BackupManager.async_get_backup (#130975) * Fix typo in backup tests (#130978) * Adjust backup NewBackup class (#130976) * Remove class backup.BackupUploadMetadata (#130977) Remove class backup.BackupMetadata * Report backup size in bytes instead of MB (#131028) Co-authored-by: Robert Resch <robert@resch.dev> * Speed up CI for feature branch (#131030) * Speed up CI for feature branch * adjust * fix * fix * fix * fix * Rename remove to delete in backup websocket type (#131023) * Revert "Speed up CI for feature branch" (#131074) Revert "Speed up CI for feature branch (#131030)" This reverts commit 791280506d1859b1a722f5064d75bcbe48acc1c3. * Rename class BaseBackup to AgentBackup (#131083) * Rename class BaseBackup to AgentBackup * Update tests * Speed up CI for backup feature branch (#131079) * Add backup platform to the hassio integration (#130991) * Add backup platform to the hassio integration * Add hassio to after_dependencies of backup * Address review comments * Remove redundant hassio parametrization of tests * Add tests * Address review comments * Bump CI cache version * Revert "Bump CI cache version" This reverts commit 2ab4d2b1795c953ccfc9b17c47f9df3faac83749. * Extend backup info class AgentBackup (#131110) * Extend backup info class AgentBackup * Update kitchen sink * Update kitchen sink test * Update kitchen sink test * Exclude cloud and hassio from core files (#131117) * Remove unnecessary **kwargs from backup API (#131124) * Fix backup tests (#131128) * Freeze backup dataclasses (#131122) * Protect CoreLocalBackupAgent.load_backups (#131126) * Use backup metadata v2 in core/container backups (#131125) * Extend backup creation API (#131121) * Extend backup creation API * Add tests * Fix merge * Fix merge * Return agent errors when deleting a backup (#131142) * Return agent errors when deleting a backup * Remove redundant calls to dict.keys() * Add enum type for backup folder (#131158) * Add method AgentBackup.from_dict (#131164) * Remove WS command backup/agents/list_backups (#131163) * Handle backup schedule (#131127) * Add backup schedule handling * Fix unrelated incorrect type annotation in test * Clarify delay save * Make the backup time compatible with the recorder nightly job * Update create backup parameters * Use typed dict for create backup parameters * Simplify schedule state * Group create backup parameters * Move parameter * Fix typo * Use Folder model * Handle deserialization of folders better * Fail on attempt to include addons or folders in core backup (#131204) * Fix AgentBackup test (#131201) * Add options to WS command backup/restore (#131194) * Add options to WS command backup/restore * Add tests * Fix test * Teach core backup to restore only database or only settings (#131225) * Exclude tmp_backups/*.tar from backups (#131243) * Add WS command backup/subscribe_events (#131250) * Clean up temporary directory after restoring backup (#131263) * Improve hassio backup agent list (#131268) * Include `last_automatic_backup` in reply to backup/info (#131293) Include last_automatic_backup in reply to backup/info * Handle backup delete after config (#131259) * Handle delete after copies * Handle delete after days * Add some test examples * Test config_delete_after_logic * Test config_delete_after_copies_logic * Test more delete after days * Add debug logs * Always delete the oldest backup first * Never remove the last backup * Clean up words Co-authored-by: Erik Montnemery <erik@montnemery.com> * Fix after cleaning words * Use utcnow * Remove duplicate guard * Simplify sorting * Delete backups even if there are agent errors on get backups --------- Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename backup delete after to backup retention (#131364) * Rename backup delete after to backup retention * Tweak * Remove length limit on `agent_ids` when configuring backup (#132057) Remove length limit on agent_ids when configuring backup * Rename backup retention_config to retention (#132068) * Modify backup agent API to be stream oriented (#132090) * Modify backup agent API to be stream oriented * Fix tests * Adjust after code review * Remove no longer needed pylint override * Improve test coverage * Change BackupAgent API to work with AsyncIterator objects * Don't close files in the event loop * Don't close files in the event loop * Fix backup manager create backup log (#132174) * Fix debug log level (#132186) * Add cloud backup agent (#129621) * Init cloud backup sync * Add more metadata * Fix typo * Adjust to base changes * Don't raise on list if more than one backup is available * Adjust to base branch * Fetch always and verify on download * Update homeassistant/components/cloud/backup.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Adjust to base branch changes * Not required anymore * Workaround * Fix blocking event loop * Fix * Add some tests * some tests * Add cloud backup delete functionality * Enable check * Fix ruff * Use fixture * Use iter_chunks instead * Remove read * Remove explicit export of read_backup * Align with BackupAgent API changes * Improve test coverage * Improve error handling * Adjust docstrings * Catch aiohttp.ClientError bubbling up from hass_nabucasa * Improve iteration --------- Co-authored-by: Erik <erik@montnemery.com> Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Krisjanis Lejejs <krisjanis.lejejs@gmail.com> * Extract file receiver from `BackupManager.async_receive_backup` to util (#132271) * Extract file receiver from BackupManager.async_receive_backup to util * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Make sure backup directory exists (#132269) * Make sure backup directory exists * Hand off directory creation to executor * Use mkdir's exist_ok feeature * Organize BackupManager instance attributes (#132277) * Don't store received backups in a TempDir (#132272) * Don't store received backups in a TempDir * Fix tests * Make sure backup directory exists * Address review comments * Fix tests * Rewrite backup manager state handling (#132375) * Rewrite backup manager state handling * Address review comments * Modify backup reader/writer API to be stream oriented (#132464) * Internalize backup tasks (#132482) * Internalize backup tasks * Update test after rebase * Handle backup error during automatic backup (#132511) * Improve backup manager state logging (#132549) * Fix backup manager state when restore completes (#132548) * Remove WS command backup/agents/download (#132664) * Add WS command backup/generate_with_stored_settings (#132671) * Add WS command backup/generate_with_stored_settings * Register the new command, add tests * Refactor local agent backup tests (#132683) * Refactor test_load_backups * Refactor test loading agents * Refactor test_delete_backup * Refactor test_upload * Clean up duplicate tests * Refactor backup manager receive tests (#132701) * Refactor backup manager receive tests * Clean up * Refactor pre and post platform tests (#132708) * Refactor backup pre platform test * Refactor backup post platform test * Bump aiohasupervisor to version 0.2.2b0 (#132704) * Bump aiohasupervisor to version 0.2.2b0 * Adjust tests * Publish event when manager is idle after creating backup (#132724) * Handle busy backup manager when uploading backup (#132736) * Adjust hassio backup agent to supervisor changes (#132732) * Adjust hassio backup agent to supervisor changes * Fix typo * Refactor test for create backup with wrong parameters (#132763) * Refactor test not loading bad backup platforms (#132769) * Improve receive backup coverage (#132758) * Refactor initiate backup test (#132829) * Rename Backup to ManagerBackup (#132841) * Refactor backup config (#132845) * Refactor backup config * Remove unnecessary condition * Adjust tests * Improve initiate backup test (#132858) * Store the time of automatic backup attempts (#132860) * Store the time of automatic backup attempts * Address review comments * Update test * Update cloud test * Save agent failures when creating backups (#132850) * Save agent failures when creating backups * Update tests * Store KnownBackups * Add test * Only clear known_backups on no error, add tests * Address review comments * Store known backups as a list * Update tests * Track all backups created with backup strategy settings (#132916) * Track all backups created with saved settings * Rename * Add explicit call to save the store * Don't register service backup.create in HassOS installations (#132932) * Revert changes to action service backup.create (#132938) * Fix logic for cleaning up temporary backup file (#132934) * Fix logic for cleaning up temporary backup file * Reduce scope of patch * Fix with_strategy_settings info not sent over websocket (#132939) * Fix with_strategy_settings info not sent over websocket * Fix kitchen sink tests * Fix cloud and hassio tests * Revert backup ci changes (#132955) Revert changes speeding up CI * Fix revert of CI changes (#132960) --------- Co-authored-by: Joakim Sørensen <joasoe@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Paul Bottein <paul.bottein@gmail.com> Co-authored-by: Krisjanis Lejejs <krisjanis.lejejs@gmail.com>
2024-12-11 20:49:34 +00:00
patch(
"homeassistant.components.hassio.backup.get_supervisor_client",
return_value=supervisor_client,
),
patch(
"homeassistant.components.hassio.discovery.get_supervisor_client",
return_value=supervisor_client,
),
patch(
"homeassistant.components.hassio.coordinator.get_supervisor_client",
return_value=supervisor_client,
),
patch(
"homeassistant.components.hassio.issues.get_supervisor_client",
return_value=supervisor_client,
),
patch(
"homeassistant.components.hassio.repairs.get_supervisor_client",
return_value=supervisor_client,
),
):
yield supervisor_client
def _validate_translation_placeholders(
full_key: str,
translation: str,
description_placeholders: dict[str, str] | None,
translation_errors: dict[str, str],
) -> str | None:
"""Raise if translation exists with missing placeholders."""
tuples = list(string.Formatter().parse(translation))
for _, placeholder, _, _ in tuples:
if placeholder is None:
continue
if (
description_placeholders is None
or placeholder not in description_placeholders
):
translation_errors[full_key] = (
f"Description not found for placeholder `{placeholder}` in {full_key}"
)
async def _validate_translation(
hass: HomeAssistant,
translation_errors: dict[str, str],
category: str,
component: str,
key: str,
description_placeholders: dict[str, str] | None,
*,
translation_required: bool = True,
) -> None:
"""Raise if translation doesn't exist."""
full_key = f"component.{component}.{category}.{key}"
translations = await async_get_translations(hass, "en", category, [component])
if (translation := translations.get(full_key)) is not None:
_validate_translation_placeholders(
full_key, translation, description_placeholders, translation_errors
)
return
if not translation_required:
return
if full_key in translation_errors:
translation_errors[full_key] = "used"
return
translation_errors[full_key] = (
f"Translation not found for {component}: `{category}.{key}`. "
f"Please add to homeassistant/components/{component}/strings.json"
)
@pytest.fixture
def ignore_translations() -> str | list[str]:
"""Ignore specific translations.
Override or parametrize this fixture with a fixture that returns,
a list of translation that should be ignored.
"""
return []
@lru_cache
def _get_integration_quality_scale(integration: str) -> dict[str, Any]:
"""Get the quality scale for an integration."""
try:
return yaml.load_yaml_dict(
f"homeassistant/components/{integration}/quality_scale.yaml"
).get("rules", {})
except FileNotFoundError:
return {}
def _get_integration_quality_scale_rule(integration: str, rule: str) -> str:
"""Get the quality scale for an integration."""
quality_scale = _get_integration_quality_scale(integration)
if not quality_scale or rule not in quality_scale:
return "todo"
status = quality_scale[rule]
return status if isinstance(status, str) else status["status"]
async def _check_config_flow_result_translations(
manager: FlowManager,
flow: FlowHandler,
result: FlowResult[FlowContext, str],
translation_errors: dict[str, str],
) -> None:
if result["type"] is FlowResultType.CREATE_ENTRY:
# No need to check translations for a completed flow
return
key_prefix = ""
if isinstance(manager, ConfigEntriesFlowManager):
category = "config"
integration = flow.handler
elif isinstance(manager, OptionsFlowManager):
category = "options"
integration = flow.hass.config_entries.async_get_entry(flow.handler).domain
elif isinstance(manager, repairs.RepairsFlowManager):
category = "issues"
integration = flow.handler
issue_id = flow.issue_id
issue = ir.async_get(flow.hass).async_get_issue(integration, issue_id)
key_prefix = f"{issue.translation_key}.fix_flow."
else:
return
# Check if this flow has been seen before
# Gets set to False on first run, and to True on subsequent runs
setattr(flow, "__flow_seen_before", hasattr(flow, "__flow_seen_before"))
if result["type"] is FlowResultType.FORM:
iqs_config_flow = _get_integration_quality_scale_rule(
integration, "config-flow"
)
if step_id := result.get("step_id"):
# neither title nor description are required
# - title defaults to integration name
# - description is optional
for header in ("title", "description"):
await _validate_translation(
flow.hass,
translation_errors,
category,
integration,
f"{key_prefix}step.{step_id}.{header}",
result["description_placeholders"],
translation_required=False,
)
if iqs_config_flow == "done" and (data_schema := result["data_schema"]):
# data and data_description are compulsory
for data_key in data_schema.schema:
for header in ("data", "data_description"):
await _validate_translation(
flow.hass,
translation_errors,
category,
integration,
f"{key_prefix}step.{step_id}.{header}.{data_key}",
result["description_placeholders"],
)
if errors := result.get("errors"):
for error in errors.values():
await _validate_translation(
flow.hass,
translation_errors,
category,
integration,
f"{key_prefix}error.{error}",
result["description_placeholders"],
)
return
if result["type"] is FlowResultType.ABORT:
# We don't need translations for a discovery flow which immediately
# aborts, since such flows won't be seen by users
if not flow.__flow_seen_before and flow.source in DISCOVERY_SOURCES:
return
await _validate_translation(
flow.hass,
translation_errors,
category,
integration,
f"{key_prefix}abort.{result["reason"]}",
result["description_placeholders"],
)
async def _check_create_issue_translations(
issue_registry: ir.IssueRegistry,
issue: ir.IssueEntry,
translation_errors: dict[str, str],
) -> None:
if issue.translation_key is None:
# `translation_key` is only None on dismissed issues
return
await _validate_translation(
issue_registry.hass,
translation_errors,
"issues",
issue.domain,
f"{issue.translation_key}.title",
issue.translation_placeholders,
)
if not issue.is_fixable:
# Description is required for non-fixable issues
await _validate_translation(
issue_registry.hass,
translation_errors,
"issues",
issue.domain,
f"{issue.translation_key}.description",
issue.translation_placeholders,
)
async def _check_exception_translation(
hass: HomeAssistant,
exception: HomeAssistantError,
translation_errors: dict[str, str],
) -> None:
if exception.translation_key is None:
return
await _validate_translation(
hass,
translation_errors,
"exceptions",
exception.translation_domain,
f"{exception.translation_key}.message",
exception.translation_placeholders,
)
@pytest.fixture(autouse=True)
async def check_translations(
ignore_translations: str | list[str],
) -> AsyncGenerator[None]:
"""Check that translation requirements are met.
Current checks:
- data entry flow results (ConfigFlow/OptionsFlow/RepairFlow)
- issue registry entries
"""
if not isinstance(ignore_translations, list):
ignore_translations = [ignore_translations]
translation_errors = {k: "unused" for k in ignore_translations}
translation_coros = set()
# Keep reference to original functions
_original_flow_manager_async_handle_step = FlowManager._async_handle_step
_original_issue_registry_async_create_issue = ir.IssueRegistry.async_get_or_create
_original_service_registry_async_call = ServiceRegistry.async_call
# Prepare override functions
async def _flow_manager_async_handle_step(
self: FlowManager, flow: FlowHandler, *args
) -> FlowResult:
result = await _original_flow_manager_async_handle_step(self, flow, *args)
await _check_config_flow_result_translations(
self, flow, result, translation_errors
)
return result
def _issue_registry_async_create_issue(
self: ir.IssueRegistry, domain: str, issue_id: str, *args, **kwargs
) -> None:
result = _original_issue_registry_async_create_issue(
self, domain, issue_id, *args, **kwargs
)
translation_coros.add(
_check_create_issue_translations(self, result, translation_errors)
)
return result
async def _service_registry_async_call(
self: ServiceRegistry,
domain: str,
service: str,
service_data: dict[str, Any] | None = None,
blocking: bool = False,
context: Context | None = None,
target: dict[str, Any] | None = None,
return_response: bool = False,
) -> ServiceResponse:
try:
return await _original_service_registry_async_call(
self,
domain,
service,
service_data,
blocking,
context,
target,
return_response,
)
except HomeAssistantError as err:
translation_coros.add(
_check_exception_translation(self._hass, err, translation_errors)
)
raise
# Use override functions
with (
patch(
"homeassistant.data_entry_flow.FlowManager._async_handle_step",
_flow_manager_async_handle_step,
),
patch(
"homeassistant.helpers.issue_registry.IssueRegistry.async_get_or_create",
_issue_registry_async_create_issue,
),
patch(
"homeassistant.core.ServiceRegistry.async_call",
_service_registry_async_call,
),
):
yield
await asyncio.gather(*translation_coros)
# Run final checks
unused_ignore = [k for k, v in translation_errors.items() if v == "unused"]
if unused_ignore:
pytest.fail(
f"Unused ignore translations: {', '.join(unused_ignore)}. "
"Please remove them from the ignore_translations fixture."
)
for description in translation_errors.values():
if description not in {"used", "unused"}:
pytest.fail(description)