Skip to content

Commit

Permalink
add login method for teacher
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasJoKuJonas committed Nov 7, 2022
1 parent 0f67eeb commit 26a8da6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
16 changes: 13 additions & 3 deletions custom_components/webuntis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ def get_timetable_object(self):
# pylint: disable=maybe-no-member
source = klassen.filter(name=self.timetable_source_id)[0]
elif self.timetable_source == "teacher":
pass
source = self.session.get_teacher(
self.timetable_source_id[1], self.timetable_source_id[0]
)
elif self.timetable_source == "subject":
pass
elif self.timetable_source == "room":
Expand All @@ -228,7 +230,11 @@ def _is_class(self):

for lesson in table:
# pylint: disable=maybe-no-member
if lesson.start < now < lesson.end and lesson.code != "cancelled" and lesson.subjects != "":
if (
lesson.start < now < lesson.end
and lesson.code != "cancelled"
and lesson.subjects != ""
):
return True
return False

Expand All @@ -245,7 +251,11 @@ def _next_class(self):

time_list = []
for lesson in table:
if lesson.start > now and lesson.code != "cancelled" and lesson.subjects != "":
if (
lesson.start > now
and lesson.code != "cancelled"
and lesson.subjects != ""
):
time_list.append(lesson.start)

return sorted(time_list)[0].astimezone()
Expand Down
27 changes: 18 additions & 9 deletions custom_components/webuntis/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
vol.Required("password"): str,
vol.Required("timetable_source"): selector.SelectSelector(
selector.SelectSelectorConfig(
options=["student", "klasse"], # "teacher", "subject", "room"
options=["student", "klasse", "teacher"], # "subject", "room"
mode="dropdown",
)
),
Expand All @@ -50,7 +50,7 @@ async def validate_input(
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
"""

if user_input["timetable_source"] == "student":
if user_input["timetable_source"] in ["student", "teacher"]:
for char in [",", " "]:
split = user_input["timetable_source_id"].split(char)
if len(split) == 2:
Expand Down Expand Up @@ -95,12 +95,16 @@ async def validate_input(
)
except Exception as exc:
raise StudentNotFound from exc
# source = session.get_student(timetable_source_id[1], timetable_source_id[0])
elif timetable_source == "klasse":
klassen = await hass.async_add_executor_job(session.klassen)
source = klassen.filter(name=timetable_source_id)[0]
elif timetable_source == "teacher":
pass
try:
source = await hass.async_add_executor_job(
session.get_teacher, timetable_source_id[1], timetable_source_id[0]
)
except Exception as exc:
raise TeacherNotFound from exc
elif timetable_source == "subject":
pass
elif timetable_source == "room":
Expand Down Expand Up @@ -136,10 +140,8 @@ async def async_step_user(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA
)
await self.async_set_unique_id(
"{username}@{school}"
.format(**user_input)
.lower()
.replace(" ", "-"))
"{username}@{school}".format(**user_input).lower().replace(" ", "-")
)
self._abort_if_unique_id_configured()

errors = {}
Expand All @@ -162,6 +164,8 @@ async def async_step_user(
errors["base"] = "name_split_error"
except StudentNotFound:
errors["base"] = "student_not_found"
except TeacherNotFound:
errors["base"] = "teacher_not_found"
except NoRightsForTimetable:
errors["base"] = "no_rights_for_timetable"

Expand Down Expand Up @@ -192,7 +196,8 @@ async def async_step_user(
options=[
"student",
"klasse",
], # "teacher", "subject", "room"
"teacher",
], # "subject", "room"
mode="dropdown",
)
),
Expand Down Expand Up @@ -229,5 +234,9 @@ class StudentNotFound(HomeAssistantError):
"""Error to indicate there is no student with this name."""


class TeacherNotFound(HomeAssistantError):
"""Error to indicate there is no teacher with this name."""


class NoRightsForTimetable(HomeAssistantError):
"""Error to indicate there is no right for timetable."""
1 change: 1 addition & 0 deletions custom_components/webuntis/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"school_not_found": "[%key:common::config_flow::error::school_not_found%]",
"name_split_error": "[%key:common::config_flow::error::name_split_error%]",
"student_not_found": "[%key:common::config_flow::error::student_not_found%]",
"teacher_not_found": "[%key:common::config_flow::error::teacher_not_found%]",
"no_rights_for_timetable": "[%key:common::config_flow::error::no_rights_for_timetable%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
Expand Down
1 change: 1 addition & 0 deletions custom_components/webuntis/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"school_not_found": "Ungültiger Schulname/-id",
"name_split_error": "Ungültiger Name. Versuche \"vor_name, nach_name\".",
"student_not_found": "Schüler nicht gefunden. Überprüfen Sie den Namen oder wählen Sie eine andere Quelle.",
"teacher_not_found": "Lehrer nicht gefunden. Überprüfen Sie den Namen oder wählen Sie eine andere Quelle.",
"no_rights_for_timetable": "Keine rechte für Stundenplan.",
"unknown": "Unerwarteter Fehler"
},
Expand Down
1 change: 1 addition & 0 deletions custom_components/webuntis/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"school_not_found": "Invalid school name/id",
"name_split_error": "Invalid name. Try \"first_name, last_name\".",
"student_not_found": "Student not found. Check the name or pick other source.",
"teacher_not_found": "Teacher not found. Check the name or pick other source.",
"no_rights_for_timetable": "No rights for timetable.",
"unknown": "Unexpected error"
},
Expand Down

0 comments on commit 26a8da6

Please sign in to comment.