Only poll HomeKit connection once for all entities on a single bridge/pairing#25249
Conversation
| # pylint: disable=not-callable | ||
| update_fn(result['value']) | ||
|
|
||
| self.async_schedule_update_ha_state() |
There was a problem hiding this comment.
Is this called from async? because you're calling it from a sync method?
If it's called from async and you don't need any work done in async_update, you could also use self.async_write_ha_state()
There was a problem hiding this comment.
It's non blocking code thats called from an async method elsewhere. Should i call it async_handle_new_values to make thise clear?
I will keep this in mind as I implement @MartinHjelmare suggestions.
There was a problem hiding this comment.
Have switched to async_write_ha_state and its now called from an explicitly async method.
|
|
||
| await self.async_update() | ||
|
|
||
| async_track_time_interval( |
There was a problem hiding this comment.
I notice that you don't track any of the unsub functions for tracking time interval. It is needed to support removing the config entry (but I haven't looked if HomeKit supports that)
There was a problem hiding this comment.
It currently doesn’t support it, but I’ll keep that in mind when I add support
There was a problem hiding this comment.
Polling is now turned off when an config entry is unloaded.
|
Thanks for the great feedback as ever @MartinHjelmare 😁 👍 @balloob are you happy your comments are addressed as well? If so i'll go ahead and squash/merge this onto dev. |
Description:
This moves the bulk of HomeKitEntity.async_update to the connection object. It is then run once for all entities on the same connection, rather than once for each entity. For example, if you have 15 bridges with 5 sensors each, right now we will need to execute 75 polls on the thread pool. This is a setup a real user has (#25178). If some of these sensors were low power enough and don't have multi-threaded HTTP implementations some of these polls could end up being serialized, and block other threads from starting.
This is also groundwork for moving from polling to event based updates - we'll hold open one connection to the bridge rather than one for each entity.
Checklist:
tox. Your PR cannot be merged unless tests pass