core/homeassistant/components/dexcom/sensor.py

115 lines
3.4 KiB
Python
Raw Normal View History

2020-07-02 00:14:54 +00:00
"""Support for Dexcom sensors."""
from __future__ import annotations
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.const import CONF_USERNAME, UnitOfBloodGlucoseConcentration
from homeassistant.core import HomeAssistant
2023-08-22 07:20:30 +00:00
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2020-07-02 00:14:54 +00:00
from .const import DOMAIN
2025-01-24 14:14:05 +00:00
from .coordinator import DexcomConfigEntry, DexcomCoordinator
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
async def async_setup_entry(
hass: HomeAssistant,
2025-01-24 14:14:05 +00:00
config_entry: DexcomConfigEntry,
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),
DexcomGlucoseValueSensor(coordinator, username, config_entry.entry_id),
2023-06-26 16:22:44 +00:00
],
)
2020-07-02 00:14:54 +00:00
class DexcomSensorEntity(CoordinatorEntity[DexcomCoordinator], SensorEntity):
2023-08-16 17:18:46 +00:00
"""Base Dexcom sensor entity."""
_attr_has_entity_name = True
2023-08-16 17:18:46 +00:00
def __init__(
self,
coordinator: DexcomCoordinator,
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."""
_attr_device_class = SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION
_attr_native_unit_of_measurement = (
UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER
)
_attr_translation_key = "glucose_value"
2023-08-16 17:18:46 +00:00
def __init__(
self,
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
def native_value(self):
2020-07-02 00:14:54 +00:00
"""Return the state of the sensor."""
if self.coordinator.data:
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."""
_attr_translation_key = "glucose_trend"
_attr_device_class = SensorDeviceClass.ENUM
_attr_options = list(TRENDS.values())
2023-08-22 07:20:30 +00:00
def __init__(
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
def native_value(self) -> str | None:
2020-07-02 00:14:54 +00:00
"""Return the state of the sensor."""
if self.coordinator.data:
return TRENDS.get(self.coordinator.data.trend)
2020-07-02 00:14:54 +00:00
return None
@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
)