mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Move dexcom coordinator to separate module (#136433)
This commit is contained in:
parent
384c173ab3
commit
f6b1786b13
@ -1,21 +1,14 @@
|
||||
"""The Dexcom integration."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from pydexcom import AccountError, Dexcom, GlucoseReading, SessionError
|
||||
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 homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import CONF_SERVER, DOMAIN, PLATFORMS, SERVER_OUS
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=180)
|
||||
from .coordinator import DexcomCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
@ -32,20 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
except SessionError as error:
|
||||
raise ConfigEntryNotReady from error
|
||||
|
||||
async def async_update_data():
|
||||
try:
|
||||
return await hass.async_add_executor_job(dexcom.get_current_glucose_reading)
|
||||
except SessionError as error:
|
||||
raise UpdateFailed(error) from error
|
||||
|
||||
coordinator = DataUpdateCoordinator[GlucoseReading](
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=entry,
|
||||
name=DOMAIN,
|
||||
update_method=async_update_data,
|
||||
update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
coordinator = DexcomCoordinator(hass, entry=entry, dexcom=dexcom)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||
|
42
homeassistant/components/dexcom/coordinator.py
Normal file
42
homeassistant/components/dexcom/coordinator.py
Normal file
@ -0,0 +1,42 @@
|
||||
"""Coordinator for the Dexcom integration."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from pydexcom import Dexcom, GlucoseReading
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
_SCAN_INTERVAL = timedelta(seconds=180)
|
||||
|
||||
|
||||
class DexcomCoordinator(DataUpdateCoordinator[GlucoseReading]):
|
||||
"""Dexcom Coordinator."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
dexcom: Dexcom,
|
||||
) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=entry,
|
||||
name=DOMAIN,
|
||||
update_interval=_SCAN_INTERVAL,
|
||||
)
|
||||
self.dexcom = dexcom
|
||||
|
||||
async def _async_update_data(self) -> GlucoseReading:
|
||||
"""Fetch data from API endpoint."""
|
||||
return await self.hass.async_add_executor_job(
|
||||
self.dexcom.get_current_glucose_reading
|
||||
)
|
@ -2,20 +2,16 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pydexcom import GlucoseReading
|
||||
|
||||
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
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
)
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import DexcomCoordinator
|
||||
|
||||
TRENDS = {
|
||||
1: "rising_quickly",
|
||||
@ -44,16 +40,14 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
|
||||
class DexcomSensorEntity(
|
||||
CoordinatorEntity[DataUpdateCoordinator[GlucoseReading]], SensorEntity
|
||||
):
|
||||
class DexcomSensorEntity(CoordinatorEntity[DexcomCoordinator], SensorEntity):
|
||||
"""Base Dexcom sensor entity."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator[GlucoseReading],
|
||||
coordinator: DexcomCoordinator,
|
||||
username: str,
|
||||
entry_id: str,
|
||||
key: str,
|
||||
@ -78,7 +72,7 @@ class DexcomGlucoseValueSensor(DexcomSensorEntity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
coordinator: DexcomCoordinator,
|
||||
username: str,
|
||||
entry_id: str,
|
||||
) -> None:
|
||||
@ -101,7 +95,7 @@ class DexcomGlucoseTrendSensor(DexcomSensorEntity):
|
||||
_attr_options = list(TRENDS.values())
|
||||
|
||||
def __init__(
|
||||
self, coordinator: DataUpdateCoordinator, username: str, entry_id: str
|
||||
self, coordinator: DexcomCoordinator, username: str, entry_id: str
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator, username, entry_id, "trend")
|
||||
|
Loading…
x
Reference in New Issue
Block a user