From 860ac1bf51a957efd43dffec0db38fcce09a8349 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 20 Jul 2026 23:13:01 +0200 Subject: [PATCH] Migrate integrations to async_get_device_by_identifier (part 1) (#176901) --- homeassistant/components/airgradient/coordinator.py | 4 ++-- homeassistant/components/airos/__init__.py | 4 ++-- homeassistant/components/airthings_ble/sensor.py | 10 ++++++---- homeassistant/components/alexa_devices/coordinator.py | 4 ++-- homeassistant/components/androidtv/diagnostics.py | 4 ++-- homeassistant/components/anthropic/__init__.py | 4 ++-- homeassistant/components/aqvify/coordinator.py | 5 +++-- homeassistant/components/bosch_shc/entity.py | 4 +++- homeassistant/components/broadlink/device.py | 4 ++-- homeassistant/components/comelit/coordinator.py | 4 ++-- homeassistant/components/comelit/utils.py | 4 +++- homeassistant/components/deconz/services.py | 4 ++-- .../components/devolo_home_network/coordinator.py | 6 ++++-- homeassistant/components/dsmr/sensor.py | 4 +++- homeassistant/components/duco/sensor.py | 4 ++-- .../components/dwd_weather_warnings/__init__.py | 4 +++- homeassistant/components/earn_e_p1/coordinator.py | 6 ++++-- homeassistant/components/enphase_envoy/coordinator.py | 9 ++------- homeassistant/components/epson/media_player.py | 4 +++- 19 files changed, 52 insertions(+), 40 deletions(-) diff --git a/homeassistant/components/airgradient/coordinator.py b/homeassistant/components/airgradient/coordinator.py index ad6c48c7d162..ef39b55b8a3f 100644 --- a/homeassistant/components/airgradient/coordinator.py +++ b/homeassistant/components/airgradient/coordinator.py @@ -75,8 +75,8 @@ class AirGradientCoordinator(DataUpdateCoordinator[AirGradientData]): ) from error if measures.firmware_version != self._current_version: device_registry = dr.async_get(self.hass) - device_entry = device_registry.async_get_device( - identifiers={(DOMAIN, self.serial_number)} + device_entry = device_registry.async_get_device_by_identifier( + (DOMAIN, self.serial_number), self.config_entry.entry_id ) assert device_entry device_registry.async_update_device( diff --git a/homeassistant/components/airos/__init__.py b/homeassistant/components/airos/__init__.py index e154c3d1b662..191209fc23b6 100644 --- a/homeassistant/components/airos/__init__.py +++ b/homeassistant/components/airos/__init__.py @@ -184,8 +184,8 @@ async def async_migrate_entry(hass: HomeAssistant, entry: AirOSConfigEntry) -> b mac_adress = dr.format_mac(entry.unique_id) device_registry = dr.async_get(hass) - if device_entry := device_registry.async_get_device( - connections={(dr.CONNECTION_NETWORK_MAC, mac_adress)} + if device_entry := device_registry.async_get_device_by_connection( + (dr.CONNECTION_NETWORK_MAC, mac_adress), entry.entry_id ): old_device_id = next( ( diff --git a/homeassistant/components/airthings_ble/sensor.py b/homeassistant/components/airthings_ble/sensor.py index afeaacc62f16..071bd1c1fa1d 100644 --- a/homeassistant/components/airthings_ble/sensor.py +++ b/homeassistant/components/airthings_ble/sensor.py @@ -169,7 +169,9 @@ PARALLEL_UPDATES = 0 @callback -def async_migrate(hass: HomeAssistant, address: str, sensor_name: str) -> None: +def async_migrate( + hass: HomeAssistant, entry_id: str, address: str, sensor_name: str +) -> None: """Migrate entities to new unique ids (with BLE Address).""" ent_reg = er.async_get(hass) unique_id_trailer = f"_{sensor_name}" @@ -179,8 +181,8 @@ def async_migrate(hass: HomeAssistant, address: str, sensor_name: str) -> None: return dev_reg = dr.async_get(hass) if not ( - device := dev_reg.async_get_device( - connections={(CONNECTION_BLUETOOTH, address)} + device := dev_reg.async_get_device_by_connection( + (CONNECTION_BLUETOOTH, address), entry_id ) ): return @@ -221,7 +223,7 @@ async def async_setup_entry( sensor_value, ) continue - async_migrate(hass, coordinator.data.address, sensor_type) + async_migrate(hass, entry.entry_id, coordinator.data.address, sensor_type) entities.append( AirthingsSensor( coordinator, coordinator.data, SENSORS_MAPPING_TEMPLATE[sensor_type] diff --git a/homeassistant/components/alexa_devices/coordinator.py b/homeassistant/components/alexa_devices/coordinator.py index 8f3bb1130596..5031537a940d 100644 --- a/homeassistant/components/alexa_devices/coordinator.py +++ b/homeassistant/components/alexa_devices/coordinator.py @@ -236,8 +236,8 @@ class AmazonDevicesCoordinator(DataUpdateCoordinator[dict[str, AmazonDevice]]): "Detected change in devices: serial %s removed", serial_num, ) - device = device_registry.async_get_device( - identifiers={(DOMAIN, serial_num)} + device = device_registry.async_get_device_by_identifier( + (DOMAIN, serial_num), self.config_entry.entry_id ) if device: device_registry.async_update_device( diff --git a/homeassistant/components/androidtv/diagnostics.py b/homeassistant/components/androidtv/diagnostics.py index e7f2cdb540c8..407964ebe134 100644 --- a/homeassistant/components/androidtv/diagnostics.py +++ b/homeassistant/components/androidtv/diagnostics.py @@ -35,8 +35,8 @@ async def async_get_config_entry_diagnostics( # Gather information how this AndroidTV device is represented in Home Assistant device_registry = dr.async_get(hass) entity_registry = er.async_get(hass) - hass_device = device_registry.async_get_device( - identifiers={(DOMAIN, str(entry.unique_id))} + hass_device = device_registry.async_get_device_by_identifier( + (DOMAIN, str(entry.unique_id)), entry.entry_id ) if not hass_device: return data diff --git a/homeassistant/components/anthropic/__init__.py b/homeassistant/components/anthropic/__init__.py index 05f30d960b90..e70e5719894d 100644 --- a/homeassistant/components/anthropic/__init__.py +++ b/homeassistant/components/anthropic/__init__.py @@ -106,8 +106,8 @@ async def async_migrate_integration(hass: HomeAssistant) -> None: DOMAIN, entry.entry_id, ) - device = device_registry.async_get_device( - identifiers={(DOMAIN, entry.entry_id)} + device = device_registry.async_get_device_by_identifier( + (DOMAIN, entry.entry_id), entry.entry_id ) if conversation_entity_id is not None: diff --git a/homeassistant/components/aqvify/coordinator.py b/homeassistant/components/aqvify/coordinator.py index 65f064e29c39..d1af79a5c647 100644 --- a/homeassistant/components/aqvify/coordinator.py +++ b/homeassistant/components/aqvify/coordinator.py @@ -126,8 +126,9 @@ class AqvifyCoordinator(DataUpdateCoordinator[AqvifyCoordinatorData]): account_id = self.config_entry.unique_id device_registry = dr.async_get(self.hass) for device_id in stale_devices: - device = device_registry.async_get_device( - identifiers={(DOMAIN, f"{account_id}_{device_id}")} + device = device_registry.async_get_device_by_identifier( + (DOMAIN, f"{account_id}_{device_id}"), + self.config_entry.entry_id, ) if device: device_registry.async_update_device( diff --git a/homeassistant/components/bosch_shc/entity.py b/homeassistant/components/bosch_shc/entity.py index 91484bc9b6fd..4b70eee92789 100644 --- a/homeassistant/components/bosch_shc/entity.py +++ b/homeassistant/components/bosch_shc/entity.py @@ -17,7 +17,9 @@ async def async_remove_devices( ) -> None: """Get item that is removed from session.""" dev_registry = dr.async_get(hass) - device = dev_registry.async_get_device(identifiers={(DOMAIN, entity.device_id)}) + device = dev_registry.async_get_device_by_identifier( + (DOMAIN, entity.device_id), entry_id + ) if device is not None: dev_registry.async_update_device(device.id, remove_config_entry_id=entry_id) diff --git a/homeassistant/components/broadlink/device.py b/homeassistant/components/broadlink/device.py index 3bc403a706a3..4124276b1327 100644 --- a/homeassistant/components/broadlink/device.py +++ b/homeassistant/components/broadlink/device.py @@ -81,8 +81,8 @@ class BroadlinkDevice[_ApiT: blk.Device = blk.Device]: """ device_registry = dr.async_get(hass) assert entry.unique_id - device_entry = device_registry.async_get_device( - identifiers={(DOMAIN, entry.unique_id)} + device_entry = device_registry.async_get_device_by_identifier( + (DOMAIN, entry.unique_id), entry.entry_id ) assert device_entry device_registry.async_update_device(device_entry.id, name=entry.title) diff --git a/homeassistant/components/comelit/coordinator.py b/homeassistant/components/comelit/coordinator.py index 374498631fcf..50948d8e36cd 100644 --- a/homeassistant/components/comelit/coordinator.py +++ b/homeassistant/components/comelit/coordinator.py @@ -145,8 +145,8 @@ class ComelitBaseCoordinator(DataUpdateCoordinator[T]): i, ) identifier = f"{self.config_entry.entry_id}-{dev_type}-{i}" - device = device_registry.async_get_device( - identifiers={(DOMAIN, identifier)} + device = device_registry.async_get_device_by_identifier( + (DOMAIN, identifier), self.config_entry.entry_id ) if device: device_registry.async_update_device( diff --git a/homeassistant/components/comelit/utils.py b/homeassistant/components/comelit/utils.py index 30f5d691f410..37b342b9695a 100644 --- a/homeassistant/components/comelit/utils.py +++ b/homeassistant/components/comelit/utils.py @@ -78,7 +78,9 @@ def _async_remove_state_config_entry_from_devices( device_registry = dr.async_get(hass) for identifier in identifiers: - device = device_registry.async_get_device(identifiers={(DOMAIN, identifier)}) + device = device_registry.async_get_device_by_identifier( + (DOMAIN, identifier), config_entry.entry_id + ) if device: _LOGGER.info( "Removing config entry %s from device %s", diff --git a/homeassistant/components/deconz/services.py b/homeassistant/components/deconz/services.py index c06112f820a2..2b87d97416d7 100644 --- a/homeassistant/components/deconz/services.py +++ b/homeassistant/components/deconz/services.py @@ -187,8 +187,8 @@ async def async_remove_orphaned_entries_service(hub: DeconzHub) -> None: ] # Don't remove the Gateway service entry - hub_service = device_registry.async_get_device( - identifiers={(DOMAIN, hub.api.config.bridge_id)} + hub_service = device_registry.async_get_device_by_identifier( + (DOMAIN, hub.api.config.bridge_id), hub.config_entry.entry_id ) if hub_service and hub_service.id in devices_to_be_removed: devices_to_be_removed.remove(hub_service.id) diff --git a/homeassistant/components/devolo_home_network/coordinator.py b/homeassistant/components/devolo_home_network/coordinator.py index b4196fbd90db..28a64eb60cd8 100644 --- a/homeassistant/components/devolo_home_network/coordinator.py +++ b/homeassistant/components/devolo_home_network/coordinator.py @@ -44,6 +44,8 @@ type DevoloHomeNetworkConfigEntry = ConfigEntry[DevoloHomeNetworkData] class DevoloDataUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]): """Class to manage fetching data from devolo Home Network devices.""" + config_entry: DevoloHomeNetworkConfigEntry + def __init__( self, hass: HomeAssistant, @@ -86,8 +88,8 @@ class DevoloDataUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]): """Update device registry with new firmware version.""" device_registry = dr.async_get(self.hass) if ( - device_entry := device_registry.async_get_device( - identifiers={(DOMAIN, self.device.serial_number)} + device_entry := device_registry.async_get_device_by_identifier( + (DOMAIN, self.device.serial_number), self.config_entry.entry_id ) ) and device_entry.sw_version != self.device.firmware_version: device_registry.async_update_device( diff --git a/homeassistant/components/dsmr/sensor.py b/homeassistant/components/dsmr/sensor.py index e4340404a1b4..71e39af38713 100644 --- a/homeassistant/components/dsmr/sensor.py +++ b/homeassistant/components/dsmr/sensor.py @@ -608,7 +608,9 @@ def rename_old_gas_to_mbus( """Rename old gas sensor to mbus variant.""" dev_reg = dr.async_get(hass) for dev_id in (mbus_device_id, entry.entry_id): - device_entry_v1 = dev_reg.async_get_device(identifiers={(DOMAIN, dev_id)}) + device_entry_v1 = dev_reg.async_get_device_by_identifier( + (DOMAIN, dev_id), entry.entry_id + ) if device_entry_v1 is not None: device_id = device_entry_v1.id diff --git a/homeassistant/components/duco/sensor.py b/homeassistant/components/duco/sensor.py index 63e8f16cd27d..b15a489cef2e 100644 --- a/homeassistant/components/duco/sensor.py +++ b/homeassistant/components/duco/sensor.py @@ -258,8 +258,8 @@ async def async_setup_entry( device_reg = dr.async_get(hass) mac = entry.unique_id for node_id in stale_node_ids: - device = device_reg.async_get_device( - identifiers={(DOMAIN, f"{mac}_{node_id}")} + device = device_reg.async_get_device_by_identifier( + (DOMAIN, f"{mac}_{node_id}"), entry.entry_id ) if device: device_reg.async_update_device( diff --git a/homeassistant/components/dwd_weather_warnings/__init__.py b/homeassistant/components/dwd_weather_warnings/__init__.py index 67818456dbe3..f9df009c52f0 100644 --- a/homeassistant/components/dwd_weather_warnings/__init__.py +++ b/homeassistant/components/dwd_weather_warnings/__init__.py @@ -12,7 +12,9 @@ async def async_setup_entry( ) -> bool: """Set up a config entry.""" device_registry = dr.async_get(hass) - if device_registry.async_get_device(identifiers={(DOMAIN, entry.entry_id)}): + if device_registry.async_get_device_by_identifier( + (DOMAIN, entry.entry_id), entry.entry_id + ): device_registry.async_clear_config_entry(entry.entry_id, entry.domain) coordinator = DwdWeatherWarningsCoordinator(hass, entry) await coordinator.async_config_entry_first_refresh() diff --git a/homeassistant/components/earn_e_p1/coordinator.py b/homeassistant/components/earn_e_p1/coordinator.py index 01866aa049f5..18e3abc52f81 100644 --- a/homeassistant/components/earn_e_p1/coordinator.py +++ b/homeassistant/components/earn_e_p1/coordinator.py @@ -20,6 +20,8 @@ _LOGGER = logging.getLogger(__name__) class EarnEP1Coordinator(DataUpdateCoordinator[dict[str, Any]]): """Coordinator for the EARN-E P1 Meter.""" + config_entry: EarnEP1ConfigEntry + def __init__( self, hass: HomeAssistant, @@ -49,8 +51,8 @@ class EarnEP1Coordinator(DataUpdateCoordinator[dict[str, Any]]): self.sw_version = device.sw_version device_registry = dr.async_get(self.hass) if ( - device_entry := device_registry.async_get_device( - identifiers={(DOMAIN, self.identifier)} + device_entry := device_registry.async_get_device_by_identifier( + (DOMAIN, self.identifier), self.config_entry.entry_id ) ) is not None: device_registry.async_update_device( diff --git a/homeassistant/components/enphase_envoy/coordinator.py b/homeassistant/components/enphase_envoy/coordinator.py index 3549c2f7c3c8..e6b4145bdcee 100644 --- a/homeassistant/components/enphase_envoy/coordinator.py +++ b/homeassistant/components/enphase_envoy/coordinator.py @@ -216,13 +216,8 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): # Add to or update device registry connections as needed device_registry = dr.async_get(self.hass) - envoy_device = device_registry.async_get_device( - identifiers={ - ( - DOMAIN, - self.envoy_serial_number, - ) - } + envoy_device = device_registry.async_get_device_by_identifier( + (DOMAIN, self.envoy_serial_number), self.config_entry.entry_id ) if envoy_device is None: _LOGGER.error( diff --git a/homeassistant/components/epson/media_player.py b/homeassistant/components/epson/media_player.py index 1b1b442c384d..bd103c567521 100644 --- a/homeassistant/components/epson/media_player.py +++ b/homeassistant/components/epson/media_player.py @@ -106,7 +106,9 @@ class EpsonProjectorMediaPlayer(MediaPlayerEntity): if old_entity_id is not None: ent_reg.async_update_entity(old_entity_id, new_unique_id=uid) dev_reg = dr.async_get(self.hass) - device = dev_reg.async_get_device({(DOMAIN, self._entry.entry_id)}) + device = dev_reg.async_get_device_by_identifier( + (DOMAIN, self._entry.entry_id), self._entry.entry_id + ) if device is not None: dev_reg.async_update_device(device.id, new_identifiers={(DOMAIN, uid)}) self.hass.async_create_task(