mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Update opentherm_gw tests to avoid patching internals (#125152)
* Update tests to avoid patching internals * * Use fixtures for tests * Update variable names in tests for clarity * Use hass.config_entries.async_setup instead of setup.async_setup_component
This commit is contained in:
parent
8f26cff65a
commit
8e03f3a045
41
tests/components/opentherm_gw/conftest.py
Normal file
41
tests/components/opentherm_gw/conftest.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
"""Test configuration for opentherm_gw."""
|
||||||
|
|
||||||
|
from collections.abc import Generator
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
from pyotgw.vars import OTGW, OTGW_ABOUT
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
VERSION_TEST = "4.2.5"
|
||||||
|
MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_TEST}"}}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||||
|
"""Override async_setup_entry."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.opentherm_gw.async_setup_entry",
|
||||||
|
return_value=True,
|
||||||
|
) as mock_setup_entry:
|
||||||
|
yield mock_setup_entry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_pyotgw() -> Generator[MagicMock]:
|
||||||
|
"""Mock a pyotgw.OpenThermGateway object."""
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.opentherm_gw.OpenThermGateway",
|
||||||
|
return_value=MagicMock(
|
||||||
|
connect=AsyncMock(return_value=MINIMAL_STATUS),
|
||||||
|
set_control_setpoint=AsyncMock(),
|
||||||
|
set_max_relative_mod=AsyncMock(),
|
||||||
|
disconnect=AsyncMock(),
|
||||||
|
),
|
||||||
|
) as mock_gateway,
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.opentherm_gw.config_flow.pyotgw.OpenThermGateway",
|
||||||
|
new=mock_gateway,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
yield mock_gateway
|
@ -1,8 +1,7 @@
|
|||||||
"""Test the Opentherm Gateway config flow."""
|
"""Test the Opentherm Gateway config flow."""
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
|
||||||
from pyotgw.vars import OTGW, OTGW_ABOUT
|
|
||||||
from serial import SerialException
|
from serial import SerialException
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
@ -25,10 +24,12 @@ from homeassistant.data_entry_flow import FlowResultType
|
|||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: "OpenTherm Gateway 4.2.5"}}
|
|
||||||
|
|
||||||
|
async def test_form_user(
|
||||||
async def test_form_user(hass: HomeAssistant) -> None:
|
hass: HomeAssistant,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
) -> None:
|
||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
@ -37,23 +38,6 @@ async def test_form_user(hass: HomeAssistant) -> None:
|
|||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with (
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.async_setup",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup,
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup_entry,
|
|
||||||
patch(
|
|
||||||
"pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS
|
|
||||||
) as mock_pyotgw_connect,
|
|
||||||
patch(
|
|
||||||
"pyotgw.OpenThermGateway.disconnect", return_value=None
|
|
||||||
) as mock_pyotgw_disconnect,
|
|
||||||
patch("pyotgw.status.StatusManager._process_updates", return_value=None),
|
|
||||||
):
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"}
|
result["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"}
|
||||||
)
|
)
|
||||||
@ -66,32 +50,16 @@ async def test_form_user(hass: HomeAssistant) -> None:
|
|||||||
CONF_DEVICE: "/dev/ttyUSB0",
|
CONF_DEVICE: "/dev/ttyUSB0",
|
||||||
CONF_ID: "test_entry_1",
|
CONF_ID: "test_entry_1",
|
||||||
}
|
}
|
||||||
assert len(mock_setup.mock_calls) == 1
|
assert mock_pyotgw.return_value.connect.await_count == 1
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert mock_pyotgw.return_value.disconnect.await_count == 1
|
||||||
assert len(mock_pyotgw_connect.mock_calls) == 1
|
|
||||||
assert len(mock_pyotgw_disconnect.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form_import(hass: HomeAssistant) -> None:
|
async def test_form_import(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
) -> None:
|
||||||
"""Test import from existing config."""
|
"""Test import from existing config."""
|
||||||
|
|
||||||
with (
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.async_setup",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup,
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup_entry,
|
|
||||||
patch(
|
|
||||||
"pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS
|
|
||||||
) as mock_pyotgw_connect,
|
|
||||||
patch(
|
|
||||||
"pyotgw.OpenThermGateway.disconnect", return_value=None
|
|
||||||
) as mock_pyotgw_disconnect,
|
|
||||||
patch("pyotgw.status.StatusManager._process_updates", return_value=None),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
context={"source": config_entries.SOURCE_IMPORT},
|
||||||
@ -105,13 +73,15 @@ async def test_form_import(hass: HomeAssistant) -> None:
|
|||||||
CONF_DEVICE: "/dev/ttyUSB1",
|
CONF_DEVICE: "/dev/ttyUSB1",
|
||||||
CONF_ID: "legacy_gateway",
|
CONF_ID: "legacy_gateway",
|
||||||
}
|
}
|
||||||
assert len(mock_setup.mock_calls) == 1
|
assert mock_pyotgw.return_value.connect.await_count == 1
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert mock_pyotgw.return_value.disconnect.await_count == 1
|
||||||
assert len(mock_pyotgw_connect.mock_calls) == 1
|
|
||||||
assert len(mock_pyotgw_disconnect.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form_duplicate_entries(hass: HomeAssistant) -> None:
|
async def test_form_duplicate_entries(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
) -> None:
|
||||||
"""Test duplicate device or id errors."""
|
"""Test duplicate device or id errors."""
|
||||||
flow1 = await hass.config_entries.flow.async_init(
|
flow1 = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
@ -123,87 +93,76 @@ async def test_form_duplicate_entries(hass: HomeAssistant) -> None:
|
|||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
with (
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.async_setup",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup,
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup_entry,
|
|
||||||
patch(
|
|
||||||
"pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS
|
|
||||||
) as mock_pyotgw_connect,
|
|
||||||
patch(
|
|
||||||
"pyotgw.OpenThermGateway.disconnect", return_value=None
|
|
||||||
) as mock_pyotgw_disconnect,
|
|
||||||
patch("pyotgw.status.StatusManager._process_updates", return_value=None),
|
|
||||||
):
|
|
||||||
result1 = await hass.config_entries.flow.async_configure(
|
result1 = await hass.config_entries.flow.async_configure(
|
||||||
flow1["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"}
|
flow1["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"}
|
||||||
)
|
)
|
||||||
|
assert result1["type"] is FlowResultType.CREATE_ENTRY
|
||||||
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
flow2["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB1"}
|
flow2["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB1"}
|
||||||
)
|
)
|
||||||
|
assert result2["type"] is FlowResultType.FORM
|
||||||
|
assert result2["errors"] == {"base": "id_exists"}
|
||||||
|
|
||||||
result3 = await hass.config_entries.flow.async_configure(
|
result3 = await hass.config_entries.flow.async_configure(
|
||||||
flow3["flow_id"], {CONF_NAME: "Test Entry 2", CONF_DEVICE: "/dev/ttyUSB0"}
|
flow3["flow_id"], {CONF_NAME: "Test Entry 2", CONF_DEVICE: "/dev/ttyUSB0"}
|
||||||
)
|
)
|
||||||
assert result1["type"] is FlowResultType.CREATE_ENTRY
|
|
||||||
assert result2["type"] is FlowResultType.FORM
|
|
||||||
assert result2["errors"] == {"base": "id_exists"}
|
|
||||||
assert result3["type"] is FlowResultType.FORM
|
assert result3["type"] is FlowResultType.FORM
|
||||||
assert result3["errors"] == {"base": "already_configured"}
|
assert result3["errors"] == {"base": "already_configured"}
|
||||||
assert len(mock_setup.mock_calls) == 1
|
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert mock_pyotgw.return_value.connect.await_count == 1
|
||||||
assert len(mock_pyotgw_connect.mock_calls) == 1
|
assert mock_pyotgw.return_value.disconnect.await_count == 1
|
||||||
assert len(mock_pyotgw_disconnect.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form_connection_timeout(hass: HomeAssistant) -> None:
|
async def test_form_connection_timeout(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
) -> None:
|
||||||
"""Test we handle connection timeout."""
|
"""Test we handle connection timeout."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
flow = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
with (
|
mock_pyotgw.return_value.connect.side_effect = TimeoutError
|
||||||
patch(
|
|
||||||
"pyotgw.OpenThermGateway.connect", side_effect=(TimeoutError)
|
result = await hass.config_entries.flow.async_configure(
|
||||||
) as mock_connect,
|
flow["flow_id"],
|
||||||
patch("pyotgw.status.StatusManager._process_updates", return_value=None),
|
|
||||||
):
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
|
||||||
{CONF_NAME: "Test Entry 1", CONF_DEVICE: "socket://192.0.2.254:1234"},
|
{CONF_NAME: "Test Entry 1", CONF_DEVICE: "socket://192.0.2.254:1234"},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result2["errors"] == {"base": "timeout_connect"}
|
assert result["errors"] == {"base": "timeout_connect"}
|
||||||
assert len(mock_connect.mock_calls) == 1
|
|
||||||
|
assert mock_pyotgw.return_value.connect.await_count == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_form_connection_error(hass: HomeAssistant) -> None:
|
async def test_form_connection_error(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
) -> None:
|
||||||
"""Test we handle serial connection error."""
|
"""Test we handle serial connection error."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
flow = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
with (
|
mock_pyotgw.return_value.connect.side_effect = SerialException
|
||||||
patch(
|
|
||||||
"pyotgw.OpenThermGateway.connect", side_effect=(SerialException)
|
result = await hass.config_entries.flow.async_configure(
|
||||||
) as mock_connect,
|
flow["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"}
|
||||||
patch("pyotgw.status.StatusManager._process_updates", return_value=None),
|
|
||||||
):
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
assert result["errors"] == {"base": "cannot_connect"}
|
||||||
assert len(mock_connect.mock_calls) == 1
|
assert mock_pyotgw.return_value.connect.await_count == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_options_form(hass: HomeAssistant) -> None:
|
async def test_options_form(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
) -> None:
|
||||||
"""Test the options form."""
|
"""Test the options form."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
@ -217,23 +176,17 @@ async def test_options_form(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
with (
|
|
||||||
patch("homeassistant.components.opentherm_gw.async_setup", return_value=True),
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.async_setup_entry", return_value=True
|
|
||||||
),
|
|
||||||
):
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_init(
|
flow = await hass.config_entries.options.async_init(
|
||||||
entry.entry_id, context={"source": "test"}, data=None
|
entry.entry_id, context={"source": "test"}, data=None
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert flow["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "init"
|
assert flow["step_id"] == "init"
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
flow["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_FLOOR_TEMP: True,
|
CONF_FLOOR_TEMP: True,
|
||||||
CONF_READ_PRECISION: PRECISION_HALVES,
|
CONF_READ_PRECISION: PRECISION_HALVES,
|
||||||
@ -248,12 +201,12 @@ async def test_options_form(hass: HomeAssistant) -> None:
|
|||||||
assert result["data"][CONF_TEMPORARY_OVRD_MODE] is True
|
assert result["data"][CONF_TEMPORARY_OVRD_MODE] is True
|
||||||
assert result["data"][CONF_FLOOR_TEMP] is True
|
assert result["data"][CONF_FLOOR_TEMP] is True
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_init(
|
flow = await hass.config_entries.options.async_init(
|
||||||
entry.entry_id, context={"source": "test"}, data=None
|
entry.entry_id, context={"source": "test"}, data=None
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"], user_input={CONF_READ_PRECISION: 0}
|
flow["flow_id"], user_input={CONF_READ_PRECISION: 0}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
@ -262,12 +215,12 @@ async def test_options_form(hass: HomeAssistant) -> None:
|
|||||||
assert result["data"][CONF_TEMPORARY_OVRD_MODE] is True
|
assert result["data"][CONF_TEMPORARY_OVRD_MODE] is True
|
||||||
assert result["data"][CONF_FLOOR_TEMP] is True
|
assert result["data"][CONF_FLOOR_TEMP] is True
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_init(
|
flow = await hass.config_entries.options.async_init(
|
||||||
entry.entry_id, context={"source": "test"}, data=None
|
entry.entry_id, context={"source": "test"}, data=None
|
||||||
)
|
)
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
flow["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_FLOOR_TEMP: False,
|
CONF_FLOOR_TEMP: False,
|
||||||
CONF_READ_PRECISION: PRECISION_TENTHS,
|
CONF_READ_PRECISION: PRECISION_TENTHS,
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
"""Test Opentherm Gateway init."""
|
"""Test Opentherm Gateway init."""
|
||||||
|
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
from pyotgw.vars import OTGW, OTGW_ABOUT
|
from pyotgw.vars import OTGW, OTGW_ABOUT
|
||||||
import pytest
|
|
||||||
|
|
||||||
from homeassistant import setup
|
|
||||||
from homeassistant.components.opentherm_gw.const import (
|
from homeassistant.components.opentherm_gw.const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
OpenThermDeviceIdentifier,
|
OpenThermDeviceIdentifier,
|
||||||
@ -14,11 +12,11 @@ from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
|
|
||||||
|
from .conftest import VERSION_TEST
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
VERSION_OLD = "4.2.5"
|
|
||||||
VERSION_NEW = "4.2.8.1"
|
VERSION_NEW = "4.2.8.1"
|
||||||
MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_OLD}"}}
|
|
||||||
MINIMAL_STATUS_UPD = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_NEW}"}}
|
MINIMAL_STATUS_UPD = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_NEW}"}}
|
||||||
MOCK_GATEWAY_ID = "mock_gateway"
|
MOCK_GATEWAY_ID = "mock_gateway"
|
||||||
MOCK_CONFIG_ENTRY = MockConfigEntry(
|
MOCK_CONFIG_ENTRY = MockConfigEntry(
|
||||||
@ -33,35 +31,28 @@ MOCK_CONFIG_ENTRY = MockConfigEntry(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# This tests needs to be adjusted to remove lingering tasks
|
|
||||||
@pytest.mark.parametrize("expected_lingering_tasks", [True])
|
|
||||||
async def test_device_registry_insert(
|
async def test_device_registry_insert(
|
||||||
hass: HomeAssistant, device_registry: dr.DeviceRegistry
|
hass: HomeAssistant,
|
||||||
|
device_registry: dr.DeviceRegistry,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that the device registry is initialized correctly."""
|
"""Test that the device registry is initialized correctly."""
|
||||||
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
||||||
|
|
||||||
with (
|
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.OpenThermGatewayHub.cleanup",
|
|
||||||
return_value=None,
|
|
||||||
),
|
|
||||||
patch("pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS),
|
|
||||||
):
|
|
||||||
await setup.async_setup_component(hass, DOMAIN, {})
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
gw_dev = device_registry.async_get_device(
|
gw_dev = device_registry.async_get_device(
|
||||||
identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")}
|
identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")}
|
||||||
)
|
)
|
||||||
assert gw_dev.sw_version == VERSION_OLD
|
assert gw_dev is not None
|
||||||
|
assert gw_dev.sw_version == VERSION_TEST
|
||||||
|
|
||||||
|
|
||||||
# This tests needs to be adjusted to remove lingering tasks
|
|
||||||
@pytest.mark.parametrize("expected_lingering_tasks", [True])
|
|
||||||
async def test_device_registry_update(
|
async def test_device_registry_update(
|
||||||
hass: HomeAssistant, device_registry: dr.DeviceRegistry
|
hass: HomeAssistant,
|
||||||
|
device_registry: dr.DeviceRegistry,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that the device registry is updated correctly."""
|
"""Test that the device registry is updated correctly."""
|
||||||
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
||||||
@ -74,19 +65,14 @@ async def test_device_registry_update(
|
|||||||
name="Mock Gateway",
|
name="Mock Gateway",
|
||||||
manufacturer="Schelte Bron",
|
manufacturer="Schelte Bron",
|
||||||
model="OpenTherm Gateway",
|
model="OpenTherm Gateway",
|
||||||
sw_version=VERSION_OLD,
|
sw_version=VERSION_TEST,
|
||||||
)
|
)
|
||||||
|
|
||||||
with (
|
mock_pyotgw.return_value.connect.return_value = MINIMAL_STATUS_UPD
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.OpenThermGatewayHub.cleanup",
|
|
||||||
return_value=None,
|
|
||||||
),
|
|
||||||
patch("pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS_UPD),
|
|
||||||
):
|
|
||||||
await setup.async_setup_component(hass, DOMAIN, {})
|
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
gw_dev = device_registry.async_get_device(
|
gw_dev = device_registry.async_get_device(
|
||||||
identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")}
|
identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")}
|
||||||
)
|
)
|
||||||
@ -96,7 +82,9 @@ async def test_device_registry_update(
|
|||||||
|
|
||||||
# Device migration test can be removed in 2025.4.0
|
# Device migration test can be removed in 2025.4.0
|
||||||
async def test_device_migration(
|
async def test_device_migration(
|
||||||
hass: HomeAssistant, device_registry: dr.DeviceRegistry
|
hass: HomeAssistant,
|
||||||
|
device_registry: dr.DeviceRegistry,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that the device registry is updated correctly."""
|
"""Test that the device registry is updated correctly."""
|
||||||
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
||||||
@ -109,22 +97,10 @@ async def test_device_migration(
|
|||||||
name="Mock Gateway",
|
name="Mock Gateway",
|
||||||
manufacturer="Schelte Bron",
|
manufacturer="Schelte Bron",
|
||||||
model="OpenTherm Gateway",
|
model="OpenTherm Gateway",
|
||||||
sw_version=VERSION_OLD,
|
sw_version=VERSION_TEST,
|
||||||
)
|
)
|
||||||
|
|
||||||
with (
|
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.OpenThermGateway",
|
|
||||||
return_value=MagicMock(
|
|
||||||
connect=AsyncMock(return_value=MINIMAL_STATUS_UPD),
|
|
||||||
set_control_setpoint=AsyncMock(),
|
|
||||||
set_max_relative_mod=AsyncMock(),
|
|
||||||
disconnect=AsyncMock(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
await setup.async_setup_component(hass, DOMAIN, {})
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
@ -158,7 +134,9 @@ async def test_device_migration(
|
|||||||
|
|
||||||
# Entity migration test can be removed in 2025.4.0
|
# Entity migration test can be removed in 2025.4.0
|
||||||
async def test_climate_entity_migration(
|
async def test_climate_entity_migration(
|
||||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
hass: HomeAssistant,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
mock_pyotgw: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that the climate entity unique_id gets migrated correctly."""
|
"""Test that the climate entity unique_id gets migrated correctly."""
|
||||||
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
||||||
@ -168,22 +146,12 @@ async def test_climate_entity_migration(
|
|||||||
unique_id=MOCK_CONFIG_ENTRY.data[CONF_ID],
|
unique_id=MOCK_CONFIG_ENTRY.data[CONF_ID],
|
||||||
)
|
)
|
||||||
|
|
||||||
with (
|
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
|
||||||
patch(
|
|
||||||
"homeassistant.components.opentherm_gw.OpenThermGateway",
|
|
||||||
return_value=MagicMock(
|
|
||||||
connect=AsyncMock(return_value=MINIMAL_STATUS_UPD),
|
|
||||||
set_control_setpoint=AsyncMock(),
|
|
||||||
set_max_relative_mod=AsyncMock(),
|
|
||||||
disconnect=AsyncMock(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
await setup.async_setup_component(hass, DOMAIN, {})
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
updated_entry = entity_registry.async_get(entry.entity_id)
|
||||||
|
assert updated_entry is not None
|
||||||
assert (
|
assert (
|
||||||
entity_registry.async_get(entry.entity_id).unique_id
|
updated_entry.unique_id
|
||||||
== f"{MOCK_CONFIG_ENTRY.data[CONF_ID]}-{OpenThermDeviceIdentifier.THERMOSTAT}-thermostat_entity"
|
== f"{MOCK_CONFIG_ENTRY.data[CONF_ID]}-{OpenThermDeviceIdentifier.THERMOSTAT}-thermostat_entity"
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user