Use enum device class in Dexcom (#112423)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Joost Lekkerkerker
2024-03-12 21:02:37 +01:00
committed by GitHub
parent c761b825ec
commit 78ea9bf681
5 changed files with 57 additions and 29 deletions

View File

@@ -2,7 +2,9 @@
from __future__ import annotations
from homeassistant.components.sensor import SensorEntity
from pydexcom import GlucoseReading
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
from homeassistant.core import HomeAssistant
@@ -13,7 +15,17 @@ from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
)
from .const import DOMAIN, GLUCOSE_TREND_ICON, MG_DL
from .const import DOMAIN, MG_DL
TRENDS = {
1: "rising_quickly",
2: "rising",
3: "rising_slightly",
4: "steady",
5: "falling_slightly",
6: "falling",
7: "falling_quickly",
}
async def async_setup_entry(
@@ -35,13 +47,19 @@ async def async_setup_entry(
)
class DexcomSensorEntity(CoordinatorEntity, SensorEntity):
class DexcomSensorEntity(
CoordinatorEntity[DataUpdateCoordinator[GlucoseReading]], SensorEntity
):
"""Base Dexcom sensor entity."""
_attr_has_entity_name = True
def __init__(
self, coordinator: DataUpdateCoordinator, username: str, entry_id: str, key: str
self,
coordinator: DataUpdateCoordinator[GlucoseReading],
username: str,
entry_id: str,
key: str,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
@@ -81,6 +99,8 @@ class DexcomGlucoseTrendSensor(DexcomSensorEntity):
"""Representation of a Dexcom glucose trend sensor."""
_attr_translation_key = "glucose_trend"
_attr_device_class = SensorDeviceClass.ENUM
_attr_options = list(TRENDS.values())
def __init__(
self, coordinator: DataUpdateCoordinator, username: str, entry_id: str
@@ -89,15 +109,15 @@ class DexcomGlucoseTrendSensor(DexcomSensorEntity):
super().__init__(coordinator, username, entry_id, "trend")
@property
def icon(self):
"""Return the icon for the frontend."""
if self.coordinator.data:
return GLUCOSE_TREND_ICON[self.coordinator.data.trend]
return GLUCOSE_TREND_ICON[0]
@property
def native_value(self):
def native_value(self) -> str | None:
"""Return the state of the sensor."""
if self.coordinator.data:
return self.coordinator.data.trend_description
return TRENDS.get(self.coordinator.data.trend)
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
)