2019-04-19 21:54:48 +00:00
|
|
|
"""Common fixtures and objects for the Switcher integration tests."""
|
2021-07-19 13:28:40 +00:00
|
|
|
from unittest.mock import AsyncMock, 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
|
|
|
|
|
|
|
|
2021-07-19 13:28:40 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_bridge(request):
|
|
|
|
"""Return a mocked SwitcherBridge."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.switcher_kis.utils.SwitcherBridge", autospec=True
|
|
|
|
) as bridge_mock:
|
|
|
|
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(
|
2021-07-19 13:28:40 +00:00
|
|
|
"homeassistant.components.switcher_kis.switch.SwitcherApi.connect",
|
|
|
|
new=api_mock,
|
2019-12-05 23:20:07 +00:00
|
|
|
),
|
|
|
|
patch(
|
2021-07-19 13:28:40 +00:00
|
|
|
"homeassistant.components.switcher_kis.switch.SwitcherApi.disconnect",
|
|
|
|
new=api_mock,
|
2019-12-05 23:20:07 +00:00
|
|
|
),
|
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()
|