Implement Alexa.PlaybackStateReporter Interface for alexa (#28215)

This commit is contained in:
ochlocracy 2019-10-25 17:53:33 -04:00 committed by Paulus Schoutsen
parent 7fee44b8c5
commit d6654eaecb
4 changed files with 67 additions and 1 deletions

View File

@ -12,9 +12,11 @@ from homeassistant.const import (
STATE_LOCKED, STATE_LOCKED,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_PAUSED,
STATE_PLAYING,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
STATE_UNLOCKED,
STATE_UNKNOWN, STATE_UNKNOWN,
STATE_UNLOCKED,
) )
import homeassistant.components.climate.const as climate import homeassistant.components.climate.const as climate
import homeassistant.components.media_player.const as media_player import homeassistant.components.media_player.const as media_player
@ -1137,3 +1139,39 @@ class AlexaDoorbellEventSource(AlexaCapability):
def capability_proactively_reported(self): def capability_proactively_reported(self):
"""Return True for proactively reported capability.""" """Return True for proactively reported capability."""
return True 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"}

View File

@ -46,6 +46,7 @@ from .capabilities import (
AlexaMotionSensor, AlexaMotionSensor,
AlexaPercentageController, AlexaPercentageController,
AlexaPlaybackController, AlexaPlaybackController,
AlexaPlaybackStateReporter,
AlexaPowerController, AlexaPowerController,
AlexaPowerLevelController, AlexaPowerLevelController,
AlexaRangeController, AlexaRangeController,
@ -422,6 +423,7 @@ class MediaPlayerCapabilities(AlexaEntity):
) )
if supported & playback_features: if supported & playback_features:
yield AlexaPlaybackController(self.entity) yield AlexaPlaybackController(self.entity)
yield AlexaPlaybackStateReporter(self.entity)
if supported & media_player.SUPPORT_SELECT_SOURCE: if supported & media_player.SUPPORT_SELECT_SOURCE:
yield AlexaInputController(self.entity) yield AlexaInputController(self.entity)

View File

@ -17,6 +17,11 @@ from homeassistant.const import (
from homeassistant.components.climate import const as climate from homeassistant.components.climate import const as climate
from homeassistant.components.alexa import smart_home from homeassistant.components.alexa import smart_home
from homeassistant.components.alexa.errors import UnsupportedProperty 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 tests.common import async_mock_service
from . import ( 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 = await reported_properties(hass, "alarm_control_panel.disarmed")
properties.assert_equal("Alexa.SecurityPanelController", "armState", "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"}
)

View File

@ -733,6 +733,7 @@ async def test_media_player(hass):
"Alexa.Speaker", "Alexa.Speaker",
"Alexa.StepSpeaker", "Alexa.StepSpeaker",
"Alexa.PlaybackController", "Alexa.PlaybackController",
"Alexa.PlaybackStateReporter",
"Alexa.EndpointHealth", "Alexa.EndpointHealth",
"Alexa.ChannelController", "Alexa.ChannelController",
) )
@ -954,6 +955,7 @@ async def test_media_player_power(hass):
"Alexa.Speaker", "Alexa.Speaker",
"Alexa.StepSpeaker", "Alexa.StepSpeaker",
"Alexa.PlaybackController", "Alexa.PlaybackController",
"Alexa.PlaybackStateReporter",
"Alexa.EndpointHealth", "Alexa.EndpointHealth",
"Alexa.ChannelController", "Alexa.ChannelController",
) )