Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Cover Platform for Chacon Dio REV-SHUTTER devices."""
2 
3 import logging
4 from typing import Any
5 
6 from dio_chacon_wifi_api.const import DeviceTypeEnum, ShutterMoveEnum
7 
9  ATTR_POSITION,
10  CoverDeviceClass,
11  CoverEntity,
12  CoverEntityFeature,
13 )
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import ChaconDioConfigEntry
18 from .entity import ChaconDioEntity
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
24  hass: HomeAssistant,
25  config_entry: ChaconDioConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up Chacon Dio cover devices."""
29  data = config_entry.runtime_data
30  client = data.client
31 
33  ChaconDioCover(client, device)
34  for device in data.list_devices
35  if device["type"] == DeviceTypeEnum.SHUTTER.value
36  )
37 
38 
40  """Object for controlling a Chacon Dio cover."""
41 
42  _attr_device_class = CoverDeviceClass.SHUTTER
43  _attr_name = None
44 
45  _attr_supported_features = (
46  CoverEntityFeature.OPEN
47  | CoverEntityFeature.CLOSE
48  | CoverEntityFeature.STOP
49  | CoverEntityFeature.SET_POSITION
50  )
51 
52  def _update_attr(self, data: dict[str, Any]) -> None:
53  """Recomputes the attributes values either at init or when the device state changes."""
54  self._attr_available_attr_available = data["connected"]
55  self._attr_current_cover_position_attr_current_cover_position = data["openlevel"]
56  self._attr_is_closing_attr_is_closing = data["movement"] == ShutterMoveEnum.DOWN.value
57  self._attr_is_opening_attr_is_opening = data["movement"] == ShutterMoveEnum.UP.value
58  self._attr_is_closed_attr_is_closed = self._attr_current_cover_position_attr_current_cover_position == 0
59 
60  async def async_close_cover(self, **kwargs: Any) -> None:
61  """Close the cover.
62 
63  Closed status is effective after the server callback that triggers callback_device_state.
64  """
65 
66  _LOGGER.debug(
67  "Close cover %s , %s, %s",
68  self.target_id,
69  self._attr_name_attr_name,
70  self.is_closedis_closed,
71  )
72 
73  # closes effectively only if cover is not already closing and not fully closed
74  if not self._attr_is_closing_attr_is_closing and not self.is_closedis_closed:
75  self._attr_is_closing_attr_is_closing = True
76  self.async_write_ha_stateasync_write_ha_state()
77 
78  await self.clientclient.move_shutter_direction(
79  self.target_id, ShutterMoveEnum.DOWN
80  )
81 
82  async def async_open_cover(self, **kwargs: Any) -> None:
83  """Open the cover.
84 
85  Opened status is effective after the server callback that triggers callback_device_state.
86  """
87 
88  _LOGGER.debug(
89  "Open cover %s , %s, %s",
90  self.target_id,
91  self._attr_name_attr_name,
92  self.current_cover_positioncurrent_cover_positioncurrent_cover_position,
93  )
94 
95  # opens effectively only if cover is not already opening and not fully opened
96  if not self._attr_is_opening_attr_is_opening and self.current_cover_positioncurrent_cover_positioncurrent_cover_position != 100:
97  self._attr_is_opening_attr_is_opening = True
98  self.async_write_ha_stateasync_write_ha_state()
99 
100  await self.clientclient.move_shutter_direction(self.target_id, ShutterMoveEnum.UP)
101 
102  async def async_stop_cover(self, **kwargs: Any) -> None:
103  """Stop the cover."""
104 
105  _LOGGER.debug("Stop cover %s , %s", self.target_id, self._attr_name_attr_name)
106 
107  self._attr_is_opening_attr_is_opening = False
108  self._attr_is_closing_attr_is_closing = False
109  self.async_write_ha_stateasync_write_ha_state()
110 
111  await self.clientclient.move_shutter_direction(self.target_id, ShutterMoveEnum.STOP)
112 
113  async def async_set_cover_position(self, **kwargs: Any) -> None:
114  """Set the cover open position in percentage.
115 
116  Closing or opening status is effective after the server callback that triggers callback_device_state.
117  """
118  position: int = kwargs[ATTR_POSITION]
119 
120  _LOGGER.debug(
121  "Set cover position %i, %s , %s", position, self.target_id, self._attr_name_attr_name
122  )
123 
124  await self.clientclient.move_shutter_percentage(self.target_id, position)
None _update_attr(self, dict[str, Any] data)
Definition: cover.py:52
None async_setup_entry(HomeAssistant hass, ChaconDioConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:27