Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Support for Bond covers."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from bond_async import Action, DeviceType
8 
10  ATTR_POSITION,
11  CoverDeviceClass,
12  CoverEntity,
13  CoverEntityFeature,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import BondConfigEntry
19 from .entity import BondEntity
20 from .models import BondData
21 from .utils import BondDevice
22 
23 
24 def _bond_to_hass_position(bond_position: int) -> int:
25  """Convert bond 0-open 100-closed to hass 0-closed 100-open."""
26  return abs(bond_position - 100)
27 
28 
29 def _hass_to_bond_position(hass_position: int) -> int:
30  """Convert hass 0-closed 100-open to bond 0-open 100-closed."""
31  return 100 - hass_position
32 
33 
35  hass: HomeAssistant,
36  entry: BondConfigEntry,
37  async_add_entities: AddEntitiesCallback,
38 ) -> None:
39  """Set up Bond cover devices."""
40  data = entry.runtime_data
42  BondCover(data, device)
43  for device in data.hub.devices
44  if device.type == DeviceType.MOTORIZED_SHADES
45  )
46 
47 
49  """Representation of a Bond cover."""
50 
51  _attr_device_class = CoverDeviceClass.SHADE
52 
53  def __init__(self, data: BondData, device: BondDevice) -> None:
54  """Create HA entity representing Bond cover."""
55  super().__init__(data, device)
56  supported_features = CoverEntityFeature(0)
57  if self._device_device.supports_set_position():
58  supported_features |= CoverEntityFeature.SET_POSITION
59  if self._device_device.supports_open():
60  supported_features |= CoverEntityFeature.OPEN
61  if self._device_device.supports_close():
62  supported_features |= CoverEntityFeature.CLOSE
63  if self._device_device.supports_tilt_open():
64  supported_features |= CoverEntityFeature.OPEN_TILT
65  if self._device_device.supports_tilt_close():
66  supported_features |= CoverEntityFeature.CLOSE_TILT
67  if self._device_device.supports_hold():
68  if self._device_device.supports_open() or self._device_device.supports_close():
69  supported_features |= CoverEntityFeature.STOP
70  if self._device_device.supports_tilt_open() or self._device_device.supports_tilt_close():
71  supported_features |= CoverEntityFeature.STOP_TILT
72  self._attr_supported_features_attr_supported_features = supported_features
73 
74  def _apply_state(self) -> None:
75  state = self._device_device.state
76  cover_open = state.get("open")
77  self._attr_is_closed_attr_is_closed = None if cover_open is None else cover_open == 0
78  if (bond_position := state.get("position")) is not None:
79  self._attr_current_cover_position_attr_current_cover_position = _bond_to_hass_position(bond_position)
80 
81  async def async_set_cover_position(self, **kwargs: Any) -> None:
82  """Set the cover position."""
83  await self._bond_bond.action(
84  self._device_id_device_id,
85  Action.set_position(_hass_to_bond_position(kwargs[ATTR_POSITION])),
86  )
87 
88  async def async_open_cover(self, **kwargs: Any) -> None:
89  """Open the cover."""
90  await self._bond_bond.action(self._device_id_device_id, Action.open())
91 
92  async def async_close_cover(self, **kwargs: Any) -> None:
93  """Close cover."""
94  await self._bond_bond.action(self._device_id_device_id, Action.close())
95 
96  async def async_stop_cover(self, **kwargs: Any) -> None:
97  """Hold cover."""
98  await self._bond_bond.action(self._device_id_device_id, Action.hold())
99 
100  async def async_open_cover_tilt(self, **kwargs: Any) -> None:
101  """Open the cover tilt."""
102  await self._bond_bond.action(self._device_id_device_id, Action.tilt_open())
103 
104  async def async_close_cover_tilt(self, **kwargs: Any) -> None:
105  """Close the cover tilt."""
106  await self._bond_bond.action(self._device_id_device_id, Action.tilt_close())
107 
108  async def async_stop_cover_tilt(self, **kwargs: Any) -> None:
109  """Stop the cover."""
110  await self._bond_bond.action(self._device_id_device_id, Action.hold())
None async_set_cover_position(self, **Any kwargs)
Definition: cover.py:81
None async_open_cover(self, **Any kwargs)
Definition: cover.py:88
None __init__(self, BondData data, BondDevice device)
Definition: cover.py:53
None async_stop_cover_tilt(self, **Any kwargs)
Definition: cover.py:108
None async_stop_cover(self, **Any kwargs)
Definition: cover.py:96
None async_open_cover_tilt(self, **Any kwargs)
Definition: cover.py:100
None async_close_cover(self, **Any kwargs)
Definition: cover.py:92
None async_close_cover_tilt(self, **Any kwargs)
Definition: cover.py:104
int _bond_to_hass_position(int bond_position)
Definition: cover.py:24
int _hass_to_bond_position(int hass_position)
Definition: cover.py:29
None async_setup_entry(HomeAssistant hass, BondConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:38