Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for Velbus covers."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from velbusaio.channels import Blind as VelbusBlind
8 
10  ATTR_POSITION,
11  CoverEntity,
12  CoverEntityFeature,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import DOMAIN
19 from .entity import VelbusEntity, api_call
20 
21 
23  hass: HomeAssistant,
24  entry: ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up Velbus switch based on config_entry."""
28  await hass.data[DOMAIN][entry.entry_id]["tsk"]
29  cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"]
30  async_add_entities(VelbusCover(channel) for channel in cntrl.get_all("cover"))
31 
32 
34  """Representation a Velbus cover."""
35 
36  _channel: VelbusBlind
37  _assumed_closed: bool
38 
39  def __init__(self, channel: VelbusBlind) -> None:
40  """Initialize the cover."""
41  super().__init__(channel)
42  if self._channel_channel.support_position():
43  self._attr_supported_features_attr_supported_features = (
44  CoverEntityFeature.OPEN
45  | CoverEntityFeature.CLOSE
46  | CoverEntityFeature.STOP
47  | CoverEntityFeature.SET_POSITION
48  )
49  else:
50  self._attr_supported_features_attr_supported_features = (
51  CoverEntityFeature.OPEN
52  | CoverEntityFeature.CLOSE
53  | CoverEntityFeature.STOP
54  )
55  self._attr_assumed_state_attr_assumed_state = True
56  # guess the state to get the open/closed icons somewhat working
57  self._assumed_closed_assumed_closed = False
58 
59  @property
60  def is_closed(self) -> bool | None:
61  """Return if the cover is closed."""
62  if self._channel_channel.support_position():
63  return self._channel_channel.is_closed()
64  return self._assumed_closed_assumed_closed
65 
66  @property
67  def is_opening(self) -> bool:
68  """Return if the cover is opening."""
69  if opening := self._channel_channel.is_opening():
70  self._assumed_closed_assumed_closed = False
71  return opening
72 
73  @property
74  def is_closing(self) -> bool:
75  """Return if the cover is closing."""
76  if closing := self._channel_channel.is_closing():
77  self._assumed_closed_assumed_closed = True
78  return closing
79 
80  @property
81  def current_cover_position(self) -> int | None:
82  """Return current position of cover.
83 
84  None is unknown, 0 is closed, 100 is fully open
85  Velbus: 100 = closed, 0 = open
86  """
87  pos = self._channel_channel.get_position()
88  if pos is not None:
89  return 100 - pos
90  return None
91 
92  @api_call
93  async def async_open_cover(self, **kwargs: Any) -> None:
94  """Open the cover."""
95  await self._channel_channel.open()
96 
97  @api_call
98  async def async_close_cover(self, **kwargs: Any) -> None:
99  """Close the cover."""
100  await self._channel_channel.close()
101 
102  @api_call
103  async def async_stop_cover(self, **kwargs: Any) -> None:
104  """Stop the cover."""
105  await self._channel_channel.stop()
106 
107  @api_call
108  async def async_set_cover_position(self, **kwargs: Any) -> None:
109  """Move the cover to a specific position."""
110  await self._channel_channel.set_position(100 - kwargs[ATTR_POSITION])
None __init__(self, VelbusBlind channel)
Definition: cover.py:39
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:108
None async_stop_cover(self, **Any kwargs)
Definition: cover.py:103
None async_close_cover(self, **Any kwargs)
Definition: cover.py:98
None async_open_cover(self, **Any kwargs)
Definition: cover.py:93
None open(self, **Any kwargs)
Definition: lock.py:86
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:26