Skip to content

Commit

Permalink
restoring coordinator in sensors
Browse files Browse the repository at this point in the history
Iterating with only part of the coordinator result end-up in loosing the refresh
  • Loading branch information
gillesvs committed Feb 3, 2024
1 parent 2ba1200 commit 5de87f1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
9 changes: 6 additions & 3 deletions custom_components/librelink/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async def async_setup_entry(

# to manage multiple patients, the API return an array of patients in "data". So we loop in the array
# and create as many devices and sensors as we do have patients.
for patients in coordinator.data["data"]:

for index, patients in enumerate(coordinator.data["data"]):
patient = patients["firstName"] + " " + patients["lastName"]
patientId = patients["patientId"]
# print(f"patient : {patient}")
Expand All @@ -56,6 +56,7 @@ async def async_setup_entry(
patients,
patientId,
patient,
index,
config_entry.entry_id,
entity_description,
)
Expand All @@ -72,19 +73,21 @@ def __init__(
patients,
patientId: str,
patient: str,
index: int,
entry_id,
description: BinarySensorEntityDescription,
) -> None:
"""Initialize the binary_sensor class."""
super().__init__(coordinator, patientId, patient, entry_id, description.key)
self.entity_description = description
self.patients = patients
self.index = index

# define state based on the entity_description key
@property
def is_on(self) -> bool:
"""Return true if the binary_sensor is on."""
return self.patients["glucoseMeasurement"][
return self.coordinator.data["data"][self.index]["glucoseMeasurement"][
self.entity_description.key
]

2 changes: 1 addition & 1 deletion custom_components/librelink/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

NAME = "LibreLink"
DOMAIN = "librelink"
VERSION = "1.1.3"
VERSION = "1.1.5"
ATTRIBUTION = "Data provided by https://libreview.com"
LOGIN_URL = "https://api.libreview.io/llu/auth/login"
CONNECTION_URL = "https://api.libreview.io/llu/connections"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/librelink/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"documentation": "https://github.com/gillesvs/librelink",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/gillesvs/librelink/issues",
"version": "1.1.3"
"version": "1.1.5"
}
38 changes: 22 additions & 16 deletions custom_components/librelink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ async def async_setup_entry(
except KeyError:
custom_unit = MG_DL

# Starts here, for each patients, new entity base on patients and not user
# Starts here, for each patients, new entity base on patients and not user
# using an index as we need to keep the coordinator in the @property to get updates from coordinator

for index, patients in enumerate(coordinator.data["data"]):
# print(index, patients)

for patients in coordinator.data["data"]:
# for patients in coordinator.data["data"]:
patient = patients["firstName"] + " " + patients["lastName"]
patientId = patients["patientId"]
print(f"patient : {patient}")
# print(f"patient : {patient}")


# I add my three sensors all instantiating a new class LibreLinSensor
Expand All @@ -88,6 +91,7 @@ async def async_setup_entry(
patients,
patientId,
patient,
index,
config_entry.entry_id,
custom_unit,
entity_description,
Expand All @@ -106,6 +110,7 @@ def __init__(
patients,
patientId: str,
patient: str,
index: int,
entry_id: str,
uom: str,
description: SensorEntityDescription,
Expand All @@ -114,6 +119,7 @@ def __init__(
super().__init__(coordinator, patientId, patient, entry_id, description.key)
self.entity_description = description
self.uom = uom
self.index = index
self.patients = patients

@property
Expand All @@ -127,7 +133,7 @@ def native_value(self):
if self.uom == MG_DL:
result = int(
(
self.patients["glucoseMeasurement"][
self.coordinator.data["data"][self.index]["glucoseMeasurement"][
"ValueInMgPerDl"
]
)
Expand All @@ -136,7 +142,7 @@ def native_value(self):
result = round(
float(
(
self.patients["glucoseMeasurement"][
self.coordinator.data["data"][self.index]["glucoseMeasurement"][
"ValueInMgPerDl"
]
/ MMOL_DL_TO_MG_DL
Expand All @@ -148,7 +154,7 @@ def native_value(self):
elif self.entity_description.key == "trend":
result = GLUCOSE_TREND_MESSAGE[
(
self.patients["glucoseMeasurement"][
self.coordinator.data["data"][self.index]["glucoseMeasurement"][
"TrendArrow"
]
)
Expand All @@ -157,7 +163,7 @@ def native_value(self):

elif self.entity_description.key == "sensor":
result = int(
(time.time() - (self.patients["sensor"]["a"]))
(time.time() - (self.coordinator.data["data"][self.index]["sensor"]["a"]))
/ 86400
)

Expand All @@ -166,7 +172,7 @@ def native_value(self):
(
datetime.now()
- datetime.strptime(
self.patients["glucoseMeasurement"][
self.coordinator.data["data"][self.index]["glucoseMeasurement"][
"Timestamp"
],
"%m/%d/%Y %I:%M:%S %p",
Expand All @@ -182,11 +188,11 @@ def native_value(self):
def icon(self):
"""Return the icon for the frontend."""

if self.patients:
if self.coordinator.data["data"][self.index]:
if self.entity_description.key in ["value", "trend"]:
return GLUCOSE_TREND_ICON[
(
self.patients["glucoseMeasurement"][
self.coordinator.data["data"][self.index]["glucoseMeasurement"][
"TrendArrow"
]
)
Expand All @@ -198,7 +204,7 @@ def icon(self):
def unit_of_measurement(self):
"""Return the icon for the frontend."""

if self.patients:
if self.coordinator.data["data"][self.index]:
if self.entity_description.key in ["sensor"]:
return self.entity_description.unit_of_measurement
elif self.entity_description.key in ["value"]:
Expand All @@ -209,15 +215,15 @@ def unit_of_measurement(self):
def extra_state_attributes(self):
"""Return the state attributes of the device."""
result = None
if self.patients:
if self.coordinator.data["data"][self.index]:
if self.entity_description.key == "sensor":
result = {
"Serial number": f"{self.patients['sensor']['pt']} {self.patients['sensor']['sn']}",
"Serial number": f"{self.coordinator.data['data'][self.index]['sensor']['pt']} {self.coordinator.data['data'][self.index]['sensor']['sn']}",
"Activation date": datetime.fromtimestamp(
(self.patients["sensor"]["a"])
(self.coordinator.data["data"][self.index]["sensor"]["a"])
),
"patientId": self.patients["patientId"],
"Patient": f"{(self.patients['lastName']).upper()} {self.patients['firstName']}",
"patientId": self.coordinator.data["data"][self.index]["patientId"],
"Patient": f"{(self.coordinator.data['data'][self.index]['lastName']).upper()} {self.coordinator.data['data'][self.index]['firstName']}",
}

return result
Expand Down

0 comments on commit 5de87f1

Please sign in to comment.