1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-02 00:20:30 +01:00

Migrate nightscout to use runtime_data (#166927)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
epenet
2026-03-31 22:48:29 +02:00
committed by GitHub
parent 19761a25da
commit 69a2284a00
2 changed files with 10 additions and 13 deletions

View File

@@ -16,8 +16,10 @@ from .const import DOMAIN
PLATFORMS = [Platform.SENSOR]
_API_TIMEOUT = SLOW_UPDATE_WARNING - 1
type NightscoutConfigEntry = ConfigEntry[NightscoutAPI]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: NightscoutConfigEntry) -> bool:
"""Set up Nightscout from a config entry."""
server_url = entry.data[CONF_URL]
api_key = entry.data.get(CONF_API_KEY)
@@ -28,8 +30,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except (ClientError, TimeoutError, OSError) as error:
raise ConfigEntryNotReady from error
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = api
entry.runtime_data = api
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
@@ -46,10 +47,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: NightscoutConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@@ -10,12 +10,12 @@ from aiohttp import ClientError
from py_nightscout import Api as NightscoutAPI
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_DATE, UnitOfBloodGlucoseConcentration
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import ATTR_DELTA, ATTR_DEVICE, ATTR_DIRECTION, DOMAIN
from . import NightscoutConfigEntry
from .const import ATTR_DELTA, ATTR_DEVICE, ATTR_DIRECTION
SCAN_INTERVAL = timedelta(minutes=1)
@@ -26,11 +26,11 @@ DEFAULT_NAME = "Blood Glucose"
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: NightscoutConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the Glucose Sensor."""
api = hass.data[DOMAIN][entry.entry_id]
api = entry.runtime_data
async_add_entities([NightscoutSensor(api, "Blood Sugar", entry.unique_id)], True)