2021-01-05 19:46:54 +00:00
|
|
|
"""Test Opentherm Gateway init."""
|
2024-03-08 13:55:15 +00:00
|
|
|
|
2024-09-03 17:19:43 +00:00
|
|
|
from unittest.mock import MagicMock
|
2021-01-05 19:46:54 +00:00
|
|
|
|
|
|
|
from pyotgw.vars import OTGW, OTGW_ABOUT
|
|
|
|
|
2024-09-01 11:28:08 +00:00
|
|
|
from homeassistant.components.opentherm_gw.const import (
|
|
|
|
DOMAIN,
|
|
|
|
OpenThermDeviceIdentifier,
|
|
|
|
)
|
2021-01-05 19:46:54 +00:00
|
|
|
from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME
|
2023-02-08 15:48:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-09-01 15:22:03 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
2021-01-05 19:46:54 +00:00
|
|
|
|
2024-09-03 17:19:43 +00:00
|
|
|
from .conftest import VERSION_TEST
|
|
|
|
|
2023-02-09 09:43:45 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2021-01-05 19:46:54 +00:00
|
|
|
|
|
|
|
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={},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-05-28 11:42:38 +00:00
|
|
|
async def test_device_registry_insert(
|
2024-09-03 17:19:43 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
device_registry: dr.DeviceRegistry,
|
|
|
|
mock_pyotgw: MagicMock,
|
2024-05-28 11:42:38 +00:00
|
|
|
) -> None:
|
2021-01-05 19:46:54 +00:00
|
|
|
"""Test that the device registry is initialized correctly."""
|
|
|
|
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
|
|
|
|
2024-09-03 17:19:43 +00:00
|
|
|
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
|
2021-01-05 19:46:54 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-09-01 11:28:08 +00:00
|
|
|
gw_dev = device_registry.async_get_device(
|
|
|
|
identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")}
|
|
|
|
)
|
2024-09-03 17:19:43 +00:00
|
|
|
assert gw_dev is not None
|
|
|
|
assert gw_dev.sw_version == VERSION_TEST
|
2021-01-05 19:46:54 +00:00
|
|
|
|
|
|
|
|
2023-02-09 09:43:45 +00:00
|
|
|
async def test_device_registry_update(
|
2024-09-03 17:19:43 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
device_registry: dr.DeviceRegistry,
|
|
|
|
mock_pyotgw: MagicMock,
|
2023-02-09 09:43:45 +00:00
|
|
|
) -> None:
|
2021-01-05 19:46:54 +00:00
|
|
|
"""Test that the device registry is updated correctly."""
|
|
|
|
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
|
|
|
|
2023-02-09 09:43:45 +00:00
|
|
|
device_registry.async_get_or_create(
|
2021-01-05 19:46:54 +00:00
|
|
|
config_entry_id=MOCK_CONFIG_ENTRY.entry_id,
|
2024-09-01 11:28:08 +00:00
|
|
|
identifiers={
|
|
|
|
(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")
|
|
|
|
},
|
2021-01-05 19:46:54 +00:00
|
|
|
name="Mock Gateway",
|
|
|
|
manufacturer="Schelte Bron",
|
|
|
|
model="OpenTherm Gateway",
|
2024-09-03 17:19:43 +00:00
|
|
|
sw_version=VERSION_TEST,
|
2021-01-05 19:46:54 +00:00
|
|
|
)
|
|
|
|
|
2024-09-03 17:19:43 +00:00
|
|
|
mock_pyotgw.return_value.connect.return_value = MINIMAL_STATUS_UPD
|
2021-01-05 19:46:54 +00:00
|
|
|
|
2024-09-03 17:19:43 +00:00
|
|
|
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
|
2021-01-05 19:46:54 +00:00
|
|
|
await hass.async_block_till_done()
|
2024-09-03 17:19:43 +00:00
|
|
|
|
2024-09-01 11:28:08 +00:00
|
|
|
gw_dev = device_registry.async_get_device(
|
|
|
|
identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")}
|
|
|
|
)
|
|
|
|
assert gw_dev is not None
|
2021-01-05 19:46:54 +00:00
|
|
|
assert gw_dev.sw_version == VERSION_NEW
|
2024-09-01 11:28:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Device migration test can be removed in 2025.4.0
|
|
|
|
async def test_device_migration(
|
2024-09-03 17:19:43 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
device_registry: dr.DeviceRegistry,
|
|
|
|
mock_pyotgw: MagicMock,
|
2024-09-01 11:28:08 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test that the device registry is updated correctly."""
|
|
|
|
MOCK_CONFIG_ENTRY.add_to_hass(hass)
|
|
|
|
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=MOCK_CONFIG_ENTRY.entry_id,
|
|
|
|
identifiers={
|
|
|
|
(DOMAIN, MOCK_GATEWAY_ID),
|
|
|
|
},
|
|
|
|
name="Mock Gateway",
|
|
|
|
manufacturer="Schelte Bron",
|
|
|
|
model="OpenTherm Gateway",
|
2024-09-03 17:19:43 +00:00
|
|
|
sw_version=VERSION_TEST,
|
2024-09-01 11:28:08 +00:00
|
|
|
)
|
|
|
|
|
2024-09-03 17:19:43 +00:00
|
|
|
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
|
2024-09-01 11:28:08 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert (
|
|
|
|
device_registry.async_get_device(identifiers={(DOMAIN, MOCK_GATEWAY_ID)})
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
|
|
|
|
gw_dev = device_registry.async_get_device(
|
|
|
|
identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")}
|
|
|
|
)
|
|
|
|
assert gw_dev is not None
|
|
|
|
|
|
|
|
assert (
|
|
|
|
device_registry.async_get_device(
|
|
|
|
identifiers={
|
|
|
|
(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.BOILER}")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
is not None
|
|
|
|
)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
device_registry.async_get_device(
|
|
|
|
identifiers={
|
|
|
|
(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.THERMOSTAT}")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
is not None
|
|
|
|
)
|
2024-09-01 15:22:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Entity migration test can be removed in 2025.4.0
|
|
|
|
async def test_climate_entity_migration(
|
2024-09-03 17:19:43 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
mock_pyotgw: MagicMock,
|
2024-09-01 15:22:03 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test that the climate entity unique_id gets migrated correctly."""
|
|
|
|
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],
|
|
|
|
)
|
|
|
|
|
2024-09-03 17:19:43 +00:00
|
|
|
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
|
2024-09-01 15:22:03 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-09-03 17:19:43 +00:00
|
|
|
updated_entry = entity_registry.async_get(entry.entity_id)
|
|
|
|
assert updated_entry is not None
|
2024-09-01 15:22:03 +00:00
|
|
|
assert (
|
2024-09-03 17:19:43 +00:00
|
|
|
updated_entry.unique_id
|
2024-09-01 15:22:03 +00:00
|
|
|
== f"{MOCK_CONFIG_ENTRY.data[CONF_ID]}-{OpenThermDeviceIdentifier.THERMOSTAT}-thermostat_entity"
|
|
|
|
)
|