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

Refactoring changes #15

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
92 changes: 33 additions & 59 deletions custom_components/librelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,67 @@

from __future__ import annotations

import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .api import LibreLinkApiClient, LibreLinkApiLogin, LibreLinkGetGraph
from .const import BASE_URL_LIST, COUNTRY, DOMAIN
from .api import LibreLinkAPI
from .const import CONF_PATIENT_ID, DOMAIN, LOGGER
from .coordinator import LibreLinkDataUpdateCoordinator

PLATFORMS: list[Platform] = [
Platform.SENSOR,
Platform.BINARY_SENSOR,
]


_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up this integration using UI."""

_LOGGER.debug(
"Appel de async_setup_entry entry: entry_id= %s, data= %s, user = %s BaseUrl = %s",
LOGGER.debug(
"Appel de async_setup_entry entry: entry_id= %s, data= %s",
entry.entry_id,
entry.data,
entry.data[CONF_USERNAME],
# entry.data[CONF_PASSWORD],
BASE_URL_LIST.get(entry.data[COUNTRY]),
)
hass.data.setdefault(DOMAIN, {})

# Using the declared API for login based on patient credentials to retreive the bearer Token
username = entry.data[CONF_USERNAME]
password = entry.data[CONF_PASSWORD]
base_url = entry.data[CONF_URL]
patient_id = entry.data[CONF_PATIENT_ID]

myLibrelinkLogin = LibreLinkApiLogin(
username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD],
base_url=BASE_URL_LIST.get(entry.data[COUNTRY]),
session=async_get_clientsession(hass),
)
domain_data = hass.data.setdefault(DOMAIN, {})

# 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()
if username not in domain_data:
# Using the declared API for login based on patient credentials to retreive the bearer Token
api = LibreLinkAPI(
base_url=base_url,
session=async_get_clientsession(hass),
)

# 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),
base_url=BASE_URL_LIST.get(entry.data[COUNTRY]),
)
# Then getting the token.
await api.async_login(username=username, password=password)

# Kept for later use in case historical data is needed
# myLibreLinkGetGraph = LibreLinkGetGraph(
# sessionToken,
# session=async_get_clientsession(hass),
# base_url=BASE_URL_LIST.get(entry.data[COUNTRY]),
# patient_id="4cd06c35-28d0-11ec-ae45-0242ac110005",
# )
# graph = await myLibreLinkGetGraph.async_get_data()
# print(f"graph {graph}")

hass.data[DOMAIN][entry.entry_id] = coordinator = LibreLinkDataUpdateCoordinator(
hass=hass,
client=myLibrelinkClient,
)
coordinator = LibreLinkDataUpdateCoordinator(
hass=hass, api=api, patient_id=patient_id
)

# First poll of the data to be ready for entities initialization
await coordinator.async_config_entry_first_refresh()

# First poll of the data to be ready for entities initialization
await coordinator.async_config_entry_first_refresh()
domain_data[username] = coordinator
else:
coordinator: LibreLinkDataUpdateCoordinator = domain_data[username]
coordinator.register_patient(patient_id)

# Then launch async_setup_entry for our declared entities in sensor.py and binary_sensor.py
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

# Reload entry when its updated.
entry.async_on_unload(entry.add_update_listener(async_reload_entry))

return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
if unloaded := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
coordinator: LibreLinkDataUpdateCoordinator = hass.data[DOMAIN][CONF_USERNAME]
coordinator.unregister_patient(entry.data[CONF_PATIENT_ID])
if coordinator.tracked_patients == 0:
hass.data[DOMAIN].pop(CONF_USERNAME)
return unloaded


async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry when it changed."""
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
Loading