diff --git a/homeassistant/components/nexia/__init__.py b/homeassistant/components/nexia/__init__.py index 94a0c49fdaa7..311103188a06 100644 --- a/homeassistant/components/nexia/__init__.py +++ b/homeassistant/components/nexia/__init__.py @@ -9,9 +9,13 @@ from nexia.thermostat import NexiaThermostat from nexia.zone import NexiaThermostatZone from homeassistant.const import CONF_PASSWORD, CONF_USERNAME -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, 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.aiohttp_client import async_get_clientsession from homeassistant.helpers.typing import ConfigType @@ -91,14 +95,14 @@ def _preregister_devices( thermostat = nexia_home.get_thermostat_by_id(thermostat_id) device_registry.async_get_or_create_device( config_entry_id=entry.entry_id, - device_info=dr.DeviceInfo(identifiers={(DOMAIN, thermostat_id)}), # type: ignore[arg-type] # until fix issue #139773 + device_info=dr.DeviceInfo(identifiers={(DOMAIN, str(thermostat_id))}), ) for zone_id in thermostat.get_zone_ids(): zone = thermostat.get_zone_by_id(zone_id) device_registry.async_get_or_create_device( config_entry_id=entry.entry_id, device_info=dr.DeviceInfo( - identifiers={(DOMAIN, zone_id)}, # type: ignore[arg-type] # until fix issue #139773 + identifiers={(DOMAIN, str(zone_id))}, suggested_area=zone.get_name(), ), ) @@ -117,11 +121,11 @@ async def async_remove_config_entry_device( nexia_home = coordinator.nexia_home dev_ids = {dev_id[1] for dev_id in device_entry.identifiers if dev_id[0] == DOMAIN} for thermostat_id in nexia_home.get_thermostat_ids(): - if thermostat_id in dev_ids: + if str(thermostat_id) in dev_ids: return False thermostat: NexiaThermostat = nexia_home.get_thermostat_by_id(thermostat_id) for zone_id in thermostat.get_zone_ids(): - if zone_id in dev_ids: + if str(zone_id) in dev_ids: return False zone: NexiaThermostatZone = thermostat.get_zone_by_id(zone_id) for room_iq_sensor in zone.get_sensors(): @@ -136,11 +140,43 @@ async def async_migrate_entry(hass: HomeAssistant, entry: NexiaConfigEntry) -> N _LOGGER.debug("Migrating from version %s", entry.version) if entry.version == 1: - # 1 -> 2: Unique ID from integer to string + # 1.1 -> 1.2: Unique ID from integer to string if entry.minor_version == 1: - minor_version = 2 hass.config_entries.async_update_entry( - entry, unique_id=str(entry.unique_id), minor_version=minor_version + entry, unique_id=str(entry.unique_id) ) + # 1.2 -> 2.1: Make nexia entity and device unique ids strings + await er.async_migrate_entries(hass, entry.entry_id, _migrate_unique_id) + _migrate_device_identifiers(dr.async_get(hass), entry.entry_id) + + hass.config_entries.async_update_entry(entry, version=2, minor_version=1) + _LOGGER.debug("Migration successful") + + +@callback +def _migrate_unique_id(entity_entry: er.RegistryEntry) -> dict[str, str] | None: + """Migrate int entity unique_ids to strings.""" + if isinstance(entity_entry.unique_id, str): + return None + return {"new_unique_id": str(entity_entry.unique_id)} + + +def _migrate_device_identifiers( + device_registry: dr.DeviceRegistry, entry_id: str +) -> None: + """Migrate int device identifiers to strings.""" + for device_entry in dr.async_entries_for_config_entry(device_registry, entry_id): + new_ids: set[tuple[str, str]] = set() + needs_update = False + for domain, identifier in device_entry.identifiers: + if domain == DOMAIN and not isinstance(identifier, str): + new_ids.add((domain, str(identifier))) + needs_update = True + else: + new_ids.add((domain, identifier)) + if needs_update: + device_registry.async_update_device( + device_entry.id, new_identifiers=new_ids + ) diff --git a/homeassistant/components/nexia/climate.py b/homeassistant/components/nexia/climate.py index a3b55d8daf9c..02e0b4a27b51 100644 --- a/homeassistant/components/nexia/climate.py +++ b/homeassistant/components/nexia/climate.py @@ -103,7 +103,7 @@ class NexiaZone(NexiaThermostatZoneEntity, ClimateEntity): self, coordinator: NexiaDataUpdateCoordinator, zone: NexiaThermostatZone ) -> None: """Initialize the thermostat.""" - super().__init__(coordinator, zone, zone.zone_id) # type: ignore[arg-type] # until fix issue #139773 + super().__init__(coordinator, zone, str(zone.zone_id)) thermostat = self._thermostat unit = thermostat.get_unit() min_humidity, max_humidity = thermostat.get_humidity_setpoint_limits() diff --git a/homeassistant/components/nexia/config_flow.py b/homeassistant/components/nexia/config_flow.py index fde6cea72298..a55139770ec7 100644 --- a/homeassistant/components/nexia/config_flow.py +++ b/homeassistant/components/nexia/config_flow.py @@ -86,8 +86,8 @@ async def validate_input(hass: HomeAssistant, data): class NexiaConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Nexia.""" - VERSION = 1 - MINOR_VERSION = 2 + VERSION = 2 + MINOR_VERSION = 1 @override async def async_step_user( diff --git a/homeassistant/components/nexia/entity.py b/homeassistant/components/nexia/entity.py index 89927bcfe6a3..9bad55fa7b73 100644 --- a/homeassistant/components/nexia/entity.py +++ b/homeassistant/components/nexia/entity.py @@ -53,7 +53,7 @@ class NexiaThermostatEntity(NexiaEntity): thermostat_id = thermostat.thermostat_id self._attr_device_info = DeviceInfo( configuration_url=self.coordinator.nexia_home.root_url, - identifiers={(DOMAIN, thermostat_id)}, # type: ignore[arg-type] # until fix issue #139773 + identifiers={(DOMAIN, str(thermostat_id))}, manufacturer=MANUFACTURER, model=thermostat.get_model(), name=thermostat.get_name(), @@ -108,14 +108,14 @@ class NexiaThermostatZoneEntity(NexiaThermostatEntity): thermostat = zone.thermostat dev_info = DeviceInfo( configuration_url=coordinator.nexia_home.root_url, - identifiers={(DOMAIN, zone.zone_id)}, # type: ignore[arg-type] # until fix issue #139773 + identifiers={(DOMAIN, str(zone.zone_id))}, manufacturer=MANUFACTURER, model=thermostat.get_model(), name=zone.get_name(), sw_version=thermostat.get_firmware(), via_device_id=dr.async_get_device_id_by_identifier( coordinator.hass, - (DOMAIN, thermostat.thermostat_id), # type: ignore[arg-type] # until fix issue #139773 + (DOMAIN, str(thermostat.thermostat_id)), config_entry_id=coordinator.config_entry.entry_id, ), ) @@ -169,7 +169,7 @@ class NexiaRoomIQEntity(NexiaThermostatZoneEntity): sw_version=None, # not reported via_device_id=dr.async_get_device_id_by_identifier( coordinator.hass, - (DOMAIN, zone.zone_id), # type: ignore[arg-type] # until fix issue #139773 + (DOMAIN, str(zone.zone_id)), config_entry_id=coordinator.config_entry.entry_id, ), ) diff --git a/homeassistant/components/nexia/scene.py b/homeassistant/components/nexia/scene.py index f765a34bbbaa..eaa151f55652 100644 --- a/homeassistant/components/nexia/scene.py +++ b/homeassistant/components/nexia/scene.py @@ -42,7 +42,7 @@ class NexiaAutomationScene(NexiaEntity, Scene): self, coordinator: NexiaDataUpdateCoordinator, automation: NexiaAutomation ) -> None: """Initialize the automation scene.""" - super().__init__(coordinator, automation.automation_id) # type: ignore[arg-type] # until fix issue #139773 + super().__init__(coordinator, str(automation.automation_id)) self._attr_name = automation.name self._automation = automation self._attr_extra_state_attributes = {ATTR_DESCRIPTION: automation.description} diff --git a/homeassistant/components/nexia/switch.py b/homeassistant/components/nexia/switch.py index b2ba23fe817b..370bc74bd4a6 100644 --- a/homeassistant/components/nexia/switch.py +++ b/homeassistant/components/nexia/switch.py @@ -68,8 +68,7 @@ class NexiaHoldSwitch(NexiaThermostatZoneEntity, SwitchEntity): self, coordinator: NexiaDataUpdateCoordinator, zone: NexiaThermostatZone ) -> None: """Initialize the hold mode switch.""" - zone_id = zone.zone_id - super().__init__(coordinator, zone, zone_id) # type: ignore[arg-type] # until fix issue #139773 + super().__init__(coordinator, zone, str(zone.zone_id)) @property @override diff --git a/tests/components/nexia/conftest.py b/tests/components/nexia/conftest.py index 54ef6b074c32..44d69cdf381e 100644 --- a/tests/components/nexia/conftest.py +++ b/tests/components/nexia/conftest.py @@ -11,6 +11,7 @@ from nexia.zone import NexiaThermostatZone import pytest from pytest_homeassistant.common import MockConfigEntry +from homeassistant.components.nexia.config_flow import NexiaConfigFlow from homeassistant.components.nexia.const import DOMAIN from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant @@ -409,7 +410,8 @@ async def setup_integration( entry = MockConfigEntry( domain=DOMAIN, data={CONF_USERNAME: "mock", CONF_PASSWORD: "mock"}, - minor_version=2, + version=NexiaConfigFlow.VERSION, + minor_version=NexiaConfigFlow.MINOR_VERSION, unique_id=unique_id, ) entry.add_to_hass(hass) diff --git a/tests/components/nexia/test_init.py b/tests/components/nexia/test_init.py index 734857b474f5..623ba649fd71 100644 --- a/tests/components/nexia/test_init.py +++ b/tests/components/nexia/test_init.py @@ -1,13 +1,15 @@ """The init tests for the nexia platform.""" -from unittest.mock import MagicMock, NonCallableMock, patch +from unittest.mock import AsyncMock, MagicMock, NonCallableMock, patch import aiohttp from nexia.home import NexiaHome +import pytest from pytest_homeassistant.common import MockConfigEntry from pytest_homeassistant.typing import WebSocketGenerator from homeassistant.components.nexia import _preregister_devices +from homeassistant.components.nexia.config_flow import NexiaConfigFlow from homeassistant.components.nexia.const import DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState from homeassistant.const import CONF_PASSWORD, CONF_USERNAME @@ -86,7 +88,7 @@ async def test_device_remove_devices( async def test_migrate_entry_minor_version_1_2(hass: HomeAssistant) -> None: - """Test migrating a 1.1 config entry to 1.2.""" + """Test migrating a 1.1 config entry.""" with patch("homeassistant.components.nexia.async_setup_entry", return_value=True): entry = MockConfigEntry( domain=DOMAIN, @@ -97,11 +99,122 @@ async def test_migrate_entry_minor_version_1_2(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) assert await hass.config_entries.async_setup(entry.entry_id) - assert entry.version == 1 - assert entry.minor_version == 2 + assert entry.state is ConfigEntryState.LOADED + assert entry.version == NexiaConfigFlow.VERSION + assert entry.minor_version == NexiaConfigFlow.MINOR_VERSION assert entry.unique_id == "123456" +@pytest.mark.usefixtures("patch_nexia_home") +async def test_migrate_entity_identifiers( + hass: HomeAssistant, entity_registry: er.EntityRegistry +) -> None: + """Test migrating config entity identifiers to string.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={CONF_USERNAME: "mock", CONF_PASSWORD: "mock"}, + version=1, + minor_version=2, + ) + entry.add_to_hass(hass) + + int_entry = entity_registry.async_get_or_create( + "climate", + DOMAIN, + 85034552, + config_entry=entry, + ) + str_entry = entity_registry.async_get_or_create( + "sensor", + DOMAIN, + "already_a_string", + config_entry=entry, + ) + modified_before = str_entry.modified_at + assert await hass.config_entries.async_setup(entry.entry_id) is True + assert entry.state is ConfigEntryState.LOADED + + migrated_entry = entity_registry.async_get(int_entry.entity_id) + assert migrated_entry is not None + assert migrated_entry.unique_id == "85034552" + assert migrated_entry.entity_id == int_entry.entity_id + + unchanged_entry = entity_registry.async_get(str_entry.entity_id) + assert unchanged_entry is not None + assert unchanged_entry.unique_id == "already_a_string" + assert unchanged_entry.modified_at == modified_before + + +@pytest.mark.usefixtures("patch_nexia_home") +async def test_migrate_device_identifiers( + hass: HomeAssistant, device_registry: dr.DeviceRegistry +) -> None: + """Test migrating config device identifiers to string.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={CONF_USERNAME: "mock", CONF_PASSWORD: "mock"}, + version=1, + minor_version=2, + ) + entry.add_to_hass(hass) + + int_device = device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + identifiers={(DOMAIN, 12345), ("other_domain", "not_touched")}, + name="Center", + ) + str_device = device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + identifiers={(DOMAIN, "already_a_string")}, + name="Center", + ) + modified_before = str_device.modified_at + assert await hass.config_entries.async_setup(entry.entry_id) is True + assert entry.state is ConfigEntryState.LOADED + + migrated_device = device_registry.async_get(int_device.id) + assert migrated_device is not None + assert migrated_device.identifiers == { + (DOMAIN, "12345"), + ("other_domain", "not_touched"), + } + assert migrated_device.id == int_device.id + + unchanged_device = device_registry.async_get(str_device.id) + assert unchanged_device is not None + assert unchanged_device.identifiers == {(DOMAIN, "already_a_string")} + assert unchanged_device.modified_at == modified_before + + +@patch("homeassistant.components.nexia.async_migrate_entry") +async def test_string_identifiers( + mock_migrate_entry: AsyncMock, + hass: HomeAssistant, + patch_nexia_home: NexiaHome, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test the identifiers are strings.""" + entry = await setup_integration(hass, patch_nexia_home) + + # verify migration was not called + mock_migrate_entry.assert_not_awaited() + + entities = entity_registry.entities + entity_entries = entities.get_entries_for_config_entry_id(entry.entry_id) + assert len(entity_entries) > 0 + for entity_entry in entity_entries: + assert isinstance(entity_entry.unique_id, str) + + device_entries = dr.async_entries_for_config_entry(device_registry, entry.entry_id) + assert len(device_entries) > 0 + for device_entry in device_entries: + for domain, ident in device_entry.identifiers: + assert isinstance(ident, str), ( + f"Device [{device_entry.name}] has non-string identifier ({domain}, {ident})" + ) + + async def test_device_preregistration( hass: HomeAssistant, mock_nexia_home: NexiaHome, device_registry: dr.DeviceRegistry ) -> None: @@ -117,8 +230,7 @@ async def test_device_preregistration( for thermostat_id in thermostat_ids: thermostat = mock_nexia_home.get_thermostat_by_id(thermostat_id) device = device_registry.async_get_device_by_identifier( - (DOMAIN, thermostat.thermostat_id), # type: ignore[arg-type] # until fix issue #139773 - entry.entry_id, + (DOMAIN, str(thermostat.thermostat_id)), entry.entry_id ) assert device is not None @@ -128,8 +240,7 @@ async def test_device_preregistration( for zone_id in zone_ids: zone = thermostat.get_zone_by_id(zone_id) device = device_registry.async_get_device_by_identifier( - (DOMAIN, zone.zone_id), # type: ignore[arg-type] # until fix issue #139773 - entry.entry_id, + (DOMAIN, str(zone.zone_id)), entry.entry_id ) assert device is not None assert device.area_id == slugify(zone.get_name()) @@ -144,22 +255,19 @@ async def test_device_via_device_links( config_entry = await setup_integration(hass, patch_nexia_home) thermostat_device = device_registry.async_get_device_by_identifier( - (DOMAIN, 2000004), # type: ignore[arg-type] # until fix issue #139773 - config_entry.entry_id, + (DOMAIN, "2000004"), config_entry.entry_id ) assert thermostat_device is not None zone_device = device_registry.async_get_device_by_identifier( - (DOMAIN, 500), # type: ignore[arg-type] # until fix issue #139773 - config_entry.entry_id, + (DOMAIN, "500"), config_entry.entry_id ) assert zone_device is not None assert zone_device.via_device_id == thermostat_device.id assert zone_device.area_id == "zone3" sensor_device = device_registry.async_get_device_by_identifier( - (DOMAIN, "502"), - config_entry.entry_id, + (DOMAIN, "502"), config_entry.entry_id ) assert sensor_device is not None assert sensor_device.via_device_id == zone_device.id