Ensure entity update is scheduled with event loop #172
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was getting errors regarding non-thread-safe operations in the BI entity update code in the latest version. It was filling my logs with massive stack traces each time an update was triggered. I narrowed it down to the fact that
homeassistant.helpers.event.async_track_time_interval
, which is used for scheduling BI updates, can accept either a callable or an awaitable. If a callable, it's scheduled in a new thread/threadpool that's not coordinated with the Home Assistant async event loop. Therefore, in_update_entities
when you callself._hass.async_create_task(self.async_update(now))
it's scheduling an async task cross-thread with no event loop. To fix this, I turned_update_entities
into an awaitable method and verified that the callback is run with an async event loop.I believe the code works fine without change, but there may be a small thread safety risk and it really fills up the logs.
System: