Integration __init__.py async_setup methodology on failures and retries

Looking for advice on the best practice here and proposed solution

Use Case: the async_setup routine is communicating with a cloud service; which may be unreachable for a variety of reasons (service unavailable, internet connection down, etc.). I would like the integration to continue to retry but not block startup of HA.

Currently, if the communication succeeds I call:

hass.helpers.discovery.load_platform('climate', DOMAIN, s30api, config)

Which then loads the climate entities.

Proposed solution: spin off an async task via

asyncio.create_task(myretrytask())

and, have it call

hass.helpers.discovery.load_platform(....)

if it succeeds in connecting.

Is that valid or can load_platform only be called during the startup phase?

Better solutions and/or reference examples?

I have usually tried to work around this type of issue by implementing a manager for my integration that is just initialised during async_setup or async_setup_entry and which then takes care of the communication with the external service and generates entities etc.

Maybe this can give you some inspiration?

Thank you, this looks super helpful. I am on a similar path, with an api manager. But at a more primitive level of HA integration. I’ll use this as a reference.

And since it uses a free public web service, so I’ll be able to watch it in the debugger!

1 Like