From 78a5dc71ac34637d8216f759be04ccc79c9f7b56 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Thu, 11 Jul 2019 17:35:46 +0200 Subject: [PATCH] Fix powercontrol media player alexa (#25080) --- homeassistant/components/alexa/entities.py | 6 +-- homeassistant/components/alexa/handlers.py | 59 ++++++++++------------ tests/components/alexa/test_smart_home.py | 11 ++++ 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/homeassistant/components/alexa/entities.py b/homeassistant/components/alexa/entities.py index 7caec1b541d..2a6498fdcaf 100644 --- a/homeassistant/components/alexa/entities.py +++ b/homeassistant/components/alexa/entities.py @@ -339,16 +339,12 @@ class MediaPlayerCapabilities(AlexaEntity): def interfaces(self): """Yield the supported interfaces.""" yield AlexaEndpointHealth(self.hass, self.entity) + yield AlexaPowerController(self.entity) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) if supported & media_player.const.SUPPORT_VOLUME_SET: yield AlexaSpeaker(self.entity) - power_features = (media_player.SUPPORT_TURN_ON | - media_player.SUPPORT_TURN_OFF) - if supported & power_features: - yield AlexaPowerController(self.entity) - step_volume_features = (media_player.const.SUPPORT_VOLUME_MUTE | media_player.const.SUPPORT_VOLUME_STEP) if supported & step_volume_features: diff --git a/homeassistant/components/alexa/handlers.py b/homeassistant/components/alexa/handlers.py index 3cdd4e741af..b66fbf82c0f 100644 --- a/homeassistant/components/alexa/handlers.py +++ b/homeassistant/components/alexa/handlers.py @@ -4,45 +4,26 @@ import logging import math from homeassistant import core as ha -from homeassistant.util.decorator import Registry -import homeassistant.util.color as color_util -from homeassistant.const import ( - ATTR_ENTITY_ID, - ATTR_TEMPERATURE, - SERVICE_LOCK, - SERVICE_MEDIA_NEXT_TRACK, - SERVICE_MEDIA_PAUSE, - SERVICE_MEDIA_PLAY, - SERVICE_MEDIA_PREVIOUS_TRACK, - SERVICE_MEDIA_STOP, - SERVICE_SET_COVER_POSITION, - SERVICE_TURN_OFF, - SERVICE_TURN_ON, - SERVICE_UNLOCK, - SERVICE_VOLUME_DOWN, - SERVICE_VOLUME_MUTE, - SERVICE_VOLUME_SET, - SERVICE_VOLUME_UP, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, -) -from homeassistant.components.climate import const as climate from homeassistant.components import cover, fan, group, light, media_player +from homeassistant.components.climate import const as climate +from homeassistant.const import ( + ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, ATTR_TEMPERATURE, SERVICE_LOCK, + SERVICE_MEDIA_NEXT_TRACK, SERVICE_MEDIA_PAUSE, SERVICE_MEDIA_PLAY, + SERVICE_MEDIA_PREVIOUS_TRACK, SERVICE_MEDIA_STOP, + SERVICE_SET_COVER_POSITION, SERVICE_TURN_OFF, SERVICE_TURN_ON, + SERVICE_UNLOCK, SERVICE_VOLUME_DOWN, SERVICE_VOLUME_MUTE, + SERVICE_VOLUME_SET, SERVICE_VOLUME_UP, TEMP_CELSIUS, TEMP_FAHRENHEIT) +import homeassistant.util.color as color_util +from homeassistant.util.decorator import Registry from homeassistant.util.temperature import convert as convert_temperature from .const import ( - API_TEMP_UNITS, - API_THERMOSTAT_MODES, - API_THERMOSTAT_PRESETS, - Cause, -) + API_TEMP_UNITS, API_THERMOSTAT_MODES, API_THERMOSTAT_PRESETS, Cause) from .entities import async_get_entities -from .state_report import async_enable_proactive_mode from .errors import ( - AlexaInvalidValueError, - AlexaTempRangeError, - AlexaUnsupportedThermostatModeError, -) + AlexaInvalidValueError, AlexaTempRangeError, + AlexaUnsupportedThermostatModeError) +from .state_report import async_enable_proactive_mode _LOGGER = logging.getLogger(__name__) HANDLERS = Registry() @@ -99,6 +80,12 @@ async def async_api_turn_on(hass, config, directive, context): service = SERVICE_TURN_ON if domain == cover.DOMAIN: service = cover.SERVICE_OPEN_COVER + elif domain == media_player.DOMAIN: + supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) + power_features = (media_player.SUPPORT_TURN_ON | + media_player.SUPPORT_TURN_OFF) + if not supported & power_features: + service = media_player.SERVICE_MEDIA_PLAY await hass.services.async_call(domain, service, { ATTR_ENTITY_ID: entity.entity_id @@ -118,6 +105,12 @@ async def async_api_turn_off(hass, config, directive, context): service = SERVICE_TURN_OFF if entity.domain == cover.DOMAIN: service = cover.SERVICE_CLOSE_COVER + elif domain == media_player.DOMAIN: + supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) + power_features = (media_player.SUPPORT_TURN_ON | + media_player.SUPPORT_TURN_OFF) + if not supported & power_features: + service = media_player.SERVICE_MEDIA_STOP await hass.services.async_call(domain, service, { ATTR_ENTITY_ID: entity.entity_id diff --git a/tests/components/alexa/test_smart_home.py b/tests/components/alexa/test_smart_home.py index 5f751a10039..62dbbfdc693 100644 --- a/tests/components/alexa/test_smart_home.py +++ b/tests/components/alexa/test_smart_home.py @@ -558,12 +558,23 @@ async def test_media_player_power(hass): assert_endpoint_capabilities( appliance, 'Alexa.InputController', + 'Alexa.PowerController', 'Alexa.Speaker', 'Alexa.StepSpeaker', 'Alexa.PlaybackController', 'Alexa.EndpointHealth', ) + await assert_request_calls_service( + 'Alexa.PowerController', 'TurnOn', 'media_player#test', + 'media_player.media_play', + hass) + + await assert_request_calls_service( + 'Alexa.PowerController', 'TurnOff', 'media_player#test', + 'media_player.media_stop', + hass) + async def test_alert(hass): """Test alert discovery."""