Skip to content

Commit

Permalink
Add account type
Browse files Browse the repository at this point in the history
  • Loading branch information
Giga77 authored Mar 10, 2024
1 parent 9d10b13 commit 50bbd37
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
1 change: 1 addition & 0 deletions custom_components/ecole_directe/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{
vol.Required("username"): str,
vol.Required("password"): str,
vol.Required("account_type"): vol.In({'eleve': 'Student', 'famille': 'Family'})
}
)

Expand Down
19 changes: 14 additions & 5 deletions custom_components/ecole_directe/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
"""Get the latest data from Ecole Directe and updates the state."""

data = self.config_entry.data
self.data = {
"account_type": data['account_type'],
}

session = await self.hass.async_add_executor_job(get_ecoledirecte_session, data)

Expand All @@ -48,11 +51,12 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:

self.data['session'] = session

try:
self.data['messages'] = await self.hass.async_add_executor_job(getMessages, session, None)
except Exception as ex:
self.data['messages'] = None
_LOGGER.warning("Error getting messages from ecole directe: %s", ex)
if (data['account_type'] == 'famille'):
try:
self.data['messages'] = await self.hass.async_add_executor_job(getMessages, session, None, None)
except Exception as ex:
self.data['messages'] = None
_LOGGER.warning("Error getting messages for family from ecole directe: %s", ex)

for eleve in session.eleves:
if "CAHIER_DE_TEXTES" in eleve.modules:
Expand All @@ -65,5 +69,10 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
self.data['notes'+ eleve.eleve_id] = await self.hass.async_add_executor_job(getNotes, session, eleve)
except Exception as ex:
_LOGGER.warning("Error getting notes from ecole directe: %s", ex)
if "MESSAGERIE" in eleve.modules:
try:
self.data['notes'+ eleve.eleve_id] = await self.hass.async_add_executor_job(getMessages, session, eleve, None)
except Exception as ex:
_LOGGER.warning("Error getting notes from ecole directe: %s", ex)

return self.data
42 changes: 25 additions & 17 deletions custom_components/ecole_directe/ecoleDirecte_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
_LOGGER = logging.getLogger(__name__)

APIURL="https://api.ecoledirecte.com/v3"
APIVERSION="4.19.0"
APIVERSION="4.53.0"

def encodeString(string):
return string.replace("%", "%25").replace("&", "%26").replace("+", "%2B").replace("+", "%2B").replace("\\", "\\\\\\").replace("\\\\", "\\\\\\\\")
Expand Down Expand Up @@ -81,38 +81,46 @@ def get_ecoledirecte_session(data) -> ED_Session | None:

return session

def getMessages(session, year):
return getResponse(session,
f"{APIURL}/Eleves/{session.id}/messages.awp?force=false&typeRecuperation=received&idClasseur=0&orderBy=date&order=desc&query=&onlyRead=&page=0&itemsPerPage=10&getAll=0&verbe=get&v={APIVERSION}",
def getMessages(session, eleve, year):
if(eleve == None):
return getResponse(session,
f"{APIURL}/familles/{session.id}/messages.awp?force=false&typeRecuperation=received&idClasseur=0&orderBy=date&order=desc&query=&onlyRead=&page=0&itemsPerPage=100&getAll=0&verbe=get&v={APIVERSION}",
encodeBody({"data": {"anneeMessages": year}}))
return getResponse(session,
f"{APIURL}/eleves/{eleve.eleve_id}/messages.awp?force=false&typeRecuperation=received&idClasseur=0&orderBy=date&order=desc&query=&onlyRead=&page=0&itemsPerPage=100&getAll=0&verbe=get&v={APIVERSION}",
encodeBody({"data": {"anneeMessages": year}}))

def getHomework(session, eleve, date):
return getResponse(session,
f"{APIURL}/Eleves/{eleve.eleve_id}/cahierdetexte/{date}.awp?verbe=get&v={APIVERSION}",
f"{APIURL}/eleves/{eleve.eleve_id}/cahierdetexte/{date}.awp?verbe=get&v={APIVERSION}",
"data={}")

def getHomework(session, eleve):
return getResponse(session,
f"{APIURL}/Eleves/{eleve.eleve_id}/cahierdetexte.awp?verbe=get&v={APIVERSION}",
f"{APIURL}/eleves/{eleve.eleve_id}/cahierdetexte.awp?verbe=get&v={APIVERSION}",
"data={}")

def getNotes(session, eleve):
return getResponse(session,
f"{APIURL}/Eleves/{eleve.eleve_id}/notes.awp?verbe=get&v={APIVERSION}",
f"{APIURL}/eleves/{eleve.eleve_id}/notes.awp?verbe=get&v={APIVERSION}",
"data='data={\"anneeScolaire\": \"\"}'")

def getHeaders(token):
headers = {
"authority": "api.ecoledirecte.com",
"accept": "application/json, text/plain, */*",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0",
"content-type": "application/x-www-form-urlencoded",
"origin": "https://www.ecoledirecte.com",
"sec-fetch-site": "same-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://www.ecoledirecte.com/",
"accept-language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-language": "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"DNT": "1",
"Host": "api.ecoledirecte.com",
"Origin": "https://www.ecoledirecte.com",
"Referer": "https://www.ecoledirecte.com/",
"Sec-fetch-dest": "empty",
"Sec-fetch-mode": "cors",
"Sec-fetch-site": "same-site",
"Sec-GPC": "1",
"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0"
}
if token != None:
headers["X-Token"] = token
Expand Down
3 changes: 2 additions & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "Ecole Directe for Home Assistant",
"country": "FR"
"country": "FR",
"render_readme": true
}

0 comments on commit 50bbd37

Please sign in to comment.