Home Assistant Unofficial Reference 2024.12.1
remote.py
Go to the documentation of this file.
1 """Remote control support for Android TV Remote."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from collections.abc import Iterable
7 from typing import Any
8 
10  ATTR_ACTIVITY,
11  ATTR_DELAY_SECS,
12  ATTR_HOLD_SECS,
13  ATTR_NUM_REPEATS,
14  DEFAULT_DELAY_SECS,
15  DEFAULT_HOLD_SECS,
16  DEFAULT_NUM_REPEATS,
17  RemoteEntity,
18  RemoteEntityFeature,
19 )
20 from homeassistant.core import HomeAssistant, callback
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from . import AndroidTVRemoteConfigEntry
24 from .const import CONF_APP_NAME
25 from .entity import AndroidTVRemoteBaseEntity
26 
27 PARALLEL_UPDATES = 0
28 
29 
31  hass: HomeAssistant,
32  config_entry: AndroidTVRemoteConfigEntry,
33  async_add_entities: AddEntitiesCallback,
34 ) -> None:
35  """Set up the Android TV remote entity based on a config entry."""
36  api = config_entry.runtime_data
37  async_add_entities([AndroidTVRemoteEntity(api, config_entry)])
38 
39 
41  """Android TV Remote Entity."""
42 
43  _attr_supported_features = RemoteEntityFeature.ACTIVITY
44 
45  def _update_current_app(self, current_app: str) -> None:
46  """Update current app info."""
47  self._attr_current_activity_attr_current_activity = (
48  self._apps[current_app].get(CONF_APP_NAME, current_app)
49  if current_app in self._apps
50  else current_app
51  )
52 
53  @callback
54  def _current_app_updated(self, current_app: str) -> None:
55  """Update the state when the current app changes."""
56  self._update_current_app_update_current_app(current_app)
57  self.async_write_ha_stateasync_write_ha_state()
58 
59  async def async_added_to_hass(self) -> None:
60  """Register callbacks."""
61  await super().async_added_to_hass()
62 
63  self._attr_activity_list_attr_activity_list = [
64  app.get(CONF_APP_NAME, "") for app in self._apps.values()
65  ]
66  self._update_current_app_update_current_app(self._api_api.current_app)
67  self._api_api.add_current_app_updated_callback(self._current_app_updated_current_app_updated)
68 
69  async def async_will_remove_from_hass(self) -> None:
70  """Remove callbacks."""
71  await super().async_will_remove_from_hass()
72 
73  self._api_api.remove_current_app_updated_callback(self._current_app_updated_current_app_updated)
74 
75  async def async_turn_on(self, **kwargs: Any) -> None:
76  """Turn the Android TV on."""
77  if not self.is_onis_on:
78  self._send_key_command_send_key_command("POWER")
79  activity = kwargs.get(ATTR_ACTIVITY, "")
80  if activity:
81  activity = next(
82  (
83  app_id
84  for app_id, app in self._apps.items()
85  if app.get(CONF_APP_NAME, "") == activity
86  ),
87  activity,
88  )
89  self._send_launch_app_command_send_launch_app_command(activity)
90 
91  async def async_turn_off(self, **kwargs: Any) -> None:
92  """Turn the Android TV off."""
93  if self.is_onis_on:
94  self._send_key_command_send_key_command("POWER")
95 
96  async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
97  """Send commands to one device."""
98  num_repeats = kwargs.get(ATTR_NUM_REPEATS, DEFAULT_NUM_REPEATS)
99  delay_secs = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
100  hold_secs = kwargs.get(ATTR_HOLD_SECS, DEFAULT_HOLD_SECS)
101 
102  for _ in range(num_repeats):
103  for single_command in command:
104  if hold_secs:
105  self._send_key_command_send_key_command(single_command, "START_LONG")
106  await asyncio.sleep(hold_secs)
107  self._send_key_command_send_key_command(single_command, "END_LONG")
108  else:
109  self._send_key_command_send_key_command(single_command, "SHORT")
110  await asyncio.sleep(delay_secs)
None _send_key_command(self, str key_code, str direction="SHORT")
Definition: entity.py:67
None async_send_command(self, Iterable[str] command, **Any kwargs)
Definition: remote.py:96
None async_setup_entry(HomeAssistant hass, AndroidTVRemoteConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: remote.py:34
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88