mirror of
https://github.com/home-assistant/core.git
synced 2026-08-07 22:05:35 +01:00
Migrate integrations to async_get_device_by_identifier (part 4) (#176908)
This commit is contained in:
@@ -117,8 +117,9 @@ def _async_register_base_station(
|
||||
)
|
||||
|
||||
# Check for an old system ID format and remove it:
|
||||
if old_base_station := device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, system.system_id)} # type: ignore[arg-type]
|
||||
if old_base_station := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, system.system_id), # type: ignore[arg-type]
|
||||
entry.entry_id,
|
||||
):
|
||||
# Update the new base station with any properties the user might have configured
|
||||
# on the old base station:
|
||||
|
||||
@@ -244,8 +244,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: SmartThingsConfigEntry)
|
||||
|
||||
def handle_deleted_device(device_id: str) -> None:
|
||||
"""Handle a deleted device."""
|
||||
dev_entry = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, device_id)},
|
||||
dev_entry = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, device_id), entry.entry_id
|
||||
)
|
||||
if dev_entry is not None:
|
||||
device_registry.async_update_device(
|
||||
@@ -587,7 +587,9 @@ def create_devices(
|
||||
if mac_connections:
|
||||
kwargs.setdefault(ATTR_CONNECTIONS, set()).update(mac_connections)
|
||||
if (
|
||||
device_registry.async_get_device({(DOMAIN, device.device.device_id)})
|
||||
device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, device.device.device_id), entry.entry_id
|
||||
)
|
||||
is None
|
||||
):
|
||||
kwargs.update(
|
||||
|
||||
@@ -102,8 +102,8 @@ class SmhiFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
)
|
||||
|
||||
device_reg = dr.async_get(self.hass)
|
||||
if device := device_reg.async_get_device(
|
||||
identifiers={(DOMAIN, f"{old_lat}, {old_lon}")}
|
||||
if device := device_reg.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{old_lat}, {old_lon}"), reconfigure_entry.entry_id
|
||||
):
|
||||
device_reg.async_update_device(
|
||||
device.id, new_identifiers={(DOMAIN, f"{lat}, {lon}")}
|
||||
|
||||
@@ -194,13 +194,12 @@ class SolarLogDeviceDataCoordinator(DataUpdateCoordinator[dict[int, InverterData
|
||||
if did == removed_device[0]:
|
||||
device_name = dn
|
||||
break
|
||||
if device := device_registry.async_get_device(
|
||||
identifiers={
|
||||
(
|
||||
DOMAIN,
|
||||
f"{self.config_entry.entry_id}_{slugify(device_name)}",
|
||||
)
|
||||
}
|
||||
if device := device_registry.async_get_device_by_identifier(
|
||||
(
|
||||
DOMAIN,
|
||||
f"{self.config_entry.entry_id}_{slugify(device_name)}",
|
||||
),
|
||||
self.config_entry.entry_id,
|
||||
):
|
||||
device_registry.async_update_device(
|
||||
device_id=device.id,
|
||||
|
||||
@@ -125,8 +125,8 @@ async def async_setup_entry(
|
||||
player = coordinator.player
|
||||
_LOGGER.debug("Setting up media_player device and entity for player %s", player)
|
||||
device_registry = dr.async_get(hass)
|
||||
server_device = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, coordinator.server_uuid)},
|
||||
server_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, coordinator.server_uuid), entry.entry_id
|
||||
)
|
||||
|
||||
name = player.name
|
||||
|
||||
@@ -64,7 +64,9 @@ async def async_migrate_entry(hass: HomeAssistant, entry: SteamConfigEntry) -> b
|
||||
hass.config_entries.async_add_subentry(entry, subentry)
|
||||
|
||||
dev_reg = dr.async_get(hass)
|
||||
if device := dev_reg.async_get_device({(DOMAIN, entry.entry_id)}):
|
||||
if device := dev_reg.async_get_device_by_identifier(
|
||||
(DOMAIN, entry.entry_id), entry.entry_id
|
||||
):
|
||||
if TYPE_CHECKING:
|
||||
assert entry.unique_id
|
||||
dev_reg.async_update_device(
|
||||
|
||||
@@ -67,7 +67,9 @@ class TailscaleDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
||||
device_registry = dr.async_get(self.hass)
|
||||
|
||||
for device_id in stale_device_ids:
|
||||
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
|
||||
device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, device_id), self.config_entry.entry_id
|
||||
)
|
||||
if device:
|
||||
LOGGER.debug("Removing stale device: %s", device_id)
|
||||
device_registry.async_remove_device(device.id)
|
||||
|
||||
@@ -148,8 +148,8 @@ class TedeeApiCoordinator(DataUpdateCoordinator[dict[int, TedeeLock]]):
|
||||
_LOGGER.debug("Removed locks: %s", ", ".join(map(str, removed_locks)))
|
||||
device_registry = dr.async_get(self.hass)
|
||||
for lock_id in removed_locks:
|
||||
if device := device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, str(lock_id))}
|
||||
if device := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, str(lock_id)), self.config_entry.entry_id
|
||||
):
|
||||
device_registry.async_update_device(
|
||||
device_id=device.id,
|
||||
|
||||
@@ -106,8 +106,8 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
||||
):
|
||||
device_registry = dr.async_get(self.hass)
|
||||
for device_id in stale_device_ids:
|
||||
device = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, device_id)}
|
||||
device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, device_id), self.config_entry.entry_id
|
||||
)
|
||||
if device:
|
||||
device_registry.async_update_device(
|
||||
|
||||
@@ -18,8 +18,8 @@ async def async_get_config_entry_diagnostics(
|
||||
device_registry = dr.async_get(hass)
|
||||
device = cast(
|
||||
dr.DeviceEntry,
|
||||
device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, entry.data[CONF_GATEWAY_ID])}
|
||||
device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, entry.data[CONF_GATEWAY_ID]), entry.entry_id
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -59,8 +59,8 @@ class TRMNLCoordinator(DataUpdateCoordinator[dict[int, Device]]):
|
||||
if self.data is not None:
|
||||
device_registry = dr.async_get(self.hass)
|
||||
for device_id in set(self.data) - set(new_data):
|
||||
if entry := device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, str(device_id))}
|
||||
if entry := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, str(device_id)), self.config_entry.entry_id
|
||||
):
|
||||
device_registry.async_update_device(
|
||||
device_id=entry.id,
|
||||
|
||||
@@ -163,8 +163,8 @@ class DeviceListener(SharingDeviceListener):
|
||||
def async_remove_device(self, device_id: str) -> None:
|
||||
"""Remove device from Home Assistant."""
|
||||
device_registry = dr.async_get(self.hass)
|
||||
device_entry = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, device_id)}
|
||||
device_entry = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, device_id), self._entry.entry_id
|
||||
)
|
||||
if device_entry is not None:
|
||||
device_registry.async_remove_device(device_entry.id)
|
||||
|
||||
@@ -64,8 +64,8 @@ async def async_migrate_entry(hass: HomeAssistant, entry: TwinklyConfigEntry) ->
|
||||
entity_entry.entity_id, new_unique_id=device_info["mac"]
|
||||
)
|
||||
device_registry = dr.async_get(hass)
|
||||
device_entry = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, identifier)}
|
||||
device_entry = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, identifier), entry.entry_id
|
||||
)
|
||||
if device_entry:
|
||||
device_registry.async_update_device(
|
||||
|
||||
@@ -105,8 +105,8 @@ class TwinklyCoordinator(DataUpdateCoordinator[TwinklyData]):
|
||||
def _async_update_device_info(self, name: str) -> None:
|
||||
"""Update the device info."""
|
||||
device_registry = dr.async_get(self.hass)
|
||||
device = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, self.data.device_info["mac"])},
|
||||
device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, self.data.device_info["mac"]), self.config_entry.entry_id
|
||||
)
|
||||
if device:
|
||||
device_registry.async_update_device(
|
||||
|
||||
@@ -128,8 +128,9 @@ def async_migrate_entities_unique_ids(
|
||||
# migrate device identifiers and update version
|
||||
device_reg = dr.async_get(hass)
|
||||
for monitor in metrics.values():
|
||||
if device := device_reg.async_get_device(
|
||||
{(DOMAIN, f"{coordinator.config_entry.entry_id}_{monitor.monitor_name!s}")}
|
||||
if device := device_reg.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{coordinator.config_entry.entry_id}_{monitor.monitor_name!s}"),
|
||||
coordinator.config_entry.entry_id,
|
||||
):
|
||||
new_identifier = {
|
||||
(DOMAIN, f"{coordinator.config_entry.entry_id}_{monitor.monitor_id!s}")
|
||||
@@ -139,8 +140,9 @@ def async_migrate_entities_unique_ids(
|
||||
new_identifiers=new_identifier,
|
||||
sw_version=coordinator.api.version.version,
|
||||
)
|
||||
if device := device_reg.async_get_device(
|
||||
{(DOMAIN, f"{coordinator.config_entry.entry_id}_update")}
|
||||
if device := device_reg.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{coordinator.config_entry.entry_id}_update"),
|
||||
coordinator.config_entry.entry_id,
|
||||
):
|
||||
device_reg.async_update_device(
|
||||
device.id,
|
||||
|
||||
@@ -69,8 +69,8 @@ class UptimeRobotDataUpdateCoordinator(
|
||||
device_registry = dr.async_get(self.hass)
|
||||
|
||||
for monitor_id in stale_ids:
|
||||
if device := device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, str(monitor_id))}
|
||||
if device := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, str(monitor_id)), self.config_entry.entry_id
|
||||
):
|
||||
device_registry.async_update_device(
|
||||
device_id=device.id,
|
||||
|
||||
@@ -100,8 +100,8 @@ async def async_migrate_integration(hass: HomeAssistant) -> None:
|
||||
entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
|
||||
if TYPE_CHECKING:
|
||||
assert entry.unique_id is not None
|
||||
device = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, entry.unique_id)}
|
||||
device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, entry.unique_id), entry.entry_id
|
||||
)
|
||||
|
||||
for entity_entry in entities:
|
||||
|
||||
@@ -148,7 +148,9 @@ class WattsVisionHubCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
||||
for device_id in stale_device_ids:
|
||||
_LOGGER.info("Removing stale device: %s", device_id)
|
||||
|
||||
device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
|
||||
device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, device_id), self.config_entry.entry_id
|
||||
)
|
||||
if device:
|
||||
device_registry.async_update_device(
|
||||
device_id=device.id,
|
||||
|
||||
@@ -168,7 +168,9 @@ def _reattach_device_to_hub(
|
||||
device_registry = dr.async_get(hass)
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
device = device_registry.async_get_device(identifiers={(DOMAIN, str(device_id))})
|
||||
device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, str(device_id)), source_entry.entry_id
|
||||
)
|
||||
if device is None:
|
||||
return
|
||||
|
||||
|
||||
@@ -160,14 +160,18 @@ async def async_migrate_entry(hass: HomeAssistant, entry: XboxConfigEntry) -> bo
|
||||
)
|
||||
hass.config_entries.async_add_subentry(entry, subentry)
|
||||
|
||||
if device := dev_reg.async_get_device({(DOMAIN, friend.xuid)}):
|
||||
if device := dev_reg.async_get_device_by_identifier(
|
||||
(DOMAIN, friend.xuid), entry.entry_id
|
||||
):
|
||||
dev_reg.async_update_device(
|
||||
device.id,
|
||||
remove_config_entry_id=entry.entry_id,
|
||||
add_config_subentry_id=subentry.subentry_id,
|
||||
add_config_entry_id=entry.entry_id,
|
||||
)
|
||||
if device := dev_reg.async_get_device({(DOMAIN, "xbox_live")}):
|
||||
if device := dev_reg.async_get_device_by_identifier(
|
||||
(DOMAIN, "xbox_live"), entry.entry_id
|
||||
):
|
||||
dev_reg.async_update_device(
|
||||
device.id, new_identifiers={(DOMAIN, client.xuid)}
|
||||
)
|
||||
|
||||
@@ -80,8 +80,8 @@ class YoLinkHomeMessageListener(MessageListener):
|
||||
and msg_data.get("event") is not None
|
||||
):
|
||||
device_registry = dr.async_get(self._hass)
|
||||
device_entry = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, device_coordinator.device.device_id)}
|
||||
device_entry = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, device_coordinator.device.device_id), self._entry.entry_id
|
||||
)
|
||||
if device_entry is None:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user