From dbc825f3ffa4313eca7e5c408051df94110ca0e0 Mon Sep 17 00:00:00 2001 From: albaintor <118518828+albaintor@users.noreply.github.com> Date: Wed, 22 May 2024 15:20:30 +0200 Subject: [PATCH] Added 2 other commands for FF/RW using companion protocol, which is better and works for all apps --- intg-appletv/driver.py | 13 ++++++++++++- intg-appletv/tv.py | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/intg-appletv/driver.py b/intg-appletv/driver.py index 18ab4b5..642347c 100644 --- a/intg-appletv/driver.py +++ b/intg-appletv/driver.py @@ -46,6 +46,10 @@ class SimpleCommands(str, Enum): """Skip forward a time interval.""" SKIP_BACKWARD = "SKIP_BACKWARD" """Skip forward a time interval.""" + FAST_FORWARD_BEGIN = "FAST_FORWARD_BEGIN" + """Fast forward using Companion protocol.""" + REWIND_BEGIN = "REWIND_BEGIN" + """Rewind using Companion protocol.""" @api.listens_to(ucapi.Events.CONNECT) @@ -267,6 +271,10 @@ async def media_player_cmd_handler( res = await device.skip_forward() case SimpleCommands.SKIP_BACKWARD: res = await device.skip_backward() + case SimpleCommands.FAST_FORWARD_BEGIN: + res = await device.fast_forward_companion() + case SimpleCommands.REWIND_BEGIN: + res = await device.rewind_companion() return res @@ -514,7 +522,10 @@ def _register_available_entities(identifier: str, name: str) -> bool: SimpleCommands.TOP_MENU.value, SimpleCommands.APP_SWITCHER.value, SimpleCommands.SCREENSAVER.value, - + SimpleCommands.SKIP_FORWARD.value, + SimpleCommands.SKIP_BACKWARD.value, + SimpleCommands.FAST_FORWARD_BEGIN.value, + SimpleCommands.REWIND_BEGIN.value ] }, cmd_handler=media_player_cmd_handler, diff --git a/intg-appletv/tv.py b/intg-appletv/tv.py index 2cc7de2..0ae8921 100644 --- a/intg-appletv/tv.py +++ b/intg-appletv/tv.py @@ -20,6 +20,8 @@ import pyatv import pyatv.const import ucapi +from pyatv.core.facade import FacadeRemoteControl + from config import AtvDevice, AtvProtocol from pyatv.const import ( DeviceState, @@ -581,6 +583,24 @@ async def rewind(self) -> ucapi.StatusCodes: """Long press key left for rewind.""" await self._atv.remote_control.left(InputAction.Hold) + @async_handle_atvlib_errors + async def fast_forward_companion(self) -> ucapi.StatusCodes: + """Fast-forward using companion protocol.""" + companion = cast(FacadeRemoteControl, self._atv.remote_control).get(Protocol.Companion) + if companion: + await companion.api.mediacontrol_command(command=MediaControlCommand.FastForwardBegin) + else: + await self._atv.remote_control.right(InputAction.Hold) + + @async_handle_atvlib_errors + async def rewind_companion(self) -> ucapi.StatusCodes: + """Rewind using companion protocol.""" + companion = cast(FacadeRemoteControl, self._atv.remote_control).get(Protocol.Companion) + if companion: + await companion.api.mediacontrol_command(command=MediaControlCommand.RewindBegin) + else: + await self._atv.remote_control.left(InputAction.Hold) + @async_handle_atvlib_errors async def next(self) -> ucapi.StatusCodes: """Press key next."""