Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Switcher integration Cover platform."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any, cast
7 
8 from aioswitcher.api import SwitcherApi, SwitcherBaseResponse
9 from aioswitcher.device import DeviceCategory, ShutterDirection, SwitcherShutter
10 
12  ATTR_POSITION,
13  CoverDeviceClass,
14  CoverEntity,
15  CoverEntityFeature,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.core import HomeAssistant, callback
19 from homeassistant.exceptions import HomeAssistantError
20 from homeassistant.helpers.dispatcher import async_dispatcher_connect
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from .const import SIGNAL_DEVICE_ADD
24 from .coordinator import SwitcherDataUpdateCoordinator
25 from .entity import SwitcherEntity
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 API_SET_POSITON = "set_position"
30 API_STOP = "stop_shutter"
31 
32 
34  hass: HomeAssistant,
35  config_entry: ConfigEntry,
36  async_add_entities: AddEntitiesCallback,
37 ) -> None:
38  """Set up Switcher cover from config entry."""
39 
40  @callback
41  def async_add_cover(coordinator: SwitcherDataUpdateCoordinator) -> None:
42  """Add cover from Switcher device."""
43  entities: list[CoverEntity] = []
44 
45  if coordinator.data.device_type.category in (
46  DeviceCategory.SHUTTER,
47  DeviceCategory.SINGLE_SHUTTER_DUAL_LIGHT,
48  DeviceCategory.DUAL_SHUTTER_SINGLE_LIGHT,
49  ):
50  number_of_covers = len(cast(SwitcherShutter, coordinator.data).position)
51  if number_of_covers == 1:
52  entities.append(SwitcherSingleCoverEntity(coordinator, 0))
53  else:
54  entities.extend(
55  SwitcherMultiCoverEntity(coordinator, i)
56  for i in range(number_of_covers)
57  )
58  async_add_entities(entities)
59 
60  config_entry.async_on_unload(
61  async_dispatcher_connect(hass, SIGNAL_DEVICE_ADD, async_add_cover)
62  )
63 
64 
66  """Representation of a Switcher cover entity."""
67 
68  _attr_device_class = CoverDeviceClass.SHUTTER
69  _attr_supported_features = (
70  CoverEntityFeature.OPEN
71  | CoverEntityFeature.CLOSE
72  | CoverEntityFeature.SET_POSITION
73  | CoverEntityFeature.STOP
74  )
75  _cover_id: int
76 
77  @callback
78  def _handle_coordinator_update(self) -> None:
79  """Handle updated data from the coordinator."""
80  self._update_data_update_data()
81  self.async_write_ha_stateasync_write_ha_state()
82 
83  def _update_data(self) -> None:
84  """Update data from device."""
85  data = cast(SwitcherShutter, self.coordinator.data)
86  self._attr_current_cover_position_attr_current_cover_position = data.position[self._cover_id]
87  self._attr_is_closed_attr_is_closed = data.position[self._cover_id] == 0
88  self._attr_is_closing_attr_is_closing = (
89  data.direction[self._cover_id] == ShutterDirection.SHUTTER_DOWN
90  )
91  self._attr_is_opening_attr_is_opening = (
92  data.direction[self._cover_id] == ShutterDirection.SHUTTER_UP
93  )
94 
95  async def _async_call_api(self, api: str, *args: Any) -> None:
96  """Call Switcher API."""
97  _LOGGER.debug("Calling api for %s, api: '%s', args: %s", self.namename, api, args)
98  response: SwitcherBaseResponse | None = None
99  error = None
100 
101  try:
102  async with SwitcherApi(
103  self.coordinator.data.device_type,
104  self.coordinator.data.ip_address,
105  self.coordinator.data.device_id,
106  self.coordinator.data.device_key,
107  self.coordinator.token,
108  ) as swapi:
109  response = await getattr(swapi, api)(*args)
110  except (TimeoutError, OSError, RuntimeError) as err:
111  error = repr(err)
112 
113  if error or not response or not response.successful:
114  self.coordinator.last_update_success = False
115  self.async_write_ha_stateasync_write_ha_state()
116  raise HomeAssistantError(
117  f"Call api for {self.name} failed, api: '{api}', "
118  f"args: {args}, response/error: {response or error}"
119  )
120 
121  async def async_close_cover(self, **kwargs: Any) -> None:
122  """Close cover."""
123  await self._async_call_api_async_call_api(API_SET_POSITON, 0, self._cover_id)
124 
125  async def async_open_cover(self, **kwargs: Any) -> None:
126  """Open cover."""
127  await self._async_call_api_async_call_api(API_SET_POSITON, 100, self._cover_id)
128 
129  async def async_set_cover_position(self, **kwargs: Any) -> None:
130  """Move the cover to a specific position."""
131  await self._async_call_api_async_call_api(
132  API_SET_POSITON, kwargs[ATTR_POSITION], self._cover_id
133  )
134 
135  async def async_stop_cover(self, **kwargs: Any) -> None:
136  """Stop the cover."""
137  await self._async_call_api_async_call_api(API_STOP, self._cover_id)
138 
139 
141  """Representation of a Switcher single cover entity."""
142 
143  _attr_name = None
144 
145  def __init__(
146  self,
147  coordinator: SwitcherDataUpdateCoordinator,
148  cover_id: int,
149  ) -> None:
150  """Initialize the entity."""
151  super().__init__(coordinator)
152  self._cover_id_cover_id = cover_id
153 
154  self._attr_unique_id_attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}"
155 
156  self._update_data_update_data()
157 
158 
160  """Representation of a Switcher multiple cover entity."""
161 
162  _attr_translation_key = "cover"
163 
164  def __init__(
165  self,
166  coordinator: SwitcherDataUpdateCoordinator,
167  cover_id: int,
168  ) -> None:
169  """Initialize the entity."""
170  super().__init__(coordinator)
171  self._cover_id_cover_id = cover_id
172 
173  self._attr_translation_placeholders_attr_translation_placeholders = {"cover_id": str(cover_id + 1)}
174  self._attr_unique_id_attr_unique_id = (
175  f"{coordinator.device_id}-{coordinator.mac_address}-{cover_id}"
176  )
177 
178  self._update_data_update_data()
None __init__(self, SwitcherDataUpdateCoordinator coordinator, int cover_id)
Definition: cover.py:168
None __init__(self, SwitcherDataUpdateCoordinator coordinator, int cover_id)
Definition: cover.py:149
str|UndefinedType|None name(self)
Definition: entity.py:738
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:37
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103