Update unique_id to string in Honeywell (#116726)

* Update unique_id to string

* Update homeassistant/components/honeywell/climate.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/honeywell/climate.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/honeywell/climate.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Add typing for devices

* Add tests

* Use methods to verify unique_id

* Update tests/components/honeywell/test_climate.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
mkmer 2024-05-04 08:18:50 -04:00 committed by GitHub
parent f9d95efac0
commit 2132b170f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 4 deletions

View file

@ -35,7 +35,11 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError 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.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback 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) heat_away_temp = entry.options.get(CONF_HEAT_AWAY_TEMPERATURE)
data: HoneywellData = hass.data[DOMAIN][entry.entry_id] data: HoneywellData = hass.data[DOMAIN][entry.entry_id]
_async_migrate_unique_id(hass, data.devices)
async_add_entities( async_add_entities(
[ [
HoneywellUSThermostat(data, device, cool_away_temp, heat_away_temp) 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) 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( def remove_stale_devices(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
@ -161,7 +180,7 @@ class HoneywellUSThermostat(ClimateEntity):
self._away = False self._away = False
self._retry = 0 self._retry = 0
self._attr_unique_id = device.deviceid self._attr_unique_id = str(device.deviceid)
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device.deviceid)}, identifiers={(DOMAIN, device.deviceid)},

View file

@ -29,13 +29,19 @@ from homeassistant.components.climate import (
SERVICE_SET_TEMPERATURE, SERVICE_SET_TEMPERATURE,
HVACMode, 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 ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
Platform,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
@ -1264,3 +1270,22 @@ async def test_aux_heat_off_service_call(
blocking=True, blocking=True,
) )
device.set_system_mode.assert_called_once_with("off") 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)