Update bthome to use entry.runtime_data (#122304)

This commit is contained in:
J. Nick Koston 2024-07-21 08:19:46 -05:00 committed by GitHub
parent 8994c18f73
commit 7f852d0f73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 46 additions and 60 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from functools import partial
import logging import logging
from bthome_ble import BTHomeBluetoothDeviceData, SensorUpdate from bthome_ble import BTHomeBluetoothDeviceData, SensorUpdate
@ -12,7 +13,6 @@ from homeassistant.components.bluetooth import (
BluetoothScanningMode, BluetoothScanningMode,
BluetoothServiceInfoBleak, BluetoothServiceInfoBleak,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
@ -29,6 +29,7 @@ from .const import (
BTHomeBleEvent, BTHomeBleEvent,
) )
from .coordinator import BTHomePassiveBluetoothProcessorCoordinator from .coordinator import BTHomePassiveBluetoothProcessorCoordinator
from .types import BTHomeConfigEntry
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.EVENT, Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.EVENT, Platform.SENSOR]
@ -37,16 +38,14 @@ _LOGGER = logging.getLogger(__name__)
def process_service_info( def process_service_info(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: BTHomeConfigEntry,
data: BTHomeBluetoothDeviceData,
service_info: BluetoothServiceInfoBleak,
device_registry: DeviceRegistry, device_registry: DeviceRegistry,
service_info: BluetoothServiceInfoBleak,
) -> SensorUpdate: ) -> SensorUpdate:
"""Process a BluetoothServiceInfoBleak, running side effects and returning sensor data.""" """Process a BluetoothServiceInfoBleak, running side effects and returning sensor data."""
coordinator = entry.runtime_data
data = coordinator.device_data
update = data.update(service_info) update = data.update(service_info)
coordinator: BTHomePassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
entry.entry_id
]
discovered_event_classes = coordinator.discovered_event_classes discovered_event_classes = coordinator.discovered_event_classes
if entry.data.get(CONF_SLEEPY_DEVICE, False) != data.sleepy_device: if entry.data.get(CONF_SLEEPY_DEVICE, False) != data.sleepy_device:
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(
@ -117,7 +116,7 @@ def format_discovered_event_class(address: str) -> SignalType[str, BTHomeBleEven
return SignalType(f"{DOMAIN}_discovered_event_class_{address}") return SignalType(f"{DOMAIN}_discovered_event_class_{address}")
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: BTHomeConfigEntry) -> bool:
"""Set up BTHome Bluetooth from a config entry.""" """Set up BTHome Bluetooth from a config entry."""
address = entry.unique_id address = entry.unique_id
assert address is not None assert address is not None
@ -128,34 +127,26 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
data = BTHomeBluetoothDeviceData(**kwargs) data = BTHomeBluetoothDeviceData(**kwargs)
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = ( event_classes = set(entry.data.get(CONF_DISCOVERED_EVENT_CLASSES, ()))
BTHomePassiveBluetoothProcessorCoordinator( coordinator = BTHomePassiveBluetoothProcessorCoordinator(
hass, hass,
_LOGGER, _LOGGER,
address=address, address=address,
mode=BluetoothScanningMode.PASSIVE, mode=BluetoothScanningMode.PASSIVE,
update_method=lambda service_info: process_service_info( update_method=partial(process_service_info, hass, entry, device_registry),
hass, entry, data, service_info, device_registry device_data=data,
), discovered_event_classes=event_classes,
device_data=data, connectable=False,
discovered_event_classes=set( entry=entry,
entry.data.get(CONF_DISCOVERED_EVENT_CLASSES, [])
),
connectable=False,
entry=entry,
)
) )
entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload( # only start after all platforms have had a chance to subscribe
coordinator.async_start() entry.async_on_unload(coordinator.async_start())
) # only start after all platforms have had a chance to subscribe
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: BTHomeConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -7,7 +7,6 @@ from bthome_ble import (
SensorUpdate, SensorUpdate,
) )
from homeassistant import config_entries
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
@ -21,12 +20,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
from .const import DOMAIN from .coordinator import BTHomePassiveBluetoothDataProcessor
from .coordinator import (
BTHomePassiveBluetoothDataProcessor,
BTHomePassiveBluetoothProcessorCoordinator,
)
from .device import device_key_to_bluetooth_entity_key from .device import device_key_to_bluetooth_entity_key
from .types import BTHomeConfigEntry
BINARY_SENSOR_DESCRIPTIONS = { BINARY_SENSOR_DESCRIPTIONS = {
BTHomeBinarySensorDeviceClass.BATTERY: BinarySensorEntityDescription( BTHomeBinarySensorDeviceClass.BATTERY: BinarySensorEntityDescription(
@ -172,13 +168,11 @@ def sensor_update_to_bluetooth_data_update(
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: config_entries.ConfigEntry, entry: BTHomeConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the BTHome BLE binary sensors.""" """Set up the BTHome BLE binary sensors."""
coordinator: BTHomePassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]
processor = BTHomePassiveBluetoothDataProcessor( processor = BTHomePassiveBluetoothDataProcessor(
sensor_update_to_bluetooth_data_update sensor_update_to_bluetooth_data_update
) )

View File

@ -13,10 +13,10 @@ from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothDataProcessor, PassiveBluetoothDataProcessor,
PassiveBluetoothProcessorCoordinator, PassiveBluetoothProcessorCoordinator,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import CONF_SLEEPY_DEVICE from .const import CONF_SLEEPY_DEVICE
from .types import BTHomeConfigEntry
class BTHomePassiveBluetoothProcessorCoordinator( class BTHomePassiveBluetoothProcessorCoordinator(
@ -33,7 +33,7 @@ class BTHomePassiveBluetoothProcessorCoordinator(
update_method: Callable[[BluetoothServiceInfoBleak], SensorUpdate], update_method: Callable[[BluetoothServiceInfoBleak], SensorUpdate],
device_data: BTHomeBluetoothDeviceData, device_data: BTHomeBluetoothDeviceData,
discovered_event_classes: set[str], discovered_event_classes: set[str],
entry: ConfigEntry, entry: BTHomeConfigEntry,
connectable: bool = False, connectable: bool = False,
) -> None: ) -> None:
"""Initialize the BTHome Bluetooth Passive Update Processor Coordinator.""" """Initialize the BTHome Bluetooth Passive Update Processor Coordinator."""

View File

@ -9,7 +9,6 @@ from homeassistant.components.event import (
EventEntity, EventEntity,
EventEntityDescription, EventEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -24,7 +23,7 @@ from .const import (
EVENT_TYPE, EVENT_TYPE,
BTHomeBleEvent, BTHomeBleEvent,
) )
from .coordinator import BTHomePassiveBluetoothProcessorCoordinator from .types import BTHomeConfigEntry
DESCRIPTIONS_BY_EVENT_CLASS = { DESCRIPTIONS_BY_EVENT_CLASS = {
EVENT_CLASS_BUTTON: EventEntityDescription( EVENT_CLASS_BUTTON: EventEntityDescription(
@ -103,13 +102,11 @@ class BTHomeEventEntity(EventEntity):
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: BTHomeConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up BTHome event.""" """Set up BTHome event."""
coordinator: BTHomePassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]
address = coordinator.address address = coordinator.address
ent_reg = er.async_get(hass) ent_reg = er.async_get(hass)
async_add_entities( async_add_entities(

View File

@ -9,7 +9,6 @@ from bthome_ble.const import (
ExtendedSensorDeviceClass as BTHomeExtendedSensorDeviceClass, ExtendedSensorDeviceClass as BTHomeExtendedSensorDeviceClass,
) )
from homeassistant import config_entries
from homeassistant.components.bluetooth.passive_update_processor import ( from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothDataUpdate, PassiveBluetoothDataUpdate,
PassiveBluetoothProcessorEntity, PassiveBluetoothProcessorEntity,
@ -45,12 +44,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
from .const import DOMAIN from .coordinator import BTHomePassiveBluetoothDataProcessor
from .coordinator import (
BTHomePassiveBluetoothDataProcessor,
BTHomePassiveBluetoothProcessorCoordinator,
)
from .device import device_key_to_bluetooth_entity_key from .device import device_key_to_bluetooth_entity_key
from .types import BTHomeConfigEntry
SENSOR_DESCRIPTIONS = { SENSOR_DESCRIPTIONS = {
# Acceleration (m/s²) # Acceleration (m/s²)
@ -394,13 +390,11 @@ def sensor_update_to_bluetooth_data_update(
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: config_entries.ConfigEntry, entry: BTHomeConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the BTHome BLE sensors.""" """Set up the BTHome BLE sensors."""
coordinator: BTHomePassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]
processor = BTHomePassiveBluetoothDataProcessor( processor = BTHomePassiveBluetoothDataProcessor(
sensor_update_to_bluetooth_data_update sensor_update_to_bluetooth_data_update
) )

View File

@ -0,0 +1,10 @@
"""The BTHome Bluetooth integration."""
from typing import TYPE_CHECKING
from homeassistant.config_entries import ConfigEntry
if TYPE_CHECKING:
from .coordinator import BTHomePassiveBluetoothProcessorCoordinator
type BTHomeConfigEntry = ConfigEntry[BTHomePassiveBluetoothProcessorCoordinator]