Use runtime_data in dexcom (#136441)
parent
f3e13f4662
commit
4dc873416f
|
@ -2,16 +2,15 @@
|
|||
|
||||
from pydexcom import AccountError, Dexcom, SessionError
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .const import CONF_SERVER, DOMAIN, PLATFORMS, SERVER_OUS
|
||||
from .coordinator import DexcomCoordinator
|
||||
from .const import CONF_SERVER, PLATFORMS, SERVER_OUS
|
||||
from .coordinator import DexcomConfigEntry, DexcomCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: DexcomConfigEntry) -> bool:
|
||||
"""Set up Dexcom from a config entry."""
|
||||
try:
|
||||
dexcom = await hass.async_add_executor_job(
|
||||
|
@ -28,15 +27,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
coordinator = DexcomCoordinator(hass, entry=entry, dexcom=dexcom)
|
||||
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: DexcomConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
|
|
@ -15,6 +15,8 @@ _LOGGER = logging.getLogger(__name__)
|
|||
|
||||
_SCAN_INTERVAL = timedelta(seconds=180)
|
||||
|
||||
type DexcomConfigEntry = ConfigEntry[DexcomCoordinator]
|
||||
|
||||
|
||||
class DexcomCoordinator(DataUpdateCoordinator[GlucoseReading]):
|
||||
"""Dexcom Coordinator."""
|
||||
|
@ -22,7 +24,7 @@ class DexcomCoordinator(DataUpdateCoordinator[GlucoseReading]):
|
|||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: DexcomConfigEntry,
|
||||
dexcom: Dexcom,
|
||||
) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_USERNAME, UnitOfBloodGlucoseConcentration
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
|
@ -11,7 +10,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import DexcomCoordinator
|
||||
from .coordinator import DexcomConfigEntry, DexcomCoordinator
|
||||
|
||||
TRENDS = {
|
||||
1: "rising_quickly",
|
||||
|
@ -26,11 +25,11 @@ TRENDS = {
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: DexcomConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Dexcom sensors."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
coordinator = config_entry.runtime_data
|
||||
username = config_entry.data[CONF_USERNAME]
|
||||
async_add_entities(
|
||||
[
|
||||
|
|
Loading…
Reference in New Issue