diff --git a/tests/components/opentherm_gw/conftest.py b/tests/components/opentherm_gw/conftest.py index 057f47a169df05cfcc002e3e048ec59731a9b5d4..9c90c74b04bc3c48d4a7e09648991c9e03dafbbe 100644 --- a/tests/components/opentherm_gw/conftest.py +++ b/tests/components/opentherm_gw/conftest.py @@ -6,8 +6,14 @@ from unittest.mock import AsyncMock, MagicMock, patch from pyotgw.vars import OTGW, OTGW_ABOUT 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" MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_TEST}"}} +MOCK_GATEWAY_ID = "mock_gateway" @pytest.fixture @@ -39,3 +45,18 @@ def mock_pyotgw() -> Generator[MagicMock]: ), ): 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={}, + ) diff --git a/tests/components/opentherm_gw/test_init.py b/tests/components/opentherm_gw/test_init.py index 2116967d7206583c03dee9b806138bfcbb51fcd5..4085e25c614d776520dc13c0f0dd47bf794fbacd 100644 --- a/tests/components/opentherm_gw/test_init.py +++ b/tests/components/opentherm_gw/test_init.py @@ -8,38 +8,28 @@ from homeassistant.components.opentherm_gw.const import ( DOMAIN, OpenThermDeviceIdentifier, ) -from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME +from homeassistant.const import CONF_ID from homeassistant.core import HomeAssistant 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 VERSION_NEW = "4.2.8.1" 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( hass: HomeAssistant, device_registry: dr.DeviceRegistry, + mock_config_entry: MockConfigEntry, mock_pyotgw: MagicMock, ) -> None: """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() gw_dev = device_registry.async_get_device( @@ -52,13 +42,14 @@ async def test_device_registry_insert( async def test_device_registry_update( hass: HomeAssistant, device_registry: dr.DeviceRegistry, + mock_config_entry: MockConfigEntry, mock_pyotgw: MagicMock, ) -> None: """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( - config_entry_id=MOCK_CONFIG_ENTRY.entry_id, + config_entry_id=mock_config_entry.entry_id, identifiers={ (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 - 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() gw_dev = device_registry.async_get_device( @@ -84,13 +75,14 @@ async def test_device_registry_update( async def test_device_migration( hass: HomeAssistant, device_registry: dr.DeviceRegistry, + mock_config_entry: MockConfigEntry, mock_pyotgw: MagicMock, ) -> None: """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( - config_entry_id=MOCK_CONFIG_ENTRY.entry_id, + config_entry_id=mock_config_entry.entry_id, identifiers={ (DOMAIN, MOCK_GATEWAY_ID), }, @@ -100,7 +92,7 @@ async def test_device_migration( 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() assert ( @@ -136,22 +128,23 @@ async def test_device_migration( async def test_climate_entity_migration( hass: HomeAssistant, entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, mock_pyotgw: MagicMock, ) -> None: """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( domain="climate", 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() updated_entry = entity_registry.async_get(entry.entity_id) assert updated_entry is not None assert ( 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" )