Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: more sensors #6

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions custom_components/carelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
SENSOR_KEY_RESERVOIR_LEVEL,
SENSOR_KEY_RESERVOIR_AMOUNT,
SENSOR_KEY_RESERVOIR_REMAINING_UNITS,
SENSOR_KEY_ACTIVE_INSULIN,
SENSOR_KEY_ACTIVE_INSULIN_ATTRS,
SENSOR_KEY_LAST_ALARM,
SENSOR_KEY_LAST_ALARM_ATTRS,
SENSOR_KEY_ACTIVE_BASAL_PATTERN,
SENSOR_KEY_AVG_GLUCOSE,
SENSOR_KEY_BELOW_HYPO_LIMIT,
SENSOR_KEY_ABOVE_HYPER_LIMIT,
SENSOR_KEY_TIME_IN_RANGE,
SENSOR_KEY_MAX_AUTO_BASAL_RATE,
SENSOR_KEY_SG_BELOW_LIMIT,
BINARY_SENSOR_KEY_PUMP_COMM_STATE,
BINARY_SENSOR_KEY_SENSOR_COMM_STATE,
BINARY_SENSOR_KEY_CONDUIT_IN_RANGE,
Expand Down Expand Up @@ -109,6 +120,10 @@ async def _async_update_data(self):
TIMEZONE = ZoneInfo(TIMEZONE_MAP)

recent_data["lastSG"] = recent_data.setdefault("lastSG", {})
recent_data["activeInsulin"] = recent_data.setdefault(
"activeInsulin", {})
recent_data["basel"] = recent_data.setdefault("basel", {})
recent_data["lastAlarm"] = recent_data.setdefault("lastAlarm", {})

if "datetime" in recent_data["lastSG"]:
# Last Glucose level sensors
Expand Down Expand Up @@ -154,6 +169,54 @@ async def _async_update_data(self):
data[SENSOR_KEY_LASTSG_TREND] = recent_data.setdefault(
"lastSGTrend", UNAVAILABLE)

if "amount" in recent_data["activeInsulin"]:
# Active insuline sensor

activeInsulin = recent_data["activeInsulin"]

date_time_local = datetime.strptime(
activeInsulin["datetime"], "%Y-%m-%dT%H:%M:%S.%fZ"
).replace(tzinfo=None)

data[SENSOR_KEY_ACTIVE_INSULIN] = recent_data["activeInsulin"].setdefault(
"amount", UNAVAILABLE)
data[SENSOR_KEY_ACTIVE_INSULIN_ATTRS] = {
"last_update": date_time_local.replace(tzinfo=TIMEZONE)}
else:
data[SENSOR_KEY_ACTIVE_INSULIN] = None
data[SENSOR_KEY_ACTIVE_INSULIN_ATTRS] = {}

if "datetime" in recent_data["lastAlarm"]:
# Last alarm sensor

lastAlarm = recent_data["lastAlarm"]

date_time_local = datetime.strptime(
lastAlarm["datetime"], "%Y-%m-%dT%H:%M:%S.000-00:00"
).replace(tzinfo=None)

data[SENSOR_KEY_LAST_ALARM] = date_time_local.replace(
tzinfo=TIMEZONE)
data[SENSOR_KEY_LAST_ALARM_ATTRS] = lastAlarm
else:
data[SENSOR_KEY_ACTIVE_INSULIN] = None
data[SENSOR_KEY_ACTIVE_INSULIN_ATTRS] = {}

data[SENSOR_KEY_ACTIVE_BASAL_PATTERN] = recent_data["basel"].setdefault(
"activeBasalPattern", UNAVAILABLE)
data[SENSOR_KEY_AVG_GLUCOSE] = recent_data.setdefault(
"averageSG", UNAVAILABLE)
data[SENSOR_KEY_BELOW_HYPO_LIMIT] = recent_data.setdefault(
"belowHypoLimit", UNAVAILABLE)
data[SENSOR_KEY_ABOVE_HYPER_LIMIT] = recent_data.setdefault(
"aboveHyperLimit", UNAVAILABLE)
data[SENSOR_KEY_TIME_IN_RANGE] = recent_data.setdefault(
"timeInRange", UNAVAILABLE)
data[SENSOR_KEY_MAX_AUTO_BASAL_RATE] = recent_data.setdefault(
"maxAutoBasalRate", UNAVAILABLE)
data[SENSOR_KEY_SG_BELOW_LIMIT] = recent_data.setdefault(
"sgBelowLimit", UNAVAILABLE)

