cover.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. Platform to control tuya cover devices.
  3. """
  4. import logging
  5. from homeassistant.components.cover import (
  6. CoverEntity,
  7. DEVICE_CLASSES,
  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. if dclass in DEVICE_CLASSES:
  48. return dclass
  49. else:
  50. return None
  51. @property
  52. def supported_features(self):
  53. """Inform HA of the supported features."""
  54. return self._support_flags
  55. @property
  56. def current_cover_position(self):
  57. """Return current position of cover."""
  58. if self._position_dps:
  59. return self._position_dps.get_value(self._device)
  60. if self._open_dps:
  61. state = self._open_dps.get_value(self._device)
  62. if state is not None:
  63. return 100 if state else 0
  64. if self._action_dps:
  65. state = self._action_dps.get_value(self._device)
  66. return 100 if state == "opened" else 0 if state == "closed" else 50
  67. @property
  68. def is_opening(self):
  69. """Return if the cover is opening or not."""
  70. # If dps is available to inform current action, use that
  71. if self._action_dps:
  72. return self._action_dps.get_value(self._device) == "opening"
  73. # Otherwise use last command and check it hasn't completed
  74. if self._control_dps:
  75. return (
  76. self._control_dps.get_value(self._device) == "open"
  77. and self.current_cover_position != 100
  78. )
  79. @property
  80. def is_closing(self):
  81. """Return if the cover is closing or not."""
  82. # If dps is available to inform current action, use that
  83. if self._action_dps:
  84. return self._action_dps.get_value(self._device) == "closing"
  85. # Otherwise use last command and check it hasn't completed
  86. if self._control_dps:
  87. return (
  88. self._control_dps.get_value(self._device) == "close"
  89. and not self.is_closed
  90. )
  91. @property
  92. def is_closed(self):
  93. """Return if the cover is closed or not."""
  94. return self.current_cover_position == 0
  95. async def async_open_cover(self, **kwargs):
  96. """Open the cover."""
  97. if self._control_dps and "open" in self._control_dps.values(self._device):
  98. await self._control_dps.async_set_value(self._device, "open")
  99. elif self._position_dps:
  100. await self._position_dps.async_set_value(self._device, 100)
  101. else:
  102. raise NotImplementedError()
  103. async def async_close_cover(self, **kwargs):
  104. """Close the cover."""
  105. if self._control_dps and "close" in self._control_dps.values(self._device):
  106. await self._control_dps.async_set_value(self._device, "close")
  107. elif self._position_dps:
  108. await self._position_dps.async_set_value(self._device, 0)
  109. else:
  110. raise NotImplementedError()
  111. async def async_set_cover_position(self, position, **kwargs):
  112. """Set the cover to a specific position."""
  113. if position is None:
  114. raise AttributeError()
  115. if self._position_dps:
  116. await self._position_dps.async_set_value(self._device, position)
  117. else:
  118. raise NotImplementedError()
  119. async def async_stop_cover(self, **kwargs):
  120. """Stop the cover."""
  121. if self._control_dps and "stop" in self._control_dps.values(self._device):
  122. await self._control_dps.async_set_value(self._device, "stop")
  123. else:
  124. raise NotImplementedError()