Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for RFXtrx switches."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import RFXtrx as rfxtrxmod
9 
10 from homeassistant.components.switch import SwitchEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_COMMAND_OFF, CONF_COMMAND_ON, STATE_ON
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.entity import Entity
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import DeviceTuple, async_setup_platform_entry, get_pt2262_cmd
18 from .const import (
19  COMMAND_OFF_LIST,
20  COMMAND_ON_LIST,
21  CONF_DATA_BITS,
22  DEVICE_PACKET_TYPE_LIGHTING4,
23  DOMAIN,
24 )
25 from .entity import RfxtrxCommandEntity
26 
27 DATA_SWITCH = f"{DOMAIN}_switch"
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 
32 def supported(event: rfxtrxmod.RFXtrxEvent) -> bool:
33  """Return whether an event supports switch."""
34  return (
35  isinstance(event.device, rfxtrxmod.LightingDevice)
36  and not event.device.known_to_be_dimmable
37  and not event.device.known_to_be_rollershutter
38  or isinstance(event.device, rfxtrxmod.RfyDevice)
39  )
40 
41 
43  hass: HomeAssistant,
44  config_entry: ConfigEntry,
45  async_add_entities: AddEntitiesCallback,
46 ) -> None:
47  """Set up config entry."""
48 
49  def _constructor(
50  event: rfxtrxmod.RFXtrxEvent,
51  auto: rfxtrxmod.RFXtrxEvent | None,
52  device_id: DeviceTuple,
53  entity_info: dict[str, Any],
54  ) -> list[Entity]:
55  return [
57  event.device,
58  device_id,
59  entity_info.get(CONF_DATA_BITS),
60  entity_info.get(CONF_COMMAND_ON),
61  entity_info.get(CONF_COMMAND_OFF),
62  event=event if auto else None,
63  )
64  ]
65 
67  hass, config_entry, async_add_entities, supported, _constructor
68  )
69 
70 
72  """Representation of a RFXtrx switch."""
73 
74  def __init__(
75  self,
76  device: rfxtrxmod.RFXtrxDevice,
77  device_id: DeviceTuple,
78  data_bits: int | None = None,
79  cmd_on: int | None = None,
80  cmd_off: int | None = None,
81  event: rfxtrxmod.RFXtrxEvent | None = None,
82  ) -> None:
83  """Initialize the RFXtrx switch."""
84  super().__init__(device, device_id, event=event)
85  self._data_bits_data_bits = data_bits
86  self._cmd_on_cmd_on = cmd_on
87  self._cmd_off_cmd_off = cmd_off
88 
89  async def async_added_to_hass(self) -> None:
90  """Restore device state."""
91  await super().async_added_to_hass()
92 
93  if self._event_event is None:
94  old_state = await self.async_get_last_stateasync_get_last_state()
95  if old_state is not None:
96  self._attr_is_on_attr_is_on = old_state.state == STATE_ON
97 
98  def _apply_event_lighting4(self, event: rfxtrxmod.RFXtrxEvent) -> None:
99  """Apply event for a lighting 4 device."""
100  if self._data_bits_data_bits is not None:
101  cmdstr = get_pt2262_cmd(event.device.id_string, self._data_bits_data_bits)
102  assert cmdstr
103  cmd = int(cmdstr, 16)
104  if cmd == self._cmd_on_cmd_on:
105  self._attr_is_on_attr_is_on = True
106  elif cmd == self._cmd_off_cmd_off:
107  self._attr_is_on_attr_is_on = False
108  else:
109  self._attr_is_on_attr_is_on = True
110 
111  def _apply_event_standard(self, event: rfxtrxmod.RFXtrxEvent) -> None:
112  assert isinstance(event, rfxtrxmod.ControlEvent)
113  if event.values["Command"] in COMMAND_ON_LIST:
114  self._attr_is_on_attr_is_on = True
115  elif event.values["Command"] in COMMAND_OFF_LIST:
116  self._attr_is_on_attr_is_on = False
117 
118  def _apply_event(self, event: rfxtrxmod.RFXtrxEvent) -> None:
119  """Apply command from rfxtrx."""
120  super()._apply_event(event)
121  if event.device.packettype == DEVICE_PACKET_TYPE_LIGHTING4:
122  self._apply_event_lighting4_apply_event_lighting4(event)
123  else:
124  self._apply_event_standard_apply_event_standard(event)
125 
126  @callback
128  self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple
129  ) -> None:
130  """Check if event applies to me and update."""
131  if self._event_applies_event_applies(event, device_id):
132  self._apply_event_apply_event_apply_event(event)
133 
134  self.async_write_ha_stateasync_write_ha_state()
135 
136  async def async_turn_on(self, **kwargs: Any) -> None:
137  """Turn the device on."""
138  if self._cmd_on_cmd_on is not None:
139  await self._async_send(self._device_device.send_command, self._cmd_on_cmd_on)
140  else:
141  await self._async_send(self._device_device.send_on)
142  self._attr_is_on_attr_is_on = True
143  self.async_write_ha_stateasync_write_ha_state()
144 
145  async def async_turn_off(self, **kwargs: Any) -> None:
146  """Turn the device off."""
147  if self._cmd_off_cmd_off is not None:
148  await self._async_send(self._device_device.send_command, self._cmd_off_cmd_off)
149  else:
150  await self._async_send(self._device_device.send_off)
151  self._attr_is_on_attr_is_on = False
152  self.async_write_ha_stateasync_write_ha_state()
None _apply_event(self, rfxtrxmod.RFXtrxEvent event)
Definition: entity.py:91
bool _event_applies(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
Definition: entity.py:77
None _apply_event(self, rfxtrxmod.RFXtrxEvent event)
Definition: switch.py:118
None _handle_event(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
Definition: switch.py:129
None _apply_event_lighting4(self, rfxtrxmod.RFXtrxEvent event)
Definition: switch.py:98
None _apply_event_standard(self, rfxtrxmod.RFXtrxEvent event)
Definition: switch.py:111
None __init__(self, rfxtrxmod.RFXtrxDevice device, DeviceTuple device_id, int|None data_bits=None, int|None cmd_on=None, int|None cmd_off=None, rfxtrxmod.RFXtrxEvent|None event=None)
Definition: switch.py:82
bool supported(rfxtrxmod.RFXtrxEvent event)
Definition: switch.py:32
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:46
str|None get_pt2262_cmd(str device_id, int data_bits)
Definition: __init__.py:377
None async_setup_platform_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities, Callable[[rfxtrxmod.RFXtrxEvent], bool] supported, Callable[[rfxtrxmod.RFXtrxEvent, rfxtrxmod.RFXtrxEvent|None, DeviceTuple, dict[str, Any],], list[Entity],] constructor)
Definition: __init__.py:308