Skip to content

Commit

Permalink
Merge pull request #28 from hacf-fr/dev
Browse files Browse the repository at this point in the history
fix datetime issues and add lunch break option
  • Loading branch information
Giga77 authored Sep 20, 2024
2 parents 198d373 + b36fb8d commit 36bf505
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 118 deletions.
7 changes: 7 additions & 0 deletions custom_components/ecole_directe/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .const import (
DEFAULT_ALLOW_NOTIFICATION,
DEFAULT_LUNCH_BREAK_TIME,
DOMAIN,
DEFAULT_REFRESH_INTERVAL,
FILENAME_QCM,
Expand Down Expand Up @@ -134,6 +135,12 @@ async def async_step_init(
"refresh_interval", DEFAULT_REFRESH_INTERVAL
),
): int,
vol.Optional(
"lunch_break_time",
default=self.config_entry.options.get(
"lunch_break_time", DEFAULT_LUNCH_BREAK_TIME
),
): str,
vol.Optional(
"decode_html",
default=self.config_entry.options.get("decode_html", False),
Expand Down
2 changes: 2 additions & 0 deletions custom_components/ecole_directe/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
DEFAULT_ALLOW_NOTIFICATION = False
DEFAULT_LUNCH_BREAK_TIME = "13:00"
MAX_STATE_ATTRS_BYTES = 16384

DEBUG_ON = False
102 changes: 46 additions & 56 deletions custom_components/ecole_directe/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
)

from .const import (
DEBUG_ON,
DEFAULT_LUNCH_BREAK_TIME,
DEFAULT_REFRESH_INTERVAL,
EVENT_TYPE,
)
Expand All @@ -49,6 +51,8 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:

async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
"""Get the latest data from Ecole Directe and updates the state."""
if DEBUG_ON:
_LOGGER.info("DEBUG MODE ON")

previous_data = None if self.data is None else self.data.copy()

Expand All @@ -72,30 +76,21 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
year_data = f"{str(current_year - 1)}-{str(current_year)}"

