From 55a7ea6cc55488b5216119d2c0decf2600942755 Mon Sep 17 00:00:00 2001
From: Pascal Vizeli <pvizeli@syshack.ch>
Date: Mon, 29 Apr 2019 22:40:55 +0200
Subject: [PATCH] Fix media_player alexa power control bug (#23537)

---
 homeassistant/components/alexa/smart_home.py |  6 ++++-
 tests/components/alexa/test_smart_home.py    | 26 ++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/homeassistant/components/alexa/smart_home.py b/homeassistant/components/alexa/smart_home.py
index e16a1d45ab7..8a95f4702c7 100644
--- a/homeassistant/components/alexa/smart_home.py
+++ b/homeassistant/components/alexa/smart_home.py
@@ -911,13 +911,17 @@ class _MediaPlayerCapabilities(_AlexaEntity):
         return [_DisplayCategory.TV]
 
     def interfaces(self):
-        yield _AlexaPowerController(self.entity)
         yield _AlexaEndpointHealth(self.hass, 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/tests/components/alexa/test_smart_home.py b/tests/components/alexa/test_smart_home.py
index 924a568dea2..20b4495cd1a 100644
--- a/tests/components/alexa/test_smart_home.py
+++ b/tests/components/alexa/test_smart_home.py
@@ -574,6 +574,32 @@ async def test_media_player(hass):
         payload={'volumeSteps': -20})
 
 
+async def test_media_player_power(hass):
+    """Test media player discovery with mapped on/off."""
+    device = (
+        'media_player.test',
+        'off', {
+            'friendly_name': "Test media player",
+            'supported_features': 0xfa3f,
+            'volume_level': 0.75
+        }
+    )
+    appliance = await discovery_test(device, hass)
+
+    assert appliance['endpointId'] == 'media_player#test'
+    assert appliance['displayCategories'][0] == "TV"
+    assert appliance['friendlyName'] == "Test media player"
+
+    assert_endpoint_capabilities(
+        appliance,
+        'Alexa.InputController',
+        'Alexa.Speaker',
+        'Alexa.StepSpeaker',
+        'Alexa.PlaybackController',
+        'Alexa.EndpointHealth',
+    )
+
+
 async def test_alert(hass):
     """Test alert discovery."""
     device = ('alert.test', 'off', {'friendly_name': "Test alert"})