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]
_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."""
host = entry.data[CONF_HOST]
port = entry.data[CONF_PORT]
@ -51,18 +52,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator = DelugeDataUpdateCoordinator(hass, api, entry)
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)
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."""
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)
class DelugeEntity(CoordinatorEntity[DelugeDataUpdateCoordinator]):

View File

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

View File

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

View File

@ -5,21 +5,21 @@ from __future__ import annotations
from typing import Any
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DelugeEntity
from .const import DOMAIN
from . import DelugeConfigEntry, DelugeEntity
from .coordinator import DelugeDataUpdateCoordinator
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: DelugeConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""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):