What is the proper way for an integration to wait until everything else has loaded before loading itself?

Hello Home Assistant pros, I am the developer of a custom component called Magic Areas which relies on your existing entities to do its thing.

One of the features creates groups for sensors, so if you have 3 motion sensors in an area it will create a group with those 3 motion sensors as an “aggregate” sensor. That is one example of why Magic Areas needs to have all your entities loaded already when it starts up.

For this, I accomplish that by having this on my async_setup_entry code:


    # Wait for Hass to have started before setting up.
    if hass.is_running:
        hass.create_task(_async_setup_integration())
    else:
        hass.bus.async_listen_once(
            EVENT_HOMEASSISTANT_STARTED, _async_setup_integration
        )

Source hass-magic_areas/custom_components/magic_areas/__init__.py at 80ac446f6e97b50acc01da6010fa1d3b3c89bc17 · jseidl/hass-magic_areas · GitHub

There’s also the concept of meta areas, like “Global” or “Exterior” which depends on entities of the other general Magic Areas.

I’m working on an update where those meta-areas gets automatically reloaded if one of the underlying entries are reloaded, to pick up any new configuration.

Using my current code of waiting for home assistant to start, whenever the reload is triggered, Home Assistant complains that the platforms are already loaded (looks like my waiting for home assistant to initialize confuses the stack somehow).

This is solved by skipping this block and just calling

await _async_setup_integration()

Instead. But evidently this creates the race condition where not everything is loaded.

I’ve added a bunch of platforms to the after_dependencies section on my manifest but that seems to have zero effect, as this only guarantees they’re loaded but not that all the entities have been created yet on those platforms.

Before I start making some horrible war crimes on code, I wanted to get your opinion so I can implement this right. Looking forward to learning from you guys, thanks in advance! Let me know if you need any other information on my side.

Current reload logic: hass-magic_areas/custom_components/magic_areas/base/magic.py at hotfix/reload-meta-area-on-new-area-load · jseidl/hass-magic_areas · GitHub

1 Like

What about, you go and initialize the integration without being worried about anything first and after home assistant started event, you initialize your sensors with a reload?

Doing that will fill the log with errors. e.g. this two and a half year old open issue:

Seems that this issue in particular can be prevented if the rest of the integration is “prepared” for the entity not being there. (Since the log entries flooding the logs are from the integration itself)

I have such logs in Magic Areas that are currently warnings but if that is expected I can downgrade them to debug.

Still, @fuatakgun’s idea is worth a try since all I have to do is re-level some log entries and add a listener to HA start to reload.

Thank you all so far for the input. Really appreciate it.

1 Like

Please keep us posted