2018-05-01 17:35:23 +00:00
|
|
|
"""Fixtures for component testing."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2024-05-22 07:34:17 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-07-01 09:51:51 +00:00
|
|
|
from collections.abc import Callable, Generator
|
2024-07-10 21:50:47 +00:00
|
|
|
from importlib.util import find_spec
|
2024-06-03 15:43:18 +00:00
|
|
|
from pathlib import Path
|
2024-03-21 09:49:32 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2023-05-25 09:59:20 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2018-05-01 17:35:23 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-03-21 09:49:32 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
2024-03-29 01:23:21 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-03-21 09:49:32 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
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
|
2024-03-21 09:49:32 +00:00
|
|
|
|
2020-08-12 14:08:33 +00:00
|
|
|
|
2024-07-10 21:50:47 +00:00
|
|
|
@pytest.fixture(scope="session", autouse=find_spec("zeroconf") is not None)
|
2024-06-06 15:24:22 +00:00
|
|
|
def patch_zeroconf_multiple_catcher() -> Generator[None]:
|
2024-07-10 21:50:47 +00:00
|
|
|
"""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
|
2020-08-12 14:08:33 +00:00
|
|
|
|
2018-11-21 19:55:21 +00:00
|
|
|
|
2023-12-01 06:14:13 +00:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
2024-06-06 15:24:22 +00:00
|
|
|
def prevent_io() -> Generator[None]:
|
2018-11-21 19:55:21 +00:00
|
|
|
"""Fixture to prevent certain I/O from happening."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2022-07-07 08:57:44 +00:00
|
|
|
"homeassistant.components.http.ban.load_yaml_config_file",
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2018-11-21 19:55:21 +00:00
|
|
|
yield
|
2022-03-14 08:50:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-06-06 15:24:22 +00:00
|
|
|
def entity_registry_enabled_by_default() -> Generator[None]:
|
2022-03-14 08:50:55 +00:00
|
|
|
"""Test fixture that ensures all entities are enabled in the registry."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.entity.Entity.entity_registry_enabled_default",
|
|
|
|
return_value=True,
|
2023-03-26 13:21:19 +00:00
|
|
|
):
|
|
|
|
yield
|
2023-05-25 09:59:20 +00:00
|
|
|
|
|
|
|
|
2023-05-25 11:45:19 +00:00
|
|
|
# Blueprint test fixtures
|
|
|
|
@pytest.fixture(name="stub_blueprint_populate")
|
2024-06-06 15:24:22 +00:00
|
|
|
def stub_blueprint_populate_fixture() -> Generator[None]:
|
2023-05-25 11:45:19 +00:00
|
|
|
"""Stub copying the blueprints to the config folder."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
from .blueprint.common import stub_blueprint_populate_fixture_helper
|
2023-05-25 11:45:19 +00:00
|
|
|
|
|
|
|
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")
|
2024-06-06 15:24:22 +00:00
|
|
|
def mock_tts_get_cache_files_fixture() -> Generator[MagicMock]:
|
2023-05-25 09:59:20 +00:00
|
|
|
"""Mock the list TTS cache function."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# 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,
|
2024-06-06 15:24:22 +00:00
|
|
|
) -> Generator[MagicMock]:
|
2023-05-25 09:59:20 +00:00
|
|
|
"""Mock the TTS cache dir in memory."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# 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."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# 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(
|
2024-06-03 15:43:18 +00:00
|
|
|
tmp_path: Path,
|
|
|
|
mock_tts_init_cache_dir: MagicMock,
|
|
|
|
mock_tts_get_cache_files: MagicMock,
|
|
|
|
request: pytest.FixtureRequest,
|
2024-06-06 15:24:22 +00:00
|
|
|
) -> Generator[Path]:
|
2023-05-25 09:59:20 +00:00
|
|
|
"""Mock the TTS cache dir with empty dir."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# 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")
|
2024-06-06 15:24:22 +00:00
|
|
|
def tts_mutagen_mock_fixture() -> Generator[MagicMock]:
|
2023-05-25 09:59:20 +00:00
|
|
|
"""Mock writing tags."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# 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()
|
2023-12-09 18:41:37 +00:00
|
|
|
|
|
|
|
|
2024-03-29 13:38:58 +00:00
|
|
|
@pytest.fixture(name="mock_conversation_agent")
|
|
|
|
def mock_conversation_agent_fixture(hass: HomeAssistant) -> MockAgent:
|
|
|
|
"""Mock a conversation agent."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
from .conversation.common import mock_conversation_agent_fixture_helper
|
2024-03-29 13:38:58 +00:00
|
|
|
|
|
|
|
return mock_conversation_agent_fixture_helper(hass)
|
|
|
|
|
|
|
|
|
2024-07-10 21:50:47 +00:00
|
|
|
@pytest.fixture(scope="session", autouse=find_spec("ffmpeg") is not None)
|
2024-06-06 15:24:22 +00:00
|
|
|
def prevent_ffmpeg_subprocess() -> Generator[None]:
|
2024-07-10 21:50:47 +00:00
|
|
|
"""If installed, prevent ffmpeg from creating a subprocess."""
|
2023-12-09 18:41:37 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.ffmpeg.FFVersion.get_version", return_value="6.0"
|
|
|
|
):
|
|
|
|
yield
|
2024-03-21 09:49:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-05-22 07:34:17 +00:00
|
|
|
def mock_light_entities() -> list[MockLight]:
|
2024-03-21 09:49:32 +00:00
|
|
|
"""Return mocked light entities."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
from .light.common import MockLight
|
2024-03-21 09:49:32 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
MockLight("Ceiling", STATE_ON),
|
|
|
|
MockLight("Ceiling", STATE_OFF),
|
|
|
|
MockLight(None, STATE_OFF),
|
|
|
|
]
|
2024-03-28 11:07:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-05-22 07:34:17 +00:00
|
|
|
def mock_sensor_entities() -> dict[str, MockSensor]:
|
2024-03-28 11:07:55 +00:00
|
|
|
"""Return mocked sensor entities."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
from .sensor.common import get_mock_sensor_entities
|
2024-03-28 11:07:55 +00:00
|
|
|
|
|
|
|
return get_mock_sensor_entities()
|
2024-03-28 12:56:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-05-22 07:34:17 +00:00
|
|
|
def mock_switch_entities() -> list[MockSwitch]:
|
2024-03-28 12:56:23 +00:00
|
|
|
"""Return mocked toggle entities."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
from .switch.common import get_mock_switch_entities
|
2024-03-28 12:56:23 +00:00
|
|
|
|
2024-04-02 06:25:28 +00:00
|
|
|
return get_mock_switch_entities()
|
2024-03-29 01:23:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-05-22 07:34:17 +00:00
|
|
|
def mock_legacy_device_scanner() -> MockScanner:
|
2024-03-29 01:23:21 +00:00
|
|
|
"""Return mocked legacy device scanner entity."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
from .device_tracker.common import MockScanner
|
2024-03-29 01:23:21 +00:00
|
|
|
|
|
|
|
return MockScanner()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-05-22 07:34:17 +00:00
|
|
|
def mock_legacy_device_tracker_setup() -> Callable[[HomeAssistant, MockScanner], None]:
|
2024-03-29 01:23:21 +00:00
|
|
|
"""Return setup callable for legacy device tracker setup."""
|
2024-06-11 13:04:00 +00:00
|
|
|
# pylint: disable-next=import-outside-toplevel
|
|
|
|
from .device_tracker.common import mock_legacy_device_tracker_setup
|
2024-03-29 01:23:21 +00:00
|
|
|
|
|
|
|
return mock_legacy_device_tracker_setup
|