Skip to content

Commit

Permalink
fix payload - format devoirs and notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Giga77 committed Mar 15, 2024
1 parent 268fed6 commit e7aa3f4
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 48 deletions.
3 changes: 1 addition & 2 deletions custom_components/ecole_directe/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions custom_components/ecole_directe/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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[
Expand Down
13 changes: 0 additions & 13 deletions custom_components/ecole_directe/ecoleDirecte_formatter.py

This file was deleted.

106 changes: 99 additions & 7 deletions custom_components/ecole_directe/ecoleDirecte_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -165,15 +257,15 @@ 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}",
None,
)


def getHomework(session, eleve):
def getDevoirs(session, eleve):
return getResponse(
session,
f"{APIURL}/eleves/{eleve.eleve_id}/cahierdetexte.awp?verbe=get&v={APIVERSION}",
Expand Down
43 changes: 21 additions & 22 deletions custom_components/ecole_directe/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand All @@ -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))

Expand Down Expand Up @@ -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
Expand All @@ -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,
}

Expand All @@ -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,
Expand Down

0 comments on commit e7aa3f4

Please sign in to comment.