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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 57 additions and 29 deletions

View File

@ -3,7 +3,7 @@
from datetime import timedelta
import logging
from pydexcom import AccountError, Dexcom, SessionError
from pydexcom import AccountError, Dexcom, GlucoseReading, SessionError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
@ -43,7 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except SessionError as error:
raise UpdateFailed(error) from error
coordinator = DataUpdateCoordinator(
coordinator = DataUpdateCoordinator[GlucoseReading](
hass,
_LOGGER,
name=DOMAIN,

View File

@ -5,19 +5,6 @@ from homeassistant.const import Platform
DOMAIN = "dexcom"
PLATFORMS = [Platform.SENSOR]
GLUCOSE_TREND_ICON = [
"mdi:help",
"mdi:arrow-up-thick",
"mdi:arrow-up",
"mdi:arrow-top-right",
"mdi:arrow-right",
"mdi:arrow-bottom-right",
"mdi:arrow-down",
"mdi:arrow-down-thick",
"mdi:help",
"mdi:alert-circle-outline",
]
MMOL_L = "mmol/L"
MG_DL = "mg/dL"

View File

@ -3,6 +3,18 @@
"sensor": {
"glucose_value": {
"default": "mdi:diabetes"
},
"glucose_trend": {
"default": "mdi:help",
"state": {
"rising_quickly": "mdi:arrow-up-thick",
"rising": "mdi:arrow-up",
"rising_slightly": "mdi:arrow-top-right",
"steady": "mdi:arrow-right",
"falling_slightly": "mdi:arrow-bottom-right",
"falling": "mdi:arrow-down",
"falling_quickly": "mdi:arrow-down-thick"
}
}
}
}

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
)

View File

@ -35,7 +35,16 @@
"name": "Glucose value"
},
"glucose_trend": {
"name": "Glucose trend"
"name": "Glucose trend",
"state": {
"rising_quickly": "Rising quickly",
"rising": "Rising",
"rising_slightly": "Rising slightly",
"steady": "Steady",
"falling_slightly": "Falling slightly",
"falling": "Falling",
"falling_quickly": "Falling quickly"
}
}
}
}