Home Assistant Unofficial Reference 2024.12.1
event.py
Go to the documentation of this file.
1 """Support for govee_ble event entities."""
2 
3 from __future__ import annotations
4 
5 from govee_ble import ModelInfo, SensorType
6 
8  BluetoothServiceInfoBleak,
9  async_last_service_info,
10 )
12  EventDeviceClass,
13  EventEntity,
14  EventEntityDescription,
15 )
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers import device_registry as dr
18 from homeassistant.helpers.dispatcher import async_dispatcher_connect
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import DOMAIN
22 from .coordinator import GoveeBLEConfigEntry, format_event_dispatcher_name
23 
24 BUTTON_DESCRIPTIONS = [
26  key=f"button_{i}",
27  translation_key=f"button_{i}",
28  event_types=["press"],
29  device_class=EventDeviceClass.BUTTON,
30  )
31  for i in range(6)
32 ]
33 MOTION_DESCRIPTION = EventEntityDescription(
34  key="motion",
35  event_types=["motion"],
36  device_class=EventDeviceClass.MOTION,
37 )
38 VIBRATION_DESCRIPTION = EventEntityDescription(
39  key="vibration",
40  event_types=["vibration"],
41  translation_key="vibration",
42 )
43 
44 
46  """Representation of a govee ble event entity."""
47 
48  _attr_should_poll = False
49  _attr_has_entity_name = True
50 
51  def __init__(
52  self,
53  model_info: ModelInfo,
54  service_info: BluetoothServiceInfoBleak | None,
55  address: str,
56  description: EventEntityDescription,
57  ) -> None:
58  """Initialise a govee ble event entity."""
59  self.entity_descriptionentity_description = description
60  # Matches logic in PassiveBluetoothProcessorEntity
61  name = service_info.name if service_info else model_info.model_id
62  self._attr_device_info_attr_device_info = dr.DeviceInfo(
63  name=name,
64  identifiers={(DOMAIN, address)},
65  connections={(dr.CONNECTION_BLUETOOTH, address)},
66  )
67  self._attr_unique_id_attr_unique_id = f"{address}-{description.key}"
68  self._address_address = address
70  self._address_address, self.entity_descriptionentity_description.key
71  )
72 
73  async def async_added_to_hass(self) -> None:
74  """Entity added to hass."""
75  await super().async_added_to_hass()
76  self.async_on_removeasync_on_remove(
78  self.hasshass,
79  self._signal_signal,
80  self._async_handle_event_async_handle_event,
81  )
82  )
83 
84  @callback
85  def _async_handle_event(self) -> None:
86  self._trigger_event_trigger_event(self.event_typesevent_types[0])
87  self.async_write_ha_stateasync_write_ha_state()
88 
89 
91  hass: HomeAssistant,
92  entry: GoveeBLEConfigEntry,
93  async_add_entities: AddEntitiesCallback,
94 ) -> None:
95  """Set up a govee ble event."""
96  coordinator = entry.runtime_data
97  if not (model_info := coordinator.model_info):
98  return
99  address = coordinator.address
100  sensor_type = model_info.sensor_type
101  if sensor_type is SensorType.MOTION:
102  descriptions = [MOTION_DESCRIPTION]
103  elif sensor_type is SensorType.VIBRATION:
104  descriptions = [VIBRATION_DESCRIPTION]
105  elif sensor_type is SensorType.BUTTON:
106  button_count = model_info.button_count
107  descriptions = BUTTON_DESCRIPTIONS[0:button_count]
108  else:
109  return
110  last_service_info = async_last_service_info(hass, address, False)
112  GoveeBluetoothEventEntity(model_info, last_service_info, address, description)
113  for description in descriptions
114  )
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
Definition: __init__.py:148
None __init__(self, ModelInfo model_info, BluetoothServiceInfoBleak|None service_info, str address, EventEntityDescription description)
Definition: event.py:57
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
BluetoothServiceInfoBleak|None async_last_service_info(HomeAssistant hass, str address, bool connectable=True)
Definition: api.py:80
SignalType[BTHomeBleEvent] format_event_dispatcher_name(str address, str event_class)
Definition: __init__.py:109
None async_setup_entry(HomeAssistant hass, GoveeBLEConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: event.py:94
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103