Refactor PassiveBluetoothDataUpdateCoordinator to support multiple platforms (#75642)

This commit is contained in:
J. Nick Koston 2022-07-23 13:03:01 -05:00 committed by GitHub
parent 8300d5b89e
commit c5afaa2e6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 443 additions and 234 deletions

View file

@ -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)