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