From ea642980f22e655a3e0bfc58efdbaa4c3fe7bf45 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 10 Apr 2026 10:45:04 +0200 Subject: [PATCH] Use runtime_data in switchbee (#167878) Co-authored-by: Claude Opus 4.6 (1M context) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../components/switchbee/__init__.py | 23 +++++++++---------- homeassistant/components/switchbee/button.py | 8 +++---- homeassistant/components/switchbee/climate.py | 8 +++---- .../components/switchbee/coordinator.py | 6 +++-- homeassistant/components/switchbee/cover.py | 10 ++++---- homeassistant/components/switchbee/light.py | 12 ++++------ homeassistant/components/switchbee/switch.py | 8 +++---- 7 files changed, 33 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/switchbee/__init__.py b/homeassistant/components/switchbee/__init__.py index 6e4bf004a3d..0459d872605 100644 --- a/homeassistant/components/switchbee/__init__.py +++ b/homeassistant/components/switchbee/__init__.py @@ -9,7 +9,6 @@ from aiohttp import ClientSession from switchbee.api import CentralUnitPolling, CentralUnitWsRPC, is_wsrpc_api from switchbee.api.central_unit import SwitchBeeError -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady @@ -17,7 +16,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN -from .coordinator import SwitchBeeCoordinator +from .coordinator import SwitchBeeConfigEntry, SwitchBeeCoordinator _LOGGER = logging.getLogger(__name__) @@ -53,10 +52,9 @@ async def get_api_object( return api -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: SwitchBeeConfigEntry) -> bool: """Set up SwitchBee Smart Home from a config entry.""" - hass.data.setdefault(DOMAIN, {}) central_unit = entry.data[CONF_HOST] user = entry.data[CONF_USERNAME] password = entry.data[CONF_PASSWORD] @@ -67,27 +65,28 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await coordinator.async_config_entry_first_refresh() entry.async_on_unload(entry.add_update_listener(update_listener)) - hass.data[DOMAIN][entry.entry_id] = coordinator + entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: SwitchBeeConfigEntry) -> bool: """Unload a config entry.""" - if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - hass.data[DOMAIN].pop(entry.entry_id) - - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) -async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None: +async def update_listener( + hass: HomeAssistant, config_entry: SwitchBeeConfigEntry +) -> None: """Update listener.""" await hass.config_entries.async_reload(config_entry.entry_id) -async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: +async def async_migrate_entry( + hass: HomeAssistant, config_entry: SwitchBeeConfigEntry +) -> bool: """Migrate old entry.""" _LOGGER.debug("Migrating from version %s", config_entry.version) diff --git a/homeassistant/components/switchbee/button.py b/homeassistant/components/switchbee/button.py index 1ac81ec4e0d..1e831306e87 100644 --- a/homeassistant/components/switchbee/button.py +++ b/homeassistant/components/switchbee/button.py @@ -4,23 +4,21 @@ from switchbee.api.central_unit import SwitchBeeError from switchbee.device import ApiStateCommand, DeviceType from homeassistant.components.button import ButtonEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN -from .coordinator import SwitchBeeCoordinator +from .coordinator import SwitchBeeConfigEntry from .entity import SwitchBeeEntity async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: SwitchBeeConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Switchbee button.""" - coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data async_add_entities( SwitchBeeButton(switchbee_device, coordinator) for switchbee_device in coordinator.data.values() diff --git a/homeassistant/components/switchbee/climate.py b/homeassistant/components/switchbee/climate.py index 7837798b0cb..e9e794f9910 100644 --- a/homeassistant/components/switchbee/climate.py +++ b/homeassistant/components/switchbee/climate.py @@ -23,14 +23,12 @@ from homeassistant.components.climate import ( HVACAction, HVACMode, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN -from .coordinator import SwitchBeeCoordinator +from .coordinator import SwitchBeeConfigEntry, SwitchBeeCoordinator from .entity import SwitchBeeDeviceEntity FAN_SB_TO_HASS = { @@ -75,11 +73,11 @@ SUPPORTED_FAN_MODES = [FAN_AUTO, FAN_HIGH, FAN_MEDIUM, FAN_LOW] async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: SwitchBeeConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up SwitchBee climate.""" - coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data async_add_entities( SwitchBeeClimateEntity(switchbee_device, coordinator) for switchbee_device in coordinator.data.values() diff --git a/homeassistant/components/switchbee/coordinator.py b/homeassistant/components/switchbee/coordinator.py index b0ea1707be8..6f4577f4347 100644 --- a/homeassistant/components/switchbee/coordinator.py +++ b/homeassistant/components/switchbee/coordinator.py @@ -19,16 +19,18 @@ from .const import DOMAIN, SCAN_INTERVAL_SEC _LOGGER = logging.getLogger(__name__) +type SwitchBeeConfigEntry = ConfigEntry[SwitchBeeCoordinator] + class SwitchBeeCoordinator(DataUpdateCoordinator[Mapping[int, SwitchBeeBaseDevice]]): """Class to manage fetching SwitchBee data API.""" - config_entry: ConfigEntry + config_entry: SwitchBeeConfigEntry def __init__( self, hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: SwitchBeeConfigEntry, swb_api: CentralUnitPolling | CentralUnitWsRPC, ) -> None: """Initialize.""" diff --git a/homeassistant/components/switchbee/cover.py b/homeassistant/components/switchbee/cover.py index 247063ab18a..0b05dff0cbf 100644 --- a/homeassistant/components/switchbee/cover.py +++ b/homeassistant/components/switchbee/cover.py @@ -14,23 +14,21 @@ from homeassistant.components.cover import ( CoverEntity, CoverEntityFeature, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN -from .coordinator import SwitchBeeCoordinator +from .coordinator import SwitchBeeConfigEntry from .entity import SwitchBeeDeviceEntity async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: SwitchBeeConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: - """Set up SwitchBee switch.""" - coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id] + """Set up SwitchBee covers.""" + coordinator = entry.runtime_data entities: list[CoverEntity] = [] for device in coordinator.data.values(): diff --git a/homeassistant/components/switchbee/light.py b/homeassistant/components/switchbee/light.py index 228667540df..eff93b36b39 100644 --- a/homeassistant/components/switchbee/light.py +++ b/homeassistant/components/switchbee/light.py @@ -2,19 +2,17 @@ from __future__ import annotations -from typing import Any +from typing import Any, cast from switchbee.api.central_unit import SwitchBeeDeviceOfflineError, SwitchBeeError from switchbee.device import ApiStateCommand, DeviceType, SwitchBeeDimmer from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN -from .coordinator import SwitchBeeCoordinator +from .coordinator import SwitchBeeConfigEntry, SwitchBeeCoordinator from .entity import SwitchBeeDeviceEntity MAX_BRIGHTNESS = 255 @@ -36,13 +34,13 @@ def _switchbee_brightness_to_hass(value: int) -> int: async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: SwitchBeeConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up SwitchBee light.""" - coordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data async_add_entities( - SwitchBeeLightEntity(switchbee_device, coordinator) + SwitchBeeLightEntity(cast(SwitchBeeDimmer, switchbee_device), coordinator) for switchbee_device in coordinator.data.values() if switchbee_device.type == DeviceType.Dimmer ) diff --git a/homeassistant/components/switchbee/switch.py b/homeassistant/components/switchbee/switch.py index 41538f6fd71..3332aad1ad4 100644 --- a/homeassistant/components/switchbee/switch.py +++ b/homeassistant/components/switchbee/switch.py @@ -14,23 +14,21 @@ from switchbee.device import ( ) from homeassistant.components.switch import SwitchEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN -from .coordinator import SwitchBeeCoordinator +from .coordinator import SwitchBeeConfigEntry, SwitchBeeCoordinator from .entity import SwitchBeeDeviceEntity async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: SwitchBeeConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Switchbee switch.""" - coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data async_add_entities( SwitchBeeSwitchEntity(device, coordinator)