-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Async conversion HomeKit Types #14399
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,24 +41,36 @@ def __init__(self, *args): | |
|
|
||
| def set_state(self, value): | ||
| """Change garage state if call came from HomeKit.""" | ||
| self.hass.add_job(self.async_set_state, value) | ||
|
|
||
| async def async_set_state(self, value): | ||
| """Change garage state if call came from HomeKit. Coroutine.""" | ||
| _LOGGER.debug('%s: Set state to %d', self.entity_id, value) | ||
| self.flag_target_state = True | ||
|
|
||
| params = {ATTR_ENTITY_ID: self.entity_id} | ||
| if value == 0: | ||
| self.char_current_state.set_value(3) | ||
| self.hass.components.cover.open_cover(self.entity_id) | ||
| self.hass.async_add_job(self.char_current_state.set_value, 3) | ||
| await self.hass.services.async_call( | ||
| DOMAIN, SERVICE_OPEN_COVER, params) | ||
| elif value == 1: | ||
| self.char_current_state.set_value(2) | ||
| self.hass.components.cover.close_cover(self.entity_id) | ||
| self.hass.async_add_job(self.char_current_state.set_value, 2) | ||
| await self.hass.services.async_call( | ||
| DOMAIN, SERVICE_CLOSE_COVER, params) | ||
|
|
||
| def async_update_state(self, new_state): | ||
| """Update cover state after state changed. | ||
|
|
||
| def update_state(self, new_state): | ||
| """Update cover state after state changed.""" | ||
| Method is run in the event loop. | ||
| """ | ||
| hass_state = new_state.state | ||
| if hass_state in (STATE_OPEN, STATE_CLOSED): | ||
| current_state = 0 if hass_state == STATE_OPEN else 1 | ||
| self.char_current_state.set_value(current_state) | ||
| self.hass.async_add_job( | ||
| self.char_current_state.set_value, current_state) | ||
| if not self.flag_target_state: | ||
| self.char_target_state.set_value(current_state) | ||
| self.hass.async_add_job( | ||
| self.char_target_state.set_value, current_state) | ||
| self.flag_target_state = False | ||
|
|
||
|
|
||
|
|
@@ -83,20 +95,31 @@ def __init__(self, *args): | |
| @debounce | ||
| def move_cover(self, value): | ||
| """Move cover to value if call came from HomeKit.""" | ||
| self.hass.add_job(self.async_move_cover, value) | ||
|
|
||
| async def async_move_cover(self, value): | ||
| """Move cover to value if call came from HomeKit. Coroutine.""" | ||
| _LOGGER.debug('%s: Set position to %d', self.entity_id, value) | ||
| self.homekit_target = value | ||
|
|
||
| params = {ATTR_ENTITY_ID: self.entity_id, ATTR_POSITION: value} | ||
| self.hass.services.call(DOMAIN, SERVICE_SET_COVER_POSITION, params) | ||
| params = {ATTR_ENTITY_ID: self.entity_id, | ||
| ATTR_POSITION: value} | ||
| await self.hass.services.async_call( | ||
| DOMAIN, SERVICE_SET_COVER_POSITION, params) | ||
|
|
||
| def update_state(self, new_state): | ||
| """Update cover position after state changed.""" | ||
| def async_update_state(self, new_state): | ||
| """Update cover position after state changed. | ||
|
|
||
| Method is run in the event loop. | ||
| """ | ||
| 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( | ||
| self.char_current_position.set_value, current_position) | ||
| if self.homekit_target is None or \ | ||
| abs(current_position - self.homekit_target) < 6: | ||
| self.char_target_position.set_value(current_position) | ||
| self.hass.async_add_job( | ||
| self.char_target_position.set_value, current_position) | ||
| self.homekit_target = None | ||
|
|
||
|
|
||
|
|
@@ -126,6 +149,10 @@ def __init__(self, *args): | |
| @debounce | ||
| def move_cover(self, value): | ||
| """Move cover to value if call came from HomeKit.""" | ||
| self.hass.add_job(self.async_move_cover, value) | ||
|
|
||
| async def async_move_cover(self, value): | ||
| """Move cover to value if call came from HomeKit. Coroutine.""" | ||
| _LOGGER.debug('%s: Set position to %d', self.entity_id, value) | ||
|
|
||
| if self.supports_stop: | ||
|
|
@@ -141,19 +168,24 @@ def move_cover(self, value): | |
| else: | ||
| service, position = (SERVICE_CLOSE_COVER, 0) | ||
|
|
||
| self.hass.services.call(DOMAIN, service, | ||
| {ATTR_ENTITY_ID: self.entity_id}) | ||
| params = {ATTR_ENTITY_ID: self.entity_id} | ||
| await self.hass.services.async_call(DOMAIN, service, params) | ||
|
|
||
| # Snap the current/target position to the expected final position. | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| self.hass.async_add_job(self.char_target_position.set_value, position) | ||
| self.hass.async_add_job(self.char_position_state.set_value, 2) | ||
|
|
||
| def async_update_state(self, new_state): | ||
| """Update cover position after state changed. | ||
|
|
||
| def update_state(self, new_state): | ||
| """Update cover position after state changed.""" | ||
| Method is run in the event loop. | ||
| """ | ||
| position_mapping = {STATE_OPEN: 100, STATE_CLOSED: 0} | ||
| hk_position = position_mapping.get(new_state.state) | ||
| if hk_position is not None: | ||
| self.char_current_position.set_value(hk_position) | ||
| self.char_target_position.set_value(hk_position) | ||
| self.char_position_state.set_value(2) | ||
| self.hass.async_add_job( | ||
| self.char_current_position.set_value, hk_position) | ||
| self.hass.async_add_job( | ||
| self.char_target_position.set_value, hk_position) | ||
| self.hass.async_add_job(self.char_position_state.set_value, 2) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
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.