Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """BleBox cover entity."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
8 from blebox_uniapi.cover import BleboxCoverState
9 
11  ATTR_POSITION,
12  ATTR_TILT_POSITION,
13  CoverDeviceClass,
14  CoverEntity,
15  CoverEntityFeature,
16  CoverState,
17 )
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import BleBoxConfigEntry
22 from .entity import BleBoxEntity
23 
24 BLEBOX_TO_COVER_DEVICE_CLASSES = {
25  "gate": CoverDeviceClass.GATE,
26  "gatebox": CoverDeviceClass.DOOR,
27  "shutter": CoverDeviceClass.SHUTTER,
28 }
29 
30 BLEBOX_TO_HASS_COVER_STATES = {
31  None: None,
32  # all blebox covers
33  BleboxCoverState.MOVING_DOWN: CoverState.CLOSING,
34  BleboxCoverState.MOVING_UP: CoverState.OPENING,
35  BleboxCoverState.MANUALLY_STOPPED: CoverState.OPEN,
36  BleboxCoverState.LOWER_LIMIT_REACHED: CoverState.CLOSED,
37  BleboxCoverState.UPPER_LIMIT_REACHED: CoverState.OPEN,
38  # extra states of gateController product
39  BleboxCoverState.OVERLOAD: CoverState.OPEN,
40  BleboxCoverState.MOTOR_FAILURE: CoverState.OPEN,
41  BleboxCoverState.SAFETY_STOP: CoverState.OPEN,
42 }
43 
44 
46  hass: HomeAssistant,
47  config_entry: BleBoxConfigEntry,
48  async_add_entities: AddEntitiesCallback,
49 ) -> None:
50  """Set up a BleBox entry."""
51  entities = [
52  BleBoxCoverEntity(feature)
53  for feature in config_entry.runtime_data.features.get("covers", [])
54  ]
55  async_add_entities(entities, True)
56 
57 
58 class BleBoxCoverEntity(BleBoxEntity[blebox_uniapi.cover.Cover], CoverEntity):
59  """Representation of a BleBox cover feature."""
60 
61  def __init__(self, feature: blebox_uniapi.cover.Cover) -> None:
62  """Initialize a BleBox cover feature."""
63  super().__init__(feature)
64  self._attr_device_class_attr_device_class = BLEBOX_TO_COVER_DEVICE_CLASSES[feature.device_class]
65  self._attr_supported_features_attr_supported_features = (
66  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
67  )
68  if feature.is_slider:
69  self._attr_supported_features_attr_supported_features |= CoverEntityFeature.SET_POSITION
70 
71  if feature.has_stop:
72  self._attr_supported_features_attr_supported_features |= CoverEntityFeature.STOP
73 
74  if feature.has_tilt:
75  self._attr_supported_features_attr_supported_features |= (
76  CoverEntityFeature.SET_TILT_POSITION
77  | CoverEntityFeature.OPEN_TILT
78  | CoverEntityFeature.CLOSE_TILT
79  )
80 
81  @property
82  def current_cover_position(self) -> int | None:
83  """Return the current cover position."""
84  position = self._feature.current
85  if position == -1: # possible for shutterBox
86  return None
87 
88  return None if position is None else 100 - position
89 
90  @property
91  def current_cover_tilt_position(self) -> int | None:
92  """Return the current tilt of shutter."""
93  position = self._feature.tilt_current
94  return None if position is None else 100 - position
95 
96  @property
97  def is_opening(self) -> bool | None:
98  """Return whether cover is opening."""
99  return self._is_state_is_state(CoverState.OPENING)
100 
101  @property
102  def is_closing(self) -> bool | None:
103  """Return whether cover is closing."""
104  return self._is_state_is_state(CoverState.CLOSING)
105 
106  @property
107  def is_closed(self) -> bool | None:
108  """Return whether cover is closed."""
109  return self._is_state_is_state(CoverState.CLOSED)
110 
111  async def async_open_cover(self, **kwargs: Any) -> None:
112  """Fully open the cover position."""
113  await self._feature.async_open()
114 
115  async def async_close_cover(self, **kwargs: Any) -> None:
116  """Fully close the cover position."""
117  await self._feature.async_close()
118 
119  async def async_open_cover_tilt(self, **kwargs: Any) -> None:
120  """Fully open the cover tilt."""
121  await self._feature.async_set_tilt_position(0)
122 
123  async def async_close_cover_tilt(self, **kwargs: Any) -> None:
124  """Fully close the cover tilt."""
125  # note: values are reversed
126  await self._feature.async_set_tilt_position(100)
127 
128  async def async_set_cover_position(self, **kwargs: Any) -> None:
129  """Set the cover position."""
130  position = kwargs[ATTR_POSITION]
131  await self._feature.async_set_position(100 - position)
132 
133  async def async_stop_cover(self, **kwargs: Any) -> None:
134  """Stop the cover."""
135  await self._feature.async_stop()
136 
137  async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
138  """Set the tilt position."""
139  position = kwargs[ATTR_TILT_POSITION]
140  await self._feature.async_set_tilt_position(100 - position)
141 
142  def _is_state(self, state_name) -> bool | None:
143  value = BLEBOX_TO_HASS_COVER_STATES[self._feature.state]
144  return None if value is None else value == state_name
None __init__(self, blebox_uniapi.cover.Cover feature)
Definition: cover.py:61
None async_set_cover_tilt_position(self, **Any kwargs)
Definition: cover.py:137
None async_setup_entry(HomeAssistant hass, BleBoxConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:49
None async_stop(HomeAssistant hass)
Definition: discovery.py:694