cover.py 7.8 KB

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