diff --git a/homeassistant/components/dexcom/__init__.py b/homeassistant/components/dexcom/__init__.py index 2053f266eb5..5ff95fae47e 100644 --- a/homeassistant/components/dexcom/__init__.py +++ b/homeassistant/components/dexcom/__init__.py @@ -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, diff --git a/homeassistant/components/dexcom/const.py b/homeassistant/components/dexcom/const.py index 25abb1bed26..487a844eb2b 100644 --- a/homeassistant/components/dexcom/const.py +++ b/homeassistant/components/dexcom/const.py @@ -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" diff --git a/homeassistant/components/dexcom/icons.json b/homeassistant/components/dexcom/icons.json index 9d0b3534e17..de8355ce861 100644 --- a/homeassistant/components/dexcom/icons.json +++ b/homeassistant/components/dexcom/icons.json @@ -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" + } } } } diff --git a/homeassistant/components/dexcom/sensor.py b/homeassistant/components/dexcom/sensor.py index 4c2e46ba06c..10b30f39fcb 100644 --- a/homeassistant/components/dexcom/sensor.py +++ b/homeassistant/components/dexcom/sensor.py @@ -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 + ) diff --git a/homeassistant/components/dexcom/strings.json b/homeassistant/components/dexcom/strings.json index 7efc2708bcc..91b5725d918 100644 --- a/homeassistant/components/dexcom/strings.json +++ b/homeassistant/components/dexcom/strings.json @@ -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" + } } } }