mirror of
https://github.com/home-assistant/core.git
synced 2026-08-29 02:35:05 -05:00
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
"""Common fixtures for the Flexit tests."""
|
|
|
|
from collections.abc import AsyncIterator, Generator
|
|
from contextlib import asynccontextmanager
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from modbus_connection.mock import MockModbusUnit
|
|
import pytest
|
|
|
|
from homeassistant.components.flexit.const import CONF_UNIT, DOMAIN, TYPE_TCP
|
|
from homeassistant.const import CONF_DEVICE, CONF_HOST, CONF_PORT, CONF_TYPE
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mock_get_modbus_unit(
|
|
mock_modbus_unit: MockModbusUnit,
|
|
) -> Generator[MagicMock]:
|
|
"""Patch the shared unit helper to return the in-memory mock unit."""
|
|
with patch(
|
|
"homeassistant.components.flexit.async_get_unit",
|
|
return_value=mock_modbus_unit,
|
|
) as get_unit:
|
|
yield get_unit
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mock_get_temporary_modbus_unit(
|
|
mock_modbus_unit: MockModbusUnit,
|
|
) -> Generator[MagicMock]:
|
|
"""Patch the temporary unit helper to return the in-memory mock unit."""
|
|
|
|
@asynccontextmanager
|
|
async def temporary_unit(*_: object) -> AsyncIterator[MockModbusUnit]:
|
|
yield mock_modbus_unit
|
|
|
|
get_temporary_unit = MagicMock(side_effect=temporary_unit)
|
|
with patch(
|
|
"homeassistant.components.flexit.config_flow.async_get_temporary_unit",
|
|
new=get_temporary_unit,
|
|
):
|
|
yield get_temporary_unit
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_config_entry() -> MockConfigEntry:
|
|
"""Mock a config entry."""
|
|
return MockConfigEntry(
|
|
domain=DOMAIN,
|
|
title="Flexit",
|
|
data={
|
|
CONF_TYPE: TYPE_TCP,
|
|
CONF_HOST: "1.1.1.1",
|
|
CONF_PORT: 502,
|
|
CONF_UNIT: 1,
|
|
},
|
|
entry_id="flexit_001",
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_serial_config_entry() -> MockConfigEntry:
|
|
"""Mock a serial (RTU) config entry."""
|
|
return MockConfigEntry(
|
|
domain=DOMAIN,
|
|
title="Flexit",
|
|
data={
|
|
CONF_TYPE: "serial",
|
|
CONF_DEVICE: "/dev/ttyUSB0",
|
|
"baudrate": 57600,
|
|
CONF_UNIT: 1,
|
|
},
|
|
entry_id="flexit_002",
|
|
)
|