Refactor PassiveBluetoothDataUpdateCoordinator to support multiple platforms (#75642)
This commit is contained in:
parent
8300d5b89e
commit
c5afaa2e6a
8 changed files with 443 additions and 234 deletions
|
@ -3,19 +3,14 @@ from __future__ import annotations
|
|||
|
||||
import logging
|
||||
|
||||
from sensorpush_ble import SensorPushBluetoothDeviceData
|
||||
|
||||
from homeassistant.components.bluetooth.passive_update_coordinator import (
|
||||
PassiveBluetoothDataUpdate,
|
||||
PassiveBluetoothDataUpdateCoordinator,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .sensor import sensor_update_to_bluetooth_data_update
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
|
||||
|
@ -26,22 +21,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
"""Set up SensorPush BLE device from a config entry."""
|
||||
address = entry.unique_id
|
||||
assert address is not None
|
||||
|
||||
data = SensorPushBluetoothDeviceData()
|
||||
|
||||
@callback
|
||||
def _async_update_data(
|
||||
service_info: BluetoothServiceInfo,
|
||||
) -> PassiveBluetoothDataUpdate:
|
||||
"""Update data from SensorPush Bluetooth."""
|
||||
return sensor_update_to_bluetooth_data_update(data.update(service_info))
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[
|
||||
entry.entry_id
|
||||
] = PassiveBluetoothDataUpdateCoordinator(
|
||||
hass,
|
||||
_LOGGER,
|
||||
update_method=_async_update_data,
|
||||
address=address,
|
||||
)
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
|
|
@ -3,14 +3,22 @@ from __future__ import annotations
|
|||
|
||||
from typing import Optional, Union
|
||||
|
||||
from sensorpush_ble import DeviceClass, DeviceKey, SensorDeviceInfo, SensorUpdate, Units
|
||||
from sensorpush_ble import (
|
||||
DeviceClass,
|
||||
DeviceKey,
|
||||
SensorDeviceInfo,
|
||||
SensorPushBluetoothDeviceData,
|
||||
SensorUpdate,
|
||||
Units,
|
||||
)
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.bluetooth.passive_update_coordinator import (
|
||||
PassiveBluetoothCoordinatorEntity,
|
||||
PassiveBluetoothDataProcessor,
|
||||
PassiveBluetoothDataUpdate,
|
||||
PassiveBluetoothDataUpdateCoordinator,
|
||||
PassiveBluetoothEntityKey,
|
||||
PassiveBluetoothProcessorEntity,
|
||||
)
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
|
@ -122,16 +130,23 @@ async def async_setup_entry(
|
|||
coordinator: PassiveBluetoothDataUpdateCoordinator = hass.data[DOMAIN][
|
||||
entry.entry_id
|
||||
]
|
||||
data = SensorPushBluetoothDeviceData()
|
||||
processor = PassiveBluetoothDataProcessor(
|
||||
lambda service_info: sensor_update_to_bluetooth_data_update(
|
||||
data.update(service_info)
|
||||
)
|
||||
)
|
||||
entry.async_on_unload(coordinator.async_register_processor(processor))
|
||||
entry.async_on_unload(
|
||||
coordinator.async_add_entities_listener(
|
||||
processor.async_add_entities_listener(
|
||||
SensorPushBluetoothSensorEntity, async_add_entities
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class SensorPushBluetoothSensorEntity(
|
||||
PassiveBluetoothCoordinatorEntity[
|
||||
PassiveBluetoothDataUpdateCoordinator[Optional[Union[float, int]]]
|
||||
PassiveBluetoothProcessorEntity[
|
||||
PassiveBluetoothDataProcessor[Optional[Union[float, int]]]
|
||||
],
|
||||
SensorEntity,
|
||||
):
|
||||
|
@ -140,4 +155,4 @@ class SensorPushBluetoothSensorEntity(
|
|||
@property
|
||||
def native_value(self) -> int | float | None:
|
||||
"""Return the native value."""
|
||||
return self.coordinator.entity_data.get(self.entity_key)
|
||||
return self.processor.entity_data.get(self.entity_key)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue