From 2132b170f2ac5227b6ff9a1cd718d0e2390d9c19 Mon Sep 17 00:00:00 2001 From: mkmer Date: Sat, 4 May 2024 08:18:50 -0400 Subject: [PATCH] Update unique_id to string in Honeywell (#116726) * Update unique_id to string * Update homeassistant/components/honeywell/climate.py Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/honeywell/climate.py Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/honeywell/climate.py Co-authored-by: Joost Lekkerkerker * Add typing for devices * Add tests * Use methods to verify unique_id * Update tests/components/honeywell/test_climate.py Co-authored-by: Joost Lekkerkerker --------- Co-authored-by: Joost Lekkerkerker --- homeassistant/components/honeywell/climate.py | 25 ++++++++++++++--- tests/components/honeywell/test_climate.py | 27 ++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/honeywell/climate.py b/homeassistant/components/honeywell/climate.py index ff63d66230d..f9a1cc54c7a 100644 --- a/homeassistant/components/honeywell/climate.py +++ b/homeassistant/components/honeywell/climate.py @@ -35,7 +35,11 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError -from homeassistant.helpers import device_registry as dr, issue_registry as ir +from homeassistant.helpers import ( + device_registry as dr, + entity_registry as er, + issue_registry as ir, +) from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -99,7 +103,7 @@ async def async_setup_entry( heat_away_temp = entry.options.get(CONF_HEAT_AWAY_TEMPERATURE) data: HoneywellData = hass.data[DOMAIN][entry.entry_id] - + _async_migrate_unique_id(hass, data.devices) async_add_entities( [ HoneywellUSThermostat(data, device, cool_away_temp, heat_away_temp) @@ -109,6 +113,21 @@ async def async_setup_entry( remove_stale_devices(hass, entry, data.devices) +def _async_migrate_unique_id( + hass: HomeAssistant, devices: dict[str, SomeComfortDevice] +) -> None: + """Migrate entities to string.""" + entity_registry = er.async_get(hass) + for device in devices.values(): + entity_id = entity_registry.async_get_entity_id( + "climate", DOMAIN, device.deviceid + ) + if entity_id is not None: + entity_registry.async_update_entity( + entity_id, new_unique_id=str(device.deviceid) + ) + + def remove_stale_devices( hass: HomeAssistant, config_entry: ConfigEntry, @@ -161,7 +180,7 @@ class HoneywellUSThermostat(ClimateEntity): self._away = False self._retry = 0 - self._attr_unique_id = device.deviceid + self._attr_unique_id = str(device.deviceid) self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, device.deviceid)}, diff --git a/tests/components/honeywell/test_climate.py b/tests/components/honeywell/test_climate.py index d09444808d8..b57be5f1838 100644 --- a/tests/components/honeywell/test_climate.py +++ b/tests/components/honeywell/test_climate.py @@ -29,13 +29,19 @@ from homeassistant.components.climate import ( SERVICE_SET_TEMPERATURE, HVACMode, ) -from homeassistant.components.honeywell.climate import PRESET_HOLD, RETRY, SCAN_INTERVAL +from homeassistant.components.honeywell.climate import ( + DOMAIN, + PRESET_HOLD, + RETRY, + SCAN_INTERVAL, +) from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_TEMPERATURE, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_UNAVAILABLE, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError @@ -1264,3 +1270,22 @@ async def test_aux_heat_off_service_call( blocking=True, ) device.set_system_mode.assert_called_once_with("off") + + +async def test_unique_id( + hass: HomeAssistant, + device: MagicMock, + config_entry: MagicMock, + entity_registry: er.EntityRegistry, +) -> None: + """Test unique id convert to string.""" + entity_registry.async_get_or_create( + Platform.CLIMATE, + DOMAIN, + device.deviceid, + config_entry=config_entry, + suggested_object_id=device.name, + ) + await init_integration(hass, config_entry) + entity_entry = entity_registry.async_get(f"climate.{device.name}") + assert entity_entry.unique_id == str(device.deviceid)