core/tests/components/opentherm_gw/test_init.py

158 lines
4.6 KiB
Python
Raw Normal View History

"""Test Opentherm Gateway init."""
from unittest.mock import MagicMock
from pyotgw.vars import OTGW, OTGW_ABOUT
Split opentherm_gw entities between different devices (#124869) * * Add migration from single device to multiple devices, removing all old entities * Create new devices for Boiler and Thermostat * Add classes for new entities based on the new devices * Split binary_sensor entities into devices * Split sensor entities into different devices * Move climate entity to thermostat device * Fix climate entity away mode * Fix translation placeholders * Allow sensor values with capital letters * * Add EntityCategory * Update and add device_classes * Fix translation keys * Fix climate entity category * Update tests * Handle `available` property in `entity.py` * Improve GPIO state binary_sensor translations * Fix: Updates are already subscribed to in the base entity * Remove entity_id generation from sensor and binary_sensor entities * * Use _attr_name on climate class instead of through entity_description * Add type hints * Rewrite to derive entities for all OpenTherm devices from a single base class * Improve type annotations * Use OpenThermDataSource to access status dict * Move entity_category from entity_description to _attr_entity_category * Move entity descriptions with the same translation_key closer together * Update tests * Add device migration test * * Add missing sensors and binary_sensors back * Improve migration, do not delete old entities from registry * Add comments for migration period * Use single lists for entity descriptions * Avoid changing sensor values, remove translations * * Import only required class from pyotgw * Update tests
2024-09-01 11:28:08 +00:00
from homeassistant.components.opentherm_gw.const import (
DOMAIN,
OpenThermDeviceIdentifier,
)
from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from .conftest import 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_pyotgw: MagicMock,
) -> None:
"""Test that the device registry is initialized correctly."""
MOCK_CONFIG_ENTRY.add_to_hass(hass)
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
await hass.async_block_till_done()
Split opentherm_gw entities between different devices (#124869) * * Add migration from single device to multiple devices, removing all old entities * Create new devices for Boiler and Thermostat * Add classes for new entities based on the new devices * Split binary_sensor entities into devices * Split sensor entities into different devices * Move climate entity to thermostat device * Fix climate entity away mode * Fix translation placeholders * Allow sensor values with capital letters * * Add EntityCategory * Update and add device_classes * Fix translation keys * Fix climate entity category * Update tests * Handle `available` property in `entity.py` * Improve GPIO state binary_sensor translations * Fix: Updates are already subscribed to in the base entity * Remove entity_id generation from sensor and binary_sensor entities * * Use _attr_name on climate class instead of through entity_description * Add type hints * Rewrite to derive entities for all OpenTherm devices from a single base class * Improve type annotations * Use OpenThermDataSource to access status dict * Move entity_category from entity_description to _attr_entity_category * Move entity descriptions with the same translation_key closer together * Update tests * Add device migration test * * Add missing sensors and binary_sensors back * Improve migration, do not delete old entities from registry * Add comments for migration period * Use single lists for entity descriptions * Avoid changing sensor values, remove translations * * Import only required class from pyotgw * Update tests
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
assert gw_dev.sw_version == VERSION_TEST
async def test_device_registry_update(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mock_pyotgw: MagicMock,
) -> 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,
Split opentherm_gw entities between different devices (#124869) * * Add migration from single device to multiple devices, removing all old entities * Create new devices for Boiler and Thermostat * Add classes for new entities based on the new devices * Split binary_sensor entities into devices * Split sensor entities into different devices * Move climate entity to thermostat device * Fix climate entity away mode * Fix translation placeholders * Allow sensor values with capital letters * * Add EntityCategory * Update and add device_classes * Fix translation keys * Fix climate entity category * Update tests * Handle `available` property in `entity.py` * Improve GPIO state binary_sensor translations * Fix: Updates are already subscribed to in the base entity * Remove entity_id generation from sensor and binary_sensor entities * * Use _attr_name on climate class instead of through entity_description * Add type hints * Rewrite to derive entities for all OpenTherm devices from a single base class * Improve type annotations * Use OpenThermDataSource to access status dict * Move entity_category from entity_description to _attr_entity_category * Move entity descriptions with the same translation_key closer together * Update tests * Add device migration test * * Add missing sensors and binary_sensors back * Improve migration, do not delete old entities from registry * Add comments for migration period * Use single lists for entity descriptions * Avoid changing sensor values, remove translations * * Import only required class from pyotgw * Update tests
2024-09-01 11:28:08 +00:00
identifiers={
(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")
},
name="Mock Gateway",
manufacturer="Schelte Bron",
model="OpenTherm Gateway",
sw_version=VERSION_TEST,
)
mock_pyotgw.return_value.connect.return_value = MINIMAL_STATUS_UPD
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
await hass.async_block_till_done()
Split opentherm_gw entities between different devices (#124869) * * Add migration from single device to multiple devices, removing all old entities * Create new devices for Boiler and Thermostat * Add classes for new entities based on the new devices * Split binary_sensor entities into devices * Split sensor entities into different devices * Move climate entity to thermostat device * Fix climate entity away mode * Fix translation placeholders * Allow sensor values with capital letters * * Add EntityCategory * Update and add device_classes * Fix translation keys * Fix climate entity category * Update tests * Handle `available` property in `entity.py` * Improve GPIO state binary_sensor translations * Fix: Updates are already subscribed to in the base entity * Remove entity_id generation from sensor and binary_sensor entities * * Use _attr_name on climate class instead of through entity_description * Add type hints * Rewrite to derive entities for all OpenTherm devices from a single base class * Improve type annotations * Use OpenThermDataSource to access status dict * Move entity_category from entity_description to _attr_entity_category * Move entity descriptions with the same translation_key closer together * Update tests * Add device migration test * * Add missing sensors and binary_sensors back * Improve migration, do not delete old entities from registry * Add comments for migration period * Use single lists for entity descriptions * Avoid changing sensor values, remove translations * * Import only required class from pyotgw * Update tests
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
assert gw_dev.sw_version == VERSION_NEW
Split opentherm_gw entities between different devices (#124869) * * Add migration from single device to multiple devices, removing all old entities * Create new devices for Boiler and Thermostat * Add classes for new entities based on the new devices * Split binary_sensor entities into devices * Split sensor entities into different devices * Move climate entity to thermostat device * Fix climate entity away mode * Fix translation placeholders * Allow sensor values with capital letters * * Add EntityCategory * Update and add device_classes * Fix translation keys * Fix climate entity category * Update tests * Handle `available` property in `entity.py` * Improve GPIO state binary_sensor translations * Fix: Updates are already subscribed to in the base entity * Remove entity_id generation from sensor and binary_sensor entities * * Use _attr_name on climate class instead of through entity_description * Add type hints * Rewrite to derive entities for all OpenTherm devices from a single base class * Improve type annotations * Use OpenThermDataSource to access status dict * Move entity_category from entity_description to _attr_entity_category * Move entity descriptions with the same translation_key closer together * Update tests * Add device migration test * * Add missing sensors and binary_sensors back * Improve migration, do not delete old entities from registry * Add comments for migration period * Use single lists for entity descriptions * Avoid changing sensor values, remove translations * * Import only required class from pyotgw * Update tests
2024-09-01 11:28:08 +00:00
# Device migration test can be removed in 2025.4.0
async def test_device_migration(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
mock_pyotgw: MagicMock,
Split opentherm_gw entities between different devices (#124869) * * Add migration from single device to multiple devices, removing all old entities * Create new devices for Boiler and Thermostat * Add classes for new entities based on the new devices * Split binary_sensor entities into devices * Split sensor entities into different devices * Move climate entity to thermostat device * Fix climate entity away mode * Fix translation placeholders * Allow sensor values with capital letters * * Add EntityCategory * Update and add device_classes * Fix translation keys * Fix climate entity category * Update tests * Handle `available` property in `entity.py` * Improve GPIO state binary_sensor translations * Fix: Updates are already subscribed to in the base entity * Remove entity_id generation from sensor and binary_sensor entities * * Use _attr_name on climate class instead of through entity_description * Add type hints * Rewrite to derive entities for all OpenTherm devices from a single base class * Improve type annotations * Use OpenThermDataSource to access status dict * Move entity_category from entity_description to _attr_entity_category * Move entity descriptions with the same translation_key closer together * Update tests * Add device migration test * * Add missing sensors and binary_sensors back * Improve migration, do not delete old entities from registry * Add comments for migration period * Use single lists for entity descriptions * Avoid changing sensor values, remove translations * * Import only required class from pyotgw * Update tests
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",
sw_version=VERSION_TEST,
Split opentherm_gw entities between different devices (#124869) * * Add migration from single device to multiple devices, removing all old entities * Create new devices for Boiler and Thermostat * Add classes for new entities based on the new devices * Split binary_sensor entities into devices * Split sensor entities into different devices * Move climate entity to thermostat device * Fix climate entity away mode * Fix translation placeholders * Allow sensor values with capital letters * * Add EntityCategory * Update and add device_classes * Fix translation keys * Fix climate entity category * Update tests * Handle `available` property in `entity.py` * Improve GPIO state binary_sensor translations * Fix: Updates are already subscribed to in the base entity * Remove entity_id generation from sensor and binary_sensor entities * * Use _attr_name on climate class instead of through entity_description * Add type hints * Rewrite to derive entities for all OpenTherm devices from a single base class * Improve type annotations * Use OpenThermDataSource to access status dict * Move entity_category from entity_description to _attr_entity_category * Move entity descriptions with the same translation_key closer together * Update tests * Add device migration test * * Add missing sensors and binary_sensors back * Improve migration, do not delete old entities from registry * Add comments for migration period * Use single lists for entity descriptions * Avoid changing sensor values, remove translations * * Import only required class from pyotgw * Update tests
2024-09-01 11:28:08 +00:00
)
await hass.config_entries.async_setup(MOCK_CONFIG_ENTRY.entry_id)
Split opentherm_gw entities between different devices (#124869) * * Add migration from single device to multiple devices, removing all old entities * Create new devices for Boiler and Thermostat * Add classes for new entities based on the new devices * Split binary_sensor entities into devices * Split sensor entities into different devices * Move climate entity to thermostat device * Fix climate entity away mode * Fix translation placeholders * Allow sensor values with capital letters * * Add EntityCategory * Update and add device_classes * Fix translation keys * Fix climate entity category * Update tests * Handle `available` property in `entity.py` * Improve GPIO state binary_sensor translations * Fix: Updates are already subscribed to in the base entity * Remove entity_id generation from sensor and binary_sensor entities * * Use _attr_name on climate class instead of through entity_description * Add type hints * Rewrite to derive entities for all OpenTherm devices from a single base class * Improve type annotations * Use OpenThermDataSource to access status dict * Move entity_category from entity_description to _attr_entity_category * Move entity descriptions with the same translation_key closer together * Update tests * Add device migration test * * Add missing sensors and binary_sensors back * Improve migration, do not delete old entities from registry * Add comments for migration period * Use single lists for entity descriptions * Avoid changing sensor values, remove translations * * Import only required class from pyotgw * Update tests
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
)
# Entity migration test can be removed in 2025.4.0
async def test_climate_entity_migration(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_pyotgw: MagicMock,
) -> 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],
)
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"
)