core/tests/components/elgato/conftest.py

109 lines
3.3 KiB
Python
Raw Normal View History

2022-01-14 16:16:59 +00:00
"""Fixtures for Elgato integration tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch
2023-02-08 09:22:12 +00:00
from elgato import BatteryInfo, ElgatoNoBatteryError, Info, Settings, State
2022-01-14 16:16:59 +00:00
import pytest
from homeassistant.components.elgato.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_PORT
2022-01-14 16:16:59 +00:00
from homeassistant.core import HomeAssistant
2023-02-08 09:22:12 +00:00
from tests.common import MockConfigEntry, get_fixture_path, load_fixture
from tests.components.light.conftest import mock_light_profiles # noqa: F401
2022-01-14 16:16:59 +00:00
2023-02-07 18:14:13 +00:00
@pytest.fixture
def device_fixtures() -> str:
"""Return the device fixtures for a specific device."""
return "key-light"
@pytest.fixture
def state_variant() -> str:
"""Return the state variant to load for a device."""
return "state"
2022-01-14 16:16:59 +00:00
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
title="CN11A1A00001",
domain=DOMAIN,
data={
CONF_HOST: "127.0.0.1",
CONF_MAC: "AA:BB:CC:DD:EE:FF",
CONF_PORT: 9123,
},
2022-01-14 16:16:59 +00:00
unique_id="CN11A1A00001",
)
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Mock setting up a config entry."""
with patch(
"homeassistant.components.elgato.async_setup_entry", return_value=True
) as mock_setup:
yield mock_setup
2022-06-22 20:37:25 +00:00
@pytest.fixture
def mock_onboarding() -> Generator[None, MagicMock, None]:
"""Mock that Home Assistant is currently onboarding."""
with patch(
"homeassistant.components.onboarding.async_is_onboarded",
return_value=False,
) as mock_onboarding:
yield mock_onboarding
2022-01-14 16:16:59 +00:00
@pytest.fixture
2023-02-07 18:14:13 +00:00
def mock_elgato(
device_fixtures: str, state_variant: str
) -> Generator[None, MagicMock, None]:
2022-01-14 16:16:59 +00:00
"""Return a mocked Elgato client."""
with patch(
"homeassistant.components.elgato.coordinator.Elgato", autospec=True
2023-02-19 19:14:18 +00:00
) as elgato_mock, patch(
"homeassistant.components.elgato.config_flow.Elgato", new=elgato_mock
):
2022-01-14 16:16:59 +00:00
elgato = elgato_mock.return_value
2023-02-07 18:14:13 +00:00
elgato.info.return_value = Info.parse_raw(
load_fixture(f"{device_fixtures}/info.json", DOMAIN)
)
2022-01-14 16:16:59 +00:00
elgato.state.return_value = State.parse_raw(
2023-02-07 18:14:13 +00:00
load_fixture(f"{device_fixtures}/{state_variant}.json", DOMAIN)
2022-01-14 16:16:59 +00:00
)
elgato.settings.return_value = Settings.parse_raw(
2023-02-07 18:14:13 +00:00
load_fixture(f"{device_fixtures}/settings.json", DOMAIN)
2022-01-14 16:16:59 +00:00
)
2023-02-08 09:22:12 +00:00
# This may, or may not, be a battery-powered device
if get_fixture_path(f"{device_fixtures}/battery.json", DOMAIN).exists():
elgato.has_battery.return_value = True
elgato.battery.return_value = BatteryInfo.parse_raw(
load_fixture(f"{device_fixtures}/battery.json", DOMAIN)
)
else:
elgato.has_battery.return_value = False
elgato.battery.side_effect = ElgatoNoBatteryError
2022-01-14 16:16:59 +00:00
yield elgato
@pytest.fixture
async def init_integration(
2023-02-07 18:14:13 +00:00
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_elgato: MagicMock,
2022-01-14 16:16:59 +00:00
) -> MockConfigEntry:
"""Set up the Elgato integration for testing."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
return mock_config_entry