Migrate opentherm_gw climate entity unique_id (#125024)

* Migrate climate entity unique_id to match the format used by other opentherm_gw entities
* Add test to verify migration
This commit is contained in:
mvn23 2024-09-01 17:22:03 +02:00 committed by GitHub
parent fa21613951
commit 56667ec2bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import pyotgw.vars as gw_vars
from serial import SerialException
import voluptuous as vol
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
ATTR_DATE,
@ -27,7 +28,11 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
entity_registry as er,
)
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.typing import ConfigType
@ -132,6 +137,18 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
},
)
# Migration can be removed in 2025.4.0
ent_reg = er.async_get(hass)
if (
entity_id := ent_reg.async_get_entity_id(
CLIMATE_DOMAIN, DOMAIN, config_entry.data[CONF_ID]
)
) is not None:
ent_reg.async_update_entity(
entity_id,
new_unique_id=f"{config_entry.data[CONF_ID]}-{OpenThermDeviceIdentifier.THERMOSTAT}-thermostat_entity",
)
config_entry.add_update_listener(options_updated)
try:

View File

@ -103,7 +103,6 @@ class OpenThermClimate(OpenThermEntity, ClimateEntity):
self._attr_precision = options[CONF_READ_PRECISION]
self._attr_target_temperature_step = options.get(CONF_SET_PRECISION)
self.temporary_ovrd_mode = options.get(CONF_TEMPORARY_OVRD_MODE, True)
self._attr_unique_id = gw_hub.hub_id
@callback
def update_options(self, entry):

View File

@ -12,7 +12,7 @@ from homeassistant.components.opentherm_gw.const import (
)
from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry
@ -154,3 +154,36 @@ async def test_device_migration(
)
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
) -> 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],
)
with (
patch(
"homeassistant.components.opentherm_gw.OpenThermGateway",
return_value=MagicMock(
connect=AsyncMock(return_value=MINIMAL_STATUS_UPD),
set_control_setpoint=AsyncMock(),
set_max_relative_mod=AsyncMock(),
disconnect=AsyncMock(),
),
),
):
await setup.async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
assert (
entity_registry.async_get(entry.entity_id).unique_id
== f"{MOCK_CONFIG_ENTRY.data[CONF_ID]}-{OpenThermDeviceIdentifier.THERMOSTAT}-thermostat_entity"
)