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 <erik@montnemery.com>
This commit is contained in:
myMartek 2024-04-16 16:16:32 +02:00 committed by GitHub
parent e9894f8e91
commit 18ac9a7ba5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 12 deletions

View file

@ -5,10 +5,14 @@ from collections.abc import Iterable
import logging import logging
from typing import Any from typing import Any
from pyatv.const import InputAction
from homeassistant.components.remote import ( from homeassistant.components.remote import (
ATTR_DELAY_SECS, ATTR_DELAY_SECS,
ATTR_HOLD_SECS,
ATTR_NUM_REPEATS, ATTR_NUM_REPEATS,
DEFAULT_DELAY_SECS, DEFAULT_DELAY_SECS,
DEFAULT_HOLD_SECS,
RemoteEntity, RemoteEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -29,7 +33,6 @@ COMMAND_TO_ATTRIBUTE = {
"turn_off": ("power", "turn_off"), "turn_off": ("power", "turn_off"),
"volume_up": ("audio", "volume_up"), "volume_up": ("audio", "volume_up"),
"volume_down": ("audio", "volume_down"), "volume_down": ("audio", "volume_down"),
"home_hold": ("remote_control", "home"),
} }
@ -66,6 +69,7 @@ class AppleTVRemote(AppleTVEntity, RemoteEntity):
"""Send a command to one device.""" """Send a command to one device."""
num_repeats = kwargs[ATTR_NUM_REPEATS] num_repeats = kwargs[ATTR_NUM_REPEATS]
delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS) delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
hold_secs = kwargs.get(ATTR_HOLD_SECS, DEFAULT_HOLD_SECS)
if not self.atv: if not self.atv:
_LOGGER.error("Unable to send commands, not connected to %s", self.name) _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") raise ValueError("Command not found. Exiting sequence")
_LOGGER.info("Sending command %s", single_command) _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) await asyncio.sleep(delay)

View file

@ -5,25 +5,37 @@ from unittest.mock import AsyncMock
import pytest import pytest
from homeassistant.components.apple_tv.remote import AppleTVRemote 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( @pytest.mark.parametrize(
("command", "method"), ("command", "method", "hold_secs"),
[ [
("up", "remote_control.up"), ("up", "remote_control.up", 0.0),
("wakeup", "power.turn_on"), ("wakeup", "power.turn_on", 0.0),
("volume_up", "audio.volume_up"), ("volume_up", "audio.volume_up", 0.0),
("home_hold", "remote_control.home"), ("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.""" """Test "send_command" method."""
remote = AppleTVRemote("test", "test", None) remote = AppleTVRemote("test", "test", None)
remote.atv = AsyncMock() remote.atv = AsyncMock()
await remote.async_send_command( 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 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=<InputAction.Hold: 2>)"
)
else:
assert str(remote.atv.method_calls[0]) == f"call.{method}()"