Hi, I’m working on an integration. In sensor.py
I have an async_setup_entry
that looks like this:
async def async_setup_entry(hass, entry, async_add_devices):
"""Set up the sensor platform."""
devices = [OccupancyEntity(
entity_description=entity_description,
client=hass.data[DOMAIN][entry.entry_id],
location_name=location['name'],
) for (location, entity_description) in _get_location_description_pairs()]
async_add_devices(devices, update_before_add=True)
The Occupancy#__init__()
function sets self._attr_device_info
like this:
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
name=f"{location_name}",
)
This code creates 4 entities with DeviceInfo
, so I’d expect 4 devices and 4 entities. When I install the integration, all 4 entities are created as expected, but only some get a device created as well (usually only 1, sometimes 2 or 3 get created as well - seems to be some sort of race condition).
Once it’s in this state, if I restart home assistant the devices get created as expected.
I have also tried the following:
- If I just do
async_add_devices(devices)
(noupdate_before_add
) all devices and entities are created as expected, but obviously the state isn’t populated until the next poll interval, which isn’t ideal for my use case. - I’ve tried calling
async_add_devices
once per device (withupdate_before_add
). This results in similar behaviour (entities created but not all devices)
Does anybody have any ideas on how I can create these devices WITH initial data populated from the data source AND having all device registry entries created as expected?