cover.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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._init_end(dps_map)
  40. self._support_flags = 0
  41. if self._position_dp:
  42. self._support_flags |= CoverEntityFeature.SET_POSITION
  43. if self._control_dp:
  44. if "stop" in self._control_dp.values(self._device):
  45. self._support_flags |= CoverEntityFeature.STOP
  46. if "open" in self._control_dp.values(self._device):
  47. self._support_flags |= CoverEntityFeature.OPEN
  48. if "close" in self._control_dp.values(self._device):
  49. self._support_flags |= CoverEntityFeature.CLOSE
  50. # Tilt not yet supported, as no test devices known
  51. @property
  52. def device_class(self):
  53. """Return the class of ths device"""
  54. dclass = self._config.device_class
  55. try:
  56. return CoverDeviceClass(dclass)
  57. except ValueError:
  58. if dclass:
  59. _LOGGER.warning(f"Unrecognised cover device class of {dclass} ignored")
  60. return None
  61. @property
  62. def supported_features(self):
  63. """Inform HA of the supported features."""
  64. return self._support_flags
  65. def _state_to_percent(self, state):
  66. """Convert a state to percent open"""
  67. if state == "opened":
  68. return 100
  69. elif state == "closed":
  70. return 0
  71. else:
  72. return 50
  73. @property
  74. def current_cover_position(self):
  75. """Return current position of cover."""
  76. if self._currentpos_dp:
  77. pos = self._currentpos_dp.get_value(self._device)
  78. if pos is not None:
  79. return pos
  80. if self._position_dp:
  81. pos = self._position_dp.get_value(self._device)
  82. return pos
  83. if self._open_dp:
  84. state = self._open_dp.get_value(self._device)
  85. if state is not None:
  86. return 100 if state else 0
  87. if self._action_dp:
  88. state = self._action_dp.get_value(self._device)
  89. return self._state_to_percent(state)
  90. @property
  91. def _current_state(self):
  92. """Return the current state of the cover if it can be determined,
  93. or None if it is inconclusive.
  94. """
  95. if self._action_dp:
  96. action = self._action_dp.get_value(self._device)
  97. if action in ["opening", "closing", "opened", "closed"]:
  98. return action
  99. if self._currentpos_dp:
  100. pos = self._currentpos_dp.get_value(self._device)
  101. # we have a current pos dp, but it isn't telling us where the
  102. # curtain is... we can't tell the state.
  103. if pos is None:
  104. return None
  105. if pos < 5:
  106. return "closed"
  107. elif pos > 95:
  108. return "opened"
  109. if self._position_dp:
  110. setpos = self._position_dp.get_value(self._device)
  111. if setpos == pos:
  112. # if the current position is around the set position,
  113. # probably the curtain is as set, somewhere in the middle
  114. # so none of opened, closed, opening or closing
  115. return None
  116. if self._control_dp:
  117. cmd = self._control_dp.get_value(self._device)
  118. pos = self.current_cover_position
  119. if pos is not None:
  120. if cmd == "open":
  121. if pos > 95:
  122. return "opened"
  123. else:
  124. return "opening"
  125. elif cmd == "close":
  126. if pos < 5:
  127. return "closed"
  128. else:
  129. return "closing"
  130. @property
  131. def is_opening(self):
  132. """Return if the cover is opening or not."""
  133. state = self._current_state
  134. if state is None:
  135. # If we return false, and is_closing and is_opening are also false,
  136. # HA assumes open. If we don't know, return None.
  137. return None
  138. else:
  139. return state == "opening"
  140. @property
  141. def is_closing(self):
  142. """Return if the cover is closing or not."""
  143. state = self._current_state
  144. if state is None:
  145. # If we return false, and is_closing and is_opening are also false,
  146. # HA assumes open. If we don't know, return None.
  147. return None
  148. else:
  149. return state == "closing"
  150. @property
  151. def is_closed(self):
  152. """Return if the cover is closed or not, if it can be determined."""
  153. state = self._current_state
  154. if state is None:
  155. # If we return false, and is_closing and is_opening are also false,
  156. # HA assumes open. If we don't know, return None.
  157. return None
  158. else:
  159. return state == "closed"
  160. async def async_open_cover(self, **kwargs):
  161. """Open the cover."""
  162. if self._control_dp and "open" in self._control_dp.values(self._device):
  163. await self._control_dp.async_set_value(self._device, "open")
  164. elif self._position_dp:
  165. pos = 100
  166. await self._position_dp.async_set_value(self._device, pos)
  167. else:
  168. raise NotImplementedError()
  169. async def async_close_cover(self, **kwargs):
  170. """Close the cover."""
  171. if self._control_dp and "close" in self._control_dp.values(self._device):
  172. await self._control_dp.async_set_value(self._device, "close")
  173. elif self._position_dp:
  174. pos = 0
  175. await self._position_dp.async_set_value(self._device, pos)
  176. else:
  177. raise NotImplementedError()
  178. async def async_set_cover_position(self, position, **kwargs):
  179. """Set the cover to a specific position."""
  180. if position is None:
  181. raise AttributeError()
  182. if self._position_dp:
  183. await self._position_dp.async_set_value(self._device, position)
  184. else:
  185. raise NotImplementedError()
  186. async def async_stop_cover(self, **kwargs):
  187. """Stop the cover."""
  188. if self._control_dp and "stop" in self._control_dp.values(self._device):
  189. await self._control_dp.async_set_value(self._device, "stop")
  190. else:
  191. raise NotImplementedError()