2019-04-19 21:54:48 +00:00
|
|
|
"""Common fixtures and objects for the Switcher integration tests."""
|
2024-03-08 13:47:22 +00:00
|
|
|
|
2024-07-01 09:54:42 +00:00
|
|
|
from collections.abc import Generator
|
2024-06-04 08:37:54 +00:00
|
|
|
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
2019-04-19 21:54:48 +00:00
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
import pytest
|
2019-04-19 21:54:48 +00:00
|
|
|
|
|
|
|
|
2024-05-24 07:15:17 +00:00
|
|
|
@pytest.fixture
|
2024-06-06 15:41:37 +00:00
|
|
|
def mock_setup_entry() -> Generator[AsyncMock]:
|
2024-05-24 07:15:17 +00:00
|
|
|
"""Override async_setup_entry."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.switcher_kis.async_setup_entry", return_value=True
|
|
|
|
) as mock_setup_entry:
|
|
|
|
yield mock_setup_entry
|
|
|
|
|
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
@pytest.fixture
|
2024-06-06 15:41:37 +00:00
|
|
|
def mock_bridge(request: pytest.FixtureRequest) -> Generator[MagicMock]:
|
2021-07-19 13:28:40 +00:00
|
|
|
"""Return a mocked SwitcherBridge."""
|
2024-05-25 10:17:33 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.switcher_kis.SwitcherBridge", autospec=True
|
|
|
|
) as bridge_mock,
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.switcher_kis.utils.SwitcherBridge",
|
|
|
|
new=bridge_mock,
|
|
|
|
),
|
|
|
|
):
|
2021-07-19 13:28:40 +00:00
|
|
|
bridge = bridge_mock.return_value
|
2019-04-19 21:54:48 +00:00
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
bridge.devices = []
|
|
|
|
if hasattr(request, "param") and request.param:
|
|
|
|
bridge.devices = request.param
|
2019-05-19 09:24:59 +00:00
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
async def start():
|
|
|
|
bridge.is_running = True
|
2019-05-19 09:24:59 +00:00
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
for device in bridge.devices:
|
|
|
|
bridge_mock.call_args[0][0](device)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
def mock_callbacks(devices):
|
|
|
|
for device in devices:
|
|
|
|
bridge_mock.call_args[0][0](device)
|
2019-05-19 09:24:59 +00:00
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
async def stop():
|
|
|
|
bridge.is_running = False
|
2019-05-19 09:24:59 +00:00
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
bridge.start = AsyncMock(side_effect=start)
|
|
|
|
bridge.mock_callbacks = Mock(side_effect=mock_callbacks)
|
|
|
|
bridge.stop = AsyncMock(side_effect=stop)
|
2019-05-19 09:24:59 +00:00
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
yield bridge
|
2019-06-14 22:48:21 +00:00
|
|
|
|
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_api():
|
|
|
|
"""Fixture for mocking aioswitcher.api.SwitcherApi."""
|
|
|
|
api_mock = AsyncMock()
|
2019-06-14 22:48:21 +00:00
|
|
|
|
|
|
|
patchers = [
|
2019-12-05 23:20:07 +00:00
|
|
|
patch(
|
2022-10-01 15:04:11 +00:00
|
|
|
"homeassistant.components.switcher_kis.switch.SwitcherType1Api.connect",
|
2021-07-19 13:28:40 +00:00
|
|
|
new=api_mock,
|
2019-12-05 23:20:07 +00:00
|
|
|
),
|
|
|
|
patch(
|
2022-10-01 15:04:11 +00:00
|
|
|
"homeassistant.components.switcher_kis.switch.SwitcherType1Api.disconnect",
|
2021-07-19 13:28:40 +00:00
|
|
|
new=api_mock,
|
2019-12-05 23:20:07 +00:00
|
|
|
),
|
2022-10-01 16:14:55 +00:00
|
|
|
patch(
|
|
|
|
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.connect",
|
|
|
|
new=api_mock,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.switcher_kis.climate.SwitcherType2Api.disconnect",
|
|
|
|
new=api_mock,
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2019-06-14 22:48:21 +00:00
|
|
|
|
|
|
|
for patcher in patchers:
|
|
|
|
patcher.start()
|
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
yield api_mock
|
2019-06-14 22:48:21 +00:00
|
|
|
|
|
|
|
for patcher in patchers:
|
|
|
|
patcher.stop()
|