core/homeassistant/components/dexcom/sensor.py

104 lines
3.2 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 SensorEntity
from homeassistant.config_entries import ConfigEntry
2020-07-02 00:14:54 +00:00
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
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
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
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."""
_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."""
_attr_translation_key = "glucose_value"
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")
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
def native_value(self):
2020-07-02 00:14:54 +00:00
"""Return the state of the sensor."""
if self.coordinator.data:
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."""
_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."""
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
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.trend_description
2020-07-02 00:14:54 +00:00
return None