# Binary Sensors

data[BINARY_SENSOR_KEY_PUMP_COMM_STATE] = recent_data.setdefault(
Expand Down
101 changes: 101 additions & 0 deletions custom_components/carelink/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
SENSOR_KEY_RESERVOIR_LEVEL = "reservoir_level"
SENSOR_KEY_RESERVOIR_AMOUNT = "reservoir_amount"
SENSOR_KEY_RESERVOIR_REMAINING_UNITS = "reservoir_remaining_units"
SENSOR_KEY_ACTIVE_INSULIN = "active_insuline"
SENSOR_KEY_ACTIVE_INSULIN_ATTRS = "active_insuline_attributes"
SENSOR_KEY_LAST_ALARM = "last_alarm"
SENSOR_KEY_LAST_ALARM_ATTRS = "last_alarm_attributes"
SENSOR_KEY_ACTIVE_BASAL_PATTERN = "active_basel_pattern"
SENSOR_KEY_AVG_GLUCOSE = "average_glucose_level"
SENSOR_KEY_BELOW_HYPO_LIMIT = "below_hypo_limit"
SENSOR_KEY_ABOVE_HYPER_LIMIT = "above_hyper_limit"
SENSOR_KEY_TIME_IN_RANGE = "time_in_range"
SENSOR_KEY_MAX_AUTO_BASAL_RATE = "max_auto_basel_rate"
SENSOR_KEY_SG_BELOW_LIMIT = "sg_below_limit"

BINARY_SENSOR_KEY_PUMP_COMM_STATE = "binary_sensor_pump_comm_state"
BINARY_SENSOR_KEY_SENSOR_COMM_STATE = "binary_sensor_sensor_comm_state"
Expand Down Expand Up @@ -160,6 +171,96 @@
icon="mdi:medication",
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_ACTIVE_INSULIN,
name="Active insuline",
native_unit_of_measurement=None,
state_class=None,
device_class=None,
icon="mdi:water-alert",
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_LAST_ALARM,
name="Last alarm",
native_unit_of_measurement=None,
state_class=None,
device_class=None,
icon="mdi:clock",
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_ACTIVE_BASAL_PATTERN,
name="Active basel pattern",
native_unit_of_measurement=None,
state_class=None,
device_class=None,
icon="mdi:chart-line",
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_AVG_GLUCOSE,
name="Average glucose level mmol",
native_unit_of_measurement=MMOL,
state_class=SensorStateClass.MEASUREMENT,
device_class=MOLAR_CONCENTRATION,
icon="mdi:chart-line",
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_AVG_GLUCOSE,
name="Average glucose level mg/dl",
native_unit_of_measurement=MGDL,
state_class=SensorStateClass.MEASUREMENT,
device_class=MOLAR_CONCENTRATION,
icon="mdi:chart-line",
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_BELOW_HYPO_LIMIT,
name="Below hypo limit",
native_unit_of_measurement=PERCENT,
state_class=SensorStateClass.TOTAL,
device_class=None,
icon=None,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_ABOVE_HYPER_LIMIT,
name="Above hyper limit",
native_unit_of_measurement=PERCENT,
state_class=SensorStateClass.TOTAL,
device_class=None,
icon=None,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_TIME_IN_RANGE,
name="Time in range",
native_unit_of_measurement=PERCENT,
state_class=SensorStateClass.TOTAL,
device_class=None,
icon=None,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_MAX_AUTO_BASAL_RATE,
name="Max auto basel rate",
native_unit_of_measurement=None,
state_class=None,
device_class=None,
icon=None,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_SG_BELOW_LIMIT,
name="Glucose below limit",
native_unit_of_measurement=None,
state_class=None,
device_class=None,
icon=None,
entity_category=None,
),
)

BINARY_SENSORS = (
Expand Down
6 changes: 6 additions & 0 deletions custom_components/carelink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,9 @@ def device_info(self) -> DeviceInfo:
@property
def entity_category(self):
return self.sensor_description.entity_category

@property
def extra_state_attributes(self):
attrKey = "{}_attributes".format(self.sensor_description.key)

return self.coordinator.data.setdefault(attrKey, {})