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

fixed optional patient parameter handling #54

Merged
merged 1 commit into from
Jan 8, 2024
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
27 changes: 25 additions & 2 deletions custom_components/carelink/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ async def __get_data(self, host, path, query_params, request_body):

return jsondata

def __selectPatient(self, patients):
patient = None
for p in patients:
if p["status"] == "ACTIVE":
patient = p
break
return patient

async def __getPatients(self):
printdbg("__getPatients()")
return await self.__get_data(
self.__carelink_server(), "patient/m2m/links/patients", None, None
)

async def __get_my_user(self):
printdbg("__get_my_user()")
return await self.__get_data(
Expand Down Expand Up @@ -286,6 +300,14 @@ async def __execute_login_procedure(self):
self.__session_monitor_data = None

# Get sessions infos if required
if not self.__carelink_patient_id:
sessionPatients = await self.__getPatients()
patient = self.__selectPatient(sessionPatients)
if patient:
self.__carelink_patient_id = patient["username"]
printdbg("Found patient %s %s (%s)" % (patient["firstName"],patient["lastName"],self.__carelink_patient_id))
else:
raise Exception("No patient found.")
if self.__session_user is None:
self.__session_user = await self.__get_my_user()
if self.__session_profile is None:
Expand Down Expand Up @@ -338,13 +360,13 @@ async def __checkAuthorizationToken(self):
printdbg("Malformed initial token")
return False

# Save expiration time
self.__auth_token_validto = datetime.utcfromtimestamp(token_validto).strftime('%a %b %d %H:%M:%S UTC %Y')
# Check expiration time stamp
tdiff = token_validto - time.time()
if tdiff < 0:
printdbg("Initial token has expired %ds ago" % abs(tdiff))
return False
# Save expiration time

printdbg("Initial token expires in %ds (%s)" % (tdiff,self.__auth_token_validto))
return True
Expand Down Expand Up @@ -394,9 +416,10 @@ async def __get_authorization_token(self):
printdbg("New Token created")
try:
cookie=os.path.join(os.getcwd(), CON_CONTEXT_COOKIE)
printdbg(f"Cookiefile: {cookie}")
with open(cookie, "w") as file:
file.write(self.__carelink_auth_token)
printdbg("Saving new token to cookies.txt")
printdbg("Writing new token to cookies.txt")
except:
printdbg("Failed to store refreshed token")
printdbg("New token is valid until " + self.__auth_token_validto)
Expand Down
14 changes: 7 additions & 7 deletions custom_components/carelink/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@
native_unit_of_measurement=None,
state_class=None,
icon="mdi:calendar-clock",
entity_category=EntityCategory.CONFIG,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_APP_MODEL_TYPE,
Expand All @@ -357,7 +357,7 @@
native_unit_of_measurement=None,
state_class=None,
icon="mdi:application",
entity_category=EntityCategory.CONFIG,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_MEDICAL_DEVICE_MANUFACTURER,
Expand All @@ -366,7 +366,7 @@
native_unit_of_measurement=None,
state_class=None,
icon="mdi:factory",
entity_category=EntityCategory.CONFIG,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_MEDICAL_DEVICE_MODEL_NUMBER,
Expand All @@ -375,7 +375,7 @@
native_unit_of_measurement=None,
state_class=None,
icon="mdi:code-tags",
entity_category=EntityCategory.CONFIG,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_MEDICAL_DEVICE_HARDWARE_REVISION,
Expand All @@ -384,7 +384,7 @@
native_unit_of_measurement=None,
state_class=None,
icon="mdi:code-tags",
entity_category=EntityCategory.CONFIG,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_MEDICAL_DEVICE_FIRMWARE_REVISION,
Expand All @@ -393,7 +393,7 @@
native_unit_of_measurement=None,
state_class=None,
icon="mdi:code-tags",
entity_category=EntityCategory.CONFIG,
entity_category=None,
),
SensorEntityDescription(
key=SENSOR_KEY_MEDICAL_DEVICE_SYSTEM_ID,
Expand All @@ -402,7 +402,7 @@
native_unit_of_measurement=None,
state_class=None,
icon="mdi:code-tags",
entity_category=EntityCategory.CONFIG,
entity_category=None,
),
)

Expand Down