core/tests/components/opentherm_gw/test_init.py

178 lines
5.2 KiB
Python
Raw Normal View History

"""Test Opentherm Gateway init."""
from unittest.mock import MagicMock
from pyotgw.vars import OTGW, OTGW_ABOUT
from homeassistant import setup
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_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import (
device_registry as dr,
entity_registry as er,
issue_registry as ir,
)
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}"}}
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)
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_config_entry: MockConfigEntry,
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_config_entry: MockConfigEntry,
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)
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_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, 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_config_entry: MockConfigEntry,
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"
)
# Deprecation test, can be removed in 2025.4.0
async def test_configuration_yaml_deprecation(
hass: HomeAssistant,
issue_registry: ir.IssueRegistry,
mock_config_entry: MockConfigEntry,
mock_pyotgw: MagicMock,
) -> None:
"""Test that existing configuration in configuration.yaml creates an issue."""
await setup.async_setup_component(
hass, DOMAIN, {DOMAIN: {"legacy_gateway": {"device": "/dev/null"}}}
)
await hass.async_block_till_done()
assert (
issue_registry.async_get_issue(
DOMAIN, "deprecated_import_from_configuration_yaml"
)
is not None
)