From 26a8da6bbe918e9c0f99efe9de338d42251cc758 Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 7 Nov 2022 15:41:13 +0000 Subject: [PATCH] add login method for teacher --- custom_components/webuntis/__init__.py | 16 ++++++++--- custom_components/webuntis/config_flow.py | 27 ++++++++++++------- custom_components/webuntis/strings.json | 1 + .../webuntis/translations/de.json | 1 + .../webuntis/translations/en.json | 1 + 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/custom_components/webuntis/__init__.py b/custom_components/webuntis/__init__.py index d03db05..b4a81b9 100644 --- a/custom_components/webuntis/__init__.py +++ b/custom_components/webuntis/__init__.py @@ -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": @@ -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 @@ -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() diff --git a/custom_components/webuntis/config_flow.py b/custom_components/webuntis/config_flow.py index 0651a12..550f876 100644 --- a/custom_components/webuntis/config_flow.py +++ b/custom_components/webuntis/config_flow.py @@ -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", ) ), @@ -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: @@ -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": @@ -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 = {} @@ -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" @@ -192,7 +196,8 @@ async def async_step_user( options=[ "student", "klasse", - ], # "teacher", "subject", "room" + "teacher", + ], # "subject", "room" mode="dropdown", ) ), @@ -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.""" diff --git a/custom_components/webuntis/strings.json b/custom_components/webuntis/strings.json index 9cbf833..67af06d 100644 --- a/custom_components/webuntis/strings.json +++ b/custom_components/webuntis/strings.json @@ -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%]" }, diff --git a/custom_components/webuntis/translations/de.json b/custom_components/webuntis/translations/de.json index 89f24fc..f6877ce 100644 --- a/custom_components/webuntis/translations/de.json +++ b/custom_components/webuntis/translations/de.json @@ -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" }, diff --git a/custom_components/webuntis/translations/en.json b/custom_components/webuntis/translations/en.json index d9d3a67..73854e0 100644 --- a/custom_components/webuntis/translations/en.json +++ b/custom_components/webuntis/translations/en.json @@ -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" },