mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 17:49:37 +01:00
Use runtime_data in switchbee (#167878)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user