From 2a86d52dba1ad63c0671f1f528f2c374aeb87aa2 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 30 May 2020 21:42:40 -0700 Subject: [PATCH] Alexa media player only include equalizer if supported sound mode (#36285) --- .../components/alexa/capabilities.py | 44 +++++++++++-------- homeassistant/components/alexa/entities.py | 6 ++- tests/components/alexa/test_smart_home.py | 11 +++-- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/alexa/capabilities.py b/homeassistant/components/alexa/capabilities.py index c8eb932a9e0..c11e974310c 100644 --- a/homeassistant/components/alexa/capabilities.py +++ b/homeassistant/components/alexa/capabilities.py @@ -1803,6 +1803,13 @@ class AlexaEqualizerController(AlexaCapability): """ supported_locales = {"en-US"} + VALID_SOUND_MODES = { + "MOVIE", + "MUSIC", + "NIGHT", + "SPORT", + "TV", + } def name(self): """Return the Alexa API name of this interface.""" @@ -1821,35 +1828,34 @@ class AlexaEqualizerController(AlexaCapability): raise UnsupportedProperty(name) sound_mode = self.entity.attributes.get(media_player.ATTR_SOUND_MODE) - if sound_mode and sound_mode.upper() in ( - "MOVIE", - "MUSIC", - "NIGHT", - "SPORT", - "TV", - ): + if sound_mode and sound_mode.upper() in self.VALID_SOUND_MODES: return sound_mode.upper() return None def configurations(self): - """Return the sound modes supported in the configurations object. - - Valid Values for modes are: MOVIE, MUSIC, NIGHT, SPORT, TV. - """ + """Return the sound modes supported in the configurations object.""" configurations = None - sound_mode_list = self.entity.attributes.get(media_player.ATTR_SOUND_MODE_LIST) - if sound_mode_list: - supported_sound_modes = [ - {"name": sound_mode.upper()} - for sound_mode in sound_mode_list - if sound_mode.upper() in ("MOVIE", "MUSIC", "NIGHT", "SPORT", "TV") - ] - + supported_sound_modes = self.get_valid_inputs( + self.entity.attributes.get(media_player.ATTR_SOUND_MODE_LIST, []) + ) + if supported_sound_modes: configurations = {"modes": {"supported": supported_sound_modes}} return configurations + @classmethod + def get_valid_inputs(cls, sound_mode_list): + """Return list of supported inputs.""" + input_list = [] + for sound_mode in sound_mode_list: + sound_mode = sound_mode.upper() + + if sound_mode in cls.VALID_SOUND_MODES: + input_list.append({"name": sound_mode}) + + return input_list + class AlexaTimeHoldController(AlexaCapability): """Implements Alexa.TimeHoldController. diff --git a/homeassistant/components/alexa/entities.py b/homeassistant/components/alexa/entities.py index e74722b89e8..972df08bd1e 100644 --- a/homeassistant/components/alexa/entities.py +++ b/homeassistant/components/alexa/entities.py @@ -547,7 +547,11 @@ class MediaPlayerCapabilities(AlexaEntity): yield AlexaChannelController(self.entity) if supported & media_player.const.SUPPORT_SELECT_SOUND_MODE: - yield AlexaEqualizerController(self.entity) + inputs = AlexaInputController.get_valid_inputs( + self.entity.attributes.get(media_player.const.ATTR_SOUND_MODE_LIST, []) + ) + if len(inputs) > 0: + yield AlexaEqualizerController(self.entity) yield AlexaEndpointHealth(self.hass, self.entity) yield Alexa(self.hass) diff --git a/tests/components/alexa/test_smart_home.py b/tests/components/alexa/test_smart_home.py index 591d200ef90..a72099da600 100644 --- a/tests/components/alexa/test_smart_home.py +++ b/tests/components/alexa/test_smart_home.py @@ -3297,8 +3297,8 @@ async def test_media_player_eq_modes(hass): assert call.data["sound_mode"] == mode.lower() -async def test_media_player_sound_mode_list_none(hass): - """Test EqualizerController bands directive not supported.""" +async def test_media_player_sound_mode_list_unsupported(hass): + """Test EqualizerController with unsupported sound modes.""" device = ( "media_player.test", "on", @@ -3306,13 +3306,18 @@ async def test_media_player_sound_mode_list_none(hass): "friendly_name": "Test media player", "supported_features": SUPPORT_SELECT_SOUND_MODE, "sound_mode": "unknown", - "sound_mode_list": None, + "sound_mode_list": ["unsupported", "non-existing"], }, ) appliance = await discovery_test(device, hass) assert appliance["endpointId"] == "media_player#test" assert appliance["friendlyName"] == "Test media player" + # Test equalizer controller is not there + assert_endpoint_capabilities( + appliance, "Alexa", "Alexa.PowerController", "Alexa.EndpointHealth", + ) + async def test_media_player_eq_bands_not_supported(hass): """Test EqualizerController bands directive not supported."""