Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for the Dynalite channels as covers."""
2 
3 from typing import Any
4 
6  ATTR_CURRENT_POSITION,
7  CoverDeviceClass,
8  CoverEntity,
9 )
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 from homeassistant.util.enum import try_parse_enum
14 
15 from .bridge import DynaliteBridge
16 from .entity import DynaliteBase, async_setup_entry_base
17 
18 
20  hass: HomeAssistant,
21  config_entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Record the async_add_entities function to add them later when received from Dynalite."""
25 
26  @callback
27  def cover_from_device(device: Any, bridge: DynaliteBridge) -> CoverEntity:
28  if device.has_tilt:
29  return DynaliteCoverWithTilt(device, bridge)
30  return DynaliteCover(device, bridge)
31 
33  hass, config_entry, async_add_entities, "cover", cover_from_device
34  )
35 
36 
38  """Representation of a Dynalite Channel as a Home Assistant Cover."""
39 
40  def __init__(self, device: Any, bridge: DynaliteBridge) -> None:
41  """Initialize the cover."""
42  super().__init__(device, bridge)
43  device_class = try_parse_enum(CoverDeviceClass, self._device_device.device_class)
44  self._attr_device_class_attr_device_class = device_class or CoverDeviceClass.SHUTTER
45 
46  @property
47  def current_cover_position(self) -> int:
48  """Return the position of the cover from 0 to 100."""
49  return self._device_device.current_cover_position
50 
51  @property
52  def is_opening(self) -> bool:
53  """Return true if cover is opening."""
54  return self._device_device.is_opening
55 
56  @property
57  def is_closing(self) -> bool:
58  """Return true if cover is closing."""
59  return self._device_device.is_closing
60 
61  @property
62  def is_closed(self) -> bool:
63  """Return true if cover is closed."""
64  return self._device_device.is_closed
65 
66  async def async_open_cover(self, **kwargs: Any) -> None:
67  """Open the cover."""
68  await self._device_device.async_open_cover(**kwargs)
69 
70  async def async_close_cover(self, **kwargs: Any) -> None:
71  """Close the cover."""
72  await self._device_device.async_close_cover(**kwargs)
73 
74  async def async_set_cover_position(self, **kwargs: Any) -> None:
75  """Set the cover position."""
76  await self._device_device.async_set_cover_position(**kwargs)
77 
78  async def async_stop_cover(self, **kwargs: Any) -> None:
79  """Stop the cover."""
80  await self._device_device.async_stop_cover(**kwargs)
81 
82  def initialize_state(self, state):
83  """Initialize the state from cache."""
84  target_level = state.attributes.get(ATTR_CURRENT_POSITION)
85  if target_level is not None:
86  self._device_device.init_level(target_level)
87 
88 
90  """Representation of a Dynalite Channel as a Home Assistant Cover that uses up and down for tilt."""
91 
92  @property
93  def current_cover_tilt_position(self) -> int:
94  """Return the current tilt position."""
95  return self._device_device.current_cover_tilt_position
96 
97  async def async_open_cover_tilt(self, **kwargs: Any) -> None:
98  """Open cover tilt."""
99  await self._device_device.async_open_cover_tilt(**kwargs)
100 
101  async def async_close_cover_tilt(self, **kwargs: Any) -> None:
102  """Close cover tilt."""
103  await self._device_device.async_close_cover_tilt(**kwargs)
104 
105  async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
106  """Set the cover tilt position."""
107  await self._device_device.async_set_cover_tilt_position(**kwargs)
108 
109  async def async_stop_cover_tilt(self, **kwargs: Any) -> None:
110  """Stop the cover tilt."""
111  await self._device_device.async_stop_cover_tilt(**kwargs)
None __init__(self, Any device, DynaliteBridge bridge)
Definition: cover.py:40
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:74
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:23
None async_setup_entry_base(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities, str platform, Callable entity_from_device)
Definition: entity.py:26