Home Assistant Unofficial Reference 2024.12.1
event.py
Go to the documentation of this file.
1 """Support for event entity."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from thinqconnect import DeviceType
8 from thinqconnect.integration import ActiveMode, ThinQPropertyEx
9 
10 from homeassistant.components.event import EventEntity, EventEntityDescription
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import ThinqConfigEntry
15 from .coordinator import DeviceDataUpdateCoordinator
16 from .entity import ThinQEntity
17 
18 NOTIFICATION_EVENT_DESC = EventEntityDescription(
19  key=ThinQPropertyEx.NOTIFICATION,
20  translation_key=ThinQPropertyEx.NOTIFICATION,
21 )
22 ERROR_EVENT_DESC = EventEntityDescription(
23  key=ThinQPropertyEx.ERROR,
24  translation_key=ThinQPropertyEx.ERROR,
25 )
26 ALL_EVENTS: tuple[EventEntityDescription, ...] = (
27  ERROR_EVENT_DESC,
28  NOTIFICATION_EVENT_DESC,
29 )
30 DEVICE_TYPE_EVENT_MAP: dict[DeviceType, tuple[EventEntityDescription, ...]] = {
31  DeviceType.AIR_CONDITIONER: (NOTIFICATION_EVENT_DESC,),
32  DeviceType.AIR_PURIFIER_FAN: (NOTIFICATION_EVENT_DESC,),
33  DeviceType.AIR_PURIFIER: (NOTIFICATION_EVENT_DESC,),
34  DeviceType.DEHUMIDIFIER: (NOTIFICATION_EVENT_DESC,),
35  DeviceType.DISH_WASHER: ALL_EVENTS,
36  DeviceType.DRYER: ALL_EVENTS,
37  DeviceType.HUMIDIFIER: (NOTIFICATION_EVENT_DESC,),
38  DeviceType.KIMCHI_REFRIGERATOR: (NOTIFICATION_EVENT_DESC,),
39  DeviceType.MICROWAVE_OVEN: (NOTIFICATION_EVENT_DESC,),
40  DeviceType.OVEN: (NOTIFICATION_EVENT_DESC,),
41  DeviceType.REFRIGERATOR: (NOTIFICATION_EVENT_DESC,),
42  DeviceType.ROBOT_CLEANER: ALL_EVENTS,
43  DeviceType.STICK_CLEANER: (NOTIFICATION_EVENT_DESC,),
44  DeviceType.STYLER: ALL_EVENTS,
45  DeviceType.WASHCOMBO_MAIN: ALL_EVENTS,
46  DeviceType.WASHCOMBO_MINI: ALL_EVENTS,
47  DeviceType.WASHER: ALL_EVENTS,
48  DeviceType.WASHTOWER_DRYER: ALL_EVENTS,
49  DeviceType.WASHTOWER: ALL_EVENTS,
50  DeviceType.WASHTOWER_WASHER: ALL_EVENTS,
51  DeviceType.WINE_CELLAR: (NOTIFICATION_EVENT_DESC,),
52 }
53 
54 _LOGGER = logging.getLogger(__name__)
55 
56 
58  hass: HomeAssistant,
59  entry: ThinqConfigEntry,
60  async_add_entities: AddEntitiesCallback,
61 ) -> None:
62  """Set up an entry for event platform."""
63  entities: list[ThinQEventEntity] = []
64  for coordinator in entry.runtime_data.coordinators.values():
65  if (
66  descriptions := DEVICE_TYPE_EVENT_MAP.get(
67  coordinator.api.device.device_type
68  )
69  ) is not None:
70  for description in descriptions:
71  entities.extend(
72  ThinQEventEntity(coordinator, description, property_id)
73  for property_id in coordinator.api.get_active_idx(
74  description.key, ActiveMode.READ_ONLY
75  )
76  )
77 
78  if entities:
79  async_add_entities(entities)
80 
81 
83  """Represent an thinq event platform."""
84 
85  def __init__(
86  self,
87  coordinator: DeviceDataUpdateCoordinator,
88  entity_description: EventEntityDescription,
89  property_id: str,
90  ) -> None:
91  """Initialize an event platform."""
92  super().__init__(coordinator, entity_description, property_id)
93 
94  # For event types.
95  self._attr_event_types_attr_event_types = self.datadatadatadata.options
96 
97  def _update_status(self) -> None:
98  """Update status itself."""
99  super()._update_status()
100 
101  _LOGGER.debug(
102  "[%s:%s] update status: %s, event_types=%s",
103  self.coordinator.device_name,
104  self.property_idproperty_id,
105  self.datadatadatadata.value,
106  self.event_typesevent_types,
107  )
108  # Handle an event.
109  if (value := self.datadatadatadata.value) is not None and value in self.event_typesevent_types:
110  self._async_handle_update_async_handle_update(value)
111 
112  def _async_handle_update(self, value: str) -> None:
113  """Handle the event."""
114  self._trigger_event_trigger_event(value)
115  self.async_write_ha_stateasync_write_ha_state()
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
Definition: __init__.py:148
None __init__(self, DeviceDataUpdateCoordinator coordinator, EventEntityDescription entity_description, str property_id)
Definition: event.py:90
None async_setup_entry(HomeAssistant hass, ThinqConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: event.py:61