From e7aa3f4c0cb57a9f6e6dbd6e7f7fd85d5173d49d Mon Sep 17 00:00:00 2001 From: Giga77 <2777446+Giga77@users.noreply.github.com> Date: Fri, 15 Mar 2024 22:44:09 +0100 Subject: [PATCH] fix payload - format devoirs and notes --- custom_components/ecole_directe/const.py | 3 +- .../ecole_directe/coordinator.py | 8 +- .../ecole_directe/ecoleDirecte_formatter.py | 13 --- .../ecole_directe/ecoleDirecte_helper.py | 106 ++++++++++++++++-- custom_components/ecole_directe/sensor.py | 43 ++++--- 5 files changed, 125 insertions(+), 48 deletions(-) delete mode 100644 custom_components/ecole_directe/ecoleDirecte_formatter.py diff --git a/custom_components/ecole_directe/const.py b/custom_components/ecole_directe/const.py index fbf1c2a..5ba35ef 100644 --- a/custom_components/ecole_directe/const.py +++ b/custom_components/ecole_directe/const.py @@ -7,5 +7,4 @@ # default values for options DEFAULT_REFRESH_INTERVAL = 30 -EVALUATIONS_TO_DISPLAY = 15 -HOMEWORK_DESC_MAX_LENGTH = 125 +NOTES_TO_DISPLAY = 15 diff --git a/custom_components/ecole_directe/coordinator.py b/custom_components/ecole_directe/coordinator.py index 2bfd4a7..433484e 100644 --- a/custom_components/ecole_directe/coordinator.py +++ b/custom_components/ecole_directe/coordinator.py @@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import TimestampDataUpdateCoordinator -from .ecoleDirecte_helper import get_ecoledirecte_session, getHomework, getNotes +from .ecoleDirecte_helper import get_ecoledirecte_session, getDevoirs, getNotes from .const import ( DEFAULT_REFRESH_INTERVAL, @@ -70,12 +70,12 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]: if "CAHIER_DE_TEXTES" in eleve.modules: try: self.data[ - "homework" + eleve.get_fullnameLower() + "devoirs" + eleve.get_fullnameLower() ] = await self.hass.async_add_executor_job( - getHomework, session, eleve + getDevoirs, session, eleve ) except Exception as ex: - _LOGGER.warning("Error getting homework from ecole directe: %s", ex) + _LOGGER.warning("Error getting devoirs from ecole directe: %s", ex) if "NOTES" in eleve.modules: try: self.data[ diff --git a/custom_components/ecole_directe/ecoleDirecte_formatter.py b/custom_components/ecole_directe/ecoleDirecte_formatter.py deleted file mode 100644 index eace282..0000000 --- a/custom_components/ecole_directe/ecoleDirecte_formatter.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Data Formatter for the Ecole Directe integration.""" - -import logging - -_LOGGER = logging.getLogger(__name__) - - -def format_homework(homework) -> dict: - return None # TODO - - -def format_note(note) -> dict: - return None # TODO diff --git a/custom_components/ecole_directe/ecoleDirecte_helper.py b/custom_components/ecole_directe/ecoleDirecte_helper.py index c23f7bf..75d2e22 100644 --- a/custom_components/ecole_directe/ecoleDirecte_helper.py +++ b/custom_components/ecole_directe/ecoleDirecte_helper.py @@ -26,17 +26,17 @@ def encodeBody(dictionnary, isRecursive=False): return body[:-1] -def getResponse(session, url, data): +def getResponse(session, url, payload): if session is not None and isLogin(session): token = session.token else: token = None - if data is None: - data = "{" + "}" + if payload is None: + payload = "data={}" - _LOGGER.debug("URL: [%s] - Data: [%s]", url, data) - response = requests.post(url, data=data, headers=getHeaders(token)) + _LOGGER.debug("URL: [%s] - Data: [%s]", url, payload) + response = requests.post(url, data=payload, headers=getHeaders(token)) if "application/json" in response.headers.get("Content-Type", ""): respJson = response.json() @@ -123,6 +123,98 @@ def get_fullname(self) -> str | None: return f"{self.eleve_firstname} {self.eleve_lastname}" +class ED_Devoir: + def __init__(self, data, pourLe): + try: + self.matiere = data["matiere"] + self.codeMatiere = data["codeMatiere"] + self.aFaire = data["aFaire"] + self.idDevoir = data["idDevoir"] + self.documentsAFaire = data["documentsAFaire"] + self.donneLe = data["donneLe"] + self.pourLe = pourLe + self.effectue = data["effectue"] + self.interrogation = data["interrogation"] + self.rendreEnLigne = data["rendreEnLigne"] + except Exception as err: + _LOGGER.warning(err) + + def format(self): + try: + return { + "matiere": self.matiere, + "codeMatiere": self.codeMatiere, + "aFaire": self.aFaire, + "idDevoir": self.idDevoir, + "documentsAFaire": self.documentsAFaire, + "donneLe": self.donneLe, + "pourLe": self.pourLe, + "effectue": self.effectue, + "interrogation": self.interrogation, + "rendreEnLigne": self.rendreEnLigne, + } + except Exception: + return {} + + +class ED_Note: + def __init__(self, data): + try: + self.id = data["id"] + self.devoir = data["devoir"] + self.codePeriode = data["codePeriode"] + self.codeMatiere = data["codeMatiere"] + self.libelleMatiere = data["libelleMatiere"] + self.codeSousMatiere = data["codeSousMatiere"] + self.typeDevoir = data["typeDevoir"] + self.enLettre = data["enLettre"] + self.commentaire = data["commentaire"] + self.uncSujet = data["uncSujet"] + self.uncCorrige = data["uncCorrige"] + self.coef = data["coef"] + self.noteSur = data["noteSur"] + self.valeur = data["valeur"] + self.nonSignificatif = data["nonSignificatif"] + self.date = data["date"] + self.dateSaisie = data["dateSaisie"] + self.valeurisee = data["valeurisee"] + self.moyenneClasse = data["moyenneClasse"] + self.minClasse = data["minClasse"] + self.maxClasse = data["maxClasse"] + self.elementsProgramme = data["elementsProgramme"] + except Exception as err: + _LOGGER.warning(err) + + def format(self): + try: + return { + "id": self.id, + "devoir": self.devoir, + "codePeriode": self.codePeriode, + "codeMatiere": self.codeMatiere, + "libelleMatiere": self.libelleMatiere, + "codeSousMatiere": self.codeSousMatiere, + "typeDevoir": self.typeDevoir, + "enLettre": self.enLettre, + "commentaire": self.commentaire, + "uncSujet": self.uncSujet, + "uncCorrige": self.uncCorrige, + "coef": self.coef, + "noteSur": self.noteSur, + "valeur": self.valeur, + "nonSignificatif": self.nonSignificatif, + "date": self.date, + "dateSaisie": self.dateSaisie, + "valeurisee": self.valeurisee, + "moyenneClasse": self.moyenneClasse, + "minClasse": self.minClasse, + "maxClasse": self.maxClasse, + "elementsProgramme": self.elementsProgramme, + } + except Exception: + return {} + + def get_ecoledirecte_session(data) -> ED_Session | None: try: _LOGGER.debug( @@ -165,7 +257,7 @@ def getMessages(session, eleve, anneeScolaire): ) -def getHomeworkByDate(session, eleve, date): +def getDevoirsByDate(session, eleve, date): return getResponse( session, f"{APIURL}/eleves/{eleve.eleve_id}/cahierdetexte/{date}.awp?verbe=get&v={APIVERSION}", @@ -173,7 +265,7 @@ def getHomeworkByDate(session, eleve, date): ) -def getHomework(session, eleve): +def getDevoirs(session, eleve): return getResponse( session, f"{APIURL}/eleves/{eleve.eleve_id}/cahierdetexte.awp?verbe=get&v={APIVERSION}", diff --git a/custom_components/ecole_directe/sensor.py b/custom_components/ecole_directe/sensor.py index e8d5b6a..3dd66c7 100644 --- a/custom_components/ecole_directe/sensor.py +++ b/custom_components/ecole_directe/sensor.py @@ -10,12 +10,11 @@ CoordinatorEntity, ) -from .ecoleDirecte_formatter import format_note, format_homework -from .ecoleDirecte_helper import ED_Eleve +from .ecoleDirecte_helper import ED_Eleve, ED_Devoir, ED_Note from .coordinator import EDDataUpdateCoordinator from .const import ( DOMAIN, - EVALUATIONS_TO_DISPLAY, + NOTES_TO_DISPLAY, ) @@ -33,7 +32,7 @@ async def async_setup_entry( for eleve in coordinator.data["session"].eleves: sensors.append(EDChildSensor(coordinator, eleve)) if "CAHIER_DE_TEXTES" in eleve.modules: - sensors.append(EDHomeworkSensor(coordinator, eleve)) + sensors.append(EDDevoirsSensor(coordinator, eleve)) if "NOTES" in eleve.modules: sensors.append(EDNotesSensor(coordinator, eleve)) @@ -134,13 +133,13 @@ def available(self) -> bool: return self.coordinator.last_update_success -class EDHomeworkSensor(EDGenericSensor): +class EDDevoirsSensor(EDGenericSensor): """Representation of a ED sensor.""" def __init__(self, coordinator: EDDataUpdateCoordinator, eleve: ED_Eleve) -> None: """Initialize the ED sensor.""" super().__init__( - coordinator, "homework" + eleve.get_fullnameLower(), eleve, "len" + coordinator, "devoirs" + eleve.get_fullnameLower(), eleve, "len" ) @property @@ -149,20 +148,23 @@ def extra_state_attributes(self): attributes = [] todo_counter = None if ( - self.coordinator.data[f"homework{self._child_info.get_fullnameLower()}"] + self.coordinator.data[f"devoirs{self._child_info.get_fullnameLower()}"] is not None ): todo_counter = 0 - for homework in self.coordinator.data[ - f"homework{self._child_info.get_fullnameLower()}" + for date in self.coordinator.data[ + f"devoirs{self._child_info.get_fullnameLower()}" ]: - attributes.append(format_homework(homework)) - if homework.done is False: - todo_counter += 1 + for devoirs in date: + devoir = ED_Devoir(devoirs, date) + if devoir is not None: + attributes.append(devoir.format()) + if devoir.effectue is False: + todo_counter += 1 return { "updated_at": self.coordinator.last_update_success_time, - "homework": attributes, + "devoirs": attributes, "todo_counter": todo_counter, } @@ -178,18 +180,15 @@ def __init__(self, coordinator: EDDataUpdateCoordinator, eleve: ED_Eleve) -> Non def extra_state_attributes(self): """Return the state attributes.""" attributes = [] + notes = self.coordinator.data["notes" + self._child_info.get_fullnameLower()] index_note = 0 - if ( - self.coordinator.data["notes" + self._child_info.get_fullnameLower()] - is not None - ): - for note in self.coordinator.data[ - "notes" + self._child_info.get_fullnameLower() - ]: + if notes is not None: + for note in notes: index_note += 1 - if index_note == EVALUATIONS_TO_DISPLAY: + if index_note == NOTES_TO_DISPLAY: break - attributes.append(format_note(note)) + n = ED_Note(note) + attributes.append(n.format()) return { "updated_at": self.coordinator.last_update_success_time,