cover.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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._open_dp:
  86. state = self._open_dp.get_value(self._device)
  87. if state is not None:
  88. return 100 if state else 0
  89. if self._action_dp:
  90. state = self._action_dp.get_value(self._device)
  91. return self._state_to_percent(state)
  92. if self._position_dp:
  93. pos = self._position_dp.get_value(self._device)
  94. return pos
  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. pos = self.current_cover_position
  105. if pos is None:
  106. return None
  107. if pos < 5:
  108. return "closed"
  109. elif pos > 95:
  110. return "opened"
  111. if self._currentpos_dp and self._position_dp:
  112. setpos = self._position_dp.get_value(self._device)
  113. if setpos == pos:
  114. # if the current position is around the set position,
  115. # which is not closed, then we want is_closed to return
  116. # false, so HA gets the full state from position.
  117. return "opened"
  118. if self._control_dp:
  119. cmd = self._control_dp.get_value(self._device)
  120. if cmd == "open":
  121. return "opening"
  122. elif cmd == "close":
  123. return "closing"
  124. @property
  125. def is_opening(self):
  126. """Return if the cover is opening or not."""
  127. state = self._current_state
  128. if state is None:
  129. # If we return false, and is_closing and is_opening are also false,
  130. # HA assumes open. If we don't know, return None.
  131. return None
  132. else:
  133. return state == "opening"
  134. @property
  135. def is_closing(self):
  136. """Return if the cover is closing 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 == "closing"
  144. @property
  145. def is_closed(self):
  146. """Return if the cover is closed or not, if it can be determined."""
  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 == "closed"
  154. async def async_open_cover(self, **kwargs):
  155. """Open the cover."""
  156. if self._control_dp and "open" in self._control_dp.values(self._device):
  157. await self._control_dp.async_set_value(self._device, "open")
  158. elif self._position_dp:
  159. pos = 100
  160. await self._position_dp.async_set_value(self._device, pos)
  161. else:
  162. raise NotImplementedError()
  163. async def async_close_cover(self, **kwargs):
  164. """Close the cover."""
  165. if self._control_dp and "close" in self._control_dp.values(self._device):
  166. await self._control_dp.async_set_value(self._device, "close")
  167. elif self._position_dp:
  168. pos = 0
  169. await self._position_dp.async_set_value(self._device, pos)
  170. else:
  171. raise NotImplementedError()
  172. async def async_set_cover_position(self, position, **kwargs):
  173. """Set the cover to a specific position."""
  174. if position is None:
  175. raise AttributeError()
  176. if self._position_dp:
  177. await self._position_dp.async_set_value(self._device, position)
  178. else:
  179. raise NotImplementedError()
  180. async def async_stop_cover(self, **kwargs):
  181. """Stop the cover."""
  182. if self._control_dp and "stop" in self._control_dp.values(self._device):
  183. await self._control_dp.async_set_value(self._device, "stop")
  184. else:
  185. raise NotImplementedError()