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