Skip to content

Commit

Permalink
Apple TV: Use replacement commands for deprecated ones (home-assistan…
Browse files Browse the repository at this point in the history
…t#102056)

Co-authored-by: Robert Resch <robert@resch.dev>
  • Loading branch information
amitfin and edenhaus authored Oct 26, 2023
1 parent 087df10 commit edf2e42
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
17 changes: 16 additions & 1 deletion homeassistant/components/apple_tv/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
_LOGGER = logging.getLogger(__name__)

PARALLEL_UPDATES = 0
COMMAND_TO_ATTRIBUTE = {
"wakeup": ("power", "turn_on"),
"suspend": ("power", "turn_off"),
"turn_on": ("power", "turn_on"),
"turn_off": ("power", "turn_off"),
"volume_up": ("audio", "volume_up"),
"volume_down": ("audio", "volume_down"),
"home_hold": ("remote_control", "home"),
}


async def async_setup_entry(
Expand Down Expand Up @@ -61,7 +70,13 @@ async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> Non

for _ in range(num_repeats):
for single_command in command:
attr_value = getattr(self.atv.remote_control, single_command, None)
attr_value = None
if attributes := COMMAND_TO_ATTRIBUTE.get(single_command):
attr_value = self.atv
for attr_name in attributes:
attr_value = getattr(attr_value, attr_name, None)
if not attr_value:
attr_value = getattr(self.atv.remote_control, single_command, None)
if not attr_value:
raise ValueError("Command not found. Exiting sequence")

Expand Down
28 changes: 28 additions & 0 deletions tests/components/apple_tv/test_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Test apple_tv remote."""
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


@pytest.mark.parametrize(
("command", "method"),
[
("up", "remote_control.up"),
("wakeup", "power.turn_on"),
("volume_up", "audio.volume_up"),
("home_hold", "remote_control.home"),
],
ids=["up", "wakeup", "volume_up", "home_hold"],
)
async def test_send_command(command: str, method: str) -> 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}
)
assert len(remote.atv.method_calls) == 1
assert str(remote.atv.method_calls[0]) == f"call.{method}()"

0 comments on commit edf2e42

Please sign in to comment.