Use runtime_data in geocaching (#144310)

This commit is contained in:
epenet 2025-05-06 10:41:56 +02:00 committed by GitHub
parent ec4f4a4a1f
commit 5df3a9d76d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 16 deletions

View File

@ -1,6 +1,5 @@
"""The Geocaching integration.""" """The Geocaching integration."""
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.config_entry_oauth2_flow import ( from homeassistant.helpers.config_entry_oauth2_flow import (
@ -8,13 +7,12 @@ from homeassistant.helpers.config_entry_oauth2_flow import (
async_get_config_entry_implementation, async_get_config_entry_implementation,
) )
from .const import DOMAIN from .coordinator import GeocachingConfigEntry, GeocachingDataUpdateCoordinator
from .coordinator import GeocachingDataUpdateCoordinator
PLATFORMS = [Platform.SENSOR] PLATFORMS = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: GeocachingConfigEntry) -> bool:
"""Set up Geocaching from a config entry.""" """Set up Geocaching from a config entry."""
implementation = await async_get_config_entry_implementation(hass, entry) implementation = await async_get_config_entry_implementation(hass, entry)
@ -25,15 +23,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {}) entry.runtime_data = coordinator
hass.data[DOMAIN][entry.entry_id] = 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: GeocachingConfigEntry) -> 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)
del hass.data[DOMAIN][entry.entry_id]
return unload_ok

View File

@ -14,14 +14,20 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import DOMAIN, ENVIRONMENT, LOGGER, UPDATE_INTERVAL from .const import DOMAIN, ENVIRONMENT, LOGGER, UPDATE_INTERVAL
type GeocachingConfigEntry = ConfigEntry[GeocachingDataUpdateCoordinator]
class GeocachingDataUpdateCoordinator(DataUpdateCoordinator[GeocachingStatus]): class GeocachingDataUpdateCoordinator(DataUpdateCoordinator[GeocachingStatus]):
"""Class to manage fetching Geocaching data from single endpoint.""" """Class to manage fetching Geocaching data from single endpoint."""
config_entry: ConfigEntry config_entry: GeocachingConfigEntry
def __init__( def __init__(
self, hass: HomeAssistant, *, entry: ConfigEntry, session: OAuth2Session self,
hass: HomeAssistant,
*,
entry: GeocachingConfigEntry,
session: OAuth2Session,
) -> None: ) -> None:
"""Initialize global Geocaching data updater.""" """Initialize global Geocaching data updater."""
self.session = session self.session = session

View File

@ -9,14 +9,13 @@ from typing import cast
from geocachingapi.models import GeocachingStatus from geocachingapi.models import GeocachingStatus
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN from .const import DOMAIN
from .coordinator import GeocachingDataUpdateCoordinator from .coordinator import GeocachingConfigEntry, GeocachingDataUpdateCoordinator
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
@ -65,11 +64,11 @@ SENSORS: tuple[GeocachingSensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: GeocachingConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up a Geocaching sensor entry.""" """Set up a Geocaching sensor entry."""
coordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities( async_add_entities(
GeocachingSensor(coordinator, description) for description in SENSORS GeocachingSensor(coordinator, description) for description in SENSORS
) )