Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InputAction support to Companion Protocol for Hid Commands #2516

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions pyatv/protocols/companion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions tests/protocols/companion/test_companion_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ async def test_remote_control_buttons(companion_client, companion_state, button)
assert companion_state.latest_button == button


@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):
duration = companion_state.duration
await companion_client.remote_control.skip_forward()
Expand Down