Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for SUPLA covers - curtains, rollershutters, entry gate etc."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from pprint import pformat
7 from typing import Any
8 
9 from homeassistant.components.cover import ATTR_POSITION, CoverDeviceClass, CoverEntity
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 from . import DOMAIN, SUPLA_COORDINATORS, SUPLA_SERVERS
15 from .entity import SuplaEntity
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 SUPLA_SHUTTER = "CONTROLLINGTHEROLLERSHUTTER"
20 SUPLA_GATE = "CONTROLLINGTHEGATE"
21 SUPLA_GARAGE_DOOR = "CONTROLLINGTHEGARAGEDOOR"
22 
23 
25  hass: HomeAssistant,
26  config: ConfigType,
27  async_add_entities: AddEntitiesCallback,
28  discovery_info: DiscoveryInfoType | None = None,
29 ) -> None:
30  """Set up the SUPLA covers."""
31  if discovery_info is None:
32  return
33 
34  _LOGGER.debug("Discovery: %s", pformat(discovery_info))
35 
36  entities: list[CoverEntity] = []
37  for device in discovery_info.values():
38  device_name = device["function_name"]
39  server_name = device["server_name"]
40 
41  if device_name == SUPLA_SHUTTER:
42  entities.append(
44  device,
45  hass.data[DOMAIN][SUPLA_SERVERS][server_name],
46  hass.data[DOMAIN][SUPLA_COORDINATORS][server_name],
47  )
48  )
49 
50  elif device_name in {SUPLA_GATE, SUPLA_GARAGE_DOOR}:
51  entities.append(
53  device,
54  hass.data[DOMAIN][SUPLA_SERVERS][server_name],
55  hass.data[DOMAIN][SUPLA_COORDINATORS][server_name],
56  )
57  )
58 
59  async_add_entities(entities)
60 
61 
63  """Representation of a SUPLA Cover."""
64 
65  @property
66  def current_cover_position(self) -> int | None:
67  """Return current position of cover. 0 is closed, 100 is open."""
68  if state := self.channel_datachannel_data.get("state"):
69  return 100 - state["shut"]
70  return None
71 
72  async def async_set_cover_position(self, **kwargs: Any) -> None:
73  """Move the cover to a specific position."""
74  await self.async_actionasync_action(
75  "REVEAL_PARTIALLY", percentage=kwargs.get(ATTR_POSITION)
76  )
77 
78  @property
79  def is_closed(self) -> bool | None:
80  """Return if the cover is closed."""
82  return None
84 
85  async def async_open_cover(self, **kwargs: Any) -> None:
86  """Open the cover."""
87  await self.async_actionasync_action("REVEAL")
88 
89  async def async_close_cover(self, **kwargs: Any) -> None:
90  """Close the cover."""
91  await self.async_actionasync_action("SHUT")
92 
93  async def async_stop_cover(self, **kwargs: Any) -> None:
94  """Stop the cover."""
95  await self.async_actionasync_action("STOP")
96 
97 
99  """Representation of a SUPLA door."""
100 
101  _attr_device_class = CoverDeviceClass.GARAGE
102 
103  @property
104  def is_closed(self) -> bool | None:
105  """Return if the door is closed or not."""
106  state = self.channel_datachannel_data.get("state")
107  if state and "hi" in state:
108  return state.get("hi")
109  return None
110 
111  async def async_open_cover(self, **kwargs: Any) -> None:
112  """Open the door."""
113  if self.is_closedis_closedis_closed:
114  await self.async_actionasync_action("OPEN_CLOSE")
115 
116  async def async_close_cover(self, **kwargs: Any) -> None:
117  """Close the door."""
118  if not self.is_closedis_closedis_closed:
119  await self.async_actionasync_action("OPEN_CLOSE")
120 
121  async def async_stop_cover(self, **kwargs: Any) -> None:
122  """Stop the door."""
123  await self.async_actionasync_action("OPEN_CLOSE")
124 
125  async def async_toggle(self, **kwargs: Any) -> None:
126  """Toggle the door."""
127  await self.async_actionasync_action("OPEN_CLOSE")
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:72
def async_action(self, action, **add_pars)
Definition: entity.py:48
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: cover.py:29