event.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. Implementation of Tuya events
  3. """
  4. from homeassistant.components.event import EventDeviceClass, EventEntity
  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. "event",
  16. TuyaLocalEvent,
  17. )
  18. class TuyaLocalEvent(TuyaLocalEntity, EventEntity):
  19. """Representation of a Tuya Event"""
  20. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  21. """
  22. Initialise the event.
  23. Args:
  24. device (TuyaLocalDevice): the device API instance.
  25. config (TuyaEntityConfig): the configuration for this entity
  26. """
  27. super().__init__()
  28. dps_map = self._init_begin(device, config)
  29. self._event_dp = dps_map.pop("event")
  30. self._init_end(dps_map)
  31. # Set up device_class via parent class attribute
  32. try:
  33. self._attr_device_class = EventDeviceClass(self._config.device_class)
  34. except ValueError:
  35. if self._config.device_class:
  36. _LOGGER.warning(
  37. "%s/%s: Unreecognised event device class of %s ignored",
  38. self._config._device.config,
  39. self.name or "event",
  40. self._config.device_class,
  41. )
  42. # Set up event_types via parent class attribute
  43. self._attr_event_types = self._event_dp.values(device)
  44. def on_receive(self, dps):
  45. """Trigger the event when dp is received"""
  46. if self._event_dp.id in dps:
  47. value = self._event_dp.get_value(self._device)
  48. if value is not None:
  49. self._trigger_event(
  50. value,
  51. self.extra_state_attributes(),
  52. )