cover.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """
  2. Platform to control 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.device_config import TuyaEntityConfig
  12. from ..helpers.mixin import TuyaLocalEntity
  13. _LOGGER = logging.getLogger(__name__)
  14. class TuyaLocalCover(TuyaLocalEntity, CoverEntity):
  15. """Representation of a Tuya Cover Entity."""
  16. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  17. """
  18. Initialise the cover device.
  19. Args:
  20. device (TuyaLocalDevice): The device API instance
  21. config (TuyaEntityConfig): The entity config
  22. """
  23. dps_map = self._init_begin(device, config)
  24. self._position_dp = dps_map.pop("position", None)
  25. self._currentpos_dp = dps_map.pop("current_position", None)
  26. self._control_dp = dps_map.pop("control", None)
  27. self._action_dp = dps_map.pop("action", None)
  28. self._open_dp = dps_map.pop("open", None)
  29. self._reversed_dp = dps_map.pop("reversed", None)
  30. self._init_end(dps_map)
  31. self._support_flags = 0
  32. if self._position_dp:
  33. self._support_flags |= CoverEntityFeature.SET_POSITION
  34. if self._control_dp:
  35. if "stop" in self._control_dp.values(self._device):
  36. self._support_flags |= CoverEntityFeature.STOP
  37. if "open" in self._control_dp.values(self._device):
  38. self._support_flags |= CoverEntityFeature.OPEN
  39. if "close" in self._control_dp.values(self._device):
  40. self._support_flags |= CoverEntityFeature.CLOSE
  41. # Tilt not yet supported, as no test devices known
  42. @property
  43. def _is_reversed(self):
  44. return self._reversed_dp and self._reversed_dp.get_value(self._device)
  45. @property
  46. def device_class(self):
  47. """Return the class of ths device"""
  48. dclass = self._config.device_class
  49. try:
  50. return CoverDeviceClass(dclass)
  51. except ValueError:
  52. if dclass:
  53. _LOGGER.warning(f"Unrecognised cover device class of {dclass} ignored")
  54. return None
  55. @property
  56. def supported_features(self):
  57. """Inform HA of the supported features."""
  58. return self._support_flags
  59. @property
  60. def current_cover_position(self):
  61. """Return current position of cover."""
  62. if self._currentpos_dp:
  63. pos = self._currentpos_dp.get_value(self._device)
  64. if pos is not None:
  65. if self._is_reversed:
  66. return 100 - pos
  67. return pos
  68. if self._position_dp:
  69. pos = self._position_dp.get_value(self._device)
  70. if self._is_reversed:
  71. return 100 - pos
  72. return pos
  73. if self._open_dp:
  74. state = self._open_dp.get_value(self._device)
  75. if state is not None:
  76. return 100 if state else 0
  77. if self._action_dp:
  78. state = self._action_dp.get_value(self._device)
  79. if state == "opened":
  80. return 100
  81. elif state == "closed":
  82. return 0
  83. else:
  84. return 50
  85. @property
  86. def is_opening(self):
  87. """Return if the cover is opening or not."""
  88. # If dps is available to inform current action, use that
  89. if self._action_dp:
  90. return self._action_dp.get_value(self._device) == "opening"
  91. # Otherwise use last command and check it hasn't completed
  92. if self._control_dp:
  93. return (
  94. self._control_dp.get_value(self._device) == "open"
  95. and self.current_cover_position != 100
  96. )
  97. @property
  98. def is_closing(self):
  99. """Return if the cover is closing or not."""
  100. # If dps is available to inform current action, use that
  101. if self._action_dp:
  102. return self._action_dp.get_value(self._device) == "closing"
  103. # Otherwise use last command and check it hasn't completed
  104. if self._control_dp:
  105. return (
  106. self._control_dp.get_value(self._device) == "close"
  107. and not self.is_closed
  108. )
  109. @property
  110. def is_closed(self):
  111. """Return if the cover is closed or not."""
  112. return self.current_cover_position == 0
  113. async def async_open_cover(self, **kwargs):
  114. """Open the cover."""
  115. if self._control_dp and "open" in self._control_dp.values(self._device):
  116. await self._control_dp.async_set_value(self._device, "open")
  117. elif self._position_dp:
  118. pos = 0 if self._is_reversed else 100
  119. await self._position_dp.async_set_value(self._device, pos)
  120. else:
  121. raise NotImplementedError()
  122. async def async_close_cover(self, **kwargs):
  123. """Close the cover."""
  124. if self._control_dp and "close" in self._control_dp.values(self._device):
  125. await self._control_dp.async_set_value(self._device, "close")
  126. elif self._position_dp:
  127. pos = 100 if self._is_reversed else 0
  128. await self._position_dp.async_set_value(self._device, pos)
  129. else:
  130. raise NotImplementedError()
  131. async def async_set_cover_position(self, position, **kwargs):
  132. """Set the cover to a specific position."""
  133. if position is None:
  134. raise AttributeError()
  135. if self._position_dp:
  136. if self._is_reversed:
  137. position = 100 - position
  138. await self._position_dp.async_set_value(self._device, position)
  139. else:
  140. raise NotImplementedError()
  141. async def async_stop_cover(self, **kwargs):
  142. """Stop the cover."""
  143. if self._control_dp and "stop" in self._control_dp.values(self._device):
  144. await self._control_dp.async_set_value(self._device, "stop")
  145. else:
  146. raise NotImplementedError()