Skip to content

Only poll HomeKit connection once for all entities on a single bridge/pairing#25249

Merged
balloob merged 10 commits into
home-assistant:devfrom
Jc2k:homekit_controller_central_fetch
Jul 22, 2019
Merged

Only poll HomeKit connection once for all entities on a single bridge/pairing#25249
balloob merged 10 commits into
home-assistant:devfrom
Jc2k:homekit_controller_central_fetch

Conversation

@Jc2k
Copy link
Copy Markdown
Member

@Jc2k Jc2k commented Jul 18, 2019

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:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the [development checklist][dev-checklist]

@ghost ghost assigned Jc2k Jul 18, 2019
@Jc2k Jc2k changed the title Only poll connection once for all entities on a single bridge/pairing Only poll HomeKit connection once for all entities on a single bridge/pairing Jul 18, 2019
# pylint: disable=not-callable
update_fn(result['value'])

self.async_schedule_update_ha_state()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have switched to async_write_ha_state and its now called from an explicitly async method.


await self.async_update()

async_track_time_interval(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It currently doesn’t support it, but I’ll keep that in mind when I add support

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polling is now turned off when an config entry is unloaded.

Comment thread homeassistant/components/homekit_controller/connection.py Outdated
Comment thread homeassistant/components/homekit_controller/__init__.py Outdated
Comment thread homeassistant/components/homekit_controller/__init__.py Outdated
Comment thread homeassistant/components/homekit_controller/__init__.py Outdated
Comment thread tests/components/homekit_controller/test_light.py Outdated
Comment thread tests/components/homekit_controller/test_light.py
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@Jc2k
Copy link
Copy Markdown
Member Author

Jc2k commented Jul 21, 2019

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.

@balloob balloob merged commit 8c69fd9 into home-assistant:dev Jul 22, 2019
@Jc2k Jc2k deleted the homekit_controller_central_fetch branch July 22, 2019 16:25
@lock lock Bot locked and limited conversation to collaborators Jul 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants