Fix flaky unifi device_tracker entity race on setup (#169359)

This commit is contained in:
epenet
2026-04-29 08:55:10 +02:00
committed by GitHub
parent e975496145
commit b56cdb9106
+14 -1
View File
@@ -6,7 +6,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.storage import Store
from homeassistant.helpers.typing import ConfigType
@@ -51,6 +51,19 @@ async def async_setup_entry(
hub = config_entry.runtime_data = UnifiHub(hass, config_entry, api)
await hub.initialize()
# Pre-populate device registry with UniFi devices before forwarding to
# platforms. Without this, device_tracker entities may be registered as
# disabled-by-default if their platform is set up before another platform
# creates the device entry, since their default enabled state depends on
# the matching device existing in the registry. Other fields are populated
# when entities with DeviceInfo are added by their respective platforms.
device_registry = dr.async_get(hass)
for device in hub.api.devices.values():
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, device.mac)},
)
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
hub.async_update_device_registry()
hub.entity_loader.load_entities()