From 18ac9a7ba5d26a49785066e8fd581659a045208c Mon Sep 17 00:00:00 2001 From: myMartek Date: Tue, 16 Apr 2024 16:16:32 +0200 Subject: [PATCH] Add select hold to AppleTVs remote entity as possible command (#105764) * Fixed home hold and added select hold * Fixed home hold and added select hold * Removed select_hold for now * Fixed wrong import block sorting * Fixed unit tests for AppleTV * Added select hold command to AppleTV integration * Removed home_hold and added hold_secs option for remote commands * Added DEFAULT_HOLD_SECS --------- Co-authored-by: Erik Montnemery --- homeassistant/components/apple_tv/remote.py | 13 +++++++-- tests/components/apple_tv/test_remote.py | 32 ++++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/apple_tv/remote.py b/homeassistant/components/apple_tv/remote.py index 822a9c3306a..aed2c0ae3f0 100644 --- a/homeassistant/components/apple_tv/remote.py +++ b/homeassistant/components/apple_tv/remote.py @@ -5,10 +5,14 @@ from collections.abc import Iterable import logging from typing import Any +from pyatv.const import InputAction + from homeassistant.components.remote import ( ATTR_DELAY_SECS, + ATTR_HOLD_SECS, ATTR_NUM_REPEATS, DEFAULT_DELAY_SECS, + DEFAULT_HOLD_SECS, RemoteEntity, ) from homeassistant.config_entries import ConfigEntry @@ -29,7 +33,6 @@ COMMAND_TO_ATTRIBUTE = { "turn_off": ("power", "turn_off"), "volume_up": ("audio", "volume_up"), "volume_down": ("audio", "volume_down"), - "home_hold": ("remote_control", "home"), } @@ -66,6 +69,7 @@ class AppleTVRemote(AppleTVEntity, RemoteEntity): """Send a command to one device.""" num_repeats = kwargs[ATTR_NUM_REPEATS] delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS) + hold_secs = kwargs.get(ATTR_HOLD_SECS, DEFAULT_HOLD_SECS) if not self.atv: _LOGGER.error("Unable to send commands, not connected to %s", self.name) @@ -84,5 +88,10 @@ class AppleTVRemote(AppleTVEntity, RemoteEntity): raise ValueError("Command not found. Exiting sequence") _LOGGER.info("Sending command %s", single_command) - await attr_value() + + if hold_secs >= 1: + await attr_value(action=InputAction.Hold) + else: + await attr_value() + await asyncio.sleep(delay) diff --git a/tests/components/apple_tv/test_remote.py b/tests/components/apple_tv/test_remote.py index f831518d75a..bc8a0e6a2dd 100644 --- a/tests/components/apple_tv/test_remote.py +++ b/tests/components/apple_tv/test_remote.py @@ -5,25 +5,37 @@ from unittest.mock import AsyncMock import pytest from homeassistant.components.apple_tv.remote import AppleTVRemote -from homeassistant.components.remote import ATTR_DELAY_SECS, ATTR_NUM_REPEATS +from homeassistant.components.remote import ( + ATTR_DELAY_SECS, + ATTR_HOLD_SECS, + ATTR_NUM_REPEATS, +) @pytest.mark.parametrize( - ("command", "method"), + ("command", "method", "hold_secs"), [ - ("up", "remote_control.up"), - ("wakeup", "power.turn_on"), - ("volume_up", "audio.volume_up"), - ("home_hold", "remote_control.home"), + ("up", "remote_control.up", 0.0), + ("wakeup", "power.turn_on", 0.0), + ("volume_up", "audio.volume_up", 0.0), + ("home", "remote_control.home", 1.0), + ("select", "remote_control.select", 1.0), ], - ids=["up", "wakeup", "volume_up", "home_hold"], + ids=["up", "wakeup", "volume_up", "home", "select"], ) -async def test_send_command(command: str, method: str) -> None: +async def test_send_command(command: str, method: str, hold_secs: float) -> None: """Test "send_command" method.""" remote = AppleTVRemote("test", "test", None) remote.atv = AsyncMock() await remote.async_send_command( - [command], **{ATTR_NUM_REPEATS: 1, ATTR_DELAY_SECS: 0} + [command], + **{ATTR_NUM_REPEATS: 1, ATTR_DELAY_SECS: 0, ATTR_HOLD_SECS: hold_secs}, ) assert len(remote.atv.method_calls) == 1 - assert str(remote.atv.method_calls[0]) == f"call.{method}()" + if hold_secs >= 1: + assert ( + str(remote.atv.method_calls[0]) + == f"call.{method}(action=)" + ) + else: + assert str(remote.atv.method_calls[0]) == f"call.{method}()"