Migrate justnimbus to use runtime_data (#147170)

This commit is contained in:
epenet 2025-06-20 08:10:06 +02:00 committed by GitHub
parent 11564e3df5
commit d16ec81727
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 17 deletions

View File

@ -2,15 +2,14 @@
from __future__ import annotations from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
from .const import DOMAIN, PLATFORMS from .const import PLATFORMS
from .coordinator import JustNimbusCoordinator from .coordinator import JustNimbusConfigEntry, JustNimbusCoordinator
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: JustNimbusConfigEntry) -> bool:
"""Set up JustNimbus from a config entry.""" """Set up JustNimbus from a config entry."""
if "zip_code" in entry.data: if "zip_code" in entry.data:
coordinator = JustNimbusCoordinator(hass, entry) coordinator = JustNimbusCoordinator(hass, entry)
@ -18,13 +17,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
raise ConfigEntryAuthFailed raise ConfigEntryAuthFailed
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: JustNimbusConfigEntry) -> 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

View File

@ -16,13 +16,17 @@ from .const import CONF_ZIP_CODE, DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type JustNimbusConfigEntry = ConfigEntry[JustNimbusCoordinator]
class JustNimbusCoordinator(DataUpdateCoordinator[justnimbus.JustNimbusModel]): class JustNimbusCoordinator(DataUpdateCoordinator[justnimbus.JustNimbusModel]):
"""Data update coordinator.""" """Data update coordinator."""
config_entry: ConfigEntry config_entry: JustNimbusConfigEntry
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: def __init__(
self, hass: HomeAssistant, config_entry: JustNimbusConfigEntry
) -> None:
"""Initialize the coordinator.""" """Initialize the coordinator."""
super().__init__( super().__init__(
hass, hass,

View File

@ -12,7 +12,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_CLIENT_ID, CONF_CLIENT_ID,
EntityCategory, EntityCategory,
@ -24,8 +23,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from . import JustNimbusCoordinator from .coordinator import JustNimbusConfigEntry, JustNimbusCoordinator
from .const import DOMAIN
from .entity import JustNimbusEntity from .entity import JustNimbusEntity
@ -102,16 +100,15 @@ SENSOR_TYPES = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: JustNimbusConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the JustNimbus sensor.""" """Set up the JustNimbus sensor."""
coordinator: JustNimbusCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities( async_add_entities(
JustNimbusSensor( JustNimbusSensor(
device_id=entry.data[CONF_CLIENT_ID], device_id=entry.data[CONF_CLIENT_ID],
description=description, description=description,
coordinator=coordinator, coordinator=entry.runtime_data,
) )
for description in SENSOR_TYPES for description in SENSOR_TYPES
) )