button.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """
  2. Platform to control Tuya buttons.
  3. Buttons provide a way to send data to a Tuya dp which may not itself
  4. be readable. If the device does not return any state for the dp, then
  5. it should be set as optional so it is not required to be present for detection.
  6. """
  7. from homeassistant.components.button import ButtonEntity, ButtonDeviceClass
  8. from ..device import TuyaLocalDevice
  9. from ..helpers.device_config import TuyaEntityConfig
  10. from ..helpers.mixin import TuyaLocalEntity
  11. class TuyaLocalButton(TuyaLocalEntity, ButtonEntity):
  12. """Representation of a Tuya Button"""
  13. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  14. """
  15. Initialize the button.
  16. Args:
  17. device (TuyaLocalDevice): The device API instance.
  18. config (TuyaEntityConfig): The config portion for this entity.
  19. """
  20. dps_map = self._init_begin(device, config)
  21. self._button_dp = dps_map.pop("button")
  22. self._init_end(dps_map)
  23. @property
  24. def device_class(self):
  25. """Return the class for this device"""
  26. dclass = self._config.device_class
  27. try:
  28. return ButtonDeviceClass(dclass)
  29. except ValueError:
  30. if dclass:
  31. _LOGGER.warning(f"Unrecognized button device class of {dclass} ignored")
  32. async def async_press(self):
  33. """Press the button"""
  34. await self._button_dp.async_set_value(self._device, True)