Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Representation of the Damper for AirTouch 5 Devices."""
2 
3 import logging
4 from typing import Any
5 
6 from airtouch5py.airtouch5_simple_client import Airtouch5SimpleClient
7 from airtouch5py.packets.zone_control import (
8  ZoneControlZone,
9  ZoneSettingPower,
10  ZoneSettingValue,
11 )
12 from airtouch5py.packets.zone_name import ZoneName
13 from airtouch5py.packets.zone_status import ZoneStatusZone
14 
16  ATTR_POSITION,
17  CoverDeviceClass,
18  CoverEntity,
19  CoverEntityFeature,
20 )
21 from homeassistant.core import HomeAssistant, callback
22 from homeassistant.helpers.device_registry import DeviceInfo
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 
25 from . import Airtouch5ConfigEntry
26 from .const import DOMAIN
27 from .entity import Airtouch5Entity
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 
33  hass: HomeAssistant,
34  config_entry: Airtouch5ConfigEntry,
35  async_add_entities: AddEntitiesCallback,
36 ) -> None:
37  """Set up the Airtouch 5 Cover entities."""
38  client = config_entry.runtime_data
39 
40  # Each zone has a cover for its open percentage
43  client, zone, client.latest_zone_status[zone.zone_number].has_sensor
44  )
45  for zone in client.zones
46  )
47 
48 
50  """How open the damper is in each zone."""
51 
52  _attr_device_class = CoverDeviceClass.DAMPER
53  _attr_translation_key = "damper"
54 
55  # Zones with temperature sensors shouldn't be manually controlled.
56  # We allow it but warn the user in the integration documentation.
57  _attr_supported_features = (
58  CoverEntityFeature.SET_POSITION
59  | CoverEntityFeature.OPEN
60  | CoverEntityFeature.CLOSE
61  )
62 
63  def __init__(
64  self, client: Airtouch5SimpleClient, zone_name: ZoneName, has_sensor: bool
65  ) -> None:
66  """Initialise the Cover Entity."""
67  super().__init__(client)
68  self._zone_name_zone_name = zone_name
69 
70  self._attr_unique_id_attr_unique_id = f"zone_{zone_name.zone_number}_open_percentage"
71  self._attr_device_info_attr_device_info = DeviceInfo(
72  identifiers={(DOMAIN, f"zone_{zone_name.zone_number}")},
73  name=zone_name.zone_name,
74  manufacturer="Polyaire",
75  model="AirTouch 5",
76  )
77 
78  @callback
79  def _async_update_attrs(self, data: dict[int, ZoneStatusZone]) -> None:
80  if self._zone_name_zone_name.zone_number not in data:
81  return
82  status = data[self._zone_name_zone_name.zone_number]
83 
84  self._attr_current_cover_position_attr_current_cover_position = int(status.open_percentage * 100)
85  if status.open_percentage == 0:
86  self._attr_is_closed_attr_is_closed = True
87  else:
88  self._attr_is_closed_attr_is_closed = False
89  self.async_write_ha_stateasync_write_ha_state()
90 
91  async def async_added_to_hass(self) -> None:
92  """Add data updated listener after this object has been initialized."""
93  await super().async_added_to_hass()
94  self._client_client.zone_status_callbacks.append(self._async_update_attrs_async_update_attrs)
95  self._async_update_attrs_async_update_attrs(self._client_client.latest_zone_status)
96 
97  async def async_will_remove_from_hass(self) -> None:
98  """Remove data updated listener after this object has been initialized."""
99  await super().async_will_remove_from_hass()
100  self._client_client.zone_status_callbacks.remove(self._async_update_attrs_async_update_attrs)
101 
102  async def async_open_cover(self, **kwargs: Any) -> None:
103  """Open the damper."""
104  await self._set_cover_position_set_cover_position(100)
105 
106  async def async_close_cover(self, **kwargs: Any) -> None:
107  """Close damper."""
108  await self._set_cover_position_set_cover_position(0)
109 
110  async def async_set_cover_position(self, **kwargs: Any) -> None:
111  """Update the damper to a specific position."""
112 
113  if (position := kwargs.get(ATTR_POSITION)) is None:
114  _LOGGER.debug("Argument `position` is missing in set_cover_position")
115  return
116  await self._set_cover_position_set_cover_position(position)
117 
118  async def _set_cover_position(self, position_percent: float) -> None:
119  power: ZoneSettingPower
120 
121  if position_percent == 0:
122  power = ZoneSettingPower.SET_TO_OFF
123  else:
124  power = ZoneSettingPower.SET_TO_ON
125 
126  zcz = ZoneControlZone(
127  self._zone_name_zone_name.zone_number,
128  ZoneSettingValue.SET_OPEN_PERCENTAGE,
129  power,
130  position_percent / 100.0,
131  )
132 
133  packet = self._client_client.data_packet_factory.zone_control([zcz])
134  await self._client_client.send_packet(packet)
None _async_update_attrs(self, dict[int, ZoneStatusZone] data)
Definition: cover.py:79
None __init__(self, Airtouch5SimpleClient client, ZoneName zone_name, bool has_sensor)
Definition: cover.py:65
None async_setup_entry(HomeAssistant hass, Airtouch5ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:36