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

Add storage for cacheable homekit entity maps. #23191

Merged
merged 1 commit into from
Apr 18, 2019

Conversation

Jc2k
Copy link
Member

@Jc2k Jc2k commented Apr 18, 2019

Description:

Non-breaking change to cache the output of /accessories in a storage. This data should be refreshed when a change is announced by the device (the c# zeroconf field for IP, the same field in the Manufacturer Data field for BLE). The goal of this is:

  • To allow homekit_controller devices to be shown in the UI immediately after a start and not have to wait for discovery to complete (this will require config entries to come to fruition)
  • To allow homekit_controller entities to be shown in the UI (albeit it as unavailable) if HA starts when an accessory is switched off (this will require config entries to come to fruition)
  • To reduce start up time for bluetooth HomeKit devices (you have to enumerate every GATT characteristic and it is slow)

I've tested pairing from a clean env, and i've tested upgrading an existing env. Works as expected in both cases.

This is the last change before the main homekit_controller config entries pull request. I'd like to get this PR into a release and then do config entries in release after. After config entires is merged coverage will be high enough to remove homekit_controller from coveragerc!

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.
  • Tests have been added to verify that the new code works.

@balloob balloob added this to the 0.92.0 milestone Apr 18, 2019
@balloob balloob merged commit 4ac9a2e into home-assistant:dev Apr 18, 2019
"""Set up for Homekit devices."""
# pylint: disable=import-error
import homekit
from homekit.controller.ip_implementation import IpPairing

map_storage = hass.data[ENTITY_MAP] = EntityMapStorage(hass)
await map_storage.async_initialize()

hass.data[CONTROLLER] = controller = homekit.Controller()

for hkid, pairing_data in load_old_pairings(hass).items():
Copy link
Member

Choose a reason for hiding this comment

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

This does I/O, which isn't allowed in a coroutine. Please add it as an executor job.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops. We already made this async in config flow and hadn't originally split this PR apart from config flow. Will fix this one now.

return
for accessory in data:

self.hass.data[ENTITY_MAP].async_create_or_update_map(
Copy link
Member

Choose a reason for hiding this comment

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

Don't call async functions from sync context and vice versa.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not actually a coroutine, but it does call store.async_delay_save and is, I believe, 'async safe'. So I could mark it with @callback or drop the async_ prefix?

Copy link
Member

Choose a reason for hiding this comment

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

Async callbacks should also only be executed in the event loop.

@MartinHjelmare
Copy link
Member

I would recommend not adding this to 0.92. Let it go out in 0.93 and the config entry can come in 0.94.

@balloob balloob removed this from the 0.92.0 milestone Apr 19, 2019
Jc2k added a commit to Jc2k/home-assistant that referenced this pull request Apr 19, 2019
cgarwood pushed a commit that referenced this pull request Apr 19, 2019
@Jc2k Jc2k deleted the homekit_pairing_entity_maps branch April 21, 2019 07:57
Jc2k added a commit to Jc2k/home-assistant that referenced this pull request Apr 22, 2019
balloob pushed a commit that referenced this pull request Apr 22, 2019
@balloob balloob mentioned this pull request May 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants