Use runtime_data in ruckus_unleashed integration (#167662)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
epenet
2026-04-08 12:13:26 +02:00
committed by GitHub
parent b697b3a54e
commit 82202ee1c2
5 changed files with 30 additions and 41 deletions

View File

@@ -5,7 +5,6 @@ import logging
from aioruckus import AjaxSession
from aioruckus.exceptions import AuthenticationError, SchemaError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
@@ -18,18 +17,18 @@ from .const import (
API_AP_MODEL,
API_SYS_SYSINFO,
API_SYS_SYSINFO_VERSION,
COORDINATOR,
DOMAIN,
MANUFACTURER,
PLATFORMS,
UNDO_UPDATE_LISTENERS,
)
from .coordinator import RuckusDataUpdateCoordinator
from .coordinator import RuckusDataUpdateCoordinator, RuckusUnleashedConfigEntry
_LOGGER = logging.getLogger(__package__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, entry: RuckusUnleashedConfigEntry
) -> bool:
"""Set up Ruckus from a config entry."""
ruckus = AjaxSession.async_create(
@@ -69,25 +68,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
),
)
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {
COORDINATOR: coordinator,
UNDO_UPDATE_LISTENERS: [],
}
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: RuckusUnleashedConfigEntry
) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
for listener in hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENERS]:
listener()
await hass.data[DOMAIN][entry.entry_id][COORDINATOR].ruckus.close()
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@@ -13,16 +13,21 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import API_CLIENT_MAC, DOMAIN, KEY_SYS_CLIENTS, SCAN_INTERVAL
type RuckusUnleashedConfigEntry = ConfigEntry[RuckusDataUpdateCoordinator]
_LOGGER = logging.getLogger(__package__)
class RuckusDataUpdateCoordinator(DataUpdateCoordinator):
"""Coordinator to manage data from Ruckus client."""
config_entry: ConfigEntry
config_entry: RuckusUnleashedConfigEntry
def __init__(
self, hass: HomeAssistant, config_entry: ConfigEntry, ruckus: AjaxSession
self,
hass: HomeAssistant,
config_entry: RuckusUnleashedConfigEntry,
ruckus: AjaxSession,
) -> None:
"""Initialize global Ruckus data updater."""
self.ruckus = ruckus
@@ -41,6 +46,11 @@ class RuckusDataUpdateCoordinator(DataUpdateCoordinator):
_LOGGER.debug("fetched %d active clients", len(clients))
return {client[API_CLIENT_MAC]: client for client in clients}
async def async_shutdown(self) -> None:
"""Close the Ruckus session on shutdown."""
await super().async_shutdown()
await self.ruckus.close()
async def _async_update_data(self) -> dict:
"""Fetch Ruckus data."""
try:

View File

@@ -5,32 +5,24 @@ from __future__ import annotations
import logging
from homeassistant.components.device_tracker import ScannerEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
API_CLIENT_HOSTNAME,
API_CLIENT_IP,
COORDINATOR,
DOMAIN,
KEY_SYS_CLIENTS,
UNDO_UPDATE_LISTENERS,
)
from .coordinator import RuckusDataUpdateCoordinator
from .const import API_CLIENT_HOSTNAME, API_CLIENT_IP, DOMAIN, KEY_SYS_CLIENTS
from .coordinator import RuckusDataUpdateCoordinator, RuckusUnleashedConfigEntry
_LOGGER = logging.getLogger(__package__)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: RuckusUnleashedConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up device tracker for Ruckus component."""
coordinator = hass.data[DOMAIN][entry.entry_id][COORDINATOR]
coordinator = entry.runtime_data
tracked: set[str] = set()
@@ -41,9 +33,7 @@ async def async_setup_entry(
router_update()
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENERS].append(
coordinator.async_add_listener(router_update)
)
entry.async_on_unload(coordinator.async_add_listener(router_update))
registry = er.async_get(hass)
restore_entities(registry, coordinator, entry, async_add_entities, tracked)
@@ -70,7 +60,7 @@ def add_new_entities(coordinator, async_add_entities, tracked):
def restore_entities(
registry: er.EntityRegistry,
coordinator: RuckusDataUpdateCoordinator,
entry: ConfigEntry,
entry: RuckusUnleashedConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
tracked: set[str],
) -> None:

View File

@@ -6,7 +6,7 @@ from unittest.mock import AsyncMock
from aioruckus.const import ERROR_CONNECT_EOF, ERROR_LOGIN_INCORRECT
from aioruckus.exceptions import AuthenticationError
from homeassistant.components.ruckus_unleashed import DOMAIN
from homeassistant.components.ruckus_unleashed.const import DOMAIN
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

View File

@@ -5,13 +5,14 @@ from unittest.mock import AsyncMock
from aioruckus.const import ERROR_CONNECT_TIMEOUT, ERROR_LOGIN_INCORRECT
from aioruckus.exceptions import AuthenticationError
from homeassistant.components.ruckus_unleashed import DOMAIN, MANUFACTURER
from homeassistant.components.ruckus_unleashed.const import (
API_AP_DEVNAME,
API_AP_MAC,
API_AP_MODEL,
API_SYS_SYSINFO,
API_SYS_SYSINFO_VERSION,
DOMAIN,
MANUFACTURER,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
@@ -89,4 +90,3 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.NOT_LOADED
assert not hass.data.get(DOMAIN)