Home Assistant Unofficial Reference 2024.12.1
event.py
Go to the documentation of this file.
1 """Event module."""
2 
3 from deebot_client.capabilities import CapabilityEvent
4 from deebot_client.device import Device
5 from deebot_client.events import CleanJobStatus, ReportStatsEvent
6 
7 from homeassistant.components.event import EventEntity, EventEntityDescription
8 from homeassistant.const import EntityCategory
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import EcovacsConfigEntry
13 from .entity import EcovacsEntity
14 from .util import get_name_key
15 
16 
18  hass: HomeAssistant,
19  config_entry: EcovacsConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Add entities for passed config_entry in HA."""
23  controller = config_entry.runtime_data
25  EcovacsLastJobEventEntity(device) for device in controller.devices
26  )
27 
28 
30  EcovacsEntity[CapabilityEvent[ReportStatsEvent]],
31  EventEntity,
32 ):
33  """Ecovacs last job event entity."""
34 
35  entity_description = EventEntityDescription(
36  key="stats_report",
37  translation_key="last_job",
38  entity_category=EntityCategory.DIAGNOSTIC,
39  event_types=["finished", "finished_with_warnings", "manually_stopped"],
40  )
41 
42  def __init__(self, device: Device) -> None:
43  """Initialize entity."""
44  super().__init__(device, device.capabilities.stats.report)
45 
46  async def async_added_to_hass(self) -> None:
47  """Set up the event listeners now that hass is ready."""
48  await super().async_added_to_hass()
49 
50  async def on_event(event: ReportStatsEvent) -> None:
51  """Handle event."""
52  if event.status in (CleanJobStatus.NO_STATUS, CleanJobStatus.CLEANING):
53  # we trigger only on job done
54  return
55 
56  event_type = get_name_key(event.status)
57  self._trigger_event_trigger_event(event_type)
58  self.async_write_ha_stateasync_write_ha_state()
59 
60  self._subscribe_subscribe(self._capability_capability.event, on_event)
None _subscribe(self, type[EventT] event_type, Callable[[EventT], Coroutine[Any, Any, None]] callback)
Definition: entity.py:87
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
Definition: __init__.py:148
None async_setup_entry(HomeAssistant hass, EcovacsConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: event.py:21