Home Assistant Unofficial Reference 2024.12.1
remote.py
Go to the documentation of this file.
1 """Support MySensors IR transceivers."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Iterable
6 from typing import Any, cast
7 
9  ATTR_COMMAND,
10  RemoteEntity,
11  RemoteEntityFeature,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import Platform
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.dispatcher import async_dispatcher_connect
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import setup_mysensors_platform
20 from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
21 from .entity import MySensorsChildEntity
22 from .helpers import on_unload
23 
24 
26  hass: HomeAssistant,
27  config_entry: ConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Set up this platform for a specific ConfigEntry(==Gateway)."""
31 
32  @callback
33  def async_discover(discovery_info: DiscoveryInfo) -> None:
34  """Discover and add a MySensors remote."""
36  hass,
37  Platform.REMOTE,
38  discovery_info,
39  MySensorsRemote,
40  async_add_entities=async_add_entities,
41  )
42 
43  on_unload(
44  hass,
45  config_entry.entry_id,
47  hass,
48  MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.REMOTE),
49  async_discover,
50  ),
51  )
52 
53 
55  """Representation of a MySensors IR transceiver."""
56 
57  _current_command: str | None = None
58 
59  @property
60  def is_on(self) -> bool | None:
61  """Return True if remote is on."""
62  set_req = self.gateway.const.SetReq
63  value = cast(str | None, self._child_child.values.get(set_req.V_LIGHT))
64  if value is None:
65  return None
66  return value == "1"
67 
68  @property
69  def supported_features(self) -> RemoteEntityFeature:
70  """Flag supported features."""
71  features = RemoteEntityFeature(0)
72  set_req = self.gateway.const.SetReq
73  if set_req.V_IR_RECORD in self._values:
74  features = features | RemoteEntityFeature.LEARN_COMMAND
75  return features
76 
77  async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
78  """Send commands to a device."""
79  for cmd in command:
80  self._current_command_current_command = cmd
81  self.gateway.set_child_value(
82  self.node_id, self.child_id, self.value_type, cmd, ack=1
83  )
84 
85  async def async_learn_command(self, **kwargs: Any) -> None:
86  """Learn a command from a device."""
87  set_req = self.gateway.const.SetReq
88  commands: list[str] | None = kwargs.get(ATTR_COMMAND)
89  if commands is None:
90  raise ValueError("Command not specified for learn_command service")
91 
92  for command in commands:
93  self.gateway.set_child_value(
94  self.node_id, self.child_id, set_req.V_IR_RECORD, command, ack=1
95  )
96 
97  async def async_turn_on(self, **kwargs: Any) -> None:
98  """Turn the IR transceiver on."""
99  set_req = self.gateway.const.SetReq
100  if self._current_command_current_command:
101  self.gateway.set_child_value(
102  self.node_id,
103  self.child_id,
104  self.value_type,
105  self._current_command_current_command,
106  ack=1,
107  )
108  self.gateway.set_child_value(
109  self.node_id, self.child_id, set_req.V_LIGHT, 1, ack=1
110  )
111 
112  async def async_turn_off(self, **kwargs: Any) -> None:
113  """Turn the IR transceiver off."""
114  set_req = self.gateway.const.SetReq
115  self.gateway.set_child_value(
116  self.node_id, self.child_id, set_req.V_LIGHT, 0, ack=1
117  )
118 
119  @callback
120  def _async_update(self) -> None:
121  """Update the controller with the latest value from a device."""
122  super()._async_update()
123  self._current_command_current_command = cast(
124  str | None, self._child_child.values.get(self.value_type)
125  )
None async_send_command(self, Iterable[str] command, **Any kwargs)
Definition: remote.py:77
None on_unload(HomeAssistant hass, GatewayId gateway_id, Callable fnct)
Definition: helpers.py:45
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: remote.py:29
None async_discover(DiscoveryInfo discovery_info)
Definition: sensor.py:217
list[MySensorsChildEntity]|None setup_mysensors_platform(HomeAssistant hass, Platform domain, DiscoveryInfo discovery_info, type[MySensorsChildEntity]|Mapping[SensorType, type[MySensorsChildEntity]] device_class,(tuple|None) device_args=None, Callable|None async_add_entities=None)
Definition: __init__.py:112
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103