cover.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. @property
  65. def is_opening(self):
  66. """Return if the cover is opening or not."""
  67. # If dps is available to inform current action, use that
  68. if self._action_dps:
  69. return self._action_dps.get_value(self._device) == "opening"
  70. # Otherwise use last command and check it hasn't completed
  71. if self._control_dps:
  72. return (
  73. self._control_dps.get_value(self._device) == "open"
  74. and self.current_cover_position != 100
  75. )
  76. @property
  77. def is_closing(self):
  78. """Return if the cover is closing or not."""
  79. # If dps is available to inform current action, use that
  80. if self._action_dps:
  81. return self._action_dps.get_value(self._device) == "closing"
  82. # Otherwise use last command and check it hasn't completed
  83. if self._control_dps:
  84. return (
  85. self._control_dps.get_value(self._device) == "close"
  86. and not self.is_closed
  87. )
  88. @property
  89. def is_closed(self):
  90. """Return if the cover is closed or not."""
  91. return self.current_cover_position == 0
  92. async def async_open_cover(self, **kwargs):
  93. """Open the cover."""
  94. if self._control_dps and "open" in self._control_dps.values(self._device):
  95. await self._control_dps.async_set_value(self._device, "open")
  96. elif self._position_dps:
  97. await self._position_dps.async_set_value(self._device, 100)
  98. else:
  99. raise NotImplementedError()
  100. async def async_close_cover(self, **kwargs):
  101. """Close the cover."""
  102. if self._control_dps and "close" in self._control_dps.values(self._device):
  103. await self._control_dps.async_set_value(self._device, "close")
  104. elif self._position_dps:
  105. await self._position_dps.async_set_value(self._device, 0)
  106. else:
  107. raise NotImplementedError()
  108. async def async_set_cover_position(self, position, **kwargs):
  109. """Set the cover to a specific position."""
  110. if position is None:
  111. raise AttributeError()
  112. if self._position_dps:
  113. await self._position_dps.async_set_value(self._device, position)
  114. else:
  115. raise NotImplementedError()
  116. async def async_stop_cover(self, **kwargs):
  117. """Stop the cover."""
  118. if self._control_dps and "stop" in self._control_dps.values(self._device):
  119. await self._control_dps.async_set_value(self._device, "stop")
  120. else:
  121. raise NotImplementedError()