Use runtime_data in fjaraskupan (#138281)

pull/138162/head^2
epenet 2025-02-11 17:00:44 +01:00 committed by GitHub
parent 1a4738b1d4
commit df4c718bac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 24 additions and 39 deletions

View File

@ -3,7 +3,6 @@
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
import logging
from fjaraskupan import Device
@ -16,7 +15,6 @@ from homeassistant.components.bluetooth import (
async_rediscover_address,
async_register_callback,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
@ -29,7 +27,7 @@ from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DISPATCH_DETECTION, DOMAIN
from .coordinator import FjaraskupanCoordinator
from .coordinator import FjaraskupanConfigEntry, FjaraskupanCoordinator
PLATFORMS = [
Platform.BINARY_SENSOR,
@ -42,26 +40,17 @@ PLATFORMS = [
_LOGGER = logging.getLogger(__name__)
@dataclass
class EntryState:
"""Store state of config entry."""
coordinators: dict[str, FjaraskupanCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: FjaraskupanConfigEntry) -> bool:
"""Set up Fjäråskupan from a config entry."""
state = EntryState({})
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = state
entry.runtime_data = {}
def detection_callback(
service_info: BluetoothServiceInfoBleak, change: BluetoothChange
) -> None:
if change != BluetoothChange.ADVERTISEMENT:
return
if data := state.coordinators.get(service_info.address):
if data := entry.runtime_data.get(service_info.address):
_LOGGER.debug("Update: %s", service_info)
data.detection_callback(service_info)
else:
@ -80,7 +69,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
coordinator.detection_callback(service_info)
state.coordinators[service_info.address] = coordinator
entry.runtime_data[service_info.address] = coordinator
async_dispatcher_send(
hass, f"{DISPATCH_DETECTION}.{entry.entry_id}", coordinator
)
@ -105,16 +94,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@callback
def async_setup_entry_platform(
hass: HomeAssistant,
entry: ConfigEntry,
entry: FjaraskupanConfigEntry,
async_add_entities: AddEntitiesCallback,
constructor: Callable[[FjaraskupanCoordinator], list[Entity]],
) -> None:
"""Set up a platform with added entities."""
entry_state: EntryState = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
entity
for coordinator in entry_state.coordinators.values()
for coordinator in entry.runtime_data.values()
for entity in constructor(coordinator)
)
@ -129,12 +117,12 @@ def async_setup_entry_platform(
)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, entry: FjaraskupanConfigEntry
) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
for device_entry in dr.async_entries_for_config_entry(
dr.async_get(hass), entry.entry_id
):

View File

@ -12,7 +12,6 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
@ -20,7 +19,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import async_setup_entry_platform
from .coordinator import FjaraskupanCoordinator
from .coordinator import FjaraskupanConfigEntry, FjaraskupanCoordinator
@dataclass(frozen=True)
@ -48,7 +47,7 @@ SENSORS = (
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FjaraskupanConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up sensors dynamically through discovery."""

View File

@ -29,6 +29,8 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import DOMAIN
type FjaraskupanConfigEntry = ConfigEntry[dict[str, FjaraskupanCoordinator]]
_LOGGER = logging.getLogger(__name__)
@ -65,12 +67,12 @@ class UnableToConnect(HomeAssistantError):
class FjaraskupanCoordinator(DataUpdateCoordinator[State]):
"""Update coordinator for each device."""
config_entry: ConfigEntry
config_entry: FjaraskupanConfigEntry
def __init__(
self,
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FjaraskupanConfigEntry,
device: Device,
device_info: DeviceInfo,
) -> None:

View File

@ -13,7 +13,6 @@ from fjaraskupan import (
)
from homeassistant.components.fan import FanEntity, FanEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import DeviceInfo
@ -25,7 +24,7 @@ from homeassistant.util.percentage import (
)
from . import async_setup_entry_platform
from .coordinator import FjaraskupanCoordinator
from .coordinator import FjaraskupanConfigEntry, FjaraskupanCoordinator
ORDERED_NAMED_FAN_SPEEDS = ["1", "2", "3", "4", "5", "6", "7", "8"]
@ -51,7 +50,7 @@ class UnsupportedPreset(HomeAssistantError):
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FjaraskupanConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up sensors dynamically through discovery."""

View File

@ -5,7 +5,6 @@ from __future__ import annotations
from typing import Any
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
@ -13,12 +12,12 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import async_setup_entry_platform
from .coordinator import FjaraskupanCoordinator
from .coordinator import FjaraskupanConfigEntry, FjaraskupanCoordinator
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FjaraskupanConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up tuya sensors dynamically through tuya discovery."""

View File

@ -3,7 +3,6 @@
from __future__ import annotations
from homeassistant.components.number import NumberEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory, UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
@ -12,12 +11,12 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import async_setup_entry_platform
from .coordinator import FjaraskupanCoordinator
from .coordinator import FjaraskupanConfigEntry, FjaraskupanCoordinator
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FjaraskupanConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up number entities dynamically through discovery."""

View File

@ -9,7 +9,6 @@ from homeassistant.components.sensor import (
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import SIGNAL_STRENGTH_DECIBELS_MILLIWATT, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
@ -19,12 +18,12 @@ from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import async_setup_entry_platform
from .coordinator import FjaraskupanCoordinator
from .coordinator import FjaraskupanConfigEntry, FjaraskupanCoordinator
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FjaraskupanConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up sensors dynamically through discovery."""