Home Assistant Unofficial Reference 2024.12.1
event.py
Go to the documentation of this file.
1 """Support for ESPHome event components."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 
7 from aioesphomeapi import EntityInfo, Event, EventInfo
8 
9 from homeassistant.components.event import EventDeviceClass, EventEntity
10 from homeassistant.core import callback
11 from homeassistant.util.enum import try_parse_enum
12 
13 from .entity import EsphomeEntity, platform_async_setup_entry
14 
15 
16 class EsphomeEvent(EsphomeEntity[EventInfo, Event], EventEntity):
17  """An event implementation for ESPHome."""
18 
19  @callback
20  def _on_static_info_update(self, static_info: EntityInfo) -> None:
21  """Set attrs from static info."""
22  super()._on_static_info_update(static_info)
23  static_info = self._static_info_static_info
24  if event_types := static_info.event_types:
25  self._attr_event_types_attr_event_types = event_types
26  self._attr_device_class_attr_device_class = try_parse_enum(
27  EventDeviceClass, static_info.device_class
28  )
29 
30  @callback
31  def _on_state_update(self) -> None:
32  self._update_state_from_entry_data_update_state_from_entry_data()
33  self._trigger_event_trigger_event(self._state_state.event_type)
34  self.async_write_ha_stateasync_write_ha_state()
35 
36 
37 async_setup_entry = partial(
38  platform_async_setup_entry,
39  info_type=EventInfo,
40  entity_type=EsphomeEvent,
41  state_type=Event,
42 )
None _on_static_info_update(self, EntityInfo static_info)
Definition: event.py:20
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
Definition: __init__.py:148