Add support for Alexa.StepSpeaker (#12183)

This commit is contained in:
Richard Lucas 2018-02-05 16:02:08 -08:00 committed by Paulus Schoutsen
parent bdaf9cfae2
commit e7a0759e1c
2 changed files with 63 additions and 0 deletions

View file

@ -352,6 +352,11 @@ class _AlexaSpeaker(_AlexaInterface):
return 'Alexa.Speaker' return 'Alexa.Speaker'
class _AlexaStepSpeaker(_AlexaInterface):
def name(self):
return 'Alexa.StepSpeaker'
class _AlexaPlaybackController(_AlexaInterface): class _AlexaPlaybackController(_AlexaInterface):
def name(self): def name(self):
return 'Alexa.PlaybackController' return 'Alexa.PlaybackController'
@ -472,6 +477,11 @@ class _MediaPlayerCapabilities(_AlexaEntity):
if supported & media_player.SUPPORT_VOLUME_SET: if supported & media_player.SUPPORT_VOLUME_SET:
yield _AlexaSpeaker(self.entity) yield _AlexaSpeaker(self.entity)
step_volume_features = (media_player.SUPPORT_VOLUME_MUTE |
media_player.SUPPORT_VOLUME_STEP)
if supported & step_volume_features:
yield _AlexaStepSpeaker(self.entity)
playback_features = (media_player.SUPPORT_PLAY | playback_features = (media_player.SUPPORT_PLAY |
media_player.SUPPORT_PAUSE | media_player.SUPPORT_PAUSE |
media_player.SUPPORT_STOP | media_player.SUPPORT_STOP |
@ -1153,6 +1163,30 @@ def async_api_adjust_volume(hass, config, request, entity):
return api_message(request) return api_message(request)
@HANDLERS.register(('Alexa.StepSpeaker', 'AdjustVolume'))
@extract_entity
@asyncio.coroutine
def async_api_adjust_volume_step(hass, config, request, entity):
"""Process an adjust volume step request."""
volume_step = round(float(request[API_PAYLOAD]['volume'] / 100), 2)
current_level = entity.attributes.get(media_player.ATTR_MEDIA_VOLUME_LEVEL)
volume = current_level + volume_step
data = {
ATTR_ENTITY_ID: entity.entity_id,
media_player.ATTR_MEDIA_VOLUME_LEVEL: volume,
}
yield from hass.services.async_call(
entity.domain, media_player.SERVICE_VOLUME_SET,
data, blocking=False)
return api_message(request)
@HANDLERS.register(('Alexa.StepSpeaker', 'SetMute'))
@HANDLERS.register(('Alexa.Speaker', 'SetMute')) @HANDLERS.register(('Alexa.Speaker', 'SetMute'))
@extract_entity @extract_entity
@asyncio.coroutine @asyncio.coroutine

View file

@ -429,6 +429,7 @@ def test_media_player(hass):
'Alexa.InputController', 'Alexa.InputController',
'Alexa.PowerController', 'Alexa.PowerController',
'Alexa.Speaker', 'Alexa.Speaker',
'Alexa.StepSpeaker',
'Alexa.PlaybackController', 'Alexa.PlaybackController',
) )
@ -492,6 +493,34 @@ def test_media_player(hass):
'media_player.volume_set', 'media_player.volume_set',
'volume_level') 'volume_level')
call, _ = yield from assert_request_calls_service(
'Alexa.StepSpeaker', 'SetMute', 'media_player#test',
'media_player.volume_mute',
hass,
payload={'mute': True})
assert call.data['is_volume_muted']
call, _, = yield from assert_request_calls_service(
'Alexa.StepSpeaker', 'SetMute', 'media_player#test',
'media_player.volume_mute',
hass,
payload={'mute': False})
assert not call.data['is_volume_muted']
call, _ = yield from assert_request_calls_service(
'Alexa.StepSpeaker', 'AdjustVolume', 'media_player#test',
'media_player.volume_set',
hass,
payload={'volume': 20})
assert call.data['volume_level'] == 0.95
call, _ = yield from assert_request_calls_service(
'Alexa.StepSpeaker', 'AdjustVolume', 'media_player#test',
'media_player.volume_set',
hass,
payload={'volume': -20})
assert call.data['volume_level'] == 0.55
@asyncio.coroutine @asyncio.coroutine
def test_alert(hass): def test_alert(hass):