Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for Fibaro cover - curtains, rollershutters etc."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from pyfibaro.fibaro_device import DeviceModel
8 
10  ATTR_POSITION,
11  ATTR_TILT_POSITION,
12  ENTITY_ID_FORMAT,
13  CoverEntity,
14  CoverEntityFeature,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import Platform
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import FibaroController
22 from .const import DOMAIN
23 from .entity import FibaroEntity
24 
25 
27  hass: HomeAssistant,
28  entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up the Fibaro covers."""
32  controller: FibaroController = hass.data[DOMAIN][entry.entry_id]
34  [FibaroCover(device) for device in controller.fibaro_devices[Platform.COVER]],
35  True,
36  )
37 
38 
40  """Representation a Fibaro Cover."""
41 
42  def __init__(self, fibaro_device: DeviceModel) -> None:
43  """Initialize the Vera device."""
44  super().__init__(fibaro_device)
45  self.entity_identity_identity_id = ENTITY_ID_FORMAT.format(self.ha_idha_id)
46 
47  if self._is_open_close_only_is_open_close_only():
48  self._attr_supported_features_attr_supported_features = (
49  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
50  )
51  if "stop" in self.fibaro_devicefibaro_device.actions:
52  self._attr_supported_features_attr_supported_features |= CoverEntityFeature.STOP
53 
54  @staticmethod
55  def bound(position):
56  """Normalize the position."""
57  if position is None:
58  return None
59  position = int(position)
60  if position <= 5:
61  return 0
62  if position >= 95:
63  return 100
64  return position
65 
66  def _is_open_close_only(self) -> bool:
67  """Return if only open / close is supported."""
68  # Normally positionable devices report the position over value,
69  # so if it is missing we have a device which supports open / close only
70  return not self.fibaro_devicefibaro_device.value.has_value
71 
72  def update(self) -> None:
73  """Update the state."""
74  super().update()
75 
76  self._attr_current_cover_position_attr_current_cover_position = self.boundbound(self.levellevel)
77  self._attr_current_cover_tilt_position_attr_current_cover_tilt_position = self.boundbound(self.level2level2)
78 
79  device_state = self.fibaro_devicefibaro_device.state
80 
81  # Be aware that opening and closing is only available for some modern
82  # devices.
83  # For example the Fibaro Roller Shutter 4 reports this correctly.
84  if device_state.has_value:
85  self._attr_is_opening_attr_is_opening = device_state.str_value().lower() == "opening"
86  self._attr_is_closing_attr_is_closing = device_state.str_value().lower() == "closing"
87 
88  closed: bool | None = None
89  if self._is_open_close_only_is_open_close_only():
90  if device_state.has_value and device_state.str_value().lower() != "unknown":
91  closed = device_state.str_value().lower() == "closed"
92  elif self.current_cover_positioncurrent_cover_positioncurrent_cover_positioncurrent_cover_position is not None:
94  self._attr_is_closed_attr_is_closed = closed
95 
96  def set_cover_position(self, **kwargs: Any) -> None:
97  """Move the cover to a specific position."""
98  self.set_levelset_level(cast(int, kwargs.get(ATTR_POSITION)))
99 
100  def set_cover_tilt_position(self, **kwargs: Any) -> None:
101  """Move the cover to a specific position."""
102  self.set_level2set_level2(cast(int, kwargs.get(ATTR_TILT_POSITION)))
103 
104  def open_cover(self, **kwargs: Any) -> None:
105  """Open the cover."""
106  self.actionaction("open")
107 
108  def close_cover(self, **kwargs: Any) -> None:
109  """Close the cover."""
110  self.actionaction("close")
111 
112  def open_cover_tilt(self, **kwargs: Any) -> None:
113  """Open the cover tilt."""
114  self.set_level2set_level2(100)
115 
116  def close_cover_tilt(self, **kwargs: Any) -> None:
117  """Close the cover."""
118  self.set_level2set_level2(0)
119 
120  def stop_cover(self, **kwargs: Any) -> None:
121  """Stop the cover."""
122  self.actionaction("stop")
None close_cover_tilt(self, **Any kwargs)
Definition: cover.py:116
None set_cover_tilt_position(self, **Any kwargs)
Definition: cover.py:100
None set_cover_position(self, **Any kwargs)
Definition: cover.py:96
None __init__(self, DeviceModel fibaro_device)
Definition: cover.py:42
None open_cover_tilt(self, **Any kwargs)
Definition: cover.py:112
None action(self, str cmd, *Any args)
Definition: entity.py:98
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:30