event.py 2.1 KB

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