Home Assistant Unofficial Reference 2024.12.1
remote.py
Go to the documentation of this file.
1 """Support for the AndroidTV remote."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Iterable
6 import logging
7 from typing import Any
8 
9 from androidtv.constants import KEYS
10 
11 from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ServiceValidationError
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .const import CONF_TURN_OFF_COMMAND, CONF_TURN_ON_COMMAND, DOMAIN
18 from .entity import AndroidTVEntity, adb_decorator
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
24  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
25 ) -> None:
26  """Set up the AndroidTV remote from a config entry."""
28 
29 
31  """Device that sends commands to a AndroidTV."""
32 
33  _attr_name = None
34  _attr_should_poll = False
35 
36  @adb_decorator()
37  async def async_turn_on(self, **kwargs: Any) -> None:
38  """Turn on the device."""
39  options = self._entry_runtime_data_entry_runtime_data.dev_opt
40  if turn_on_cmd := options.get(CONF_TURN_ON_COMMAND):
41  await self.aftvaftv.adb_shell(turn_on_cmd)
42  else:
43  await self.aftvaftv.turn_on()
44 
45  @adb_decorator()
46  async def async_turn_off(self, **kwargs: Any) -> None:
47  """Turn off the device."""
48  options = self._entry_runtime_data_entry_runtime_data.dev_opt
49  if turn_off_cmd := options.get(CONF_TURN_OFF_COMMAND):
50  await self.aftvaftv.adb_shell(turn_off_cmd)
51  else:
52  await self.aftvaftv.turn_off()
53 
54  @adb_decorator()
55  async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
56  """Send a command to a device."""
57 
58  num_repeats = kwargs[ATTR_NUM_REPEATS]
59  command_list = []
60  for cmd in command:
61  if key := KEYS.get(cmd):
62  command_list.append(f"input keyevent {key}")
63  else:
64  command_list.append(cmd)
65 
66  for _ in range(num_repeats):
67  for cmd in command_list:
68  try:
69  await self.aftvaftv.adb_shell(cmd)
70  except UnicodeDecodeError as ex:
72  translation_domain=DOMAIN,
73  translation_key="failed_send",
74  translation_placeholders={"cmd": cmd},
75  ) from ex
None async_send_command(self, Iterable[str] command, **Any kwargs)
Definition: remote.py:55
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: remote.py:25