button.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. Setup for different kinds of Tuya button devices
  3. """
  4. from homeassistant.components.button import ButtonEntity, ButtonDeviceClass
  5. from .device import TuyaLocalDevice
  6. from .helpers.config import async_tuya_setup_platform
  7. from .helpers.device_config import TuyaEntityConfig
  8. from .helpers.mixin import TuyaLocalEntity
  9. async def async_setup_entry(hass, config_entry, async_add_entities):
  10. config = {**config_entry.data, **config_entry.options}
  11. await async_tuya_setup_platform(
  12. hass,
  13. async_add_entities,
  14. config,
  15. "button",
  16. TuyaLocalButton,
  17. )
  18. class TuyaLocalButton(TuyaLocalEntity, ButtonEntity):
  19. """Representation of a Tuya Button"""
  20. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  21. """
  22. Initialize the button.
  23. Args:
  24. device (TuyaLocalDevice): The device API instance.
  25. config (TuyaEntityConfig): The config portion for this entity.
  26. """
  27. dps_map = self._init_begin(device, config)
  28. self._button_dp = dps_map.pop("button")
  29. self._init_end(dps_map)
  30. @property
  31. def device_class(self):
  32. """Return the class for this device"""
  33. dclass = self._config.device_class
  34. try:
  35. return ButtonDeviceClass(dclass)
  36. except ValueError:
  37. if dclass:
  38. _LOGGER.warning(f"Unrecognized button device class of {dclass} ignored")
  39. async def async_press(self):
  40. """Press the button"""
  41. await self._button_dp.async_set_value(self._device, True)