Skip to content
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

Use entry.async_create_background_task to get HA to manage the task lifecycle #1919

Open
make-all opened this issue May 15, 2024 · 1 comment
Labels
maintenance Changes to keep up with HA development

Comments

@make-all
Copy link
Owner

From #1917

You may want to consider switching to entry.async_create_background_task (example: https://github.com/home-assistant/core/blob/b84829f70fcd8dfb73cbd9829cf962fc029f0d8e/homeassistant/components/homekit_controller/connection.py#L352) since the task will automatically be cancelled when the config entry is unloaded. https://github.com/home-assistant/core/blob/b84829f70fcd8dfb73cbd9829cf962fc029f0d8e/homeassistant/config_entries.py#L1133

You'll have the pass the config entry to the entity when you create the object.

You can still use hass.async_create_background_task https://github.com/home-assistant/core/blob/2590db1b6dc253e4d80b3fada5b051512be1b21a/homeassistant/core.py#L843 in the mean time if you don't want to refactor. If you use that one instead of entry.async_create_background_task, you'll need make sure the task is cancelled yourself when the entry is unloaded or the entity is removed.

@make-all make-all added the bug Something isn't working label May 15, 2024
@make-all
Copy link
Owner Author

Also, using async_create_background_task instead of async_create_task will allow the task to be started during startup again, and not from EVENT_HOMEASSISTANT_STARTED, since startup does not block on background tasks. This should simplify the code.

@make-all make-all added maintenance Changes to keep up with HA development and removed bug Something isn't working labels May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Changes to keep up with HA development
Projects
Status: 🔖 Ready
Development

No branches or pull requests

1 participant