From d6654eaecb651ba895fa56cb7b917456898c87da Mon Sep 17 00:00:00 2001 From: ochlocracy <5885236+ochlocracy@users.noreply.github.com> Date: Fri, 25 Oct 2019 17:53:33 -0400 Subject: [PATCH] Implement Alexa.PlaybackStateReporter Interface for alexa (#28215) --- .../components/alexa/capabilities.py | 40 ++++++++++++++++++- homeassistant/components/alexa/entities.py | 2 + tests/components/alexa/test_capabilities.py | 24 +++++++++++ tests/components/alexa/test_smart_home.py | 2 + 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/alexa/capabilities.py b/homeassistant/components/alexa/capabilities.py index 52dde74ff3a..b5b7fa88ef8 100644 --- a/homeassistant/components/alexa/capabilities.py +++ b/homeassistant/components/alexa/capabilities.py @@ -12,9 +12,11 @@ from homeassistant.const import ( STATE_LOCKED, STATE_OFF, STATE_ON, + STATE_PAUSED, + STATE_PLAYING, STATE_UNAVAILABLE, - STATE_UNLOCKED, STATE_UNKNOWN, + STATE_UNLOCKED, ) import homeassistant.components.climate.const as climate import homeassistant.components.media_player.const as media_player @@ -1137,3 +1139,39 @@ class AlexaDoorbellEventSource(AlexaCapability): def capability_proactively_reported(self): """Return True for proactively reported capability.""" return True + + +class AlexaPlaybackStateReporter(AlexaCapability): + """Implements Alexa.PlaybackStateReporter. + + https://developer.amazon.com/docs/device-apis/alexa-playbackstatereporter.html + """ + + def name(self): + """Return the Alexa API name of this interface.""" + return "Alexa.PlaybackStateReporter" + + def properties_supported(self): + """Return what properties this entity supports.""" + return [{"name": "playbackState"}] + + def properties_proactively_reported(self): + """Return True if properties asynchronously reported.""" + return True + + def properties_retrievable(self): + """Return True if properties can be retrieved.""" + return True + + def get_property(self, name): + """Read and return a property.""" + if name != "playbackState": + raise UnsupportedProperty(name) + + playback_state = self.entity.state + if playback_state == STATE_PLAYING: + return {"state": "PLAYING"} + if playback_state == STATE_PAUSED: + return {"state": "PAUSED"} + + return {"state": "STOPPED"} diff --git a/homeassistant/components/alexa/entities.py b/homeassistant/components/alexa/entities.py index e52e1b4f87e..ef6c2902053 100644 --- a/homeassistant/components/alexa/entities.py +++ b/homeassistant/components/alexa/entities.py @@ -46,6 +46,7 @@ from .capabilities import ( AlexaMotionSensor, AlexaPercentageController, AlexaPlaybackController, + AlexaPlaybackStateReporter, AlexaPowerController, AlexaPowerLevelController, AlexaRangeController, @@ -422,6 +423,7 @@ class MediaPlayerCapabilities(AlexaEntity): ) if supported & playback_features: yield AlexaPlaybackController(self.entity) + yield AlexaPlaybackStateReporter(self.entity) if supported & media_player.SUPPORT_SELECT_SOURCE: yield AlexaInputController(self.entity) diff --git a/tests/components/alexa/test_capabilities.py b/tests/components/alexa/test_capabilities.py index 89815c72544..d2f6cddc522 100644 --- a/tests/components/alexa/test_capabilities.py +++ b/tests/components/alexa/test_capabilities.py @@ -17,6 +17,11 @@ from homeassistant.const import ( from homeassistant.components.climate import const as climate from homeassistant.components.alexa import smart_home from homeassistant.components.alexa.errors import UnsupportedProperty +from homeassistant.components.media_player.const import ( + SUPPORT_PAUSE, + SUPPORT_PLAY, + SUPPORT_STOP, +) from tests.common import async_mock_service from . import ( @@ -642,3 +647,22 @@ async def test_report_alarm_control_panel_state(hass): properties = await reported_properties(hass, "alarm_control_panel.disarmed") properties.assert_equal("Alexa.SecurityPanelController", "armState", "DISARMED") + + +async def test_report_playback_state(hass): + """Test PlaybackStateReporter implements playbackState property.""" + hass.states.async_set( + "media_player.test", + "off", + { + "friendly_name": "Test media player", + "supported_features": SUPPORT_PAUSE | SUPPORT_PLAY | SUPPORT_STOP, + "volume_level": 0.75, + }, + ) + + properties = await reported_properties(hass, "media_player.test") + + properties.assert_equal( + "Alexa.PlaybackStateReporter", "playbackState", {"state": "STOPPED"} + ) diff --git a/tests/components/alexa/test_smart_home.py b/tests/components/alexa/test_smart_home.py index 00c762103f3..3994c3d9f5d 100644 --- a/tests/components/alexa/test_smart_home.py +++ b/tests/components/alexa/test_smart_home.py @@ -733,6 +733,7 @@ async def test_media_player(hass): "Alexa.Speaker", "Alexa.StepSpeaker", "Alexa.PlaybackController", + "Alexa.PlaybackStateReporter", "Alexa.EndpointHealth", "Alexa.ChannelController", ) @@ -954,6 +955,7 @@ async def test_media_player_power(hass): "Alexa.Speaker", "Alexa.StepSpeaker", "Alexa.PlaybackController", + "Alexa.PlaybackStateReporter", "Alexa.EndpointHealth", "Alexa.ChannelController", )