2021-06-11 09:36:54 +00:00
|
|
|
"""Fixtures for WLED integration tests."""
|
2024-03-08 13:44:56 +00:00
|
|
|
|
2024-07-01 09:54:42 +00:00
|
|
|
from collections.abc import Generator
|
2022-06-22 20:37:36 +00:00
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
2021-06-11 09:36:54 +00:00
|
|
|
|
2023-08-25 13:59:30 +00:00
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
2021-06-11 09:36:54 +00:00
|
|
|
import pytest
|
2024-07-18 08:09:12 +00:00
|
|
|
from wled import Device as WLEDDevice, Releases
|
2021-06-11 09:36:54 +00:00
|
|
|
|
|
|
|
from homeassistant.components.wled.const import DOMAIN
|
2022-01-04 18:59:14 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
2021-06-11 09:36:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
2023-02-20 12:00:02 +00:00
|
|
|
from tests.common import MockConfigEntry, load_json_object_fixture
|
2021-03-02 08:02:04 +00:00
|
|
|
from tests.components.light.conftest import mock_light_profiles # noqa: F401
|
2021-06-11 09:36:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_config_entry() -> MockConfigEntry:
|
|
|
|
"""Return the default mocked config entry."""
|
|
|
|
return MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
2022-01-04 18:59:14 +00:00
|
|
|
data={CONF_HOST: "192.168.1.123"},
|
|
|
|
unique_id="aabbccddeeff",
|
2021-06-11 09:36:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-06-06 15:33:27 +00:00
|
|
|
def mock_setup_entry() -> Generator[AsyncMock]:
|
2021-06-11 09:36:54 +00:00
|
|
|
"""Mock setting up a config entry."""
|
2022-06-22 20:37:36 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.wled.async_setup_entry", return_value=True
|
|
|
|
) as mock_setup:
|
|
|
|
yield mock_setup
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-06-06 15:33:27 +00:00
|
|
|
def mock_onboarding() -> Generator[MagicMock]:
|
2022-06-22 20:37:36 +00:00
|
|
|
"""Mock that Home Assistant is currently onboarding."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onboarding.async_is_onboarded",
|
|
|
|
return_value=False,
|
|
|
|
) as mock_onboarding:
|
|
|
|
yield mock_onboarding
|
2021-06-11 09:36:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-02-20 12:00:02 +00:00
|
|
|
def device_fixture() -> str:
|
|
|
|
"""Return the device fixture for a specific device."""
|
|
|
|
return "rgb"
|
2021-06-11 09:36:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-07-18 08:09:12 +00:00
|
|
|
def mock_wled_releases() -> Generator[MagicMock]:
|
|
|
|
"""Return a mocked WLEDReleases client."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.wled.coordinator.WLEDReleases", autospec=True
|
|
|
|
) as wled_releases_mock:
|
|
|
|
wled_releases = wled_releases_mock.return_value
|
|
|
|
wled_releases.releases.return_value = Releases(
|
|
|
|
beta="1.0.0b5",
|
|
|
|
stable="0.99.0",
|
|
|
|
)
|
|
|
|
|
|
|
|
yield wled_releases
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_wled(
|
|
|
|
device_fixture: str, mock_wled_releases: MagicMock
|
|
|
|
) -> Generator[MagicMock]:
|
2021-06-11 09:36:54 +00:00
|
|
|
"""Return a mocked WLED client."""
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.wled.coordinator.WLED", autospec=True
|
|
|
|
) as wled_mock,
|
|
|
|
patch("homeassistant.components.wled.config_flow.WLED", new=wled_mock),
|
2023-02-20 12:00:02 +00:00
|
|
|
):
|
2021-06-11 09:36:54 +00:00
|
|
|
wled = wled_mock.return_value
|
2024-07-18 08:09:12 +00:00
|
|
|
wled.update.return_value = WLEDDevice.from_dict(
|
2023-02-20 12:00:02 +00:00
|
|
|
load_json_object_fixture(f"{device_fixture}.json", DOMAIN)
|
|
|
|
)
|
2021-06-11 18:55:08 +00:00
|
|
|
wled.connected = False
|
2021-10-14 22:46:26 +00:00
|
|
|
wled.host = "127.0.0.1"
|
2024-07-18 08:09:12 +00:00
|
|
|
|
2021-06-11 09:36:54 +00:00
|
|
|
yield wled
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def init_integration(
|
2023-08-25 13:59:30 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
freezer: FrozenDateTimeFactory,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
mock_wled: MagicMock,
|
2021-06-11 09:36:54 +00:00
|
|
|
) -> MockConfigEntry:
|
|
|
|
"""Set up the WLED 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()
|
|
|
|
|
2023-08-25 13:59:30 +00:00
|
|
|
# Let some time pass so coordinators can be reliably triggered by bumping
|
|
|
|
# time by SCAN_INTERVAL
|
|
|
|
freezer.tick(1)
|
|
|
|
|
2021-06-11 09:36:54 +00:00
|
|
|
return mock_config_entry
|