Remove old unique id converter for UniFi device tracker entities (#170516)

This commit is contained in:
Robert Svensson
2026-05-13 21:03:57 +02:00
committed by GitHub
parent 793018e0d6
commit 74af1fd5d4
2 changed files with 2 additions and 32 deletions
@@ -17,18 +17,15 @@ from aiounifi.models.event import Event, EventKey
from propcache.api import cached_property
from homeassistant.components.device_tracker import (
DOMAIN as DEVICE_TRACKER_DOMAIN,
ScannerEntity,
ScannerEntityDescription,
)
from homeassistant.core import Event as core_Event, HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.util import dt as dt_util
from . import UnifiConfigEntry
from .const import DOMAIN
from .entity import UnifiEntity, UnifiEntityDescription, async_device_available_fn
from .hub import UnifiHub
@@ -185,39 +182,12 @@ ENTITY_DESCRIPTIONS: tuple[UnifiTrackerEntityDescription, ...] = (
)
@callback
def async_update_unique_id(hass: HomeAssistant, config_entry: UnifiConfigEntry) -> None:
"""Normalize client unique ID to have a prefix rather than suffix.
Introduced with release 2023.12.
"""
hub = config_entry.runtime_data
ent_reg = er.async_get(hass)
@callback
def update_unique_id(obj_id: str) -> None:
"""Rework unique ID."""
new_unique_id = f"{hub.site}-{obj_id}"
if ent_reg.async_get_entity_id(DEVICE_TRACKER_DOMAIN, DOMAIN, new_unique_id):
return
unique_id = f"{obj_id}-{hub.site}"
if entity_id := ent_reg.async_get_entity_id(
DEVICE_TRACKER_DOMAIN, DOMAIN, unique_id
):
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
for obj_id in list(hub.api.clients) + list(hub.api.clients_all):
update_unique_id(obj_id)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: UnifiConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up device tracker for UniFi Network integration."""
async_update_unique_id(hass, config_entry)
config_entry.runtime_data.entity_loader.register_platform(
async_add_entities, UnifiScannerEntity, ENTITY_DESCRIPTIONS
)
@@ -586,10 +586,10 @@ async def test_restoring_client(
clients_all_payload: list[dict[str, Any]],
) -> None:
"""Verify clients are restored from clients_all if they ever was registered to entity registry."""
entity_registry.async_get_or_create( # Make sure unique ID converts to site_id-mac
entity_registry.async_get_or_create(
TRACKER_DOMAIN,
DOMAIN,
f"{clients_all_payload[0]['mac']}-site_id",
f"site_id-{clients_all_payload[0]['mac']}",
suggested_object_id=clients_all_payload[0]["hostname"],
config_entry=config_entry,
)