Use ConfigEntry.runtime_data in gardena_bluetooth (#129000)

This commit is contained in:
Jan-Philipp Benecke 2024-10-23 05:45:58 +02:00 committed by GitHub
parent 23edbe5ce7
commit 683ec87adf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 40 additions and 31 deletions

View File

@ -32,6 +32,8 @@ LOGGER = logging.getLogger(__name__)
TIMEOUT = 20.0 TIMEOUT = 20.0
DISCONNECT_DELAY = 5 DISCONNECT_DELAY = 5
type GardenaBluetoothConfigEntry = ConfigEntry[GardenaBluetoothCoordinator]
def get_connection(hass: HomeAssistant, address: str) -> CachedConnection: def get_connection(hass: HomeAssistant, address: str) -> CachedConnection:
"""Set up a cached client that keeps connection after last use.""" """Set up a cached client that keeps connection after last use."""
@ -47,7 +49,9 @@ def get_connection(hass: HomeAssistant, address: str) -> CachedConnection:
return CachedConnection(DISCONNECT_DELAY, _device_lookup) return CachedConnection(DISCONNECT_DELAY, _device_lookup)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(
hass: HomeAssistant, entry: GardenaBluetoothConfigEntry
) -> bool:
"""Set up Gardena Bluetooth from a config entry.""" """Set up Gardena Bluetooth from a config entry."""
address = entry.data[CONF_ADDRESS] address = entry.data[CONF_ADDRESS]
@ -79,17 +83,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass, LOGGER, client, uuids, device, address hass, LOGGER, client, uuids, device, address
) )
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await coordinator.async_refresh() await coordinator.async_refresh()
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(
hass: HomeAssistant, entry: GardenaBluetoothConfigEntry
) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN].pop(entry.entry_id) await entry.runtime_data.async_shutdown()
await coordinator.async_shutdown()
return unload_ok return unload_ok

View File

@ -12,13 +12,11 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
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 .const import DOMAIN from . import GardenaBluetoothConfigEntry
from .coordinator import GardenaBluetoothCoordinator
from .entity import GardenaBluetoothDescriptorEntity from .entity import GardenaBluetoothDescriptorEntity
@ -53,10 +51,12 @@ DESCRIPTIONS = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: GardenaBluetoothConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up binary sensor based on a config entry.""" """Set up binary sensor based on a config entry."""
coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
entities = [ entities = [
GardenaBluetoothBinarySensor(coordinator, description, description.context) GardenaBluetoothBinarySensor(coordinator, description, description.context)
for description in DESCRIPTIONS for description in DESCRIPTIONS

View File

@ -8,13 +8,11 @@ from gardena_bluetooth.const import Reset
from gardena_bluetooth.parse import CharacteristicBool from gardena_bluetooth.parse import CharacteristicBool
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
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 .const import DOMAIN from . import GardenaBluetoothConfigEntry
from .coordinator import GardenaBluetoothCoordinator
from .entity import GardenaBluetoothDescriptorEntity from .entity import GardenaBluetoothDescriptorEntity
@ -42,10 +40,12 @@ DESCRIPTIONS = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: GardenaBluetoothConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up button based on a config entry.""" """Set up button based on a config entry."""
coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
entities = [ entities = [
GardenaBluetoothButton(coordinator, description, description.context) GardenaBluetoothButton(coordinator, description, description.context)
for description in DESCRIPTIONS for description in DESCRIPTIONS

View File

@ -17,12 +17,11 @@ from homeassistant.components.number import (
NumberEntityDescription, NumberEntityDescription,
NumberMode, NumberMode,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime
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 .const import DOMAIN from . import GardenaBluetoothConfigEntry
from .coordinator import GardenaBluetoothCoordinator from .coordinator import GardenaBluetoothCoordinator
from .entity import GardenaBluetoothDescriptorEntity, GardenaBluetoothEntity from .entity import GardenaBluetoothDescriptorEntity, GardenaBluetoothEntity
@ -105,10 +104,12 @@ DESCRIPTIONS = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: GardenaBluetoothConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up entity based on a config entry.""" """Set up entity based on a config entry."""
coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
entities: list[NumberEntity] = [ entities: list[NumberEntity] = [
GardenaBluetoothNumber(coordinator, description, description.context) GardenaBluetoothNumber(coordinator, description, description.context)
for description in DESCRIPTIONS for description in DESCRIPTIONS

View File

@ -14,13 +14,12 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, EntityCategory from homeassistant.const import PERCENTAGE, EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .const import DOMAIN from . import GardenaBluetoothConfigEntry
from .coordinator import GardenaBluetoothCoordinator from .coordinator import GardenaBluetoothCoordinator
from .entity import GardenaBluetoothDescriptorEntity, GardenaBluetoothEntity from .entity import GardenaBluetoothDescriptorEntity, GardenaBluetoothEntity
@ -95,10 +94,12 @@ DESCRIPTIONS = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: GardenaBluetoothConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Gardena Bluetooth sensor based on a config entry.""" """Set up Gardena Bluetooth sensor based on a config entry."""
coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
entities: list[GardenaBluetoothEntity] = [ entities: list[GardenaBluetoothEntity] = [
GardenaBluetoothSensor(coordinator, description, description.context) GardenaBluetoothSensor(coordinator, description, description.context)
for description in DESCRIPTIONS for description in DESCRIPTIONS

View File

@ -7,21 +7,22 @@ from typing import Any
from gardena_bluetooth.const import Valve from gardena_bluetooth.const import Valve
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from . import GardenaBluetoothConfigEntry
from .coordinator import GardenaBluetoothCoordinator from .coordinator import GardenaBluetoothCoordinator
from .entity import GardenaBluetoothEntity from .entity import GardenaBluetoothEntity
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: GardenaBluetoothConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up switch based on a config entry.""" """Set up switch based on a config entry."""
coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
entities = [] entities = []
if GardenaBluetoothValveSwitch.characteristics.issubset( if GardenaBluetoothValveSwitch.characteristics.issubset(
coordinator.characteristics coordinator.characteristics

View File

@ -7,11 +7,10 @@ from typing import Any
from gardena_bluetooth.const import Valve from gardena_bluetooth.const import Valve
from homeassistant.components.valve import ValveEntity, ValveEntityFeature from homeassistant.components.valve import ValveEntity, ValveEntityFeature
from homeassistant.config_entries import ConfigEntry
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 .const import DOMAIN from . import GardenaBluetoothConfigEntry
from .coordinator import GardenaBluetoothCoordinator from .coordinator import GardenaBluetoothCoordinator
from .entity import GardenaBluetoothEntity from .entity import GardenaBluetoothEntity
@ -19,10 +18,12 @@ FALLBACK_WATERING_TIME_IN_SECONDS = 60 * 60
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: GardenaBluetoothConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up switch based on a config entry.""" """Set up switch based on a config entry."""
coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
entities = [] entities = []
if GardenaBluetoothValve.characteristics.issubset(coordinator.characteristics): if GardenaBluetoothValve.characteristics.issubset(coordinator.characteristics):
entities.append(GardenaBluetoothValve(coordinator)) entities.append(GardenaBluetoothValve(coordinator))