Use runtime_data in thethingsnetwork integration (#168589)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
epenet
2026-04-20 10:28:36 +02:00
committed by GitHub
co-authored by Claude Opus 4.6
parent bf2364e4cb
commit 7acc412902
3 changed files with 14 additions and 17 deletions
@@ -2,17 +2,16 @@
import logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_HOST
from homeassistant.core import HomeAssistant
from .const import DOMAIN, PLATFORMS, TTN_API_HOST
from .coordinator import TTNCoordinator
from .const import PLATFORMS, TTN_API_HOST
from .coordinator import TTNConfigEntry, TTNCoordinator
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: TTNConfigEntry) -> bool:
"""Establish connection with The Things Network."""
_LOGGER.debug(
@@ -25,14 +24,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
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: TTNConfigEntry) -> bool:
"""Unload a config entry."""
_LOGGER.debug(
@@ -41,8 +40,4 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.data.get(CONF_HOST, TTN_API_HOST),
)
# Unload entities created for each supported platform
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
del hass.data[DOMAIN][entry.entry_id]
return True
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
@@ -15,13 +15,15 @@ from .const import CONF_APP_ID, POLLING_PERIOD_S
_LOGGER = logging.getLogger(__name__)
type TTNConfigEntry = ConfigEntry[TTNCoordinator]
class TTNCoordinator(DataUpdateCoordinator[TTNClient.DATA_TYPE]):
"""TTN coordinator."""
config_entry: ConfigEntry
config_entry: TTNConfigEntry
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
def __init__(self, hass: HomeAssistant, entry: TTNConfigEntry) -> None:
"""Initialize my coordinator."""
super().__init__(
hass,
@@ -5,12 +5,12 @@ import logging
from ttn_client import TTNSensorValue
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType
from .const import CONF_APP_ID, DOMAIN
from .const import CONF_APP_ID
from .coordinator import TTNConfigEntry
from .entity import TTNEntity
_LOGGER = logging.getLogger(__name__)
@@ -18,12 +18,12 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: TTNConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Add entities for TTN."""
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data
sensors: set[tuple[str, str]] = set()