Skip to content

Commit

Permalink
improved error logging in config/ option flow
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasJoKuJonas committed Feb 29, 2024
1 parent c678f0f commit 4f68e0e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
22 changes: 9 additions & 13 deletions custom_components/webuntis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The Web Untis integration."""

from __future__ import annotations

import json
Expand Down Expand Up @@ -631,9 +632,9 @@ def _get_events(self):
elif self.calendar_room == "Room short name":
event["location"] = lesson.rooms[0].name
elif self.calendar_room == "Room short-long name":
event[
"location"
] = f"{lesson.rooms[0].name} - {lesson.rooms[0].long_name}"
event["location"] = (
f"{lesson.rooms[0].name} - {lesson.rooms[0].long_name}"
)
except IndexError:
# server does not return rooms
pass
Expand Down Expand Up @@ -926,16 +927,11 @@ async def update_notify(self):
if self.notify_data:
notification["target"] = self.notify_target
notification["data"] = self.notify_data
try:
await async_notify(
self._hass, service=self.notify_entity_id, data=notification
)
except Exception as error:
_LOGGER.warning(
"Sending notification to %s failed - %s",
self.notify_entity_id,
error,
)

await async_notify(
self._hass, service=self.notify_entity_id, data=notification
)

self.event_list_old = self.event_list


Expand Down
26 changes: 15 additions & 11 deletions custom_components/webuntis/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Config flow for webuntisnew integration."""

from __future__ import annotations

import datetime
Expand Down Expand Up @@ -158,23 +159,23 @@ async def async_step_user(
try:
info = await validate_input(self.hass, user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
errors["server"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
errors["name"] = "invalid_auth"
except BadCredentials:
errors["base"] = "bad_credentials"
errors["password"] = "bad_credentials"
except SchoolNotFound:
errors["base"] = "school_not_found"
errors["school"] = "school_not_found"
except NameSplitError:
errors["base"] = "name_split_error"
errors["timetable_source_id"] = "name_split_error"
except StudentNotFound:
errors["base"] = "student_not_found"
errors["timetable_source_id"] = "student_not_found"
except TeacherNotFound:
errors["base"] = "teacher_not_found"
errors["timetable_source_id"] = "teacher_not_found"
except ClassNotFound:
errors["base"] = "class_not_found"
errors["timetable_source_id"] = "class_not_found"
except NoRightsForTimetable:
errors["base"] = "no_rights_for_timetable"
errors["timetable_source_id"] = "no_rights_for_timetable"

except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
Expand Down Expand Up @@ -540,10 +541,13 @@ async def async_step_test(
notification["data"] = options["notify_data"]
notify_entity_id = options.get("notify_entity_id")

await async_notify(
success = await async_notify(
self.hass, service=notify_entity_id, data=notification
)
pass
if not success:
return await self.async_step_test(
None, {"base": "notification_invalid"}
)
return await self.save({})


Expand Down
4 changes: 2 additions & 2 deletions custom_components/webuntis/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"error": {
"extended_timetable": "Für die Optionen \"Fächer Filter - Beschreibung\" und \"Kalender-Beschreibung\" (Lesson Info) wird \"Erweiterten Stundenplan anfordern\" benötigt",
"unknown_service": "Service-ID nicht gefunden.",
"notification_invalid": "Benachrichtigung konnte nicht gesendet werden. Siehe Logs für mehr Infos.",
"unknown": "Unerwarteter Fehler"
},
"step": {
Expand Down Expand Up @@ -112,7 +113,6 @@
"cannot_connect": "Verbindung fehlgeschlagen. Überprüfe die Server-Domain.",
"invalid_auth": "Ungültige Authentifizierung",
"bad_credentials": "Ungültiges Passwort",

"unknown": "Unerwarteter Fehler"
},
"step": {
Expand Down Expand Up @@ -186,4 +186,4 @@
"name": "Stunden zählen"
}
}
}
}
5 changes: 2 additions & 3 deletions custom_components/webuntis/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"teacher_not_found": "Teacher not found. Check the name or choose another source.",
"class_not_found": "Class not found. Check the class or choose another source.",
"no_rights_for_timetable": "No rights for timetable.",

"unknown": "Unexpected error"
},
"step": {
Expand All @@ -33,6 +32,7 @@
"error": {
"extended_timetable": "\"Request extended timetable\" is required for the \"Subject Filter - Description\" and \"Calendar Description\" (Lesson Info) option",
"unknown_service": "Service ID not found.",
"notification_invalid": "Notification could not be sent. See logs for more information.",
"unknown": "Unexpected error"
},
"step": {
Expand Down Expand Up @@ -112,7 +112,6 @@
"cannot_connect": "Failed to connect. Please check the host.",
"invalid_auth": "Invalid authentication",
"bad_credentials": "Invalid password",

"unknown": "Unexpected error"
},
"step": {
Expand Down Expand Up @@ -186,4 +185,4 @@
"name": "Count Hours"
}
}
}
}
13 changes: 11 additions & 2 deletions custom_components/webuntis/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Miscellaneous support functions for webuntis"""


from datetime import datetime
import logging

Expand Down Expand Up @@ -112,4 +111,14 @@ async def async_notify(hass, service, data):
domain = service.split(".")[0]
service = service.split(".")[1]

await hass.services.async_call(domain, service, data, blocking=True)
try:
await hass.services.async_call(domain, service, data, blocking=True)
except Exception as error:
_LOGGER.warning(
"Sending notification to %s failed - %s",
service,
error,
)
return False

return True

0 comments on commit 4f68e0e

Please sign in to comment.