Use runtime_data in aranet (#129155)
parent
98c81fa2af
commit
93e270f379
|
@ -15,12 +15,14 @@ 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.SENSOR]
|
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type AranetConfigEntry = ConfigEntry[
|
||||||
|
PassiveBluetoothProcessorCoordinator[Aranet4Advertisement]
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def _service_info_to_adv(
|
def _service_info_to_adv(
|
||||||
service_info: BluetoothServiceInfoBleak,
|
service_info: BluetoothServiceInfoBleak,
|
||||||
|
@ -28,30 +30,25 @@ def _service_info_to_adv(
|
||||||
return Aranet4Advertisement(service_info.device, service_info.advertisement)
|
return Aranet4Advertisement(service_info.device, service_info.advertisement)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: AranetConfigEntry) -> bool:
|
||||||
"""Set up Aranet from a config entry."""
|
"""Set up Aranet from a config entry."""
|
||||||
|
|
||||||
address = entry.unique_id
|
address = entry.unique_id
|
||||||
assert address is not None
|
assert address is not None
|
||||||
coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = (
|
coordinator = PassiveBluetoothProcessorCoordinator(
|
||||||
PassiveBluetoothProcessorCoordinator(
|
hass,
|
||||||
hass,
|
_LOGGER,
|
||||||
_LOGGER,
|
address=address,
|
||||||
address=address,
|
mode=BluetoothScanningMode.PASSIVE,
|
||||||
mode=BluetoothScanningMode.PASSIVE,
|
update_method=_service_info_to_adv,
|
||||||
update_method=_service_info_to_adv,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
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: AranetConfigEntry) -> 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
|
|
||||||
|
|
|
@ -8,12 +8,10 @@ from typing import Any
|
||||||
from aranet4.client import Aranet4Advertisement
|
from aranet4.client import Aranet4Advertisement
|
||||||
from bleak.backends.device import BLEDevice
|
from bleak.backends.device import BLEDevice
|
||||||
|
|
||||||
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,
|
||||||
PassiveBluetoothEntityKey,
|
PassiveBluetoothEntityKey,
|
||||||
PassiveBluetoothProcessorCoordinator,
|
|
||||||
PassiveBluetoothProcessorEntity,
|
PassiveBluetoothProcessorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
|
@ -38,7 +36,8 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity import EntityDescription
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import ARANET_MANUFACTURER_NAME, DOMAIN
|
from . import AranetConfigEntry
|
||||||
|
from .const import ARANET_MANUFACTURER_NAME
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
|
@ -174,20 +173,17 @@ 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: AranetConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Aranet sensors."""
|
"""Set up the Aranet sensors."""
|
||||||
coordinator: PassiveBluetoothProcessorCoordinator[Aranet4Advertisement] = hass.data[
|
|
||||||
DOMAIN
|
|
||||||
][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(
|
||||||
Aranet4BluetoothSensorEntity, async_add_entities
|
Aranet4BluetoothSensorEntity, async_add_entities
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
entry.async_on_unload(coordinator.async_register_processor(processor))
|
entry.async_on_unload(entry.runtime_data.async_register_processor(processor))
|
||||||
|
|
||||||
|
|
||||||
class Aranet4BluetoothSensorEntity(
|
class Aranet4BluetoothSensorEntity(
|
||||||
|
|
Loading…
Reference in New Issue