1 """Provides the switchbot DataUpdateCoordinator."""
3 from __future__
import annotations
8 from typing
import TYPE_CHECKING
11 from switchbot
import SwitchbotModel
15 ActiveBluetoothDataUpdateCoordinator,
21 from bleak.backends.device
import BLEDevice
24 _LOGGER = logging.getLogger(__name__)
26 DEVICE_STARTUP_TIMEOUT = 30
28 type SwitchbotConfigEntry = ConfigEntry[SwitchbotDataUpdateCoordinator]
32 """Class to manage fetching switchbot data."""
37 logger: logging.Logger,
38 ble_device: BLEDevice,
39 device: switchbot.SwitchbotDevice,
43 model: SwitchbotModel,
45 """Initialize global switchbot data updater."""
49 address=ble_device.address,
52 mode=bluetooth.BluetoothScanningMode.ACTIVE,
53 connectable=connectable,
66 service_info: bluetooth.BluetoothServiceInfoBleak,
67 seconds_since_last_poll: float |
None,
72 self.
hasshass.state
is CoreState.running
73 and self.
devicedevice.poll_needed(seconds_since_last_poll)
75 bluetooth.async_ble_device_from_address(
76 self.
hasshass, service_info.device.address, connectable=
True
82 self, service_info: bluetooth.BluetoothServiceInfoBleak
84 """Poll the device."""
89 self, service_info: bluetooth.BluetoothServiceInfoBleak
91 """Handle the device going unavailable."""
98 service_info: bluetooth.BluetoothServiceInfoBleak,
99 change: bluetooth.BluetoothChange,
101 """Handle a Bluetooth event."""
102 self.
ble_deviceble_device = service_info.device
104 adv := switchbot.parse_advertisement_data(
105 service_info.device, service_info.advertisement, self.
modelmodel
109 if "modelName" in adv.data:
112 "%s: Switchbot data: %s", self.
ble_deviceble_device.address, self.
devicedevice.data
117 self.
devicedevice.update_from_advertisement(adv)
121 """Wait for the device to be ready."""
122 with contextlib.suppress(TimeoutError):
123 async
with asyncio.timeout(DEVICE_STARTUP_TIMEOUT):
bool _needs_poll(self, bluetooth.BluetoothServiceInfoBleak service_info, float|None seconds_since_last_poll)
None _async_handle_bluetooth_event(self, bluetooth.BluetoothServiceInfoBleak service_info, bluetooth.BluetoothChange change)
None __init__(self, HomeAssistant hass, logging.Logger logger, BLEDevice ble_device, switchbot.SwitchbotDevice device, str base_unique_id, str device_name, bool connectable, SwitchbotModel model)
bool async_wait_ready(self)
None _async_update(self, bluetooth.BluetoothServiceInfoBleak service_info)
None _async_handle_unavailable(self, bluetooth.BluetoothServiceInfoBleak service_info)
IssData update(pyiss.ISS iss)