Skip to content

Async conversion HomeKit Types#14399

Closed
cdce8p wants to merge 2 commits intohome-assistant:devfrom
cdce8p:homekit-async
Closed

Async conversion HomeKit Types#14399
cdce8p wants to merge 2 commits intohome-assistant:devfrom
cdce8p:homekit-async

Conversation

@cdce8p
Copy link
Copy Markdown
Member

@cdce8p cdce8p commented May 11, 2018

Description:

This is the first PR to complete the async conversion for the homekit component. I focused on converting the type classes. Another PR will deal with the main HomeKit class.

As for the changes:

  • Setter methods are called from HAP-python and thus aren't executed in the same event loop. Therefore upon execution they will call hass.add_job on their async method.
  • The async setter methods now call await hass.services.async_call for service execution.
  • The update_state method s(callbacks for track_change_listeners) are now async_update_state. I omitted the callback decorator, see Async conversion HomeKit Types #14399 (review)
  • Any call to set_value on a characteristic in HAP-python is now prefixed with hass.async_add_job (in async methods only).

Since I haven't used async for that long, any input is welcome.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

current_position = new_state.attributes.get(ATTR_CURRENT_POSITION)
if isinstance(current_position, int):
self.char_current_position.set_value(current_position)
self.hass.async_add_job(
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.

You need to await these calls if you want to mimic the old behavior (and thus method needs to be async). However, If you're going to call to the thread pool twice, you might aswell run the whole method in a thread right away, probably more efficient.

self.char_current_position.set_value(position)
self.char_target_position.set_value(position)
self.char_position_state.set_value(2)
self.hass.async_add_job(self.char_current_position.set_value, position)
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.

This is very inefficient calling 3 times async add job. They are all queued up but will also be picked up by 3 threads at the same time.

@cdce8p
Copy link
Copy Markdown
Member Author

cdce8p commented May 20, 2018

Closing this PR, since it's probably more efficient the way it's at the moment.
For the style changes, see: #14556

@cdce8p cdce8p closed this May 20, 2018
@cdce8p cdce8p deleted the homekit-async branch May 20, 2018 14:26
@home-assistant home-assistant locked and limited conversation to collaborators Sep 5, 2018
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.

3 participants