Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Format with black
Browse files Browse the repository at this point in the history
  • Loading branch information
johanohly committed Feb 9, 2024
1 parent e2f8a55 commit 78af5ed
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
4 changes: 1 addition & 3 deletions custom_components/bedste_lectio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
from .const import CONF_SCHOOL, DOMAIN
from .coordinator import BedsteLectioDataUpdateCoordinator

PLATFORMS: list[Platform] = [
Platform.SENSOR
]
PLATFORMS: list[Platform] = [Platform.SENSOR]


# https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry
Expand Down
14 changes: 6 additions & 8 deletions custom_components/bedste_lectio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ class BedsteLectioApiClientError(Exception):
"""Exception to indicate a general API error."""


class BedsteLectioApiClientCommunicationError(
BedsteLectioApiClientError
):
class BedsteLectioApiClientCommunicationError(BedsteLectioApiClientError):
"""Exception to indicate a communication error."""


class BedsteLectioApiClientAuthenticationError(
BedsteLectioApiClientError
):
class BedsteLectioApiClientAuthenticationError(BedsteLectioApiClientError):
"""Exception to indicate an authentication error."""


Expand All @@ -45,11 +41,13 @@ def __init__(
async def async_get_next_room(self) -> any:
"""Get next room from the API."""
return await self._api_wrapper(
method="get", url=f"{BEDSTELECTIO_API_URL}/ha/frontpage", headers={
method="get",
url=f"{BEDSTELECTIO_API_URL}/ha/frontpage",
headers={
"username": self._username,
"password": self._password,
"school": self._school,
}
},
)

async def async_get_schools(self) -> list[str]:
Expand Down
4 changes: 3 additions & 1 deletion custom_components/bedste_lectio/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ async def async_step_user(
errors=_errors,
)

async def _test_credentials(self, username: str, password: str, school: str) -> None:
async def _test_credentials(
self, username: str, password: str, school: str
) -> None:
"""Validate credentials."""
client = BedsteLectioApiClient(
username=username,
Expand Down
34 changes: 21 additions & 13 deletions custom_components/bedste_lectio/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,27 @@ def get_next_room(entries: list[dict[str, str]]) -> dict[str, str]:

rooms = []
for entry in entries:
dt_str =re.sub(r"(?<!\d)(\d)(?!\d)", "0\\1", entry["tidspunkt"].split(" til")[0])
date = datetime.strptime(dt_str, "%d/%m-%Y %H:%M").replace(tzinfo=ZoneInfo("Europe/Copenhagen"))
dt_str = re.sub(
r"(?<!\d)(\d)(?!\d)", "0\\1", entry["tidspunkt"].split(" til")[0]
)
date = datetime.strptime(dt_str, "%d/%m-%Y %H:%M").replace(
tzinfo=ZoneInfo("Europe/Copenhagen")
)
if date < datetime.now().astimezone(ZoneInfo("Europe/Copenhagen")):
continue

rooms.append({
"room": entry.get("lokale", "N/A"),
"activity": entry.get("navn") or entry.get("hold", "N/A"),
"class": entry.get("hold", "N/A"),
"teacher": entry.get("lærer", "N/A"),
"start": date,
})
rooms.append(
{
"room": entry.get("lokale", "N/A"),
"activity": entry.get("navn") or entry.get("hold", "N/A"),
"class": entry.get("hold", "N/A"),
"teacher": entry.get("lærer", "N/A"),
"start": date,
}
)

if rooms:
return rooms[0] # The first entry is closest to now that hasn't passed
return rooms[0] # The first entry is closest to now that hasn't passed
else:
return {
"room": "N/A",
Expand Down Expand Up @@ -94,7 +100,9 @@ def extra_state_attributes(self) -> dict[str, any]:
"""Return the state attributes of the entity."""
entries = self.coordinator.data.get("skema")
data = get_next_room(entries)
data.update({
"last_update": datetime.now().astimezone(ZoneInfo("Europe/Copenhagen")),
})
data.update(
{
"last_update": datetime.now().astimezone(ZoneInfo("Europe/Copenhagen")),
}
)
return data

0 comments on commit 78af5ed

Please sign in to comment.