Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for ISY covers."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from pyisy.constants import ISY_VALUE_UNKNOWN
8 
10  ATTR_POSITION,
11  CoverEntity,
12  CoverEntityFeature,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import Platform
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.device_registry import DeviceInfo
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import _LOGGER, DOMAIN, UOM_8_BIT_RANGE
21 from .entity import ISYNodeEntity, ISYProgramEntity
22 from .models import IsyData
23 
24 
26  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
27 ) -> None:
28  """Set up the ISY cover platform."""
29  isy_data: IsyData = hass.data[DOMAIN][entry.entry_id]
30  devices: dict[str, DeviceInfo] = isy_data.devices
31  entities: list[ISYCoverEntity | ISYCoverProgramEntity] = [
32  ISYCoverEntity(node, devices.get(node.primary_node))
33  for node in isy_data.nodes[Platform.COVER]
34  ]
35 
36  entities.extend(
37  ISYCoverProgramEntity(name, status, actions)
38  for name, status, actions in isy_data.programs[Platform.COVER]
39  )
40 
41  async_add_entities(entities)
42 
43 
45  """Representation of an ISY cover device."""
46 
47  _attr_supported_features = (
48  CoverEntityFeature.OPEN
49  | CoverEntityFeature.CLOSE
50  | CoverEntityFeature.SET_POSITION
51  )
52 
53  @property
54  def current_cover_position(self) -> int | None:
55  """Return the current cover position."""
56  if self._node_node.status == ISY_VALUE_UNKNOWN:
57  return None
58  if self._node_node.uom == UOM_8_BIT_RANGE:
59  return round(cast(float, self._node_node.status) * 100.0 / 255.0)
60  return int(sorted((0, self._node_node.status, 100))[1])
61 
62  @property
63  def is_closed(self) -> bool | None:
64  """Get whether the ISY cover device is closed."""
65  if self._node_node.status == ISY_VALUE_UNKNOWN:
66  return None
67  return bool(self._node_node.status == 0)
68 
69  async def async_open_cover(self, **kwargs: Any) -> None:
70  """Send the open cover command to the ISY cover device."""
71  if not await self._node_node.turn_on():
72  _LOGGER.error("Unable to open the cover")
73 
74  async def async_close_cover(self, **kwargs: Any) -> None:
75  """Send the close cover command to the ISY cover device."""
76  if not await self._node_node.turn_off():
77  _LOGGER.error("Unable to close the cover")
78 
79  async def async_set_cover_position(self, **kwargs: Any) -> None:
80  """Move the cover to a specific position."""
81  position = kwargs[ATTR_POSITION]
82  if self._node_node.uom == UOM_8_BIT_RANGE:
83  position = round(position * 255.0 / 100.0)
84  if not await self._node_node.turn_on(val=position):
85  _LOGGER.error("Unable to set cover position")
86 
87 
89  """Representation of an ISY cover program."""
90 
91  @property
92  def is_closed(self) -> bool:
93  """Get whether the ISY cover program is closed."""
94  return bool(self._node_node.status)
95 
96  async def async_open_cover(self, **kwargs: Any) -> None:
97  """Send the open cover command to the ISY cover program."""
98  if not await self._actions_actions.run_then():
99  _LOGGER.error("Unable to open the cover")
100 
101  async def async_close_cover(self, **kwargs: Any) -> None:
102  """Send the close cover command to the ISY cover program."""
103  if not await self._actions_actions.run_else():
104  _LOGGER.error("Unable to close the cover")
None async_close_cover(self, **Any kwargs)
Definition: cover.py:74
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:79
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:27