cover.py 5.3 KB

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