1 """Support for Nightscout sensors."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 from aiohttp
import ClientError
10 from py_nightscout
import Api
as NightscoutAPI
18 from .const
import ATTR_DELTA, ATTR_DEVICE, ATTR_DIRECTION, DOMAIN
22 _LOGGER = logging.getLogger(__name__)
24 DEFAULT_NAME =
"Blood Glucose"
30 async_add_entities: AddEntitiesCallback,
32 """Set up the Glucose Sensor."""
33 api = hass.data[DOMAIN][entry.entry_id]
38 """Implementation of a Nightscout sensor."""
40 _attr_device_class = SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION
41 _attr_native_unit_of_measurement = (
42 UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER
44 _attr_icon =
"mdi:cloud-question"
46 def __init__(self, api: NightscoutAPI, name: str, unique_id: str |
None) ->
None:
47 """Initialize the Nightscout sensor."""
54 """Fetch the latest data from Nightscout REST API and update the state."""
56 values = await self.
apiapi.get_sgvs()
57 except (ClientError, TimeoutError, OSError)
as error:
58 _LOGGER.error(
"Error fetching data. Failed with %s", error)
68 ATTR_DEVICE: value.device,
69 ATTR_DATE: value.date,
70 ATTR_DELTA: value.delta,
71 ATTR_DIRECTION: value.direction,
77 _LOGGER.warning(
"Empty reply found when expecting JSON data")
80 """Update the icon based on the direction attribute."""
82 "Flat":
"mdi:arrow-right",
83 "SingleDown":
"mdi:arrow-down",
84 "FortyFiveDown":
"mdi:arrow-bottom-right",
85 "DoubleDown":
"mdi:chevron-triple-down",
86 "SingleUp":
"mdi:arrow-up",
87 "FortyFiveUp":
"mdi:arrow-top-right",
88 "DoubleUp":
"mdi:chevron-triple-up",
90 return switcher.get(direction,
"mdi:cloud-question")
str _parse_icon(self, str direction)
_attr_extra_state_attributes
None __init__(self, NightscoutAPI api, str name, str|None unique_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)