Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Integration to integrate Keymitt BLE devices with Home Assistant."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import TYPE_CHECKING, Any
7 
8 from microbot import MicroBotApiClient, parse_advertisement_data
9 
10 from homeassistant.components import bluetooth
12  PassiveBluetoothDataUpdateCoordinator,
13 )
14 from homeassistant.const import Platform
15 from homeassistant.core import HomeAssistant, callback
16 
17 if TYPE_CHECKING:
18  from bleak.backends.device import BLEDevice
19 
20 _LOGGER: logging.Logger = logging.getLogger(__package__)
21 PLATFORMS: list[str] = [Platform.SWITCH]
22 
23 
25  """Class to manage fetching data from the MicroBot."""
26 
27  def __init__(
28  self,
29  hass: HomeAssistant,
30  client: MicroBotApiClient,
31  ble_device: BLEDevice,
32  ) -> None:
33  """Initialize."""
34  self.api: MicroBotApiClient = client
35  self.datadata: dict[str, Any] = {}
36  self.ble_deviceble_device = ble_device
37  super().__init__(
38  hass,
39  _LOGGER,
40  ble_device.address,
41  bluetooth.BluetoothScanningMode.ACTIVE,
42  )
43 
44  @callback
46  self,
47  service_info: bluetooth.BluetoothServiceInfoBleak,
48  change: bluetooth.BluetoothChange,
49  ) -> None:
50  """Handle a Bluetooth event."""
51  if adv := parse_advertisement_data(
52  service_info.device, service_info.advertisement
53  ):
54  self.datadata = adv.data
55  _LOGGER.debug("%s: MicroBot data: %s", self.ble_deviceble_device.address, self.datadata)
56  self.api.update_from_advertisement(adv)
57  super()._async_handle_bluetooth_event(service_info, change)
None _async_handle_bluetooth_event(self, bluetooth.BluetoothServiceInfoBleak service_info, bluetooth.BluetoothChange change)
Definition: coordinator.py:49
None __init__(self, HomeAssistant hass, MicroBotApiClient client, BLEDevice ble_device)
Definition: coordinator.py:32