Store runtime data inside the config entry in Deluge (#119549)

This commit is contained in:
Robert Hillis 2024-06-18 07:23:11 -04:00 committed by GitHub
parent a1a8d38181
commit 6b27e9a745
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 22 deletions

View File

@ -26,9 +26,10 @@ from .coordinator import DelugeDataUpdateCoordinator
PLATFORMS = [Platform.SENSOR, Platform.SWITCH] PLATFORMS = [Platform.SENSOR, Platform.SWITCH]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type DelugeConfigEntry = ConfigEntry[DelugeDataUpdateCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: DelugeConfigEntry) -> bool:
"""Set up Deluge from a config entry.""" """Set up Deluge from a config entry."""
host = entry.data[CONF_HOST] host = entry.data[CONF_HOST]
port = entry.data[CONF_PORT] port = entry.data[CONF_PORT]
@ -51,18 +52,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator = DelugeDataUpdateCoordinator(hass, api, entry) coordinator = DelugeDataUpdateCoordinator(hass, api, entry)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
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)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: DelugeConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
class DelugeEntity(CoordinatorEntity[DelugeDataUpdateCoordinator]): class DelugeEntity(CoordinatorEntity[DelugeDataUpdateCoordinator]):

View File

@ -4,11 +4,10 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from ssl import SSLError from ssl import SSLError
from typing import Any from typing import TYPE_CHECKING, Any
from deluge_client.client import DelugeRPCClient, FailedToReconnectException from deluge_client.client import DelugeRPCClient, FailedToReconnectException
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
@ -16,16 +15,19 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import DATA_KEYS, LOGGER from .const import DATA_KEYS, LOGGER
if TYPE_CHECKING:
from . import DelugeConfigEntry
class DelugeDataUpdateCoordinator( class DelugeDataUpdateCoordinator(
DataUpdateCoordinator[dict[Platform, dict[str, Any]]] DataUpdateCoordinator[dict[Platform, dict[str, Any]]]
): ):
"""Data update coordinator for the Deluge integration.""" """Data update coordinator for the Deluge integration."""
config_entry: ConfigEntry config_entry: DelugeConfigEntry
def __init__( def __init__(
self, hass: HomeAssistant, api: DelugeRPCClient, entry: ConfigEntry self, hass: HomeAssistant, api: DelugeRPCClient, entry: DelugeConfigEntry
) -> None: ) -> None:
"""Initialize the coordinator.""" """Initialize the coordinator."""
super().__init__( super().__init__(

View File

@ -12,14 +12,13 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_IDLE, Platform, UnitOfDataRate from homeassistant.const import STATE_IDLE, Platform, UnitOfDataRate
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 homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from . import DelugeEntity from . import DelugeConfigEntry, DelugeEntity
from .const import CURRENT_STATUS, DATA_KEYS, DOMAIN, DOWNLOAD_SPEED, UPLOAD_SPEED from .const import CURRENT_STATUS, DATA_KEYS, DOWNLOAD_SPEED, UPLOAD_SPEED
from .coordinator import DelugeDataUpdateCoordinator from .coordinator import DelugeDataUpdateCoordinator
@ -74,12 +73,13 @@ SENSOR_TYPES: tuple[DelugeSensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: DelugeConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Deluge sensor.""" """Set up the Deluge sensor."""
async_add_entities( async_add_entities(
DelugeSensor(hass.data[DOMAIN][entry.entry_id], description) DelugeSensor(entry.runtime_data, description) for description in SENSOR_TYPES
for description in SENSOR_TYPES
) )

View File

@ -5,21 +5,21 @@ from __future__ import annotations
from typing import Any from typing import Any
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
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 . import DelugeEntity from . import DelugeConfigEntry, DelugeEntity
from .const import DOMAIN
from .coordinator import DelugeDataUpdateCoordinator from .coordinator import DelugeDataUpdateCoordinator
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: DelugeConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Deluge switch.""" """Set up the Deluge switch."""
async_add_entities([DelugeSwitch(hass.data[DOMAIN][entry.entry_id])]) async_add_entities([DelugeSwitch(entry.runtime_data)])
class DelugeSwitch(DelugeEntity, SwitchEntity): class DelugeSwitch(DelugeEntity, SwitchEntity):