mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +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."""
|
"""The Dexcom integration."""
|
||||||
|
|
||||||
from datetime import timedelta
|
from pydexcom import AccountError, Dexcom, SessionError
|
||||||
import logging
|
|
||||||
|
|
||||||
from pydexcom import AccountError, Dexcom, GlucoseReading, SessionError
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
||||||
|
|
||||||
from .const import CONF_SERVER, DOMAIN, PLATFORMS, SERVER_OUS
|
from .const import CONF_SERVER, DOMAIN, PLATFORMS, SERVER_OUS
|
||||||
|
from .coordinator import DexcomCoordinator
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=180)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
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:
|
except SessionError as error:
|
||||||
raise ConfigEntryNotReady from error
|
raise ConfigEntryNotReady from error
|
||||||
|
|
||||||
async def async_update_data():
|
coordinator = DexcomCoordinator(hass, entry=entry, dexcom=dexcom)
|
||||||
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,
|
|
||||||
)
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
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 __future__ import annotations
|
||||||
|
|
||||||
from pydexcom import GlucoseReading
|
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_USERNAME, UnitOfBloodGlucoseConcentration
|
from homeassistant.const import CONF_USERNAME, UnitOfBloodGlucoseConcentration
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
CoordinatorEntity,
|
|
||||||
DataUpdateCoordinator,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import DexcomCoordinator
|
||||||
|
|
||||||
TRENDS = {
|
TRENDS = {
|
||||||
1: "rising_quickly",
|
1: "rising_quickly",
|
||||||
@ -44,16 +40,14 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class DexcomSensorEntity(
|
class DexcomSensorEntity(CoordinatorEntity[DexcomCoordinator], SensorEntity):
|
||||||
CoordinatorEntity[DataUpdateCoordinator[GlucoseReading]], SensorEntity
|
|
||||||
):
|
|
||||||
"""Base Dexcom sensor entity."""
|
"""Base Dexcom sensor entity."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator[GlucoseReading],
|
coordinator: DexcomCoordinator,
|
||||||
username: str,
|
username: str,
|
||||||
entry_id: str,
|
entry_id: str,
|
||||||
key: str,
|
key: str,
|
||||||
@ -78,7 +72,7 @@ class DexcomGlucoseValueSensor(DexcomSensorEntity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DexcomCoordinator,
|
||||||
username: str,
|
username: str,
|
||||||
entry_id: str,
|
entry_id: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -101,7 +95,7 @@ class DexcomGlucoseTrendSensor(DexcomSensorEntity):
|
|||||||
_attr_options = list(TRENDS.values())
|
_attr_options = list(TRENDS.values())
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, coordinator: DataUpdateCoordinator, username: str, entry_id: str
|
self, coordinator: DexcomCoordinator, username: str, entry_id: str
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator, username, entry_id, "trend")
|
super().__init__(coordinator, username, entry_id, "trend")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user