From 5de87f181822524ea2f8d108990909b2c3030c68 Mon Sep 17 00:00:00 2001 From: Gilles Van Schendel <51242147+gillesvs@users.noreply.github.com> Date: Sat, 3 Feb 2024 20:22:46 +0100 Subject: [PATCH] restoring coordinator in sensors Iterating with only part of the coordinator result end-up in loosing the refresh --- custom_components/librelink/binary_sensor.py | 9 +++-- custom_components/librelink/const.py | 2 +- custom_components/librelink/manifest.json | 2 +- custom_components/librelink/sensor.py | 38 +++++++++++--------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/custom_components/librelink/binary_sensor.py b/custom_components/librelink/binary_sensor.py index f6b973e..90731e0 100644 --- a/custom_components/librelink/binary_sensor.py +++ b/custom_components/librelink/binary_sensor.py @@ -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}") @@ -56,6 +56,7 @@ async def async_setup_entry( patients, patientId, patient, + index, config_entry.entry_id, entity_description, ) @@ -72,6 +73,7 @@ def __init__( patients, patientId: str, patient: str, + index: int, entry_id, description: BinarySensorEntityDescription, ) -> None: @@ -79,12 +81,13 @@ def __init__( 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 ] diff --git a/custom_components/librelink/const.py b/custom_components/librelink/const.py index 7e54aa0..18dfb8e 100644 --- a/custom_components/librelink/const.py +++ b/custom_components/librelink/const.py @@ -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" diff --git a/custom_components/librelink/manifest.json b/custom_components/librelink/manifest.json index cf27434..5e7e6b8 100644 --- a/custom_components/librelink/manifest.json +++ b/custom_components/librelink/manifest.json @@ -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" } diff --git a/custom_components/librelink/sensor.py b/custom_components/librelink/sensor.py index 352a090..f804fb5 100644 --- a/custom_components/librelink/sensor.py +++ b/custom_components/librelink/sensor.py @@ -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 @@ -88,6 +91,7 @@ async def async_setup_entry( patients, patientId, patient, + index, config_entry.entry_id, custom_unit, entity_description, @@ -106,6 +110,7 @@ def __init__( patients, patientId: str, patient: str, + index: int, entry_id: str, uom: str, description: SensorEntityDescription, @@ -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 @@ -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" ] ) @@ -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 @@ -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" ] ) @@ -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 ) @@ -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", @@ -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" ] ) @@ -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"]: @@ -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