event.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 .entity import TuyaLocalEntity
  8. from .helpers.config import async_tuya_setup_platform
  9. from .helpers.device_config import TuyaEntityConfig
  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. self._last_value = None
  34. # Set up device_class via parent class attribute
  35. try:
  36. self._attr_device_class = EventDeviceClass(self._config.device_class)
  37. except ValueError:
  38. if self._config.device_class:
  39. _LOGGER.warning(
  40. "%s/%s: Unreecognised event device class of %s ignored",
  41. self._config._device.config,
  42. self.name or "event",
  43. self._config.device_class,
  44. )
  45. # Set up event_types via parent class attribute
  46. self._attr_event_types = self._event_dp.values(device)
  47. def on_receive(self, dps, full_poll):
  48. """Trigger the event when dp is received"""
  49. if self._event_dp.id in dps:
  50. value = self._event_dp.get_value(self._device)
  51. if value is not None:
  52. # filter out full polls pulling the current ongoing value,
  53. # but limit this to allow repeats to come through on non-full polls
  54. if value == self._last_value and full_poll:
  55. _LOGGER.debug(
  56. "%s/%s value %s is the same as last value, ignoring",
  57. self._config._device.config,
  58. self.name or "event",
  59. value,
  60. )
  61. return
  62. _LOGGER.debug(
  63. "%s/%s triggering event for value %s",
  64. self._config._device.config,
  65. self.name or "event",
  66. value,
  67. )
  68. self._last_value = value
  69. self._trigger_event(
  70. value,
  71. self.extra_state_attributes,
  72. )
  73. # clear out the remembered value when a full poll comes through
  74. # with nothing
  75. elif value is None and full_poll:
  76. _LOGGER.debug(
  77. "%s/%s clearing last value due to no value in full poll",
  78. self._config._device.config,
  79. self.name or "event",
  80. )
  81. self._last_value = None