Add device to Dexcom (#98574)

This commit is contained in:
Joost Lekkerkerker 2023-08-22 09:20:30 +02:00 committed by GitHub
parent c93fcc37c4
commit 0f2b8570d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@ from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
@ -25,8 +26,10 @@ async def async_setup_entry(
unit_of_measurement = config_entry.options[CONF_UNIT_OF_MEASUREMENT] unit_of_measurement = config_entry.options[CONF_UNIT_OF_MEASUREMENT]
async_add_entities( async_add_entities(
[ [
DexcomGlucoseTrendSensor(coordinator, username), DexcomGlucoseTrendSensor(coordinator, username, config_entry.entry_id),
DexcomGlucoseValueSensor(coordinator, username, unit_of_measurement), DexcomGlucoseValueSensor(
coordinator, username, config_entry.entry_id, unit_of_measurement
),
], ],
False, False,
) )
@ -36,11 +39,15 @@ class DexcomSensorEntity(CoordinatorEntity, SensorEntity):
"""Base Dexcom sensor entity.""" """Base Dexcom sensor entity."""
def __init__( def __init__(
self, coordinator: DataUpdateCoordinator, username: str, key: str self, coordinator: DataUpdateCoordinator, username: str, entry_id: str, key: str
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self._attr_unique_id = f"{username}-{key}" self._attr_unique_id = f"{username}-{key}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, entry_id)},
name=username,
)
class DexcomGlucoseValueSensor(DexcomSensorEntity): class DexcomGlucoseValueSensor(DexcomSensorEntity):
@ -52,10 +59,11 @@ class DexcomGlucoseValueSensor(DexcomSensorEntity):
self, self,
coordinator: DataUpdateCoordinator, coordinator: DataUpdateCoordinator,
username: str, username: str,
entry_id: str,
unit_of_measurement: str, unit_of_measurement: str,
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(coordinator, username, "value") super().__init__(coordinator, username, entry_id, "value")
self._attr_native_unit_of_measurement = unit_of_measurement self._attr_native_unit_of_measurement = unit_of_measurement
self._key = "mg_dl" if unit_of_measurement == MG_DL else "mmol_l" self._key = "mg_dl" if unit_of_measurement == MG_DL else "mmol_l"
self._attr_name = f"{DOMAIN}_{username}_glucose_value" self._attr_name = f"{DOMAIN}_{username}_glucose_value"
@ -71,9 +79,11 @@ class DexcomGlucoseValueSensor(DexcomSensorEntity):
class DexcomGlucoseTrendSensor(DexcomSensorEntity): class DexcomGlucoseTrendSensor(DexcomSensorEntity):
"""Representation of a Dexcom glucose trend sensor.""" """Representation of a Dexcom glucose trend sensor."""
def __init__(self, coordinator: DataUpdateCoordinator, username: str) -> None: def __init__(
self, coordinator: DataUpdateCoordinator, username: str, entry_id: str
) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(coordinator, username, "trend") super().__init__(coordinator, username, entry_id, "trend")
self._attr_name = f"{DOMAIN}_{username}_glucose_trend" self._attr_name = f"{DOMAIN}_{username}_glucose_trend"
@property @property