Home Assistant Unofficial Reference 2024.12.1
event.py
Go to the documentation of this file.
1 """Demo platform that offers a fake event entity."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.event import EventDeviceClass, EventEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import Event, HomeAssistant, callback
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 
11 from . import DOMAIN
12 
13 
15  hass: HomeAssistant,
16  config_entry: ConfigEntry,
17  async_add_entities: AddEntitiesCallback,
18 ) -> None:
19  """Set up the demo event platform."""
21 
22 
24  """Representation of a demo event entity."""
25 
26  _attr_device_class = EventDeviceClass.BUTTON
27  _attr_event_types = ["pressed"]
28  _attr_has_entity_name = True
29  _attr_name = "Button press"
30  _attr_should_poll = False
31  _attr_translation_key = "push"
32  _attr_unique_id = "push"
33 
34  def __init__(self) -> None:
35  """Initialize the Demo event entity."""
36  self._attr_device_info_attr_device_info = DeviceInfo(
37  identifiers={(DOMAIN, "push")},
38  )
39 
40  async def async_added_to_hass(self) -> None:
41  """Register callbacks."""
42  self.hasshass.bus.async_listen("demo_button_pressed", self._async_handle_event_async_handle_event)
43 
44  @callback
45  def _async_handle_event(self, _: Event) -> None:
46  """Handle the demo button event."""
47  self._trigger_event_trigger_event("pressed")
48  self.async_write_ha_stateasync_write_ha_state()
None _async_handle_event(self, Event _)
Definition: event.py:45
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
Definition: __init__.py:148
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: event.py:18