# EDT BODY
today = (
datetime.now()
.replace(hour=0, minute=0, second=0, microsecond=0)
.strftime("%Y-%m-%d")
)
tomorrow = (
datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
+ timedelta(days=1)
).strftime("%Y-%m-%d")
current_week_begin = datetime.now().replace(
hour=0, minute=0, second=0, microsecond=0
) - timedelta(
days=datetime.now()
.replace(hour=0, minute=0, second=0, microsecond=0)
.weekday()
today = datetime.now().date()
tomorrow = datetime.now().date() + timedelta(days=1)

current_week_begin = datetime.now().date() - timedelta(
days=datetime.now().weekday()
)

current_week_plus_21 = current_week_begin + timedelta(days=21)
current_week_end = current_week_begin + timedelta(days=6)
next_week_begin = current_week_end + timedelta(days=1)
next_week_end = next_week_begin + timedelta(days=6)
after_next_week_begin = next_week_end + timedelta(days=1)

if session._account_type == "1": # famille
# if "MESSAGERIE" in session.modules:
# if DEBUG_ON or "MESSAGERIE" in session.modules:
# try:
# self.data["messages"] = await self.hass.async_add_executor_job(
# get_messages,
Expand All @@ -111,7 +106,7 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
# "Error getting messages for family from ecole directe: %s", ex
# )

if "EDFORMS" in session.modules:
if DEBUG_ON or "EDFORMS" in session.modules:
try:
self.data["formulaires"] = await self.hass.async_add_executor_job(
get_formulaires,
Expand All @@ -133,7 +128,7 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
)

for eleve in session.eleves:
if "CAHIER_DE_TEXTES" in eleve.modules:
if DEBUG_ON or "CAHIER_DE_TEXTES" in eleve.modules:
try:
self.data[
f"{eleve.get_fullname_lower()}_homework"
Expand All @@ -156,7 +151,7 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
_LOGGER.warning(
"Error getting homeworks from ecole directe: %s", ex
)
if "NOTES" in eleve.modules:
if DEBUG_ON or "NOTES" in eleve.modules:
try:
grades_evaluations = await self.hass.async_add_executor_job(
get_grades_evaluations,
Expand Down Expand Up @@ -190,73 +185,68 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
except Exception as ex:
_LOGGER.warning("Error getting grades from ecole directe: %s", ex)

if "EDT" in eleve.modules:
if DEBUG_ON or "EDT" in eleve.modules:
try:
break_time = self.config_entry.options.get(
"lunch_break_time", DEFAULT_LUNCH_BREAK_TIME
)
lunch_break_time = datetime.strptime(
break_time,
"%H:%M",
).time()
_LOGGER.warning("lunch_break_time: %s", lunch_break_time)

lessons = await self.hass.async_add_executor_job(
get_lessons,
session.token,
eleve,
current_week_begin.strftime("%Y-%m-%d"),
current_week_plus_21.strftime("%Y-%m-%d"),
self.hass.config.config_dir,
lunch_break_time,
)
self.data[f"{eleve.get_fullname_lower()}_timetable_today"] = list(
filter(
lambda lesson: lesson["start_at"] == today,
lambda lesson: lesson["start"].date() == today,
lessons,
)
)
lessons_tomorrow = list(
filter(
lambda lesson: lesson["start_at"] == tomorrow,
lambda lesson: lesson["start"].date() == tomorrow,
lessons,
)
)
self.data[f"{eleve.get_fullname_lower()}_timetable_tomorrow"] = (
list(
filter(
lambda lesson: lesson["start_at"] == tomorrow,
lessons,
)
)
lessons_tomorrow
)
self.data[f"{eleve.get_fullname_lower()}_timetable_next_day"] = (
get_next_day_lessons(
lessons,
lessons_tomorrow,
datetime.strptime(tomorrow, "%Y-%m-%d"),
tomorrow,
)
)
self.data[f"{eleve.get_fullname_lower()}_timetable_period"] = list(
filter(
lambda lesson: datetime.strptime(
lesson["start_at"], "%Y-%m-%d"
)
>= current_week_begin
and datetime.strptime(lesson["start_at"], "%Y-%m-%d")
<= current_week_end,
lambda lesson: lesson["start"].date() >= current_week_begin
and lesson["start"].date() <= current_week_end,
lessons,
)
)
self.data[f"{eleve.get_fullname_lower()}_timetable_period_1"] = (
list(
filter(
lambda lesson: datetime.strptime(
lesson["start_at"], "%Y-%m-%d"
)
>= next_week_begin
and datetime.strptime(lesson["start_at"], "%Y-%m-%d")
<= next_week_end,
lambda lesson: lesson["start"].date() >= next_week_begin
and lesson["start"].date() <= next_week_end,
lessons,
)
)
)
self.data[f"{eleve.get_fullname_lower()}_timetable_period_2"] = (
list(
filter(
lambda lesson: datetime.strptime(
lesson["start_at"], "%Y-%m-%d"
)
lambda lesson: lesson["start"].date()
>= after_next_week_begin,
lessons,
)
Expand All @@ -266,7 +256,7 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
except Exception as ex:
_LOGGER.warning("Error getting Lessons from ecole directe: %s", ex)

if "VIE_SCOLAIRE" in eleve.modules:
if DEBUG_ON or "VIE_SCOLAIRE" in eleve.modules:
try:
vie_scolaire = await self.hass.async_add_executor_job(
get_vie_scolaire,
Expand Down Expand Up @@ -323,15 +313,15 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
_LOGGER.warning(
"Error getting vie scolaire from ecole directe: %s", ex
)
# if "MESSAGERIE" in eleve.modules:
# try:
# self.data[
# "messages" + eleve.eleve_id
# ] = await self.hass.async_add_executor_job(
# get_messages, session, eleve, year_data
# )
# except Exception as ex:
# _LOGGER.warning("Error getting messages from ecole directe: %s", ex)
# if DEBUG_ON or "MESSAGERIE" in eleve.modules:
# try:
# self.data[
# "messages" + eleve.eleve_id
# ] = await self.hass.async_add_executor_job(
# get_messages, session, eleve, year_data
# )
# except Exception as ex:
# _LOGGER.warning("Error getting messages from ecole directe: %s", ex)

return self.data

Expand Down Expand Up @@ -393,13 +383,13 @@ def get_next_day_lessons(lessons, lessons_next_day, next_day):
"""get next day lessons"""
if len(lessons) == 0:
return None
if datetime.strptime(lessons[-1]["start_at"], "%Y-%m-%d") < next_day:
if lessons[-1]["start"].date() < next_day:
return None
if len(lessons_next_day) == 0:
next_day = next_day + timedelta(days=1)
lessons_next_day = list(
filter(
lambda lesson: lesson["start_at"] == next_day.strftime("%Y-%m-%d"),
lambda lesson: lesson["start"].date() == next_day,
lessons,
)
)
Expand Down
Loading

0 comments on commit 36bf505

Please sign in to comment.