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
|
|
|
|
|
2024-03-12 20:02:37 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
2024-11-15 11:13:21 +00:00
|
|
|
from homeassistant.const import CONF_USERNAME, UnitOfBloodGlucoseConcentration
|
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
|
2025-01-24 13:20:23 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-07-02 00:14:54 +00:00
|
|
|
|
2024-11-15 11:13:21 +00:00
|
|
|
from .const import DOMAIN
|
2025-01-24 14:14:05 +00:00
|
|
|
from .coordinator import DexcomConfigEntry, DexcomCoordinator
|
2024-03-12 20:02:37 +00:00
|
|
|
|
|
|
|
TRENDS = {
|
|
|
|
1: "rising_quickly",
|
|
|
|
2: "rising",
|
|
|
|
3: "rising_slightly",
|
|
|
|
4: "steady",
|
|
|
|
5: "falling_slightly",
|
|
|
|
6: "falling",
|
|
|
|
7: "falling_quickly",
|
|
|
|
}
|
2020-07-02 00:14:54 +00:00
|
|
|
|
|
|
|
|
2022-01-05 14:05:22 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
2025-01-24 14:14:05 +00:00
|
|
|
config_entry: DexcomConfigEntry,
|
2022-01-05 14:05:22 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-07-02 00:14:54 +00:00
|
|
|
"""Set up the Dexcom sensors."""
|
2025-01-24 14:14:05 +00:00
|
|
|
coordinator = config_entry.runtime_data
|
2020-07-02 00:14:54 +00:00
|
|
|
username = config_entry.data[CONF_USERNAME]
|
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),
|
2024-11-15 11:13:21 +00:00
|
|
|
DexcomGlucoseValueSensor(coordinator, username, config_entry.entry_id),
|
2023-06-26 16:22:44 +00:00
|
|
|
],
|
|
|
|
)
|
2020-07-02 00:14:54 +00:00
|
|
|
|
|
|
|
|
2025-01-24 13:20:23 +00:00
|
|
|
class DexcomSensorEntity(CoordinatorEntity[DexcomCoordinator], SensorEntity):
|
2023-08-16 17:18:46 +00:00
|
|
|
"""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__(
|
2024-03-12 20:02:37 +00:00
|
|
|
self,
|
2025-01-24 13:20:23 +00:00
|
|
|
coordinator: DexcomCoordinator,
|
2024-03-12 20:02:37 +00:00
|
|
|
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."""
|
|
|
|
|
2024-11-15 11:13:21 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION
|
|
|
|
_attr_native_unit_of_measurement = (
|
|
|
|
UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER
|
|
|
|
)
|
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,
|
2025-01-24 13:20:23 +00:00
|
|
|
coordinator: DexcomCoordinator,
|
2023-08-16 17:18:46 +00:00
|
|
|
username: str,
|
2023-08-22 07:20:30 +00:00
|
|
|
entry_id: str,
|
2023-08-16 17:18:46 +00:00
|
|
|
) -> 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")
|
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:
|
2024-11-15 11:13:21 +00:00
|
|
|
return self.coordinator.data.mg_dl
|
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"
|
2024-03-12 20:02:37 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.ENUM
|
|
|
|
_attr_options = list(TRENDS.values())
|
2023-08-22 20:09:18 +00:00
|
|
|
|
2023-08-22 07:20:30 +00:00
|
|
|
def __init__(
|
2025-01-24 13:20:23 +00:00
|
|
|
self, coordinator: DexcomCoordinator, username: str, entry_id: str
|
2023-08-22 07:20:30 +00:00
|
|
|
) -> 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
|
2024-03-12 20:02:37 +00:00
|
|
|
def native_value(self) -> str | None:
|
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:
|
2024-03-12 20:02:37 +00:00
|
|
|
return TRENDS.get(self.coordinator.data.trend)
|
2020-07-02 00:14:54 +00:00
|
|
|
return None
|
2024-03-12 20:02:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if entity is available."""
|
|
|
|
return super().available and (
|
|
|
|
self.coordinator.data is None or self.coordinator.data.trend != 9
|
|
|
|
)
|