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 from __future__ import annotations
4 
5 import asyncio
6 from collections.abc import Iterable
7 from typing import Any
8 
10  ATTR_DELAY_SECS,
11  ATTR_NUM_REPEATS,
12  DEFAULT_DELAY_SECS,
13  RemoteEntity,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.trigger import PluggableAction
18 
19 from . import LOGGER, PhilipsTVConfigEntry
20 from .coordinator import PhilipsTVDataUpdateCoordinator
21 from .entity import PhilipsJsEntity
22 from .helpers import async_get_turn_on_trigger
23 
24 
26  hass: HomeAssistant,
27  config_entry: PhilipsTVConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Set up the configuration entry."""
31  coordinator = config_entry.runtime_data
32  async_add_entities([PhilipsTVRemote(coordinator)])
33 
34 
36  """Device that sends commands."""
37 
38  _attr_translation_key = "remote"
39 
40  def __init__(
41  self,
42  coordinator: PhilipsTVDataUpdateCoordinator,
43  ) -> None:
44  """Initialize the Philips TV."""
45  super().__init__(coordinator)
46  self._tv_tv = coordinator.api
47  self._attr_unique_id_attr_unique_id = coordinator.unique_id
48  self._turn_on_turn_on = PluggableAction(self.async_write_ha_stateasync_write_ha_state)
49 
50  async def async_added_to_hass(self) -> None:
51  """Handle being added to hass."""
52  await super().async_added_to_hass()
53 
54  if (entry := self.registry_entryregistry_entry) and entry.device_id:
55  self.async_on_removeasync_on_remove(
56  self._turn_on_turn_on.async_register(
57  self.hasshasshass, async_get_turn_on_trigger(entry.device_id)
58  )
59  )
60 
61  @property
62  def is_on(self) -> bool | None:
63  """Return true if device is on."""
64  return bool(
65  self._tv_tv.on and (self._tv_tv.powerstate == "On" or self._tv_tv.powerstate is None)
66  )
67 
68  async def async_turn_on(self, **kwargs: Any) -> None:
69  """Turn the device on."""
70  if self._tv_tv.on and self._tv_tv.powerstate:
71  await self._tv_tv.setPowerState("On")
72  else:
73  await self._turn_on_turn_on.async_run(self.hasshasshass, self._context_context)
74  self.async_write_ha_stateasync_write_ha_state()
75 
76  async def async_turn_off(self, **kwargs: Any) -> None:
77  """Turn the device off."""
78  if self._tv_tv.on:
79  await self._tv_tv.sendKey("Standby")
80  self.async_write_ha_stateasync_write_ha_state()
81  else:
82  LOGGER.debug("Tv was already turned off")
83 
84  async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
85  """Send a command to one device."""
86  num_repeats = kwargs[ATTR_NUM_REPEATS]
87  delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
88 
89  for _ in range(num_repeats):
90  for single_command in command:
91  LOGGER.debug("Sending command %s", single_command)
92  await self._tv_tv.sendKey(single_command)
93  await asyncio.sleep(delay)
None async_send_command(self, Iterable[str] command, **Any kwargs)
Definition: remote.py:84
None __init__(self, PhilipsTVDataUpdateCoordinator coordinator)
Definition: remote.py:43
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_register(HomeAssistant hass, system_health.SystemHealthRegistration register)
dict[str, str] async_get_turn_on_trigger(str device_id)
Definition: turn_on.py:39
None async_setup_entry(HomeAssistant hass, PhilipsTVConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: remote.py:29