Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for AVM FRITZ!SmartHome cover devices."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
8  ATTR_POSITION,
9  CoverDeviceClass,
10  CoverEntity,
11  CoverEntityFeature,
12 )
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .coordinator import FritzboxConfigEntry
17 from .entity import FritzBoxDeviceEntity
18 
19 
21  hass: HomeAssistant,
22  entry: FritzboxConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Set up the FRITZ!SmartHome cover from ConfigEntry."""
26  coordinator = entry.runtime_data
27 
28  @callback
29  def _add_entities(devices: set[str] | None = None) -> None:
30  """Add devices."""
31  if devices is None:
32  devices = coordinator.new_devices
33  if not devices:
34  return
36  FritzboxCover(coordinator, ain)
37  for ain in devices
38  if coordinator.data.devices[ain].has_blind
39  )
40 
41  entry.async_on_unload(coordinator.async_add_listener(_add_entities))
42 
43  _add_entities(set(coordinator.data.devices))
44 
45 
47  """The cover class for FRITZ!SmartHome covers."""
48 
49  _attr_device_class = CoverDeviceClass.BLIND
50  _attr_supported_features = (
51  CoverEntityFeature.OPEN
52  | CoverEntityFeature.SET_POSITION
53  | CoverEntityFeature.CLOSE
54  | CoverEntityFeature.STOP
55  )
56 
57  @property
58  def current_cover_position(self) -> int | None:
59  """Return the current position."""
60  position = None
61  if self.datadatadatadatadata.levelpercentage is not None:
62  position = 100 - self.datadatadatadatadata.levelpercentage
63  return position
64 
65  @property
66  def is_closed(self) -> bool | None:
67  """Return if the cover is closed."""
68  if self.datadatadatadatadata.levelpercentage is None:
69  return None
70  return self.datadatadatadatadata.levelpercentage == 100 # type: ignore [no-any-return]
71 
72  async def async_open_cover(self, **kwargs: Any) -> None:
73  """Open the cover."""
74  await self.hasshasshass.async_add_executor_job(self.datadatadatadatadata.set_blind_open)
75  await self.coordinator.async_refresh()
76 
77  async def async_close_cover(self, **kwargs: Any) -> None:
78  """Close the cover."""
79  await self.hasshasshass.async_add_executor_job(self.datadatadatadatadata.set_blind_close)
80  await self.coordinator.async_refresh()
81 
82  async def async_set_cover_position(self, **kwargs: Any) -> None:
83  """Move the cover to a specific position."""
84  await self.hasshasshass.async_add_executor_job(
85  self.datadatadatadatadata.set_level_percentage, 100 - kwargs[ATTR_POSITION]
86  )
87 
88  async def async_stop_cover(self, **kwargs: Any) -> None:
89  """Stop the cover."""
90  await self.hasshasshass.async_add_executor_job(self.datadatadatadatadata.set_blind_stop)
91  await self.coordinator.async_refresh()
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:82
None async_setup_entry(HomeAssistant hass, FritzboxConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:24