mirror of
https://github.com/home-assistant/core.git
synced 2025-04-27 10:47:51 +00:00

* Opt in to install Mosquitto broker add-on in MQTT config flow * rephrase * Tests with supervisor and running add-on * Complete tests for success flows * Also set up entry in success flow * Use realistic names for addon and broker * Finetuning and fail test cases * Spelling * Improve translation strings * Update addon docstr Co-authored-by: Erik Montnemery <erik@montnemery.com> * Raise AddonError if add-on does not start * Only show the option to use the add-on * Simplify flow, rework and cleanup * Revert unrelated cleanup, process suggestion * Move ADDON_SLUG const to addon module * Move fixture to component level * Move back supervisor fixture * Move addon_setup_time_fixture and superfixe to config flow model tests * Refactor hassio fixture * Rename helpers as they are no fixtures, remove fixture from their names --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
272 lines
8.5 KiB
Python
272 lines
8.5 KiB
Python
"""Fixtures for component testing."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable, Generator
|
|
from importlib.util import find_spec
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING, Any
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
if TYPE_CHECKING:
|
|
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."""
|
|
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."""
|
|
with patch(
|
|
"homeassistant.components.http.ban.load_yaml_config_file",
|
|
):
|
|
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
|
|
|
|
|
|
# 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
|
|
@pytest.fixture(name="mock_tts_get_cache_files")
|
|
def mock_tts_get_cache_files_fixture() -> Generator[MagicMock]:
|
|
"""Mock the list TTS cache function."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .tts.common import mock_tts_get_cache_files_fixture_helper
|
|
|
|
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]:
|
|
"""Mock the TTS cache dir in memory."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .tts.common import mock_tts_init_cache_dir_fixture_helper
|
|
|
|
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
|
|
|
|
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]:
|
|
"""Mock the TTS cache dir with empty dir."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .tts.common import mock_tts_cache_dir_fixture_helper
|
|
|
|
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]:
|
|
"""Mock writing tags."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .tts.common import tts_mutagen_mock_fixture_helper
|
|
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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="discovery_info")
|
|
def discovery_info_fixture() -> Any:
|
|
"""Return the discovery info from the supervisor."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .hassio.common import mock_discovery_info
|
|
|
|
return mock_discovery_info()
|
|
|
|
|
|
@pytest.fixture(name="get_addon_discovery_info")
|
|
def get_addon_discovery_info_fixture(discovery_info: Any) -> Generator[AsyncMock]:
|
|
"""Mock get add-on discovery info."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .hassio.common import mock_get_addon_discovery_info
|
|
|
|
yield from mock_get_addon_discovery_info(discovery_info)
|
|
|
|
|
|
@pytest.fixture(name="addon_store_info")
|
|
def addon_store_info_fixture() -> Generator[AsyncMock]:
|
|
"""Mock Supervisor add-on store info."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .hassio.common import mock_addon_store_info
|
|
|
|
yield from mock_addon_store_info()
|
|
|
|
|
|
@pytest.fixture(name="addon_info")
|
|
def addon_info_fixture() -> Generator[AsyncMock]:
|
|
"""Mock Supervisor add-on info."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .hassio.common import mock_addon_info
|
|
|
|
yield from mock_addon_info()
|
|
|
|
|
|
@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")
|
|
def install_addon_fixture(
|
|
addon_store_info: AsyncMock, addon_info: AsyncMock
|
|
) -> Generator[AsyncMock]:
|
|
"""Mock install add-on."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .hassio.common import mock_install_addon
|
|
|
|
yield from mock_install_addon(addon_store_info, addon_info)
|
|
|
|
|
|
@pytest.fixture(name="start_addon")
|
|
def start_addon_fixture() -> Generator[AsyncMock]:
|
|
"""Mock start add-on."""
|
|
# pylint: disable-next=import-outside-toplevel
|
|
from .hassio.common import mock_start_addon
|
|
|
|
yield from mock_start_addon()
|