Update opentherm_gw tests to prepare for new platforms (#125172)

Move MockConfigEntry to a fixture
This commit is contained in:
mvn23 2024-09-03 21:18:38 +02:00 committed by GitHub
parent be8f14167f
commit 14482ff6da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 25 deletions

View File

@ -6,8 +6,14 @@ from unittest.mock import AsyncMock, MagicMock, patch
from pyotgw.vars import OTGW, OTGW_ABOUT from pyotgw.vars import OTGW, OTGW_ABOUT
import pytest import pytest
from homeassistant.components.opentherm_gw import DOMAIN
from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME
from tests.common import MockConfigEntry
VERSION_TEST = "4.2.5" VERSION_TEST = "4.2.5"
MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_TEST}"}} MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_TEST}"}}
MOCK_GATEWAY_ID = "mock_gateway"
@pytest.fixture @pytest.fixture
@ -39,3 +45,18 @@ def mock_pyotgw() -> Generator[MagicMock]:
), ),
): ):
yield mock_gateway yield mock_gateway
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Mock an OpenTherm Gateway config entry."""
return MockConfigEntry(
domain=DOMAIN,
title="Mock Gateway",
data={
CONF_NAME: "Mock Gateway",
CONF_DEVICE: "/dev/null",
CONF_ID: MOCK_GATEWAY_ID,
},
options={},
)

View File

@ -8,38 +8,28 @@ from homeassistant.components.opentherm_gw.const import (
DOMAIN, DOMAIN,
OpenThermDeviceIdentifier, OpenThermDeviceIdentifier,
) )
from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME from homeassistant.const import CONF_ID
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 .conftest import MOCK_GATEWAY_ID, VERSION_TEST
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
VERSION_NEW = "4.2.8.1" VERSION_NEW = "4.2.8.1"
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_CONFIG_ENTRY = MockConfigEntry(
domain=DOMAIN,
title="Mock Gateway",
data={
CONF_NAME: "Mock Gateway",
CONF_DEVICE: "/dev/null",
CONF_ID: MOCK_GATEWAY_ID,
},
options={},
)
async def test_device_registry_insert( async def test_device_registry_insert(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mock_config_entry: MockConfigEntry,
mock_pyotgw: MagicMock, 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)
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id) 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(
@ -52,13 +42,14 @@ async def test_device_registry_insert(
async def test_device_registry_update( async def test_device_registry_update(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mock_config_entry: MockConfigEntry,
mock_pyotgw: MagicMock, 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)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=MOCK_CONFIG_ENTRY.entry_id, config_entry_id=mock_config_entry.entry_id,
identifiers={ identifiers={
(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}") (DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")
}, },
@ -70,7 +61,7 @@ async def test_device_registry_update(
mock_pyotgw.return_value.connect.return_value = MINIMAL_STATUS_UPD mock_pyotgw.return_value.connect.return_value = MINIMAL_STATUS_UPD
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id) 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(
@ -84,13 +75,14 @@ async def test_device_registry_update(
async def test_device_migration( async def test_device_migration(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
mock_config_entry: MockConfigEntry,
mock_pyotgw: MagicMock, 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)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=MOCK_CONFIG_ENTRY.entry_id, config_entry_id=mock_config_entry.entry_id,
identifiers={ identifiers={
(DOMAIN, MOCK_GATEWAY_ID), (DOMAIN, MOCK_GATEWAY_ID),
}, },
@ -100,7 +92,7 @@ async def test_device_migration(
sw_version=VERSION_TEST, sw_version=VERSION_TEST,
) )
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id) await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert (
@ -136,22 +128,23 @@ async def test_device_migration(
async def test_climate_entity_migration( async def test_climate_entity_migration(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
mock_pyotgw: MagicMock, 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)
entry = entity_registry.async_get_or_create( entry = entity_registry.async_get_or_create(
domain="climate", domain="climate",
platform="opentherm_gw", platform="opentherm_gw",
unique_id=MOCK_CONFIG_ENTRY.data[CONF_ID], unique_id=mock_config_entry.data[CONF_ID],
) )
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id) await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
updated_entry = entity_registry.async_get(entry.entity_id) updated_entry = entity_registry.async_get(entry.entity_id)
assert updated_entry is not None assert updated_entry is not None
assert ( assert (
updated_entry.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"
) )