cover.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """
  2. Setup for different kinds of Tuya cover devices
  3. """
  4. from homeassistant.components.cover import (
  5. CoverDeviceClass,
  6. CoverEntity,
  7. CoverEntityFeature,
  8. )
  9. import logging
  10. from .device import TuyaLocalDevice
  11. from .helpers.config import async_tuya_setup_platform
  12. from .helpers.device_config import TuyaEntityConfig
  13. from .helpers.mixin import TuyaLocalEntity
  14. _LOGGER = logging.getLogger(__name__)
  15. async def async_setup_entry(hass, config_entry, async_add_entities):
  16. config = {**config_entry.data, **config_entry.options}
  17. await async_tuya_setup_platform(
  18. hass,
  19. async_add_entities,
  20. config,
  21. "cover",
  22. TuyaLocalCover,
  23. )
  24. class TuyaLocalCover(TuyaLocalEntity, CoverEntity):
  25. """Representation of a Tuya Cover Entity."""
  26. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  27. """
  28. Initialise the cover device.
  29. Args:
  30. device (TuyaLocalDevice): The device API instance
  31. config (TuyaEntityConfig): The entity config
  32. """
  33. dps_map = self._init_begin(device, config)
  34. self._position_dp = dps_map.pop("position", None)
  35. self._currentpos_dp = dps_map.pop("current_position", None)
  36. self._control_dp = dps_map.pop("control", None)
  37. self._action_dp = dps_map.pop("action", None)
  38. self._open_dp = dps_map.pop("open", None)
  39. self._reversed_dp = dps_map.pop("reversed", None)
  40. self._init_end(dps_map)
  41. self._support_flags = 0
  42. if self._position_dp:
  43. self._support_flags |= CoverEntityFeature.SET_POSITION
  44. if self._control_dp:
  45. if "stop" in self._control_dp.values(self._device):
  46. self._support_flags |= CoverEntityFeature.STOP
  47. if "open" in self._control_dp.values(self._device):
  48. self._support_flags |= CoverEntityFeature.OPEN
  49. if "close" in self._control_dp.values(self._device):
  50. self._support_flags |= CoverEntityFeature.CLOSE
  51. # Tilt not yet supported, as no test devices known
  52. @property
  53. def _is_reversed(self):
  54. return self._reversed_dp and self._reversed_dp.get_value(self._device)
  55. def _maybe_reverse(self, percent):
  56. """Reverse the percentage if it should be, otherwise leave it alone"""
  57. return 100 - percent if self._is_reversed else percent
  58. @property
  59. def device_class(self):
  60. """Return the class of ths device"""
  61. dclass = self._config.device_class
  62. try:
  63. return CoverDeviceClass(dclass)
  64. except ValueError:
  65. if dclass:
  66. _LOGGER.warning(f"Unrecognised cover device class of {dclass} ignored")
  67. return None
  68. @property
  69. def supported_features(self):
  70. """Inform HA of the supported features."""
  71. return self._support_flags
  72. def _state_to_percent(self, state):
  73. """Convert a state to percent open"""
  74. if state == "opened":
  75. return 100
  76. elif state == "closed":
  77. return 0
  78. else:
  79. return 50
  80. @property
  81. def current_cover_position(self):
  82. """Return current position of cover."""
  83. if self._currentpos_dp:
  84. pos = self._currentpos_dp.get_value(self._device)
  85. if pos is not None:
  86. return self._maybe_reverse(pos)
  87. if self._position_dp:
  88. pos = self._position_dp.get_value(self._device)
  89. return self._maybe_reverse(pos)
  90. if self._open_dp:
  91. state = self._open_dp.get_value(self._device)
  92. if state is not None:
  93. return 100 if state else 0
  94. if self._action_dp:
  95. state = self._action_dp.get_value(self._device)
  96. return self._state_to_percent(state)
  97. @property
  98. def is_opening(self):
  99. """Return if the cover is opening or not."""
  100. # If dps is available to inform current action, use that
  101. if self._action_dp:
  102. action = self._action_dp.get_value(self._device)
  103. if action is not None:
  104. return action == "opening"
  105. # Otherwise use last command and check it hasn't completed
  106. if self._control_dp:
  107. cmd = self._control_dp.get_value(self._device)
  108. pos = self.current_cover_position
  109. if pos is not None:
  110. return (
  111. cmd != "close"
  112. and cmd != "stop"
  113. and self.current_cover_position < 95
  114. )
  115. @property
  116. def is_closing(self):
  117. """Return if the cover is closing or not."""
  118. # If dps is available to inform current action, use that
  119. if self._action_dp:
  120. action = self._action_dp.get_value(self._device)
  121. if action is not None:
  122. return action == "closing"
  123. # Otherwise use last command and check it hasn't completed
  124. if self._control_dp:
  125. closed = self.is_closed
  126. cmd = self._control_dp.get_value(self._device)
  127. if closed is not None:
  128. return cmd != "open" and cmd != "stop" and not closed
  129. @property
  130. def is_closed(self):
  131. """Return if the cover is closed or not, if it can be determined."""
  132. # Only use position if it is reliable, otherwise curtain can become
  133. # stuck in "open" state when we don't actually know what state it is.
  134. pos = self.current_cover_position
  135. if isinstance(pos, int):
  136. return pos < 5
  137. async def async_open_cover(self, **kwargs):
  138. """Open the cover."""
  139. if self._control_dp and "open" in self._control_dp.values(self._device):
  140. await self._control_dp.async_set_value(self._device, "open")
  141. elif self._position_dp:
  142. pos = self._maybe_reverse(100)
  143. await self._position_dp.async_set_value(self._device, pos)
  144. else:
  145. raise NotImplementedError()
  146. async def async_close_cover(self, **kwargs):
  147. """Close the cover."""
  148. if self._control_dp and "close" in self._control_dp.values(self._device):
  149. await self._control_dp.async_set_value(self._device, "close")
  150. elif self._position_dp:
  151. pos = self._maybe_reverse(0)
  152. await self._position_dp.async_set_value(self._device, pos)
  153. else:
  154. raise NotImplementedError()
  155. async def async_set_cover_position(self, position, **kwargs):
  156. """Set the cover to a specific position."""
  157. if position is None:
  158. raise AttributeError()
  159. if self._position_dp:
  160. position = self._maybe_reverse(position)
  161. await self._position_dp.async_set_value(self._device, position)
  162. else:
  163. raise NotImplementedError()
  164. async def async_stop_cover(self, **kwargs):
  165. """Stop the cover."""
  166. if self._control_dp and "stop" in self._control_dp.values(self._device):
  167. await self._control_dp.async_set_value(self._device, "stop")
  168. else:
  169. raise NotImplementedError()