Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for RFXtrx covers."""
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.cover import CoverEntity, CoverEntityFeature, CoverState
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.entity import Entity
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import DeviceTuple, async_setup_platform_entry
17 from .const import (
18  COMMAND_OFF_LIST,
19  COMMAND_ON_LIST,
20  CONF_VENETIAN_BLIND_MODE,
21  CONST_VENETIAN_BLIND_MODE_EU,
22  CONST_VENETIAN_BLIND_MODE_US,
23 )
24 from .entity import RfxtrxCommandEntity
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 
29 def supported(event: rfxtrxmod.RFXtrxEvent) -> bool:
30  """Return whether an event supports cover."""
31  return bool(event.device.known_to_be_rollershutter)
32 
33 
35  hass: HomeAssistant,
36  config_entry: ConfigEntry,
37  async_add_entities: AddEntitiesCallback,
38 ) -> None:
39  """Set up config entry."""
40 
41  def _constructor(
42  event: rfxtrxmod.RFXtrxEvent,
43  auto: rfxtrxmod.RFXtrxEvent | None,
44  device_id: DeviceTuple,
45  entity_info: dict[str, Any],
46  ) -> list[Entity]:
47  return [
49  event.device,
50  device_id,
51  venetian_blind_mode=entity_info.get(CONF_VENETIAN_BLIND_MODE),
52  event=event if auto else None,
53  )
54  ]
55 
57  hass, config_entry, async_add_entities, supported, _constructor
58  )
59 
60 
62  """Representation of a RFXtrx cover."""
63 
64  _device: rfxtrxmod.RollerTrolDevice | rfxtrxmod.RfyDevice | rfxtrxmod.LightingDevice
65 
66  def __init__(
67  self,
68  device: rfxtrxmod.RFXtrxDevice,
69  device_id: DeviceTuple,
70  event: rfxtrxmod.RFXtrxEvent = None,
71  venetian_blind_mode: str | None = None,
72  ) -> None:
73  """Initialize the RFXtrx cover device."""
74  super().__init__(device, device_id, event)
75  self._venetian_blind_mode_venetian_blind_mode = venetian_blind_mode
76  self._attr_is_closed_attr_is_closed: bool | None = True
77 
78  self._attr_supported_features_attr_supported_features = (
79  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP
80  )
81 
82  if venetian_blind_mode in (
83  CONST_VENETIAN_BLIND_MODE_US,
84  CONST_VENETIAN_BLIND_MODE_EU,
85  ):
86  self._attr_supported_features_attr_supported_features |= (
87  CoverEntityFeature.OPEN_TILT
88  | CoverEntityFeature.CLOSE_TILT
89  | CoverEntityFeature.STOP_TILT
90  )
91 
92  async def async_added_to_hass(self) -> None:
93  """Restore device state."""
94  await super().async_added_to_hass()
95 
96  if self._event_event is None:
97  old_state = await self.async_get_last_stateasync_get_last_state()
98  if old_state is not None:
99  self._attr_is_closed_attr_is_closed = old_state.state != CoverState.OPEN
100 
101  async def async_open_cover(self, **kwargs: Any) -> None:
102  """Move the cover up."""
103  if self._venetian_blind_mode_venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_US:
104  await self._async_send(self._device_device.send_up05sec)
105  elif self._venetian_blind_mode_venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_EU:
106  await self._async_send(self._device_device.send_up2sec)
107  else:
108  await self._async_send(self._device_device.send_open)
109  self._attr_is_closed_attr_is_closed = False
110  self.async_write_ha_stateasync_write_ha_state()
111 
112  async def async_close_cover(self, **kwargs: Any) -> None:
113  """Move the cover down."""
114  if self._venetian_blind_mode_venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_US:
115  await self._async_send(self._device_device.send_down05sec)
116  elif self._venetian_blind_mode_venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_EU:
117  await self._async_send(self._device_device.send_down2sec)
118  else:
119  await self._async_send(self._device_device.send_close)
120  self._attr_is_closed_attr_is_closed = True
121  self.async_write_ha_stateasync_write_ha_state()
122 
123  async def async_stop_cover(self, **kwargs: Any) -> None:
124  """Stop the cover."""
125  await self._async_send(self._device_device.send_stop)
126  self._attr_is_closed_attr_is_closed = False
127  self.async_write_ha_stateasync_write_ha_state()
128 
129  async def async_open_cover_tilt(self, **kwargs: Any) -> None:
130  """Tilt the cover up."""
131  if self._venetian_blind_mode_venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_US:
132  await self._async_send(self._device_device.send_up2sec)
133  elif self._venetian_blind_mode_venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_EU:
134  await self._async_send(self._device_device.send_up05sec)
135 
136  async def async_close_cover_tilt(self, **kwargs: Any) -> None:
137  """Tilt the cover down."""
138  if self._venetian_blind_mode_venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_US:
139  await self._async_send(self._device_device.send_down2sec)
140  elif self._venetian_blind_mode_venetian_blind_mode == CONST_VENETIAN_BLIND_MODE_EU:
141  await self._async_send(self._device_device.send_down05sec)
142 
143  async def async_stop_cover_tilt(self, **kwargs: Any) -> None:
144  """Stop the cover tilt."""
145  await self._async_send(self._device_device.send_stop)
146  self._attr_is_closed_attr_is_closed = False
147  self.async_write_ha_stateasync_write_ha_state()
148 
149  def _apply_event(self, event: rfxtrxmod.RFXtrxEvent) -> None:
150  """Apply command from rfxtrx."""
151  assert isinstance(event, rfxtrxmod.ControlEvent)
152  super()._apply_event(event)
153  if event.values["Command"] in COMMAND_ON_LIST:
154  self._attr_is_closed_attr_is_closed = False
155  elif event.values["Command"] in COMMAND_OFF_LIST:
156  self._attr_is_closed_attr_is_closed = True
157 
158  @callback
160  self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple
161  ) -> None:
162  """Check if event applies to me and update."""
163  if device_id != self._device_id_device_id:
164  return
165 
166  self._apply_event_apply_event_apply_event(event)
167 
168  self.async_write_ha_stateasync_write_ha_state()
None async_open_cover(self, **Any kwargs)
Definition: cover.py:101
None _apply_event(self, rfxtrxmod.RFXtrxEvent event)
Definition: cover.py:149
None __init__(self, rfxtrxmod.RFXtrxDevice device, DeviceTuple device_id, rfxtrxmod.RFXtrxEvent event=None, str|None venetian_blind_mode=None)
Definition: cover.py:72
None async_stop_cover_tilt(self, **Any kwargs)
Definition: cover.py:143
None async_stop_cover(self, **Any kwargs)
Definition: cover.py:123
None async_close_cover(self, **Any kwargs)
Definition: cover.py:112
None async_open_cover_tilt(self, **Any kwargs)
Definition: cover.py:129
None async_close_cover_tilt(self, **Any kwargs)
Definition: cover.py:136
None _handle_event(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
Definition: cover.py:161
None _apply_event(self, rfxtrxmod.RFXtrxEvent event)
Definition: entity.py:91
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:38
bool supported(rfxtrxmod.RFXtrxEvent event)
Definition: cover.py:29
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