New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do async_setup_platform in background #36244
Conversation
@@ -273,7 +273,7 @@ async def preload_stream(hass, _): | |||
|
|||
request_stream(hass, source, keepalive=True, options=camera.stream_options) | |||
|
|||
async_when_setup(hass, DOMAIN_STREAM, preload_stream) | |||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, preload_stream) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't the old approach good? I'm just curious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bug. It needs to iterate over the entities. When stream
is set up it's not ready with adding entities. So listening to START
at least guarantees all platforms have set up their entities. It's still not perfect as camera entities might be loaded after start that also have preload stream activated…
Regarding bullet number 2 in the description: In the entity platform helper setup we do wait for the entity addition that is done as part of the platform setup. If an entity is added later, that is not waited for. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah make sense and make it real async
Startup was slightly faster on my test install. Its mostly I/O bound / python load time (primarily the ecdsa module since we don't have gmpy2 installed and are doing the math in pure python) at this point. |
The slow setup wait message now only shows what I would expect instead of climate and sensor being included as they are no longer blocked. 👍
|
You're right Martin. I have removed that code now :) |
Still working as expected for me
Tests are almost in good shape |
Are there any integrations that rely on waiting for the platform setup to add its entities before starting another task? Eg by awaiting forwarding the config entry. This is a breaking change for integrations and most integrations don't have full test coverage. |
That would not have been possible. Discory/forward is always done with async_create_task because of the circular dependency where There are integrations that keep track when all platforms are loaded, but that will work the same as we are still waiting for all these tasks to finish. |
It's possible as you mention in the second paragraph. That's what I mean. I don't think it's the same as now we can only wait for home assistant start. So integrations that assume that entities will be loaded when the platform setup is done may start the next task too early. |
The only thing that is now being run in the background compared to before is users explicit platform configuration: light:
platform: bla Discovery mechanism and forward config entry has not been changed, as it was already in the background. |
The important change is that the platform setup doesn't wait for entities now. Integrations that rely on that will experience breakage. |
Integrations are unable to load their own platforms from async_setup because of a circular dependency (platforms require component to be set up). So this would only break if an integration relies on another integration that is exposing entities. We don't have any. All our integrations providing automations do so in a generic manner and don't depend on setting up a specific integration. I don't foresee this to be a problem. |
To be clear: In the existing code there's a straight await dependency from the entity component setup to the platform setup. There's no background scheduling in that line. We do need to schedule to the background the entry forwarding that starts the entity component setup but there's nothing stopping us from having another task wait for the forwarding to be done. If we assume that entities will be added when the entry forwarding is done, and we rely on that to start our second task, this PR will break that assumption. |
Hmm maybe |
Entry forwarding is not impacting by this PR. Forwarding an entry ends up calling This only impacts loading user supplied YAML platform config. |
Fixed the websocket tests for me. Assumption is that the
|
Looks like one more here (but didn't fail locally for me)
|
The websocket api ones are stumping me. |
🤞 I think the last push on #36304 will fix it. Any time we call |
Got a clean run on #36304. Taking out all the debug prints and pushing again |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
161 files. That was a bit of a death march. Nice work 👍
3.7 tests passed, for some reason it remade the 3.8 env so its taking longer |
💃 💃 💃 💃 💃 💃 💃 |
Breaking change
Proposed change
Make
EntityComponent.async_setup(…)
no longer wait untilplatform.async_setup_platform(…)
is done.This makes sense for various reasons:
light
) to wait for thisasync_setup_platform
to finish, butasync_add_entities
was not part of what was awaited for.hass.async_create_task
so we still make sure they are finished before we finish startup.I expect an even faster startup time now :)
It could happen previously that an integration was forwarding their config entry or making a discovery for
sensor
integration and then had to wait forsensor
to set up all its platforms first (for no good reason).CC @bdraco
Type of change
Example entry for
configuration.yaml
:# Example configuration.yaml
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.The integration reached or maintains the following Integration Quality Scale: