Store Glances coordinator in runtime_data (#119607)

pull/119668/head
Rami Mosleh 2024-06-14 09:39:04 +03:00 committed by GitHub
parent 1d62056d9b
commit f3ce562847
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 12 deletions

View File

@ -40,8 +40,12 @@ CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
_LOGGER = logging.getLogger(__name__)
type GlancesConfigEntry = ConfigEntry[GlancesDataUpdateCoordinator]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, config_entry: GlancesConfigEntry
) -> bool:
"""Set up Glances from config entry."""
try:
api = await get_api(hass, dict(config_entry.data))
@ -54,20 +58,16 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
coordinator = GlancesDataUpdateCoordinator(hass, config_entry, api)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
config_entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: GlancesConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
if not hass.data[DOMAIN]:
del hass.data[DOMAIN]
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def get_api(hass: HomeAssistant, entry_data: dict[str, Any]) -> Glances:

View File

@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
REVOLUTIONS_PER_MINUTE,
@ -23,7 +22,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import GlancesDataUpdateCoordinator
from . import GlancesConfigEntry, GlancesDataUpdateCoordinator
from .const import CPU_ICON, DOMAIN
@ -288,12 +287,12 @@ SENSOR_TYPES = {
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: GlancesConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Glances sensors."""
coordinator: GlancesDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
entities: list[GlancesSensor] = []
for sensor_type, sensors in coordinator.data.items():