Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Implement a iotty Shutter Device."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from iottycloud.device import Device
9 from iottycloud.shutter import Shutter, ShutterState
10 from iottycloud.verbs import SH_DEVICE_TYPE_UID
11 
13  ATTR_POSITION,
14  CoverDeviceClass,
15  CoverEntity,
16  CoverEntityFeature,
17 )
18 from homeassistant.core import HomeAssistant, callback
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import IottyConfigEntry
22 from .api import IottyProxy
23 from .coordinator import IottyDataUpdateCoordinator
24 from .entity import IottyEntity
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 
30  hass: HomeAssistant,
31  config_entry: IottyConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Activate the iotty Shutter component."""
35  _LOGGER.debug("Setup COVER entry id is %s", config_entry.entry_id)
36 
37  coordinator = config_entry.runtime_data.coordinator
38  entities = [
40  coordinator=coordinator, iotty_cloud=coordinator.iotty, iotty_device=d
41  )
42  for d in coordinator.data.devices
43  if d.device_type == SH_DEVICE_TYPE_UID
44  if (isinstance(d, Shutter))
45  ]
46  _LOGGER.debug("Found %d Shutters", len(entities))
47 
48  async_add_entities(entities)
49 
50  known_devices: set = config_entry.runtime_data.known_devices
51  for known_device in coordinator.data.devices:
52  if known_device.device_type == SH_DEVICE_TYPE_UID:
53  known_devices.add(known_device)
54 
55  @callback
56  def async_update_data() -> None:
57  """Handle updated data from the API endpoint."""
58  if not coordinator.last_update_success:
59  return
60 
61  devices = coordinator.data.devices
62  entities = []
63  known_devices: set = config_entry.runtime_data.known_devices
64 
65  # Add entities for devices which we've not yet seen
66  for device in devices:
67  if (
68  any(d.device_id == device.device_id for d in known_devices)
69  or device.device_type != SH_DEVICE_TYPE_UID
70  ):
71  continue
72 
73  iotty_entity = IottyShutter(
74  coordinator=coordinator,
75  iotty_cloud=coordinator.iotty,
76  iotty_device=Shutter(
77  device.device_id,
78  device.serial_number,
79  device.device_type,
80  device.device_name,
81  ),
82  )
83 
84  entities.extend([iotty_entity])
85  known_devices.add(device)
86 
87  async_add_entities(entities)
88 
89  # Add a subscriber to the coordinator to discover new devices
90  coordinator.async_add_listener(async_update_data)
91 
92 
94  """Haas entity class for iotty Shutter."""
95 
96  _attr_device_class = CoverDeviceClass.SHUTTER
97  _iotty_device: Shutter
98  _attr_supported_features: CoverEntityFeature = CoverEntityFeature(0) | (
99  CoverEntityFeature.OPEN
100  | CoverEntityFeature.CLOSE
101  | CoverEntityFeature.STOP
102  | CoverEntityFeature.SET_POSITION
103  )
104 
105  def __init__(
106  self,
107  coordinator: IottyDataUpdateCoordinator,
108  iotty_cloud: IottyProxy,
109  iotty_device: Shutter,
110  ) -> None:
111  """Initialize the Shutter device."""
112  super().__init__(coordinator, iotty_cloud, iotty_device)
113 
114  @property
115  def current_cover_position(self) -> int | None:
116  """Return the current position of the shutter.
117 
118  None is unknown, 0 is closed, 100 is fully open.
119  """
120  return self._iotty_device_iotty_device_iotty_device.percentage
121 
122  @property
123  def is_closed(self) -> bool:
124  """Return true if the Shutter is closed."""
125  _LOGGER.debug(
126  "Retrieve device status for %s ? %s : %s",
127  self._iotty_device_iotty_device_iotty_device.device_id,
128  self._iotty_device_iotty_device_iotty_device.status,
129  self._iotty_device_iotty_device_iotty_device.percentage,
130  )
131  return (
132  self._iotty_device_iotty_device_iotty_device.status == ShutterState.STATIONARY
133  and self._iotty_device_iotty_device_iotty_device.percentage == 0
134  )
135 
136  @property
137  def is_opening(self) -> bool:
138  """Return true if the Shutter is opening."""
139  return self._iotty_device_iotty_device_iotty_device.status == ShutterState.OPENING
140 
141  @property
142  def is_closing(self) -> bool:
143  """Return true if the Shutter is closing."""
144  return self._iotty_device_iotty_device_iotty_device.status == ShutterState.CLOSING
145 
146  @property
147  def supported_features(self) -> CoverEntityFeature:
148  """Flag supported features."""
149  return self._attr_supported_features
150 
151  async def async_open_cover(self, **kwargs: Any) -> None:
152  """Open the cover."""
153  await self._iotty_cloud_iotty_cloud.command(
154  self._iotty_device_iotty_device_iotty_device.device_id, self._iotty_device_iotty_device_iotty_device.cmd_open()
155  )
156  await self.coordinator.async_request_refresh()
157 
158  async def async_close_cover(self, **kwargs: Any) -> None:
159  """Close cover."""
160  await self._iotty_cloud_iotty_cloud.command(
161  self._iotty_device_iotty_device_iotty_device.device_id, self._iotty_device_iotty_device_iotty_device.cmd_close()
162  )
163  await self.coordinator.async_request_refresh()
164 
165  async def async_set_cover_position(self, **kwargs: Any) -> None:
166  """Move the cover to a specific position."""
167  percentage = kwargs[ATTR_POSITION]
168  await self._iotty_cloud_iotty_cloud.command(
169  self._iotty_device_iotty_device_iotty_device.device_id,
170  self._iotty_device_iotty_device_iotty_device.cmd_move_to(),
171  {"open_percentage": percentage},
172  )
173  await self.coordinator.async_request_refresh()
174 
175  async def async_stop_cover(self, **kwargs: Any) -> None:
176  """Stop the cover."""
177  await self._iotty_cloud_iotty_cloud.command(
178  self._iotty_device_iotty_device_iotty_device.device_id, self._iotty_device_iotty_device_iotty_device.cmd_stop()
179  )
180  await self.coordinator.async_request_refresh()
181 
182  @callback
183  def _handle_coordinator_update(self) -> None:
184  """Handle updated data from the coordinator."""
185 
186  device: Device = next(
187  device
188  for device in self.coordinator.data.devices
189  if device.device_id == self._iotty_device_iotty_device_iotty_device.device_id
190  )
191  if isinstance(device, Shutter):
192  self._iotty_device_iotty_device_iotty_device = device
193  self.async_write_ha_stateasync_write_ha_state()
CoverEntityFeature supported_features(self)
Definition: cover.py:147
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:165
None async_open_cover(self, **Any kwargs)
Definition: cover.py:151
None async_stop_cover(self, **Any kwargs)
Definition: cover.py:175
None __init__(self, IottyDataUpdateCoordinator coordinator, IottyProxy iotty_cloud, Shutter iotty_device)
Definition: cover.py:110
None async_close_cover(self, **Any kwargs)
Definition: cover.py:158
None async_setup_entry(HomeAssistant hass, IottyConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:33