1 """Component for handling incoming events as a platform."""
3 from __future__
import annotations
5 from dataclasses
import asdict, dataclass
6 from datetime
import datetime, timedelta
7 from enum
import StrEnum
9 from typing
import Any, Self, final
11 from propcache
import cached_property
23 from .const
import ATTR_EVENT_TYPE, ATTR_EVENT_TYPES, DOMAIN
25 _LOGGER = logging.getLogger(__name__)
26 DATA_COMPONENT: HassKey[EntityComponent[EventEntity]] =
HassKey(DOMAIN)
27 ENTITY_ID_FORMAT = DOMAIN +
".{}"
28 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
29 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
34 """Device class for events."""
45 "PLATFORM_SCHEMA_BASE",
49 "EventEntityDescription",
55 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
56 """Set up Event entities."""
57 component = hass.data[DATA_COMPONENT] = EntityComponent[EventEntity](
58 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
60 await component.async_setup(config)
65 """Set up a config entry."""
70 """Unload a config entry."""
75 """A class that describes event entities."""
77 device_class: EventDeviceClass |
None =
None
78 event_types: list[str] |
None =
None
83 """Object to hold extra stored data."""
85 last_event_type: str |
None
86 last_event_attributes: dict[str, Any] |
None
89 """Return a dict representation of the event data."""
93 def from_dict(cls, restored: dict[str, Any]) -> Self |
None:
94 """Initialize a stored event state from a dict."""
97 restored[
"last_event_type"],
98 restored[
"last_event_attributes"],
104 CACHED_PROPERTIES_WITH_ATTR_ = {
111 """Representation of an Event entity."""
113 _entity_component_unrecorded_attributes = frozenset({ATTR_EVENT_TYPES})
115 entity_description: EventEntityDescription
116 _attr_device_class: EventDeviceClass |
None
117 _attr_event_types: list[str]
120 __last_event_triggered: datetime |
None =
None
121 __last_event_type: str |
None =
None
122 __last_event_attributes: dict[str, Any] |
None =
None
126 """Return the class of this entity."""
127 if hasattr(self,
"_attr_device_class"):
128 return self._attr_device_class
129 if hasattr(self,
"entity_description"):
130 return self.entity_description.device_class
135 """Return a list of possible events."""
136 if hasattr(self,
"_attr_event_types"):
137 return self._attr_event_types
139 hasattr(self,
"entity_description")
140 and self.entity_description.event_types
is not None
142 return self.entity_description.event_types
147 self, event_type: str, event_attributes: dict[str, Any] |
None =
None
149 """Process a new event."""
151 raise ValueError(f
"Invalid event type {event_type} for {self.entity_id}")
157 """Return True if an unnamed entity should be named by its device class.
159 For events this is True if the entity has a device class.
166 """Return capability attributes."""
174 """Return the entity state."""
177 return last_event.isoformat(timespec=
"milliseconds")
182 """Return the state attributes."""
185 attributes |= last_event_attributes
190 """Call when the event entity is added to hass."""
194 and state.state
is not None
203 """Return event specific state data to be restored."""
210 """Restore event specific state date."""
213 return EventExtraStoredData.from_dict(restored_last_extra_data.as_dict())
EventExtraStoredData|None async_get_last_event_data(self)
EventDeviceClass|None device_class(self)
dict[str, Any] state_attributes(self)
bool _default_to_device_class_name(self)
dict[str, list[str]] capability_attributes(self)
None async_internal_added_to_hass(self)
EventExtraStoredData extra_restore_state_data(self)
list[str] event_types(self)
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
str|None device_class(self)
State|None async_get_last_state(self)
ExtraStoredData|None async_get_last_extra_data(self)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)