2020-07-02 00:14:54 +00:00
|
|
|
"""Support for Dexcom sensors."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2022-01-05 14:05:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2022-01-05 14:05:22 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-07-02 00:14:54 +00:00
|
|
|
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
|
2022-01-05 14:05:22 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-22 07:20:30 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2022-01-05 14:05:22 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-08-16 17:18:46 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2020-07-02 00:14:54 +00:00
|
|
|
|
2024-03-05 20:36:11 +00:00
|
|
|
from .const import DOMAIN, GLUCOSE_TREND_ICON, MG_DL
|
2020-07-02 00:14:54 +00:00
|
|
|
|
|
|
|
|
2022-01-05 14:05:22 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-07-02 00:14:54 +00:00
|
|
|
"""Set up the Dexcom sensors."""
|
2024-03-05 20:36:11 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
2020-07-02 00:14:54 +00:00
|
|
|
username = config_entry.data[CONF_USERNAME]
|
|
|
|
unit_of_measurement = config_entry.options[CONF_UNIT_OF_MEASUREMENT]
|
2023-06-26 16:22:44 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
2023-08-22 07:20:30 +00:00
|
|
|
DexcomGlucoseTrendSensor(coordinator, username, config_entry.entry_id),
|
|
|
|
DexcomGlucoseValueSensor(
|
|
|
|
coordinator, username, config_entry.entry_id, unit_of_measurement
|
|
|
|
),
|
2023-06-26 16:22:44 +00:00
|
|
|
],
|
|
|
|
)
|
2020-07-02 00:14:54 +00:00
|
|
|
|
|
|
|
|
2023-08-16 17:18:46 +00:00
|
|
|
class DexcomSensorEntity(CoordinatorEntity, SensorEntity):
|
|
|
|
"""Base Dexcom sensor entity."""
|
|
|
|
|
2023-08-22 20:09:18 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2023-08-16 17:18:46 +00:00
|
|
|
def __init__(
|
2023-08-22 07:20:30 +00:00
|
|
|
self, coordinator: DataUpdateCoordinator, username: str, entry_id: str, key: str
|
2023-08-16 17:18:46 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self._attr_unique_id = f"{username}-{key}"
|
2023-08-22 07:20:30 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, entry_id)},
|
|
|
|
name=username,
|
|
|
|
)
|
2023-08-16 17:18:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DexcomGlucoseValueSensor(DexcomSensorEntity):
|
2020-07-02 00:14:54 +00:00
|
|
|
"""Representation of a Dexcom glucose value sensor."""
|
|
|
|
|
2023-08-22 20:09:18 +00:00
|
|
|
_attr_translation_key = "glucose_value"
|
2023-06-27 08:17:09 +00:00
|
|
|
|
2023-08-16 17:18:46 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
username: str,
|
2023-08-22 07:20:30 +00:00
|
|
|
entry_id: str,
|
2023-08-16 17:18:46 +00:00
|
|
|
unit_of_measurement: str,
|
|
|
|
) -> None:
|
2020-07-02 00:14:54 +00:00
|
|
|
"""Initialize the sensor."""
|
2023-08-22 07:20:30 +00:00
|
|
|
super().__init__(coordinator, username, entry_id, "value")
|
2023-06-27 08:17:09 +00:00
|
|
|
self._attr_native_unit_of_measurement = unit_of_measurement
|
|
|
|
self._key = "mg_dl" if unit_of_measurement == MG_DL else "mmol_l"
|
2020-07-02 00:14:54 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:12 +00:00
|
|
|
def native_value(self):
|
2020-07-02 00:14:54 +00:00
|
|
|
"""Return the state of the sensor."""
|
2020-08-30 18:20:01 +00:00
|
|
|
if self.coordinator.data:
|
2023-06-27 08:17:09 +00:00
|
|
|
return getattr(self.coordinator.data, self._key)
|
2020-07-02 00:14:54 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-08-16 17:18:46 +00:00
|
|
|
class DexcomGlucoseTrendSensor(DexcomSensorEntity):
|
2020-07-02 00:14:54 +00:00
|
|
|
"""Representation of a Dexcom glucose trend sensor."""
|
|
|
|
|
2023-08-22 20:09:18 +00:00
|
|
|
_attr_translation_key = "glucose_trend"
|
|
|
|
|
2023-08-22 07:20:30 +00:00
|
|
|
def __init__(
|
|
|
|
self, coordinator: DataUpdateCoordinator, username: str, entry_id: str
|
|
|
|
) -> None:
|
2020-07-02 00:14:54 +00:00
|
|
|
"""Initialize the sensor."""
|
2023-08-22 07:20:30 +00:00
|
|
|
super().__init__(coordinator, username, entry_id, "trend")
|
2020-07-02 00:14:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon for the frontend."""
|
2020-08-30 18:20:01 +00:00
|
|
|
if self.coordinator.data:
|
|
|
|
return GLUCOSE_TREND_ICON[self.coordinator.data.trend]
|
2020-07-02 00:14:54 +00:00
|
|
|
return GLUCOSE_TREND_ICON[0]
|
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:12 +00:00
|
|
|
def native_value(self):
|
2020-07-02 00:14:54 +00:00
|
|
|
"""Return the state of the sensor."""
|
2020-08-30 18:20:01 +00:00
|
|
|
if self.coordinator.data:
|
|
|
|
return self.coordinator.data.trend_description
|
2020-07-02 00:14:54 +00:00
|
|
|
return None
|