Implement Alexa.SeekController Interface for media_player in Alexa (#28299)
* Implement Alexa.SeekController Interface for Alexa * Added error handling and duration checks. * Split out media_player SeekController tests and added error test.
This commit is contained in:
1 parent
bfe4a85e9d
commit
ff5b070f4b
5 files changed
+190
-9
No files matched your search
@@ -1175,3 +1175,14 @@ class AlexaPlaybackStateReporter(AlexaCapability):
|
||||
return {"state": "PAUSED"}
|
||||
|
||||
return {"state": "STOPPED"}
|
||||
|
||||
|
||||
class AlexaSeekController(AlexaCapability):
|
||||
"""Implements Alexa.SeekController.
|
||||
|
||||
https://developer.amazon.com/docs/device-apis/alexa-seekcontroller.html
|
||||
"""
|
||||
|
||||
def name(self):
|
||||
"""Return the Alexa API name of this interface."""
|
||||
return "Alexa.SeekController"
|
||||
@@ -52,6 +52,7 @@ from .capabilities import (
|
||||
AlexaRangeController,
|
||||
AlexaSceneController,
|
||||
AlexaSecurityPanelController,
|
||||
AlexaSeekController,
|
||||
AlexaSpeaker,
|
||||
AlexaStepSpeaker,
|
||||
AlexaTemperatureSensor,
|
||||
@@ -425,6 +426,9 @@ class MediaPlayerCapabilities(AlexaEntity):
|
||||
yield AlexaPlaybackController(self.entity)
|
||||
yield AlexaPlaybackStateReporter(self.entity)
|
||||
|
||||
if supported & media_player.const.SUPPORT_SEEK:
|
||||
yield AlexaSeekController(self.entity)
|
||||
|
||||
if supported & media_player.SUPPORT_SELECT_SOURCE:
|
||||
yield AlexaInputController(self.entity)
|
||||
|
||||
|
||||
@@ -111,3 +111,10 @@ class AlexaInvalidDirectiveError(AlexaError):
|
||||
|
||||
namespace = "Alexa"
|
||||
error_type = "INVALID_DIRECTIVE"
|
||||
|
||||
|
||||
class AlexaVideoActionNotPermittedForContentError(AlexaError):
|
||||
"""Class to represent action not permitted for content errors."""
|
||||
|
||||
namespace = "Alexa.Video"
|
||||
error_type = "ACTION_NOT_PERMITTED_FOR_CONTENT"
|
||||
@@ -54,6 +54,7 @@ from .errors import (
|
||||
AlexaSecurityPanelUnauthorizedError,
|
||||
AlexaTempRangeError,
|
||||
AlexaUnsupportedThermostatModeError,
|
||||
AlexaVideoActionNotPermittedForContentError,
|
||||
)
|
||||
from .state_report import async_enable_proactive_mode
|
||||
|
||||
@@ -1186,3 +1187,45 @@ async def async_api_skipchannel(hass, config, directive, context):
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@HANDLERS.register(("Alexa.SeekController", "AdjustSeekPosition"))
|
||||
async def async_api_seek(hass, config, directive, context):
|
||||
"""Process a seek request."""
|
||||
entity = directive.entity
|
||||
position_delta = int(directive.payload["deltaPositionMilliseconds"])
|
||||
|
||||
current_position = entity.attributes.get(media_player.ATTR_MEDIA_POSITION)
|
||||
if not current_position:
|
||||
msg = f"{entity} did not return the current media position."
|
||||
raise AlexaVideoActionNotPermittedForContentError(msg)
|
||||
|
||||
seek_position = int(current_position) + int(position_delta / 1000)
|
||||
|
||||
if seek_position < 0:
|
||||
seek_position = 0
|
||||
|
||||
media_duration = entity.attributes.get(media_player.ATTR_MEDIA_DURATION)
|
||||
if media_duration and 0 < int(media_duration) < seek_position:
|
||||
seek_position = media_duration
|
||||
|
||||
data = {
|
||||
ATTR_ENTITY_ID: entity.entity_id,
|
||||
media_player.ATTR_MEDIA_SEEK_POSITION: seek_position,
|
||||
}
|
||||
|
||||
await hass.services.async_call(
|
||||
media_player.DOMAIN,
|
||||
media_player.SERVICE_MEDIA_SEEK,
|
||||
data,
|
||||
blocking=False,
|
||||
context=context,
|
||||
)
|
||||
|
||||
# convert seconds to milliseconds for StateReport.
|
||||
seek_position = int(seek_position * 1000)
|
||||
|
||||
payload = {"properties": [{"name": "positionMilliseconds", "value": seek_position}]}
|
||||
return directive.response(
|
||||
name="StateReport", namespace="Alexa.SeekController", payload=payload
|
||||
)
|
||||
Reference in new issue
Block a user