cover.py 7.9 KB

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