Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for Xiaomi curtain."""
2 
3 from typing import Any
4 
5 from homeassistant.components.cover import ATTR_POSITION, CoverEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 
10 from .const import DOMAIN, GATEWAYS_KEY
11 from .entity import XiaomiDevice
12 
13 ATTR_CURTAIN_LEVEL = "curtain_level"
14 
15 DATA_KEY_PROTO_V1 = "status"
16 DATA_KEY_PROTO_V2 = "curtain_status"
17 
18 
20  hass: HomeAssistant,
21  config_entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Perform the setup for Xiaomi devices."""
25  entities = []
26  gateway = hass.data[DOMAIN][GATEWAYS_KEY][config_entry.entry_id]
27  for device in gateway.devices["cover"]:
28  model = device["model"]
29  if model in ("curtain", "curtain.aq2", "curtain.hagl04"):
30  if "proto" not in device or int(device["proto"][0:1]) == 1:
31  data_key = DATA_KEY_PROTO_V1
32  else:
33  data_key = DATA_KEY_PROTO_V2
34  entities.append(
35  XiaomiGenericCover(device, "Curtain", data_key, gateway, config_entry)
36  )
37  async_add_entities(entities)
38 
39 
41  """Representation of a XiaomiGenericCover."""
42 
43  def __init__(self, device, name, data_key, xiaomi_hub, config_entry):
44  """Initialize the XiaomiGenericCover."""
45  self._data_key_data_key = data_key
46  self._pos_pos = 0
47  super().__init__(device, name, xiaomi_hub, config_entry)
48 
49  @property
50  def current_cover_position(self) -> int:
51  """Return the current position of the cover."""
52  return self._pos_pos
53 
54  @property
55  def is_closed(self) -> bool:
56  """Return if the cover is closed."""
57  return self.current_cover_positioncurrent_cover_positioncurrent_cover_positioncurrent_cover_position <= 0
58 
59  def close_cover(self, **kwargs: Any) -> None:
60  """Close the cover."""
61  self._write_to_hub_write_to_hub(self._sid_sid, **{self._data_key_data_key: "close"})
62 
63  def open_cover(self, **kwargs: Any) -> None:
64  """Open the cover."""
65  self._write_to_hub_write_to_hub(self._sid_sid, **{self._data_key_data_key: "open"})
66 
67  def stop_cover(self, **kwargs: Any) -> None:
68  """Stop the cover."""
69  self._write_to_hub_write_to_hub(self._sid_sid, **{self._data_key_data_key: "stop"})
70 
71  def set_cover_position(self, **kwargs: Any) -> None:
72  """Move the cover to a specific position."""
73  position = kwargs.get(ATTR_POSITION)
74  if self._data_key_data_key == DATA_KEY_PROTO_V2:
75  self._write_to_hub_write_to_hub(self._sid_sid, **{ATTR_CURTAIN_LEVEL: position})
76  else:
77  self._write_to_hub_write_to_hub(self._sid_sid, **{ATTR_CURTAIN_LEVEL: str(position)})
78 
79  def parse_data(self, data, raw_data):
80  """Parse data sent by gateway."""
81  if ATTR_CURTAIN_LEVEL in data:
82  self._pos_pos = int(data[ATTR_CURTAIN_LEVEL])
83  return True
84  return False
def __init__(self, device, name, data_key, xiaomi_hub, config_entry)
Definition: cover.py:43
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:23