Skip to content

Commit

Permalink
Move dexcom coordinator to separate module (home-assistant#136433)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jan 24, 2025
1 parent 384c173 commit f6b1786
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
26 changes: 3 additions & 23 deletions homeassistant/components/dexcom/__init__.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
42 changes: 42 additions & 0 deletions homeassistant/components/dexcom/coordinator.py
Original file line number Diff line number Diff line change
@@ -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
)
18 changes: 6 additions & 12 deletions homeassistant/components/dexcom/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -78,7 +72,7 @@ class DexcomGlucoseValueSensor(DexcomSensorEntity):

def __init__(
self,
coordinator: DataUpdateCoordinator,
coordinator: DexcomCoordinator,
username: str,
entry_id: str,
) -> None:
Expand All @@ -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")
Expand Down

0 comments on commit f6b1786

Please sign in to comment.