Home Assistant Unofficial Reference 2024.12.1
remote.py
Go to the documentation of this file.
1 """Remote control support for Apple TV."""
2 
3 import asyncio
4 from collections.abc import Iterable
5 import logging
6 from typing import Any
7 
8 from pyatv.const import InputAction
9 
11  ATTR_DELAY_SECS,
12  ATTR_HOLD_SECS,
13  ATTR_NUM_REPEATS,
14  DEFAULT_DELAY_SECS,
15  DEFAULT_HOLD_SECS,
16  RemoteEntity,
17 )
18 from homeassistant.const import CONF_NAME
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from . import AppleTvConfigEntry
23 from .entity import AppleTVEntity
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 PARALLEL_UPDATES = 0
28 COMMAND_TO_ATTRIBUTE = {
29  "wakeup": ("power", "turn_on"),
30  "suspend": ("power", "turn_off"),
31  "turn_on": ("power", "turn_on"),
32  "turn_off": ("power", "turn_off"),
33  "volume_up": ("audio", "volume_up"),
34  "volume_down": ("audio", "volume_down"),
35 }
36 
37 
39  hass: HomeAssistant,
40  config_entry: AppleTvConfigEntry,
41  async_add_entities: AddEntitiesCallback,
42 ) -> None:
43  """Load Apple TV remote based on a config entry."""
44  name: str = config_entry.data[CONF_NAME]
45  # apple_tv config entries always have a unique id
46  assert config_entry.unique_id is not None
47  manager = config_entry.runtime_data
48  async_add_entities([AppleTVRemote(name, config_entry.unique_id, manager)])
49 
50 
52  """Device that sends commands to an Apple TV."""
53 
54  @property
55  def is_on(self) -> bool:
56  """Return true if device is on."""
57  return self.atvatv is not None
58 
59  async def async_turn_on(self, **kwargs: Any) -> None:
60  """Turn the device on."""
61  await self.managermanager.connect()
62 
63  async def async_turn_off(self, **kwargs: Any) -> None:
64  """Turn the device off."""
65  await self.managermanager.disconnect()
66 
67  async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
68  """Send a command to one device."""
69  num_repeats = kwargs[ATTR_NUM_REPEATS]
70  delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
71  hold_secs = kwargs.get(ATTR_HOLD_SECS, DEFAULT_HOLD_SECS)
72 
73  if not self.atvatv:
74  _LOGGER.error("Unable to send commands, not connected to %s", self.namename)
75  return
76 
77  for _ in range(num_repeats):
78  for single_command in command:
79  attr_value: Any = None
80  if attributes := COMMAND_TO_ATTRIBUTE.get(single_command):
81  attr_value = self.atvatv
82  for attr_name in attributes:
83  attr_value = getattr(attr_value, attr_name, None)
84  if not attr_value:
85  attr_value = getattr(self.atvatv.remote_control, single_command, None)
86  if not attr_value:
87  raise ValueError("Command not found. Exiting sequence")
88 
89  _LOGGER.debug("Sending command %s", single_command)
90 
91  if hold_secs >= 1:
92  await attr_value(action=InputAction.Hold)
93  else:
94  await attr_value()
95 
96  await asyncio.sleep(delay)
None async_send_command(self, Iterable[str] command, **Any kwargs)
Definition: remote.py:67
str|UndefinedType|None name(self)
Definition: entity.py:738
None async_setup_entry(HomeAssistant hass, AppleTvConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: remote.py:42