Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for IKEA Tradfri covers."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from typing import Any, cast
7 
8 from pytradfri.command import Command
9 
10 from homeassistant.components.cover import ATTR_POSITION, CoverEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import CONF_GATEWAY_ID, COORDINATOR, COORDINATOR_LIST, DOMAIN, KEY_API
16 from .coordinator import TradfriDeviceDataUpdateCoordinator
17 from .entity import TradfriBaseEntity
18 
19 
21  hass: HomeAssistant,
22  config_entry: ConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Load Tradfri covers based on a config entry."""
26  gateway_id = config_entry.data[CONF_GATEWAY_ID]
27  coordinator_data = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
28  api = coordinator_data[KEY_API]
29 
32  device_coordinator,
33  api,
34  gateway_id,
35  )
36  for device_coordinator in coordinator_data[COORDINATOR_LIST]
37  if device_coordinator.device.has_blind_control
38  )
39 
40 
42  """The platform class required by Home Assistant."""
43 
44  _attr_name = None
45 
46  def __init__(
47  self,
48  device_coordinator: TradfriDeviceDataUpdateCoordinator,
49  api: Callable[[Command | list[Command]], Any],
50  gateway_id: str,
51  ) -> None:
52  """Initialize a switch."""
53  super().__init__(
54  device_coordinator=device_coordinator,
55  api=api,
56  gateway_id=gateway_id,
57  )
58 
59  self._device_control_device_control = self._device.blind_control
60  self._device_data_device_data = self._device_control_device_control.blinds[0]
61 
62  def _refresh(self) -> None:
63  """Refresh the device."""
64  self._device_data_device_data = self.coordinator.data.blind_control.blinds[0]
65 
66  @property
67  def extra_state_attributes(self) -> dict[str, str] | None:
68  """Return the state attributes."""
69  return {"model": self._device.device_info.model_number}
70 
71  @property
72  def current_cover_position(self) -> int | None:
73  """Return current position of cover.
74 
75  None is unknown, 0 is closed, 100 is fully open.
76  """
77  if not self._device_data_device_data:
78  return None
79  return 100 - cast(int, self._device_data_device_data.current_cover_position)
80 
81  async def async_set_cover_position(self, **kwargs: Any) -> None:
82  """Move the cover to a specific position."""
83  if not self._device_control_device_control:
84  return
85  await self._api_api(self._device_control_device_control.set_state(100 - kwargs[ATTR_POSITION]))
86 
87  async def async_open_cover(self, **kwargs: Any) -> None:
88  """Open the cover."""
89  if not self._device_control_device_control:
90  return
91  await self._api_api(self._device_control_device_control.set_state(0))
92 
93  async def async_close_cover(self, **kwargs: Any) -> None:
94  """Close cover."""
95  if not self._device_control_device_control:
96  return
97  await self._api_api(self._device_control_device_control.set_state(100))
98 
99  async def async_stop_cover(self, **kwargs: Any) -> None:
100  """Close cover."""
101  if not self._device_control_device_control:
102  return
103  await self._api_api(self._device_control_device_control.trigger_blind())
104 
105  @property
106  def is_closed(self) -> bool:
107  """Return if the cover is closed or not."""
None async_stop_cover(self, **Any kwargs)
Definition: cover.py:99
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:81
dict[str, str]|None extra_state_attributes(self)
Definition: cover.py:67
None async_open_cover(self, **Any kwargs)
Definition: cover.py:87
None __init__(self, TradfriDeviceDataUpdateCoordinator device_coordinator, Callable[[Command|list[Command]], Any] api, str gateway_id)
Definition: cover.py:51
None async_close_cover(self, **Any kwargs)
Definition: cover.py:93
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:24