Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """The govee Bluetooth integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from logging import Logger
7 
8 from govee_ble import GoveeBluetoothDeviceData, ModelInfo, SensorUpdate, get_model_info
9 
11  BluetoothScanningMode,
12  BluetoothServiceInfoBleak,
13 )
15  PassiveBluetoothDataProcessor,
16  PassiveBluetoothProcessorCoordinator,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.core import CoreState, HomeAssistant
20 from homeassistant.helpers.dispatcher import async_dispatcher_send
21 
22 from .const import CONF_DEVICE_TYPE, DOMAIN
23 
24 type GoveeBLEConfigEntry = ConfigEntry[GoveeBLEBluetoothProcessorCoordinator]
25 
26 
28  hass: HomeAssistant,
29  entry: GoveeBLEConfigEntry,
30  service_info: BluetoothServiceInfoBleak,
31 ) -> SensorUpdate:
32  """Process a BluetoothServiceInfoBleak, running side effects and returning sensor data."""
33  coordinator = entry.runtime_data
34  data = coordinator.device_data
35  update = data.update(service_info)
36  if not coordinator.model_info and (device_type := data.device_type):
37  hass.config_entries.async_update_entry(
38  entry, data={**entry.data, CONF_DEVICE_TYPE: device_type}
39  )
40  coordinator.set_model_info(device_type)
41  if update.events and hass.state is CoreState.running:
42  # Do not fire events on data restore
43  address = service_info.device.address
44  for event in update.events.values():
45  key = event.device_key.key
46  signal = format_event_dispatcher_name(address, key)
47  async_dispatcher_send(hass, signal)
48 
49  return update
50 
51 
52 def format_event_dispatcher_name(address: str, key: str) -> str:
53  """Format an event dispatcher name."""
54  return f"{DOMAIN}_{address}_{key}"
55 
56 
58  PassiveBluetoothProcessorCoordinator[SensorUpdate]
59 ):
60  """Define a govee ble Bluetooth Passive Update Processor Coordinator."""
61 
62  def __init__(
63  self,
64  hass: HomeAssistant,
65  logger: Logger,
66  address: str,
67  mode: BluetoothScanningMode,
68  update_method: Callable[[BluetoothServiceInfoBleak], SensorUpdate],
69  device_data: GoveeBluetoothDeviceData,
70  entry: ConfigEntry,
71  ) -> None:
72  """Initialize the Govee BLE Bluetooth Passive Update Processor Coordinator."""
73  super().__init__(hass, logger, address, mode, update_method)
74  self.device_datadevice_data = device_data
75  self.entryentry = entry
76  self.model_infomodel_info: ModelInfo | None = None
77  if device_type := entry.data.get(CONF_DEVICE_TYPE):
78  self.set_model_infoset_model_info(device_type)
79 
80  def set_model_info(self, device_type: str) -> None:
81  """Set the model info."""
82  self.model_infomodel_info = get_model_info(device_type)
83 
84 
86  PassiveBluetoothDataProcessor[_T, SensorUpdate]
87 ):
88  """Define a govee-ble Bluetooth Passive Update Data Processor."""
89 
90  coordinator: GoveeBLEBluetoothProcessorCoordinator
None __init__(self, HomeAssistant hass, Logger logger, str address, BluetoothScanningMode mode, Callable[[BluetoothServiceInfoBleak], SensorUpdate] update_method, GoveeBluetoothDeviceData device_data, ConfigEntry entry)
Definition: coordinator.py:71
SensorUpdate process_service_info(HomeAssistant hass, GoveeBLEConfigEntry entry, BluetoothServiceInfoBleak service_info)
Definition: coordinator.py:31
str format_event_dispatcher_name(str address, str key)
Definition: coordinator.py:52
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193