Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for WiLight Cover."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pywilight.const import (
8  COVER_V1,
9  ITEM_COVER,
10  WL_CLOSE,
11  WL_CLOSING,
12  WL_OPEN,
13  WL_OPENING,
14  WL_STOP,
15  WL_STOPPED,
16 )
17 
18 from homeassistant.components.cover import ATTR_POSITION, CoverEntity
19 from homeassistant.config_entries import ConfigEntry
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from .const import DOMAIN
24 from .entity import WiLightDevice
25 from .parent_device import WiLightParent
26 
27 
29  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
30 ) -> None:
31  """Set up WiLight covers from a config entry."""
32  parent: WiLightParent = hass.data[DOMAIN][entry.entry_id]
33 
34  # Handle a discovered WiLight device.
35  entities = []
36  assert parent.api
37  for item in parent.api.items:
38  if item["type"] != ITEM_COVER:
39  continue
40  index = item["index"]
41  item_name = item["name"]
42  if item["sub_type"] != COVER_V1:
43  continue
44  entities.append(WiLightCover(parent.api, index, item_name))
45 
46  async_add_entities(entities)
47 
48 
49 def wilight_to_hass_position(value: int) -> int:
50  """Convert wilight position 1..255 to hass format 0..100."""
51  return min(100, round((value * 100) / 255))
52 
53 
54 def hass_to_wilight_position(value: int) -> int:
55  """Convert hass position 0..100 to wilight 1..255 scale."""
56  return min(255, round((value * 255) / 100))
57 
58 
60  """Representation of a WiLights cover."""
61 
62  _attr_name = None
63 
64  @property
65  def current_cover_position(self) -> int | None:
66  """Return current position of cover.
67 
68  None is unknown, 0 is closed, 100 is fully open.
69  """
70  if "position_current" in self._status_status:
71  return wilight_to_hass_position(self._status_status["position_current"])
72  return None
73 
74  @property
75  def is_opening(self) -> bool | None:
76  """Return if the cover is opening or not."""
77  if "motor_state" not in self._status_status:
78  return None
79  return self._status_status["motor_state"] == WL_OPENING
80 
81  @property
82  def is_closing(self) -> bool | None:
83  """Return if the cover is closing or not."""
84  if "motor_state" not in self._status_status:
85  return None
86  return self._status_status["motor_state"] == WL_CLOSING
87 
88  @property
89  def is_closed(self) -> bool | None:
90  """Return if the cover is closed or not."""
91  if "motor_state" not in self._status_status or "position_current" not in self._status_status:
92  return None
93  return (
94  self._status_status["motor_state"] == WL_STOPPED
95  and wilight_to_hass_position(self._status_status["position_current"]) == 0
96  )
97 
98  async def async_open_cover(self, **kwargs: Any) -> None:
99  """Open the cover."""
100  await self._client_client.cover_command(self._index_index, WL_OPEN)
101 
102  async def async_close_cover(self, **kwargs: Any) -> None:
103  """Close cover."""
104  await self._client_client.cover_command(self._index_index, WL_CLOSE)
105 
106  async def async_set_cover_position(self, **kwargs: Any) -> None:
107  """Move the cover to a specific position."""
108  position = hass_to_wilight_position(kwargs[ATTR_POSITION])
109  await self._client_client.set_cover_position(self._index_index, position)
110 
111  async def async_stop_cover(self, **kwargs: Any) -> None:
112  """Stop the cover."""
113  await self._client_client.cover_command(self._index_index, WL_STOP)
None set_cover_position(self, **Any kwargs)
Definition: __init__.py:426
None async_open_cover(self, **Any kwargs)
Definition: cover.py:98
None async_close_cover(self, **Any kwargs)
Definition: cover.py:102
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:106
int hass_to_wilight_position(int value)
Definition: cover.py:54
int wilight_to_hass_position(int value)
Definition: cover.py:49
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:30