Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for ADS covers."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import pyads
8 import voluptuous as vol
9 
11  ATTR_POSITION,
12  DEVICE_CLASSES_SCHEMA,
13  PLATFORM_SCHEMA as COVER_PLATFORM_SCHEMA,
14  CoverDeviceClass,
15  CoverEntity,
16  CoverEntityFeature,
17 )
18 from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
19 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
23 
24 from .const import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE
25 from .entity import AdsEntity
26 from .hub import AdsHub
27 
28 DEFAULT_NAME = "ADS Cover"
29 
30 CONF_ADS_VAR_SET_POS = "adsvar_set_position"
31 CONF_ADS_VAR_OPEN = "adsvar_open"
32 CONF_ADS_VAR_CLOSE = "adsvar_close"
33 CONF_ADS_VAR_STOP = "adsvar_stop"
34 CONF_ADS_VAR_POSITION = "adsvar_position"
35 
36 STATE_KEY_POSITION = "position"
37 
38 PLATFORM_SCHEMA = COVER_PLATFORM_SCHEMA.extend(
39  {
40  vol.Required(CONF_ADS_VAR): cv.string,
41  vol.Optional(CONF_ADS_VAR_POSITION): cv.string,
42  vol.Optional(CONF_ADS_VAR_SET_POS): cv.string,
43  vol.Optional(CONF_ADS_VAR_CLOSE): cv.string,
44  vol.Optional(CONF_ADS_VAR_OPEN): cv.string,
45  vol.Optional(CONF_ADS_VAR_STOP): cv.string,
46  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
47  vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
48  }
49 )
50 
51 
53  hass: HomeAssistant,
54  config: ConfigType,
55  add_entities: AddEntitiesCallback,
56  discovery_info: DiscoveryInfoType | None = None,
57 ) -> None:
58  """Set up the cover platform for ADS."""
59  ads_hub = hass.data[DATA_ADS]
60 
61  ads_var_is_closed: str = config[CONF_ADS_VAR]
62  ads_var_position: str | None = config.get(CONF_ADS_VAR_POSITION)
63  ads_var_pos_set: str | None = config.get(CONF_ADS_VAR_SET_POS)
64  ads_var_open: str | None = config.get(CONF_ADS_VAR_OPEN)
65  ads_var_close: str | None = config.get(CONF_ADS_VAR_CLOSE)
66  ads_var_stop: str | None = config.get(CONF_ADS_VAR_STOP)
67  name: str = config[CONF_NAME]
68  device_class: CoverDeviceClass | None = config.get(CONF_DEVICE_CLASS)
69 
71  [
72  AdsCover(
73  ads_hub,
74  ads_var_is_closed,
75  ads_var_position,
76  ads_var_pos_set,
77  ads_var_open,
78  ads_var_close,
79  ads_var_stop,
80  name,
81  device_class,
82  )
83  ]
84  )
85 
86 
88  """Representation of ADS cover."""
89 
90  def __init__(
91  self,
92  ads_hub: AdsHub,
93  ads_var_is_closed: str,
94  ads_var_position: str | None,
95  ads_var_pos_set: str | None,
96  ads_var_open: str | None,
97  ads_var_close: str | None,
98  ads_var_stop: str | None,
99  name: str,
100  device_class: CoverDeviceClass | None,
101  ) -> None:
102  """Initialize AdsCover entity."""
103  super().__init__(ads_hub, name, ads_var_is_closed)
104  if self._attr_unique_id_attr_unique_id_attr_unique_id is None:
105  if ads_var_position is not None:
106  self._attr_unique_id_attr_unique_id_attr_unique_id = ads_var_position
107  elif ads_var_pos_set is not None:
108  self._attr_unique_id_attr_unique_id_attr_unique_id = ads_var_pos_set
109  elif ads_var_open is not None:
110  self._attr_unique_id_attr_unique_id_attr_unique_id = ads_var_open
111 
112  self._state_dict[STATE_KEY_POSITION] = None
113  self._ads_var_position_ads_var_position = ads_var_position
114  self._ads_var_pos_set_ads_var_pos_set = ads_var_pos_set
115  self._ads_var_open_ads_var_open = ads_var_open
116  self._ads_var_close_ads_var_close = ads_var_close
117  self._ads_var_stop_ads_var_stop = ads_var_stop
118  self._attr_device_class_attr_device_class = device_class
119  self._attr_supported_features_attr_supported_features = (
120  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
121  )
122  if ads_var_stop is not None:
123  self._attr_supported_features_attr_supported_features |= CoverEntityFeature.STOP
124  if ads_var_pos_set is not None:
125  self._attr_supported_features_attr_supported_features |= CoverEntityFeature.SET_POSITION
126 
127  async def async_added_to_hass(self) -> None:
128  """Register device notification."""
129  if self._ads_var_ads_var is not None:
130  await self.async_initialize_deviceasync_initialize_device(self._ads_var_ads_var, pyads.PLCTYPE_BOOL)
131 
132  if self._ads_var_position_ads_var_position is not None:
133  await self.async_initialize_deviceasync_initialize_device(
134  self._ads_var_position_ads_var_position, pyads.PLCTYPE_BYTE, STATE_KEY_POSITION
135  )
136 
137  @property
138  def is_closed(self) -> bool | None:
139  """Return if the cover is closed."""
140  if self._ads_var_ads_var is not None:
141  return self._state_dict[STATE_KEY_STATE]
142  if self._ads_var_position_ads_var_position is not None:
143  return self._state_dict[STATE_KEY_POSITION] == 0
144  return None
145 
146  @property
147  def current_cover_position(self) -> int:
148  """Return current position of cover."""
149  return self._state_dict[STATE_KEY_POSITION]
150 
151  def stop_cover(self, **kwargs: Any) -> None:
152  """Fire the stop action."""
153  if self._ads_var_stop_ads_var_stop:
154  self._ads_hub_ads_hub.write_by_name(self._ads_var_stop_ads_var_stop, True, pyads.PLCTYPE_BOOL)
155 
156  def set_cover_position(self, **kwargs: Any) -> None:
157  """Set cover position."""
158  position = kwargs[ATTR_POSITION]
159  if self._ads_var_pos_set_ads_var_pos_set is not None:
160  self._ads_hub_ads_hub.write_by_name(
161  self._ads_var_pos_set_ads_var_pos_set, position, pyads.PLCTYPE_BYTE
162  )
163 
164  def open_cover(self, **kwargs: Any) -> None:
165  """Move the cover up."""
166  if self._ads_var_open_ads_var_open is not None:
167  self._ads_hub_ads_hub.write_by_name(self._ads_var_open_ads_var_open, True, pyads.PLCTYPE_BOOL)
168  elif self._ads_var_pos_set_ads_var_pos_set is not None:
169  self.set_cover_positionset_cover_positionset_cover_position(position=100)
170 
171  def close_cover(self, **kwargs: Any) -> None:
172  """Move the cover down."""
173  if self._ads_var_close_ads_var_close is not None:
174  self._ads_hub_ads_hub.write_by_name(self._ads_var_close_ads_var_close, True, pyads.PLCTYPE_BOOL)
175  elif self._ads_var_pos_set_ads_var_pos_set is not None:
176  self.set_cover_positionset_cover_positionset_cover_position(position=0)
177 
178  @property
179  def available(self) -> bool:
180  """Return False if state has not been updated yet."""
181  if self._ads_var_ads_var is not None or self._ads_var_position_ads_var_position is not None:
182  return (
183  self._state_dict[STATE_KEY_STATE] is not None
184  or self._state_dict[STATE_KEY_POSITION] is not None
185  )
186  return True
None close_cover(self, **Any kwargs)
Definition: cover.py:171
None open_cover(self, **Any kwargs)
Definition: cover.py:164
None __init__(self, AdsHub ads_hub, str ads_var_is_closed, str|None ads_var_position, str|None ads_var_pos_set, str|None ads_var_open, str|None ads_var_close, str|None ads_var_stop, str name, CoverDeviceClass|None device_class)
Definition: cover.py:101
None set_cover_position(self, **Any kwargs)
Definition: cover.py:156
None stop_cover(self, **Any kwargs)
Definition: cover.py:151
None async_initialize_device(self, str ads_var, type plctype, str state_key=STATE_KEY_STATE, int|None factor=None)
Definition: entity.py:37
None set_cover_position(self, **Any kwargs)
Definition: __init__.py:426
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: cover.py:57
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)