Home Assistant Unofficial Reference 2024.12.1
event.py
Go to the documentation of this file.
1 """Support for Nanoleaf event entity."""
2 
3 from homeassistant.components.event import EventEntity
4 from homeassistant.core import HomeAssistant, callback
5 from homeassistant.helpers.dispatcher import async_dispatcher_connect
6 from homeassistant.helpers.entity_platform import AddEntitiesCallback
7 
8 from . import NanoleafConfigEntry, NanoleafCoordinator
9 from .const import TOUCH_MODELS
10 from .entity import NanoleafEntity
11 
12 
14  hass: HomeAssistant,
15  entry: NanoleafConfigEntry,
16  async_add_entities: AddEntitiesCallback,
17 ) -> None:
18  """Set up the Nanoleaf event."""
19  coordinator = entry.runtime_data
20  if coordinator.nanoleaf.model in TOUCH_MODELS:
22 
23 
25  """Representation of a Nanoleaf event entity."""
26 
27  _attr_event_types = [
28  "swipe_up",
29  "swipe_down",
30  "swipe_left",
31  "swipe_right",
32  ]
33  _attr_translation_key = "touch"
34 
35  def __init__(self, coordinator: NanoleafCoordinator) -> None:
36  """Initialize the Nanoleaf event entity."""
37  super().__init__(coordinator)
38  self._attr_unique_id_attr_unique_id = f"{self._nanoleaf.serial_no}_gesture"
39 
40  async def async_added_to_hass(self) -> None:
41  """Subscribe to Nanoleaf events."""
42  await super().async_added_to_hass()
43  self.async_on_removeasync_on_remove(
45  self.hasshass,
46  f"nanoleaf_gesture_{self._nanoleaf.serial_no}",
47  self._async_handle_event_async_handle_event,
48  )
49  )
50 
51  @callback
52  def _async_handle_event(self, gesture: str) -> None:
53  """Handle the event."""
54  self._trigger_event_trigger_event(gesture)
55  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, NanoleafCoordinator coordinator)
Definition: event.py:35
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, NanoleafConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: event.py:17
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103