button.py 1.7 KB

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