Home Assistant Unofficial Reference 2024.12.1
remote.py
Go to the documentation of this file.
1 """Support for the DIRECTV remote."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Iterable
6 from datetime import timedelta
7 import logging
8 from typing import Any
9 
10 from directv import DIRECTV, DIRECTVError
11 
12 from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .const import DOMAIN
18 from .entity import DIRECTVEntity
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 SCAN_INTERVAL = timedelta(minutes=2)
23 
24 
26  hass: HomeAssistant,
27  entry: ConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Load DirecTV remote based on a config entry."""
31  dtv = hass.data[DOMAIN][entry.entry_id]
32 
34  (
36  dtv=dtv,
37  name=str.title(location.name),
38  address=location.address,
39  )
40  for location in dtv.device.locations
41  ),
42  True,
43  )
44 
45 
47  """Device that sends commands to a DirecTV receiver."""
48 
49  def __init__(self, *, dtv: DIRECTV, name: str, address: str = "0") -> None:
50  """Initialize DirecTV remote."""
51  super().__init__(
52  dtv=dtv,
53  name=name,
54  address=address,
55  )
56 
57  self._attr_unique_id_attr_unique_id = self._device_id_device_id
58  self._attr_available_attr_available = False
59  self._attr_is_on_attr_is_on = True
60 
61  async def async_update(self) -> None:
62  """Update device state."""
63  status = await self.dtvdtv.status(self._address_address)
64 
65  if status in ("active", "standby"):
66  self._attr_available_attr_available = True
67  self._attr_is_on_attr_is_on = status == "active"
68  else:
69  self._attr_available_attr_available = False
70  self._attr_is_on_attr_is_on = False
71 
72  async def async_turn_on(self, **kwargs: Any) -> None:
73  """Turn the device on."""
74  await self.dtvdtv.remote("poweron", self._address_address)
75 
76  async def async_turn_off(self, **kwargs: Any) -> None:
77  """Turn the device off."""
78  await self.dtvdtv.remote("poweroff", self._address_address)
79 
80  async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
81  """Send a command to a device.
82 
83  Supported keys: power, poweron, poweroff, format,
84  pause, rew, replay, stop, advance, ffwd, record,
85  play, guide, active, list, exit, back, menu, info,
86  up, down, left, right, select, red, green, yellow,
87  blue, chanup, chandown, prev, 0, 1, 2, 3, 4, 5,
88  6, 7, 8, 9, dash, enter
89  """
90  num_repeats = kwargs[ATTR_NUM_REPEATS]
91 
92  for _ in range(num_repeats):
93  for single_command in command:
94  try:
95  await self.dtvdtv.remote(single_command, self._address_address)
96  except DIRECTVError:
97  _LOGGER.exception(
98  "Sending command %s to device %s failed",
99  single_command,
100  self._device_id_device_id,
101  )
None async_send_command(self, Iterable[str] command, **Any kwargs)
Definition: remote.py:80
None __init__(self, *DIRECTV dtv, str name, str address="0")
Definition: remote.py:49
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: remote.py:29