Add base entity for Dexcom (#98158)

This commit is contained in:
Joost Lekkerkerker 2023-08-16 19:18:46 +02:00 committed by GitHub
parent 3e1d2a1000
commit b9203cbeaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,10 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from .const import COORDINATOR, DOMAIN, GLUCOSE_TREND_ICON, GLUCOSE_VALUE_ICON, MG_DL
@ -29,18 +32,33 @@ async def async_setup_entry(
)
class DexcomGlucoseValueSensor(CoordinatorEntity, SensorEntity):
class DexcomSensorEntity(CoordinatorEntity, SensorEntity):
"""Base Dexcom sensor entity."""
def __init__(
self, coordinator: DataUpdateCoordinator, username: str, key: str
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._attr_unique_id = f"{username}-{key}"
class DexcomGlucoseValueSensor(DexcomSensorEntity):
"""Representation of a Dexcom glucose value sensor."""
_attr_icon = GLUCOSE_VALUE_ICON
def __init__(self, coordinator, username, unit_of_measurement):
def __init__(
self,
coordinator: DataUpdateCoordinator,
username: str,
unit_of_measurement: str,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
super().__init__(coordinator, username, "value")
self._attr_native_unit_of_measurement = unit_of_measurement
self._key = "mg_dl" if unit_of_measurement == MG_DL else "mmol_l"
self._attr_name = f"{DOMAIN}_{username}_glucose_value"
self._attr_unique_id = f"{username}-value"
@property
def native_value(self):
@ -50,14 +68,13 @@ class DexcomGlucoseValueSensor(CoordinatorEntity, SensorEntity):
return None
class DexcomGlucoseTrendSensor(CoordinatorEntity, SensorEntity):
class DexcomGlucoseTrendSensor(DexcomSensorEntity):
"""Representation of a Dexcom glucose trend sensor."""
def __init__(self, coordinator, username):
def __init__(self, coordinator: DataUpdateCoordinator, username: str) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
super().__init__(coordinator, username, "trend")
self._attr_name = f"{DOMAIN}_{username}_glucose_trend"
self._attr_unique_id = f"{username}-trend"
@property
def icon(self):