From f20d6f5f5430782a31c4cf3b479f89752fe2bd46 Mon Sep 17 00:00:00 2001 From: Daniel Morgan Date: Sat, 5 Oct 2024 12:52:12 +0000 Subject: [PATCH 1/3] Add Input.Action to Companion --- pyatv/protocols/companion/__init__.py | 27 ++++++-- scripts/setup_dev_env.sh | 2 +- .../companion/test_companion_functional.py | 69 +++++++++++++++++++ 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/pyatv/protocols/companion/__init__.py b/pyatv/protocols/companion/__init__.py index 7d48dda76..1dd281407 100644 --- a/pyatv/protocols/companion/__init__.py +++ b/pyatv/protocols/companion/__init__.py @@ -378,12 +378,29 @@ async def screensaver(self) -> None: await self._press_button(HidCommand.Screensaver) async def _press_button( - self, command: HidCommand, action: InputAction = InputAction.SingleTap + self, + command: HidCommand, + action: InputAction = InputAction.SingleTap, + delay: float = 1, ) -> None: - if action != InputAction.SingleTap: - raise NotImplementedError(f"{action} not supported for {command} (yet)") - await self.api.hid_command(True, command) - await self.api.hid_command(False, command) + if action == InputAction.SingleTap: + await self.api.hid_command(True, command) + await self.api.hid_command(False, command) + + elif action == InputAction.Hold: + await self.api.hid_command(True, command) + await asyncio.sleep(delay) + await self.api.hid_command(False, command) + + elif action == InputAction.DoubleTap: + # First press + await self.api.hid_command(True, command) + await self.api.hid_command(False, command) + # Second press + await self.api.hid_command(True, command) + await self.api.hid_command(False, command) + else: + raise exceptions.NotSupportedError(f"unsupported input action: {action}") class CompanionAudio(Audio): diff --git a/scripts/setup_dev_env.sh b/scripts/setup_dev_env.sh index 8a346d3df..cd621e216 100755 --- a/scripts/setup_dev_env.sh +++ b/scripts/setup_dev_env.sh @@ -29,7 +29,7 @@ $found_version -m venv . echo "-> Activating virtual environment..." source bin/activate -sed -i '' 's/false/true/' pyvenv.cfg +sed -i 's/false/true/' pyvenv.cfg echo "-> Upgrading pip..." pip install --upgrade pip diff --git a/tests/protocols/companion/test_companion_functional.py b/tests/protocols/companion/test_companion_functional.py index e4c55c63e..11ca0c18c 100644 --- a/tests/protocols/companion/test_companion_functional.py +++ b/tests/protocols/companion/test_companion_functional.py @@ -218,6 +218,75 @@ async def test_remote_control_buttons(companion_client, companion_state, button) assert companion_state.latest_button == button +async def test_button_up_actions(self): + await self.atv.remote_control.up(action=InputAction.DoubleTap) + await self.wait_for_button_press("up", InputAction.DoubleTap) + + await self.atv.remote_control.up(action=InputAction.Hold) + await self.wait_for_button_press("up", InputAction.Hold) + + +async def test_button_down_actions(self): + await self.atv.remote_control.down(action=InputAction.DoubleTap) + await self.wait_for_button_press("down", InputAction.DoubleTap) + + await self.atv.remote_control.down(action=InputAction.Hold) + await self.wait_for_button_press("down", InputAction.Hold) + + +async def test_button_left_actions(self): + await self.atv.remote_control.left(action=InputAction.DoubleTap) + await self.wait_for_button_press("left", InputAction.DoubleTap) + + await self.atv.remote_control.left(action=InputAction.Hold) + await self.wait_for_button_press("left", InputAction.Hold) + + +async def test_button_right_actions(self): + await self.atv.remote_control.right(action=InputAction.DoubleTap) + await self.wait_for_button_press("right", InputAction.DoubleTap) + + await self.atv.remote_control.right(action=InputAction.Hold) + await self.wait_for_button_press("right", InputAction.Hold) + + +async def test_button_top_menu(self): + await self.atv.remote_control.top_menu() + await self.wait_for_button_press("top_menu", InputAction.SingleTap) + + +async def test_button_home(self): + await self.atv.remote_control.home() + await self.wait_for_button_press("home", InputAction.SingleTap) + + await self.atv.remote_control.home(action=InputAction.DoubleTap) + await self.wait_for_button_press("home", InputAction.DoubleTap) + + await self.atv.remote_control.home(action=InputAction.Hold) + await self.wait_for_button_press("home", InputAction.Hold) + + +async def test_button_home_hold(self): + await self.atv.remote_control.home_hold() + await self.wait_for_button_press("home", InputAction.Hold) + + +async def test_button_select_actions(self): + await self.atv.remote_control.select(action=InputAction.DoubleTap) + await self.wait_for_button_press("select", InputAction.DoubleTap) + + await self.atv.remote_control.select(action=InputAction.Hold) + await self.wait_for_button_press("select", InputAction.Hold) + + +async def test_button_menu_actions(self): + await self.atv.remote_control.menu(action=InputAction.DoubleTap) + await self.wait_for_button_press("menu", InputAction.DoubleTap) + + await self.atv.remote_control.menu(action=InputAction.Hold) + await self.wait_for_button_press("menu", InputAction.Hold) + + async def test_remote_control_skip_forward_backward(companion_client, companion_state): duration = companion_state.duration await companion_client.remote_control.skip_forward() From 83d979c5c0dd1b849b845026fd7ecf5a59a32f8d Mon Sep 17 00:00:00 2001 From: dmrgn1991 <120037490+dmrgn1991@users.noreply.github.com> Date: Sat, 5 Oct 2024 08:55:01 -0400 Subject: [PATCH 2/3] Update setup_dev_env.sh --- scripts/setup_dev_env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/setup_dev_env.sh b/scripts/setup_dev_env.sh index cd621e216..8a346d3df 100755 --- a/scripts/setup_dev_env.sh +++ b/scripts/setup_dev_env.sh @@ -29,7 +29,7 @@ $found_version -m venv . echo "-> Activating virtual environment..." source bin/activate -sed -i 's/false/true/' pyvenv.cfg +sed -i '' 's/false/true/' pyvenv.cfg echo "-> Upgrading pip..." pip install --upgrade pip From da2713389c64298e146296a6d9ec79f9a916cc3d Mon Sep 17 00:00:00 2001 From: dmrgn1991 <120037490+dmrgn1991@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:11:26 -0400 Subject: [PATCH 3/3] Update test_companion_functional.py --- .../companion/test_companion_functional.py | 89 +++++-------------- 1 file changed, 22 insertions(+), 67 deletions(-) diff --git a/tests/protocols/companion/test_companion_functional.py b/tests/protocols/companion/test_companion_functional.py index 11ca0c18c..75dd93c9e 100644 --- a/tests/protocols/companion/test_companion_functional.py +++ b/tests/protocols/companion/test_companion_functional.py @@ -218,73 +218,28 @@ async def test_remote_control_buttons(companion_client, companion_state, button) assert companion_state.latest_button == button -async def test_button_up_actions(self): - await self.atv.remote_control.up(action=InputAction.DoubleTap) - await self.wait_for_button_press("up", InputAction.DoubleTap) - - await self.atv.remote_control.up(action=InputAction.Hold) - await self.wait_for_button_press("up", InputAction.Hold) - - -async def test_button_down_actions(self): - await self.atv.remote_control.down(action=InputAction.DoubleTap) - await self.wait_for_button_press("down", InputAction.DoubleTap) - - await self.atv.remote_control.down(action=InputAction.Hold) - await self.wait_for_button_press("down", InputAction.Hold) - - -async def test_button_left_actions(self): - await self.atv.remote_control.left(action=InputAction.DoubleTap) - await self.wait_for_button_press("left", InputAction.DoubleTap) - - await self.atv.remote_control.left(action=InputAction.Hold) - await self.wait_for_button_press("left", InputAction.Hold) - - -async def test_button_right_actions(self): - await self.atv.remote_control.right(action=InputAction.DoubleTap) - await self.wait_for_button_press("right", InputAction.DoubleTap) - - await self.atv.remote_control.right(action=InputAction.Hold) - await self.wait_for_button_press("right", InputAction.Hold) - - -async def test_button_top_menu(self): - await self.atv.remote_control.top_menu() - await self.wait_for_button_press("top_menu", InputAction.SingleTap) - - -async def test_button_home(self): - await self.atv.remote_control.home() - await self.wait_for_button_press("home", InputAction.SingleTap) - - await self.atv.remote_control.home(action=InputAction.DoubleTap) - await self.wait_for_button_press("home", InputAction.DoubleTap) - - await self.atv.remote_control.home(action=InputAction.Hold) - await self.wait_for_button_press("home", InputAction.Hold) - - -async def test_button_home_hold(self): - await self.atv.remote_control.home_hold() - await self.wait_for_button_press("home", InputAction.Hold) - - -async def test_button_select_actions(self): - await self.atv.remote_control.select(action=InputAction.DoubleTap) - await self.wait_for_button_press("select", InputAction.DoubleTap) - - await self.atv.remote_control.select(action=InputAction.Hold) - await self.wait_for_button_press("select", InputAction.Hold) - - -async def test_button_menu_actions(self): - await self.atv.remote_control.menu(action=InputAction.DoubleTap) - await self.wait_for_button_press("menu", InputAction.DoubleTap) - - await self.atv.remote_control.menu(action=InputAction.Hold) - await self.wait_for_button_press("menu", InputAction.Hold) +@pytest.mark.parametrize( + "button,action", + [ + ("up", InputAction.DoubleTap), + ("up", InputAction.Hold), + ("down", InputAction.DoubleTap), + ("down", InputAction.Hold), + ("left", InputAction.DoubleTap), + ("left", InputAction.Hold), + ("right", InputAction.DoubleTap), + ("right", InputAction.Hold), + ("select", InputAction.DoubleTap), + ("select", InputAction.Hold), + ("menu", InputAction.DoubleTap), + ("menu", InputAction.Hold), + ("home", InputAction.DoubleTap), + ("home", InputAction.Hold), + ], +) +async def test_button_actions(companion_client, companion_state, button, action): + await getattr(companion_client.remote_control, button)(action=action) + assert companion_state.latest_button == button async def test_remote_control_skip_forward_backward(companion_client, companion_state):