Convert qingping to use entry.runtime_data (#122528)

This commit is contained in:
J. Nick Koston 2024-07-25 05:19:55 -05:00 committed by GitHub
parent a89853da9d
commit 78e24be1e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 33 deletions

View file

@ -14,37 +14,31 @@ 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 .const import DOMAIN
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type QingpingConfigEntry = ConfigEntry[PassiveBluetoothProcessorCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: QingpingConfigEntry) -> bool:
"""Set up Qingping BLE device from a config entry.""" """Set up Qingping BLE device from a config entry."""
address = entry.unique_id address = entry.unique_id
assert address is not None assert address is not None
data = QingpingBluetoothDeviceData() data = QingpingBluetoothDeviceData()
coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = ( coordinator = entry.runtime_data = PassiveBluetoothProcessorCoordinator(
PassiveBluetoothProcessorCoordinator( hass,
hass, _LOGGER,
_LOGGER, address=address,
address=address, mode=BluetoothScanningMode.PASSIVE,
mode=BluetoothScanningMode.PASSIVE, update_method=data.update,
update_method=data.update,
)
) )
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: QingpingConfigEntry) -> 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 qingping_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,
@ -16,14 +15,13 @@ from homeassistant.components.binary_sensor import (
from homeassistant.components.bluetooth.passive_update_processor import ( from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothDataProcessor, PassiveBluetoothDataProcessor,
PassiveBluetoothDataUpdate, PassiveBluetoothDataUpdate,
PassiveBluetoothProcessorCoordinator,
PassiveBluetoothProcessorEntity, PassiveBluetoothProcessorEntity,
) )
from homeassistant.core import HomeAssistant 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 . import QingpingConfigEntry
from .device import device_key_to_bluetooth_entity_key from .device import device_key_to_bluetooth_entity_key
BINARY_SENSOR_DESCRIPTIONS = { BINARY_SENSOR_DESCRIPTIONS = {
@ -75,13 +73,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: QingpingConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Qingping BLE sensors.""" """Set up the Qingping BLE sensors."""
coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]
processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update) processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
entry.async_on_unload( entry.async_on_unload(
processor.async_add_entities_listener( processor.async_add_entities_listener(

View file

@ -8,11 +8,9 @@ from qingping_ble import (
Units, Units,
) )
from homeassistant import config_entries
from homeassistant.components.bluetooth.passive_update_processor import ( from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothDataProcessor, PassiveBluetoothDataProcessor,
PassiveBluetoothDataUpdate, PassiveBluetoothDataUpdate,
PassiveBluetoothProcessorCoordinator,
PassiveBluetoothProcessorEntity, PassiveBluetoothProcessorEntity,
) )
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -35,7 +33,7 @@ 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 . import QingpingConfigEntry
from .device import device_key_to_bluetooth_entity_key from .device import device_key_to_bluetooth_entity_key
SENSOR_DESCRIPTIONS = { SENSOR_DESCRIPTIONS = {
@ -143,13 +141,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: QingpingConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Qingping BLE sensors.""" """Set up the Qingping BLE sensors."""
coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]
processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update) processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
entry.async_on_unload( entry.async_on_unload(
processor.async_add_entities_listener( processor.async_add_entities_listener(