Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base entity for Android TV Remote."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from androidtvremote2 import AndroidTVRemote, ConnectionClosed
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
11 from homeassistant.core import callback
12 from homeassistant.exceptions import HomeAssistantError
13 from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
14 from homeassistant.helpers.entity import Entity
15 
16 from .const import CONF_APPS, DOMAIN
17 
18 
20  """Android TV Remote Base Entity."""
21 
22  _attr_name = None
23  _attr_has_entity_name = True
24  _attr_should_poll = False
25 
26  def __init__(self, api: AndroidTVRemote, config_entry: ConfigEntry) -> None:
27  """Initialize the entity."""
28  self._api_api = api
29  self._host_host = config_entry.data[CONF_HOST]
30  self._name_name = config_entry.data[CONF_NAME]
31  self._apps: dict[str, Any] = config_entry.options.get(CONF_APPS, {})
32  self._attr_unique_id_attr_unique_id = config_entry.unique_id
33  self._attr_is_on_attr_is_on = api.is_on
34  device_info = api.device_info
35  assert config_entry.unique_id
36  assert device_info
37  self._attr_device_info_attr_device_info = DeviceInfo(
38  connections={(CONNECTION_NETWORK_MAC, config_entry.data[CONF_MAC])},
39  identifiers={(DOMAIN, config_entry.unique_id)},
40  name=self._name_name,
41  manufacturer=device_info["manufacturer"],
42  model=device_info["model"],
43  )
44 
45  @callback
46  def _is_available_updated(self, is_available: bool) -> None:
47  """Update the state when the device is ready to receive commands or is unavailable."""
48  self._attr_available_attr_available = is_available
49  self.async_write_ha_stateasync_write_ha_state()
50 
51  @callback
52  def _is_on_updated(self, is_on: bool) -> None:
53  """Update the state when device turns on or off."""
54  self._attr_is_on_attr_is_on = is_on
55  self.async_write_ha_stateasync_write_ha_state()
56 
57  async def async_added_to_hass(self) -> None:
58  """Register callbacks."""
59  self._api_api.add_is_available_updated_callback(self._is_available_updated_is_available_updated)
60  self._api_api.add_is_on_updated_callback(self._is_on_updated_is_on_updated)
61 
62  async def async_will_remove_from_hass(self) -> None:
63  """Remove callbacks."""
64  self._api_api.remove_is_available_updated_callback(self._is_available_updated_is_available_updated)
65  self._api_api.remove_is_on_updated_callback(self._is_on_updated_is_on_updated)
66 
67  def _send_key_command(self, key_code: str, direction: str = "SHORT") -> None:
68  """Send a key press to Android TV.
69 
70  This does not block; it buffers the data and arranges for it to be sent out asynchronously.
71  """
72  try:
73  self._api_api.send_key_command(key_code, direction)
74  except ConnectionClosed as exc:
75  raise HomeAssistantError(
76  "Connection to Android TV device is closed"
77  ) from exc
78 
79  def _send_launch_app_command(self, app_link: str) -> None:
80  """Launch an app on Android TV.
81 
82  This does not block; it buffers the data and arranges for it to be sent out asynchronously.
83  """
84  try:
85  self._api_api.send_launch_app_command(app_link)
86  except ConnectionClosed as exc:
87  raise HomeAssistantError(
88  "Connection to Android TV device is closed"
89  ) from exc
None _send_key_command(self, str key_code, str direction="SHORT")
Definition: entity.py:67
None __init__(self, AndroidTVRemote api, ConfigEntry config_entry)
Definition: entity.py:26