From 683ec87adf8ca5268b3f0b087da3b10540edde44 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Wed, 23 Oct 2024 05:45:58 +0200 Subject: [PATCH] Use ConfigEntry.runtime_data in gardena_bluetooth (#129000) --- .../components/gardena_bluetooth/__init__.py | 15 ++++++++++----- .../components/gardena_bluetooth/binary_sensor.py | 10 +++++----- .../components/gardena_bluetooth/button.py | 10 +++++----- .../components/gardena_bluetooth/number.py | 9 +++++---- .../components/gardena_bluetooth/sensor.py | 9 +++++---- .../components/gardena_bluetooth/switch.py | 9 +++++---- .../components/gardena_bluetooth/valve.py | 9 +++++---- 7 files changed, 40 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/gardena_bluetooth/__init__.py b/homeassistant/components/gardena_bluetooth/__init__.py index b6a26456168..7aae629974c 100644 --- a/homeassistant/components/gardena_bluetooth/__init__.py +++ b/homeassistant/components/gardena_bluetooth/__init__.py @@ -32,6 +32,8 @@ LOGGER = logging.getLogger(__name__) TIMEOUT = 20.0 DISCONNECT_DELAY = 5 +type GardenaBluetoothConfigEntry = ConfigEntry[GardenaBluetoothCoordinator] + def get_connection(hass: HomeAssistant, address: str) -> CachedConnection: """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) -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.""" 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.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator + entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await coordinator.async_refresh() 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.""" if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN].pop(entry.entry_id) - await coordinator.async_shutdown() + await entry.runtime_data.async_shutdown() return unload_ok diff --git a/homeassistant/components/gardena_bluetooth/binary_sensor.py b/homeassistant/components/gardena_bluetooth/binary_sensor.py index be6d8bbeede..d3ae096e291 100644 --- a/homeassistant/components/gardena_bluetooth/binary_sensor.py +++ b/homeassistant/components/gardena_bluetooth/binary_sensor.py @@ -12,13 +12,11 @@ from homeassistant.components.binary_sensor import ( BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN -from .coordinator import GardenaBluetoothCoordinator +from . import GardenaBluetoothConfigEntry from .entity import GardenaBluetoothDescriptorEntity @@ -53,10 +51,12 @@ DESCRIPTIONS = ( async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, + entry: GardenaBluetoothConfigEntry, + async_add_entities: AddEntitiesCallback, ) -> None: """Set up binary sensor based on a config entry.""" - coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data entities = [ GardenaBluetoothBinarySensor(coordinator, description, description.context) for description in DESCRIPTIONS diff --git a/homeassistant/components/gardena_bluetooth/button.py b/homeassistant/components/gardena_bluetooth/button.py index 67377dc684e..9d87cba2446 100644 --- a/homeassistant/components/gardena_bluetooth/button.py +++ b/homeassistant/components/gardena_bluetooth/button.py @@ -8,13 +8,11 @@ from gardena_bluetooth.const import Reset from gardena_bluetooth.parse import CharacteristicBool from homeassistant.components.button import ButtonEntity, ButtonEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN -from .coordinator import GardenaBluetoothCoordinator +from . import GardenaBluetoothConfigEntry from .entity import GardenaBluetoothDescriptorEntity @@ -42,10 +40,12 @@ DESCRIPTIONS = ( async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, + entry: GardenaBluetoothConfigEntry, + async_add_entities: AddEntitiesCallback, ) -> None: """Set up button based on a config entry.""" - coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data entities = [ GardenaBluetoothButton(coordinator, description, description.context) for description in DESCRIPTIONS diff --git a/homeassistant/components/gardena_bluetooth/number.py b/homeassistant/components/gardena_bluetooth/number.py index d3c178ee637..b55630fa797 100644 --- a/homeassistant/components/gardena_bluetooth/number.py +++ b/homeassistant/components/gardena_bluetooth/number.py @@ -17,12 +17,11 @@ from homeassistant.components.number import ( NumberEntityDescription, NumberMode, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN +from . import GardenaBluetoothConfigEntry from .coordinator import GardenaBluetoothCoordinator from .entity import GardenaBluetoothDescriptorEntity, GardenaBluetoothEntity @@ -105,10 +104,12 @@ DESCRIPTIONS = ( async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, + entry: GardenaBluetoothConfigEntry, + async_add_entities: AddEntitiesCallback, ) -> None: """Set up entity based on a config entry.""" - coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data entities: list[NumberEntity] = [ GardenaBluetoothNumber(coordinator, description, description.context) for description in DESCRIPTIONS diff --git a/homeassistant/components/gardena_bluetooth/sensor.py b/homeassistant/components/gardena_bluetooth/sensor.py index 19fefefa9aa..ee8a2663218 100644 --- a/homeassistant/components/gardena_bluetooth/sensor.py +++ b/homeassistant/components/gardena_bluetooth/sensor.py @@ -14,13 +14,12 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import PERCENTAGE, EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.dt as dt_util -from .const import DOMAIN +from . import GardenaBluetoothConfigEntry from .coordinator import GardenaBluetoothCoordinator from .entity import GardenaBluetoothDescriptorEntity, GardenaBluetoothEntity @@ -95,10 +94,12 @@ DESCRIPTIONS = ( async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, + entry: GardenaBluetoothConfigEntry, + async_add_entities: AddEntitiesCallback, ) -> None: """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] = [ GardenaBluetoothSensor(coordinator, description, description.context) for description in DESCRIPTIONS diff --git a/homeassistant/components/gardena_bluetooth/switch.py b/homeassistant/components/gardena_bluetooth/switch.py index 58b4b2e4e51..f82c39025a5 100644 --- a/homeassistant/components/gardena_bluetooth/switch.py +++ b/homeassistant/components/gardena_bluetooth/switch.py @@ -7,21 +7,22 @@ from typing import Any from gardena_bluetooth.const import Valve from homeassistant.components.switch import SwitchEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN +from . import GardenaBluetoothConfigEntry from .coordinator import GardenaBluetoothCoordinator from .entity import GardenaBluetoothEntity async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, + entry: GardenaBluetoothConfigEntry, + async_add_entities: AddEntitiesCallback, ) -> None: """Set up switch based on a config entry.""" - coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data entities = [] if GardenaBluetoothValveSwitch.characteristics.issubset( coordinator.characteristics diff --git a/homeassistant/components/gardena_bluetooth/valve.py b/homeassistant/components/gardena_bluetooth/valve.py index 877cc5b505e..ae6bf56a7ff 100644 --- a/homeassistant/components/gardena_bluetooth/valve.py +++ b/homeassistant/components/gardena_bluetooth/valve.py @@ -7,11 +7,10 @@ from typing import Any from gardena_bluetooth.const import Valve from homeassistant.components.valve import ValveEntity, ValveEntityFeature -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN +from . import GardenaBluetoothConfigEntry from .coordinator import GardenaBluetoothCoordinator from .entity import GardenaBluetoothEntity @@ -19,10 +18,12 @@ FALLBACK_WATERING_TIME_IN_SECONDS = 60 * 60 async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, + entry: GardenaBluetoothConfigEntry, + async_add_entities: AddEntitiesCallback, ) -> None: """Set up switch based on a config entry.""" - coordinator: GardenaBluetoothCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data entities = [] if GardenaBluetoothValve.characteristics.issubset(coordinator.characteristics): entities.append(GardenaBluetoothValve(coordinator))