Update dexcom to use CoordinatorEntity (#39464)
parent
c4f5a05b27
commit
1fbb158211
|
@ -1,6 +1,6 @@
|
|||
"""Support for Dexcom sensors."""
|
||||
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import COORDINATOR, DOMAIN, GLUCOSE_TREND_ICON, GLUCOSE_VALUE_ICON, MG_DL
|
||||
|
||||
|
@ -16,17 +16,17 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
async_add_entities(sensors, False)
|
||||
|
||||
|
||||
class DexcomGlucoseValueSensor(Entity):
|
||||
class DexcomGlucoseValueSensor(CoordinatorEntity):
|
||||
"""Representation of a Dexcom glucose value sensor."""
|
||||
|
||||
def __init__(self, coordinator, username, unit_of_measurement):
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
self._state = None
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
self._attribute_unit_of_measurement = (
|
||||
"mg_dl" if unit_of_measurement == MG_DL else "mmol_l"
|
||||
)
|
||||
self._coordinator = coordinator
|
||||
self._name = f"{DOMAIN}_{username}_glucose_value"
|
||||
self._unique_id = f"{username}-value"
|
||||
|
||||
|
@ -48,43 +48,23 @@ class DexcomGlucoseValueSensor(Entity):
|
|||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
if self._coordinator.data:
|
||||
return getattr(self._coordinator.data, self._attribute_unit_of_measurement)
|
||||
if self.coordinator.data:
|
||||
return getattr(self.coordinator.data, self._attribute_unit_of_measurement)
|
||||
return None
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return self._coordinator.last_update_success
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return False, updates are controlled via coordinator."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Device unique id."""
|
||||
return self._unique_id
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the latest state of the sensor."""
|
||||
await self._coordinator.async_request_refresh()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""When entity is added to hass."""
|
||||
self.async_on_remove(
|
||||
self._coordinator.async_add_listener(self.async_write_ha_state)
|
||||
)
|
||||
|
||||
|
||||
class DexcomGlucoseTrendSensor(Entity):
|
||||
class DexcomGlucoseTrendSensor(CoordinatorEntity):
|
||||
"""Representation of a Dexcom glucose trend sensor."""
|
||||
|
||||
def __init__(self, coordinator, username):
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
self._state = None
|
||||
self._coordinator = coordinator
|
||||
self._name = f"{DOMAIN}_{username}_glucose_trend"
|
||||
self._unique_id = f"{username}-trend"
|
||||
|
||||
|
@ -96,38 +76,18 @@ class DexcomGlucoseTrendSensor(Entity):
|
|||
@property
|
||||
def icon(self):
|
||||
"""Return the icon for the frontend."""
|
||||
if self._coordinator.data:
|
||||
return GLUCOSE_TREND_ICON[self._coordinator.data.trend]
|
||||
if self.coordinator.data:
|
||||
return GLUCOSE_TREND_ICON[self.coordinator.data.trend]
|
||||
return GLUCOSE_TREND_ICON[0]
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
if self._coordinator.data:
|
||||
return self._coordinator.data.trend_description
|
||||
if self.coordinator.data:
|
||||
return self.coordinator.data.trend_description
|
||||
return None
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return self._coordinator.last_update_success
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return False, updates are controlled via coordinator."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Device unique id."""
|
||||
return self._unique_id
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the latest state of the sensor."""
|
||||
await self._coordinator.async_request_refresh()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""When entity is added to hass."""
|
||||
self.async_on_remove(
|
||||
self._coordinator.async_add_listener(self.async_write_ha_state)
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue