-
-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Add HomeKit support for media players #14446
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
Merged
cdce8p
merged 7 commits into
home-assistant:dev
from
schmittx:homekit-media-player-support
May 25, 2018
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
799e09b
Initial commit
schmittx 6c14d2f
Style clean up
schmittx 860ab26
Move mode validation to util and update tests
schmittx 757727a
Style clean up and update tests
schmittx e905a6e
Update tests
schmittx 63d8d7a
Fix error after rebase
schmittx ce068be
Minor style and test fixes
schmittx 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
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 |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| """Class to hold all media player accessories.""" | ||
| import logging | ||
|
|
||
| from pyhap.const import CATEGORY_SWITCH | ||
|
|
||
| from homeassistant.const import ( | ||
| ATTR_ENTITY_ID, CONF_MODE, SERVICE_MEDIA_PAUSE, SERVICE_MEDIA_PLAY, | ||
| SERVICE_MEDIA_STOP, SERVICE_TURN_OFF, SERVICE_TURN_ON, SERVICE_VOLUME_MUTE, | ||
| STATE_OFF, STATE_PLAYING, STATE_UNKNOWN) | ||
| from homeassistant.components.media_player import ( | ||
| ATTR_MEDIA_VOLUME_MUTED, DOMAIN) | ||
|
|
||
| from . import TYPES | ||
| from .accessories import HomeAccessory | ||
| from .const import ( | ||
| CHAR_NAME, CHAR_ON, ON_OFF, PLAY_PAUSE, PLAY_STOP, SERV_SWITCH, | ||
| TOGGLE_MUTE) | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| MODE_FRIENDLY_NAME = {ON_OFF: 'Power', | ||
| PLAY_PAUSE: 'Play/Pause', | ||
| PLAY_STOP: 'Play/Stop', | ||
| TOGGLE_MUTE: 'Mute'} | ||
|
|
||
|
|
||
| @TYPES.register('MediaPlayer') | ||
| class MediaPlayer(HomeAccessory): | ||
| """Generate a Media Player accessory.""" | ||
|
|
||
| def __init__(self, *args): | ||
| """Initialize a Switch accessory object.""" | ||
| super().__init__(*args, category=CATEGORY_SWITCH) | ||
| self._flag = {ON_OFF: False, PLAY_PAUSE: False, | ||
| PLAY_STOP: False, TOGGLE_MUTE: False} | ||
| self.chars = {ON_OFF: None, PLAY_PAUSE: None, | ||
| PLAY_STOP: None, TOGGLE_MUTE: None} | ||
| modes = self.config[CONF_MODE] | ||
|
|
||
| if ON_OFF in modes: | ||
| serv_on_off = self.add_preload_service(SERV_SWITCH, CHAR_NAME) | ||
| serv_on_off.configure_char( | ||
| CHAR_NAME, value=self.generate_service_name(ON_OFF)) | ||
| self.chars[ON_OFF] = serv_on_off.configure_char( | ||
| CHAR_ON, value=False, setter_callback=self.set_on_off) | ||
|
|
||
| if PLAY_PAUSE in modes: | ||
| serv_play_pause = self.add_preload_service(SERV_SWITCH, CHAR_NAME) | ||
| serv_play_pause.configure_char( | ||
| CHAR_NAME, value=self.generate_service_name(PLAY_PAUSE)) | ||
| self.chars[PLAY_PAUSE] = serv_play_pause.configure_char( | ||
| CHAR_ON, value=False, setter_callback=self.set_play_pause) | ||
|
|
||
| if PLAY_STOP in modes: | ||
| serv_play_stop = self.add_preload_service(SERV_SWITCH, CHAR_NAME) | ||
| serv_play_stop.configure_char( | ||
| CHAR_NAME, value=self.generate_service_name(PLAY_STOP)) | ||
| self.chars[PLAY_STOP] = serv_play_stop.configure_char( | ||
| CHAR_ON, value=False, setter_callback=self.set_play_stop) | ||
|
|
||
| if TOGGLE_MUTE in modes: | ||
| serv_toggle_mute = self.add_preload_service(SERV_SWITCH, CHAR_NAME) | ||
| serv_toggle_mute.configure_char( | ||
| CHAR_NAME, value=self.generate_service_name(TOGGLE_MUTE)) | ||
| self.chars[TOGGLE_MUTE] = serv_toggle_mute.configure_char( | ||
| CHAR_ON, value=False, setter_callback=self.set_toggle_mute) | ||
|
|
||
| def generate_service_name(self, mode): | ||
| """Generate name for individual service.""" | ||
| return '{} {}'.format(self.display_name, MODE_FRIENDLY_NAME[mode]) | ||
|
|
||
| def set_on_off(self, value): | ||
| """Move switch state to value if call came from HomeKit.""" | ||
| _LOGGER.debug('%s: Set switch state for "on_off" to %s', | ||
| self.entity_id, value) | ||
| self._flag[ON_OFF] = True | ||
| service = SERVICE_TURN_ON if value else SERVICE_TURN_OFF | ||
| params = {ATTR_ENTITY_ID: self.entity_id} | ||
| self.hass.services.call(DOMAIN, service, params) | ||
|
|
||
| def set_play_pause(self, value): | ||
| """Move switch state to value if call came from HomeKit.""" | ||
| _LOGGER.debug('%s: Set switch state for "play_pause" to %s', | ||
| self.entity_id, value) | ||
| self._flag[PLAY_PAUSE] = True | ||
| service = SERVICE_MEDIA_PLAY if value else SERVICE_MEDIA_PAUSE | ||
| params = {ATTR_ENTITY_ID: self.entity_id} | ||
| self.hass.services.call(DOMAIN, service, params) | ||
|
|
||
| def set_play_stop(self, value): | ||
| """Move switch state to value if call came from HomeKit.""" | ||
| _LOGGER.debug('%s: Set switch state for "play_stop" to %s', | ||
| self.entity_id, value) | ||
| self._flag[PLAY_STOP] = True | ||
| service = SERVICE_MEDIA_PLAY if value else SERVICE_MEDIA_STOP | ||
| params = {ATTR_ENTITY_ID: self.entity_id} | ||
| self.hass.services.call(DOMAIN, service, params) | ||
|
|
||
| def set_toggle_mute(self, value): | ||
| """Move switch state to value if call came from HomeKit.""" | ||
| _LOGGER.debug('%s: Set switch state for "toggle_mute" to %s', | ||
| self.entity_id, value) | ||
| self._flag[TOGGLE_MUTE] = True | ||
| params = {ATTR_ENTITY_ID: self.entity_id, | ||
| ATTR_MEDIA_VOLUME_MUTED: value} | ||
| self.hass.services.call(DOMAIN, SERVICE_VOLUME_MUTE, params) | ||
|
|
||
| def update_state(self, new_state): | ||
| """Update switch state after state changed.""" | ||
| current_state = new_state.state | ||
|
|
||
| if self.chars[ON_OFF]: | ||
| hk_state = current_state not in (STATE_OFF, STATE_UNKNOWN, 'None') | ||
| if not self._flag[ON_OFF]: | ||
| _LOGGER.debug('%s: Set current state for "on_off" to %s', | ||
| self.entity_id, hk_state) | ||
| self.chars[ON_OFF].set_value(hk_state) | ||
| self._flag[ON_OFF] = False | ||
|
|
||
| if self.chars[PLAY_PAUSE]: | ||
| hk_state = current_state == STATE_PLAYING | ||
| if not self._flag[PLAY_PAUSE]: | ||
| _LOGGER.debug('%s: Set current state for "play_pause" to %s', | ||
| self.entity_id, hk_state) | ||
| self.chars[PLAY_PAUSE].set_value(hk_state) | ||
| self._flag[PLAY_PAUSE] = False | ||
|
|
||
| if self.chars[PLAY_STOP]: | ||
| hk_state = current_state == STATE_PLAYING | ||
| if not self._flag[PLAY_STOP]: | ||
| _LOGGER.debug('%s: Set current state for "play_stop" to %s', | ||
| self.entity_id, hk_state) | ||
| self.chars[PLAY_STOP].set_value(hk_state) | ||
| self._flag[PLAY_STOP] = False | ||
|
|
||
| if self.chars[TOGGLE_MUTE]: | ||
| current_state = new_state.attributes.get(ATTR_MEDIA_VOLUME_MUTED) | ||
| if not self._flag[TOGGLE_MUTE]: | ||
| _LOGGER.debug('%s: Set current state for "toggle_mute" to %s', | ||
| self.entity_id, current_state) | ||
| self.chars[TOGGLE_MUTE].set_value(current_state) | ||
| self._flag[TOGGLE_MUTE] = False | ||
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
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.
self.charsshould be private. (The empty line above can be deleted.)Uh oh!
There was an error while loading. Please reload this page.
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.
I think it cannot be private in order for the tests to function? Correct?
All other components (
type_lights, etc.) use non-privateself.chars.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.
Yea, you're right. Please ignore that comment.