Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Cover integration microBees."""
2 
3 from typing import Any
4 
5 from microBeesPy import Actuator
6 
8  CoverDeviceClass,
9  CoverEntity,
10  CoverEntityFeature,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import HomeAssistantError
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.event import async_call_later
17 
18 from .const import DOMAIN
19 from .coordinator import MicroBeesUpdateCoordinator
20 from .entity import MicroBeesEntity
21 
22 COVER_IDS = {47: "roller_shutter"}
23 
24 
26  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
27 ) -> None:
28  """Set up the microBees cover platform."""
29  coordinator: MicroBeesUpdateCoordinator = hass.data[DOMAIN][
30  entry.entry_id
31  ].coordinator
32 
34  MBCover(
35  coordinator,
36  bee_id,
37  next(
38  (actuator.id for actuator in bee.actuators if actuator.deviceID == 551),
39  None,
40  ),
41  next(
42  (actuator.id for actuator in bee.actuators if actuator.deviceID == 552),
43  None,
44  ),
45  )
46  for bee_id, bee in coordinator.data.bees.items()
47  if bee.productID in COVER_IDS
48  )
49 
50 
52  """Representation of a microBees cover."""
53 
54  _attr_device_class = CoverDeviceClass.SHUTTER
55  _attr_supported_features = (
56  CoverEntityFeature.OPEN | CoverEntityFeature.STOP | CoverEntityFeature.CLOSE
57  )
58 
59  def __init__(self, coordinator, bee_id, actuator_up_id, actuator_down_id) -> None:
60  """Initialize the microBees cover."""
61  super().__init__(coordinator, bee_id)
62  self.actuator_up_idactuator_up_id = actuator_up_id
63  self.actuator_down_idactuator_down_id = actuator_down_id
64  self._attr_is_closed_attr_is_closed = None
65 
66  @property
67  def name(self) -> str:
68  """Name of the cover."""
69  return self.beebee.name
70 
71  @property
72  def actuator_up(self) -> Actuator:
73  """Return the rolling up actuator."""
74  return self.coordinator.data.actuators[self.actuator_up_idactuator_up_id]
75 
76  @property
77  def actuator_down(self) -> Actuator:
78  """Return the rolling down actuator."""
79  return self.coordinator.data.actuators[self.actuator_down_idactuator_down_id]
80 
81  def _reset_open_close(self, *_: Any) -> None:
82  """Reset the opening and closing state."""
83  self._attr_is_opening_attr_is_opening = False
84  self._attr_is_closing_attr_is_closing = False
85  self.async_write_ha_stateasync_write_ha_state()
86 
87  async def async_open_cover(self, **kwargs: Any) -> None:
88  """Open the cover."""
89  sendCommand = await self.coordinator.microbees.sendCommand(
90  self.actuator_up_idactuator_up_id,
91  self.actuator_upactuator_up.configuration.actuator_timing * 1000,
92  )
93 
94  if not sendCommand:
95  raise HomeAssistantError(f"Failed to open {self.name}")
96 
97  self._attr_is_opening_attr_is_opening = True
99  self.hasshasshass,
100  self.actuator_downactuator_down.configuration.actuator_timing,
101  self._reset_open_close_reset_open_close,
102  )
103 
104  async def async_close_cover(self, **kwargs: Any) -> None:
105  """Close the cover."""
106  sendCommand = await self.coordinator.microbees.sendCommand(
107  self.actuator_down_idactuator_down_id,
108  self.actuator_downactuator_down.configuration.actuator_timing * 1000,
109  )
110  if not sendCommand:
111  raise HomeAssistantError(f"Failed to close {self.name}")
112 
113  self._attr_is_closing_attr_is_closing = True
115  self.hasshasshass,
116  self.actuator_downactuator_down.configuration.actuator_timing,
117  self._reset_open_close_reset_open_close,
118  )
119 
120  async def async_stop_cover(self, **kwargs: Any) -> None:
121  """Stop the cover."""
122  if self.is_openingis_opening:
123  await self.coordinator.microbees.sendCommand(self.actuator_up_idactuator_up_id, 0)
124  if self.is_closingis_closing:
125  await self.coordinator.microbees.sendCommand(self.actuator_down_idactuator_down_id, 0)
None async_stop_cover(self, **Any kwargs)
Definition: cover.py:120
None async_open_cover(self, **Any kwargs)
Definition: cover.py:87
None __init__(self, coordinator, bee_id, actuator_up_id, actuator_down_id)
Definition: cover.py:59
None async_close_cover(self, **Any kwargs)
Definition: cover.py:104
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:27
CALLBACK_TYPE async_call_later(HomeAssistant hass, float|timedelta delay, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action)
Definition: event.py:1597