diff --git a/custom_components/ecole_directe/coordinator.py b/custom_components/ecole_directe/coordinator.py index 433484e..0bdcadf 100644 --- a/custom_components/ecole_directe/coordinator.py +++ b/custom_components/ecole_directe/coordinator.py @@ -76,6 +76,7 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]: ) except Exception as ex: _LOGGER.warning("Error getting devoirs from ecole directe: %s", ex) + self.data["devoirs" + eleve.get_fullnameLower()] = {} if "NOTES" in eleve.modules: try: self.data[ @@ -85,6 +86,7 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]: ) except Exception as ex: _LOGGER.warning("Error getting notes from ecole directe: %s", ex) + self.data["notes" + eleve.get_fullnameLower()] = {} # if "MESSAGERIE" in eleve.modules: # try: # self.data['messages'+ eleve.eleve_id] = await self.hass.async_add_executor_job(getMessages, session, eleve, yearData) diff --git a/custom_components/ecole_directe/ecoleDirecte_helper.py b/custom_components/ecole_directe/ecoleDirecte_helper.py index 75d2e22..5861cf6 100644 --- a/custom_components/ecole_directe/ecoleDirecte_helper.py +++ b/custom_components/ecole_directe/ecoleDirecte_helper.py @@ -35,8 +35,8 @@ def getResponse(session, url, payload): if payload is None: payload = "data={}" - _LOGGER.debug("URL: [%s] - Data: [%s]", url, payload) - response = requests.post(url, data=payload, headers=getHeaders(token)) + _LOGGER.debug("URL: [%s] - Payload: [%s]", url, payload) + response = requests.post(url, data=payload, headers=getHeaders(token), timeout=60) if "application/json" in response.headers.get("Content-Type", ""): respJson = response.json() @@ -90,7 +90,7 @@ def __init__(self, data): class ED_Eleve: def __init__( self, - data, + data=None, id=None, firstName=None, lastName=None, @@ -106,8 +106,9 @@ def __init__( self.eleve_firstname = firstName self.modules = modules else: - self.classe_id = data["classe"]["id"] - self.classe_name = data["classe"]["libelle"] + if "classe" in data: + self.classe_id = data["classe"]["id"] + self.classe_name = data["classe"]["libelle"] self.eleve_id = data["id"] self.eleve_lastname = data["nom"] self.eleve_firstname = data["prenom"] @@ -126,18 +127,45 @@ def get_fullname(self) -> str | None: 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"] + if "matiere" in data: + self.matiere = data["matiere"] + else: + self.matiere = "" + if "codeMatiere" in data: + self.codeMatiere = data["codeMatiere"] + else: + self.codeMatiere = "" + if "aFaire" in data: + self.aFaire = data["aFaire"] + else: + self.aFaire = "" + if "idDevoir" in data: + self.idDevoir = data["idDevoir"] + else: + self.idDevoir = "" + if "documentsAFaire" in data: + self.documentsAFaire = data["documentsAFaire"] + else: + self.documentsAFaire = "" + if "donneLe" in data: + self.donneLe = data["donneLe"] + else: + self.donneLe = "" self.pourLe = pourLe - self.effectue = data["effectue"] - self.interrogation = data["interrogation"] - self.rendreEnLigne = data["rendreEnLigne"] + if "effectue" in data: + self.effectue = data["effectue"] + else: + self.effectue = "" + if "interrogation" in data: + self.interrogation = data["interrogation"] + else: + self.interrogation = "" + if "rendreEnLigne" in data: + self.rendreEnLigne = data["rendreEnLigne"] + else: + self.rendreEnLigne = "" except Exception as err: - _LOGGER.warning(err) + _LOGGER.warning("ED_Devoir Error: [%s] - Data[%s]", err, data) def format(self): try: @@ -160,30 +188,96 @@ def format(self): 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"] + if "id" in data: + self.id = data["id"] + else: + self.id = "" + if "devoir" in data: + self.devoir = data["devoir"] + else: + self.devoir = "" + if "codePeriode" in data: + self.codePeriode = data["codePeriode"] + else: + self.codePeriode = "" + if "codeMatiere" in data: + self.codeMatiere = data["codeMatiere"] + else: + self.codeMatiere = "" + if "libelleMatiere" in data: + self.libelleMatiere = data["libelleMatiere"] + else: + self.libelleMatiere = "" + if "codeSousMatiere" in data: + self.codeSousMatiere = data["codeSousMatiere"] + else: + self.codeSousMatiere = "" + if "typeDevoir" in data: + self.typeDevoir = data["typeDevoir"] + else: + self.typeDevoir = "" + if "enLettre" in data: + self.enLettre = data["enLettre"] + else: + self.enLettre = "" + if "commentaire" in data: + self.commentaire = data["commentaire"] + else: + self.commentaire = "" + if "uncSujet" in data: + self.uncSujet = data["uncSujet"] + else: + self.uncSujet = "" + if "uncCorrige" in data: + self.uncCorrige = data["uncCorrige"] + else: + self.uncCorrige = "" + if "coef" in data: + self.coef = data["coef"] + else: + self.coef = "" + if "noteSur" in data: + self.noteSur = data["noteSur"] + else: + self.noteSur = "" + if "valeur" in data: + self.valeur = data["valeur"] + else: + self.valeur = "" + if "nonSignificatif" in data: + self.nonSignificatif = data["nonSignificatif"] + else: + self.nonSignificatif = "" + if "date" in data: + self.date = data["date"] + else: + self.date = "" + if "dateSaisie" in data: + self.dateSaisie = data["dateSaisie"] + else: + self.dateSaisie = "" + if "valeurisee" in data: + self.valeurisee = data["valeurisee"] + else: + self.valeurisee = "" + if "moyenneClasse" in data: + self.moyenneClasse = data["moyenneClasse"] + else: + self.moyenneClasse = "" + if "minClasse" in data: + self.minClasse = data["minClasse"] + else: + self.minClasse = "" + if "maxClasse" in data: + self.maxClasse = data["maxClasse"] + else: + self.maxClasse = "" + if "elementsProgramme" in data: + self.elementsProgramme = data["elementsProgramme"] + else: + self.elementsProgramme = "" except Exception as err: - _LOGGER.warning(err) + _LOGGER.warning("ED_Note error: [%s] - Data[%s]", err, data) def format(self): try: @@ -216,9 +310,12 @@ def format(self): def get_ecoledirecte_session(data) -> ED_Session | None: + """Function connecting to Ecole Directe""" try: _LOGGER.debug( - f"Try connection for username: {data['username']} and password: {data['password']}" + "Try connection for username: {%s} and password: {%s}", + data["username"], + data["password"], ) login = getResponse( @@ -235,7 +332,8 @@ def get_ecoledirecte_session(data) -> ED_Session | None: ) _LOGGER.info( - f"Connection OK - identifiant: [{login["data"]["accounts"][0]["identifiant"]}]" + "Connection OK - identifiant: [{%s}]", + login["data"]["accounts"][0]["identifiant"], ) return ED_Session(login) except Exception as err: diff --git a/custom_components/ecole_directe/sensor.py b/custom_components/ecole_directe/sensor.py index 3dd66c7..93b38ca 100644 --- a/custom_components/ecole_directe/sensor.py +++ b/custom_components/ecole_directe/sensor.py @@ -78,7 +78,7 @@ def name(self): @property def native_value(self): """Return the state of the sensor.""" - if self.coordinator.data[self._name] is None: + if self._name not in self.coordinator.data: return "unavailable" elif self._state == "len": return len(self.coordinator.data[self._name]) @@ -95,8 +95,7 @@ def extra_state_attributes(self): def available(self) -> bool: """Return if entity is available.""" return ( - self.coordinator.last_update_success - and self.coordinator.data[self._name] is not None + self.coordinator.last_update_success and self._name in self.coordinator.data ) @@ -146,21 +145,24 @@ def __init__(self, coordinator: EDDataUpdateCoordinator, eleve: ED_Eleve) -> Non def extra_state_attributes(self): """Return the state attributes.""" attributes = [] - todo_counter = None - if ( - self.coordinator.data[f"devoirs{self._child_info.get_fullnameLower()}"] - is not None - ): - todo_counter = 0 + todo_counter = 0 + if f"devoirs{self._child_info.get_fullnameLower()}" in self.coordinator.data: for date in self.coordinator.data[ f"devoirs{self._child_info.get_fullnameLower()}" ]: 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 + for devoir_json in devoirs: + devoir = ED_Devoir(devoir_json, date) + if devoir is not None: + attributes.append(devoir.format()) + if devoir.effectue is False: + todo_counter += 1 + else: + attributes.append( + { + "Erreur": f"devoirs{self._child_info.get_fullnameLower()} n'existe pas." + } + ) return { "updated_at": self.coordinator.last_update_success_time,