From fab991bbf64ebee8000e9f28048853c9868d022e Mon Sep 17 00:00:00 2001 From: Richard Lucas Date: Sat, 17 Feb 2018 05:54:15 -0800 Subject: [PATCH] Map Alexa StepVolume responses to volume_up/down (#12467) It turns out I misunderstood which media_player services are available when a media player supports StepVolume. This PR maps the Alexa StepSpeaker messages to the volume_up and volume_down services. Currently Alexa allows you to specify the number of steps but the media player volume_up and volume_down services don't support this. For now I just look to see if the steps are +/- and call up/down accordingly. --- homeassistant/components/alexa/smart_home.py | 22 ++++++++++++-------- tests/components/alexa/test_smart_home.py | 6 ++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/alexa/smart_home.py b/homeassistant/components/alexa/smart_home.py index a4f0225d22d..b2f8146bfcf 100644 --- a/homeassistant/components/alexa/smart_home.py +++ b/homeassistant/components/alexa/smart_home.py @@ -1178,20 +1178,24 @@ def async_api_adjust_volume(hass, config, request, 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]['volumeSteps'] / 100), 2) - - current_level = entity.attributes.get(media_player.ATTR_MEDIA_VOLUME_LEVEL) - - volume = current_level + volume_step + # media_player volume up/down service does not support specifying steps + # each component handles it differently e.g. via config. + # For now we use the volumeSteps returned to figure out if we + # should step up/down + volume_step = request[API_PAYLOAD]['volumeSteps'] 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) + if volume_step > 0: + yield from hass.services.async_call( + entity.domain, media_player.SERVICE_VOLUME_UP, + data, blocking=False) + elif volume_step < 0: + yield from hass.services.async_call( + entity.domain, media_player.SERVICE_VOLUME_DOWN, + data, blocking=False) return api_message(request) diff --git a/tests/components/alexa/test_smart_home.py b/tests/components/alexa/test_smart_home.py index 9654c667c5f..ca49950e2a1 100644 --- a/tests/components/alexa/test_smart_home.py +++ b/tests/components/alexa/test_smart_home.py @@ -515,17 +515,15 @@ def test_media_player(hass): call, _ = yield from assert_request_calls_service( 'Alexa.StepSpeaker', 'AdjustVolume', 'media_player#test', - 'media_player.volume_set', + 'media_player.volume_up', hass, payload={'volumeSteps': 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', + 'media_player.volume_down', hass, payload={'volumeSteps': -20}) - assert call.data['volume_level'] == 0.55 @asyncio.coroutine