2022-07-22 03:17:09 +00:00
|
|
|
"""The INKBIRD Bluetooth integration."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2022-07-22 03:17:09 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2025-02-21 12:22:34 +00:00
|
|
|
from inkbird_ble import INKBIRDBluetoothDeviceData, SensorUpdate
|
2022-08-09 05:36:39 +00:00
|
|
|
|
2025-02-21 12:22:34 +00:00
|
|
|
from homeassistant.components.bluetooth import (
|
|
|
|
BluetoothScanningMode,
|
|
|
|
BluetoothServiceInfo,
|
|
|
|
)
|
2022-07-24 00:33:47 +00:00
|
|
|
from homeassistant.components.bluetooth.passive_update_processor import (
|
|
|
|
PassiveBluetoothProcessorCoordinator,
|
2022-07-22 03:17:09 +00:00
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import Platform
|
2025-02-21 12:22:34 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-07-22 03:17:09 +00:00
|
|
|
|
2025-02-21 12:22:34 +00:00
|
|
|
from .const import CONF_DEVICE_TYPE, DOMAIN
|
2022-07-22 03:17:09 +00:00
|
|
|
|
|
|
|
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up INKBIRD BLE device from a config entry."""
|
|
|
|
address = entry.unique_id
|
|
|
|
assert address is not None
|
2025-02-21 12:22:34 +00:00
|
|
|
device_type: str | None = entry.data.get(CONF_DEVICE_TYPE)
|
|
|
|
data = INKBIRDBluetoothDeviceData(device_type)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_on_update(service_info: BluetoothServiceInfo) -> SensorUpdate:
|
|
|
|
"""Handle update callback from the passive BLE processor."""
|
|
|
|
nonlocal device_type
|
|
|
|
update = data.update(service_info)
|
|
|
|
if device_type is None and data.device_type is not None:
|
|
|
|
device_type_str = str(data.device_type)
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
entry, data={**entry.data, CONF_DEVICE_TYPE: device_type_str}
|
|
|
|
)
|
|
|
|
device_type = device_type_str
|
|
|
|
return update
|
|
|
|
|
|
|
|
coordinator = PassiveBluetoothProcessorCoordinator(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
address=address,
|
|
|
|
mode=BluetoothScanningMode.ACTIVE,
|
|
|
|
update_method=_async_on_update,
|
2022-07-22 03:17:09 +00:00
|
|
|
)
|
2025-02-21 12:22:34 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
2022-07-22 03:17:09 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2025-02-21 12:22:34 +00:00
|
|
|
# only start after all platforms have had a chance to subscribe
|
|
|
|
entry.async_on_unload(coordinator.async_start())
|
2022-07-22 03:17:09 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|