Skip to content
Open
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
9 changes: 6 additions & 3 deletions custom_components/librelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)

# Then getting the token. This token is a long life token, so initializaing at HA start up is enough
sessionToken = await myLibrelinkLogin.async_get_token()
loginResponse = await myLibrelinkLogin.async_get_token()
sessionToken = loginResponse["token"]
accountId = loginResponse["accountId"]

# The retrieved token will be used to initiate the coordinator which will be used to update the data on a regular basis
myLibrelinkClient = LibreLinkApiClient(
sessionToken,
session=async_get_clientsession(hass),
token=sessionToken,
base_url=BASE_URL_LIST.get(entry.data[COUNTRY]),
session=async_get_clientsession(hass),
account_id=accountId,
)

# Kept for later use in case historical data is needed
Expand Down
35 changes: 25 additions & 10 deletions custom_components/librelink/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import asyncio
import hashlib
import logging
import socket

Expand All @@ -27,28 +28,41 @@ class LibreLinkApiClient:
token: The long life token to authenticate.
base_url: For API calls depending on your location
Session: aiottp object for the open session
account_id_hash: SHA256 hash of the account ID for API authentication
"""

def __init__(
self, token: str, base_url: str, session: aiohttp.ClientSession
self, token: str, base_url: str, session: aiohttp.ClientSession, account_id: str = None
) -> None:
"""Sample API Client."""
self._token = token
self._session = session
self.connection_url = base_url + CONNECTION_URL
# Hash the account ID for the account-id header (required as of version 4.16.0)
self._account_id_hash = hashlib.sha256(account_id.encode()).hexdigest() if account_id else None

async def async_get_data(self) -> any:
"""Get data from the API."""
# Build headers with required account-id hash (required as of API version 4.16.0)
headers = {
"accept-encoding": "gzip",
"cache-control": "no-cache",
"connection": "Keep-Alive",
"content-type": "application/json",
"product": PRODUCT,
"version": VERSION_APP,
"authorization": "Bearer " + self._token,
}

# Add hashed account ID if available
if self._account_id_hash:
headers["account-id"] = self._account_id_hash

APIreponse = await api_wrapper(
self._session,
method="get",
url=self.connection_url,
headers={
"product": PRODUCT,
"version": VERSION_APP,
"Application": APPLICATION,
"Authorization": "Bearer " + self._token,
},
headers=headers,
data={},
)

Expand Down Expand Up @@ -143,8 +157,8 @@ def __init__(
self.login_url = base_url + LOGIN_URL
self._session = session

async def async_get_token(self) -> any:
"""Get token from the API."""
async def async_get_token(self) -> dict:
"""Get token and account ID from the API."""
reponseLogin = await api_wrapper(
self._session,
method="post",
Expand All @@ -166,8 +180,9 @@ async def async_get_token(self) -> any:
)

monToken = reponseLogin["data"]["authTicket"]["token"]
accountId = reponseLogin["data"]["user"]["id"]

return monToken
return {"token": monToken, "accountId": accountId}


################################################################
Expand Down
2 changes: 1 addition & 1 deletion custom_components/librelink/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"United States": "https://api-us.libreview.io",
}
PRODUCT = "llu.android"
VERSION_APP = "4.7"
VERSION_APP = "4.16.0"
APPLICATION = "application/json"
GLUCOSE_VALUE_ICON = "mdi:diabetes"
GLUCOSE_TREND_ICON = [
Expand Down