Migrate integrations to async_get_device_by_identifier (part 1) (#176901)

This commit is contained in:
Erik Montnemery
2026-07-20 23:13:01 +02:00
committed by GitHub
parent f5093afa58
commit 860ac1bf51
19 changed files with 52 additions and 40 deletions
@@ -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(
+2 -2
View File
@@ -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(
(
@@ -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]
@@ -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(
@@ -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
@@ -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:
@@ -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(
+3 -1
View File
@@ -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)
+2 -2
View File
@@ -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)
@@ -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(
+3 -1
View File
@@ -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",
+2 -2
View File
@@ -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)
@@ -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(
+3 -1
View File
@@ -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
+2 -2
View File
@@ -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(
@@ -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()
@@ -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(
@@ -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(
@@ -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(