Skip to content

Commit

Permalink
add datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurkash committed Jul 16, 2024
1 parent 3ac9f31 commit 573123f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
34 changes: 16 additions & 18 deletions custom_components/loe_outages/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

import logging
import aiohttp
from .models import OutageSchedule
import datetime
import pytz
from .models import OutageSchedule

LOGGER = logging.getLogger(__name__)


class LoeOutagesApi:
"""Class to interact with API for Loe outages."""

schedule: list[OutageSchedule]
schedules: list[OutageSchedule]

def __init__(self, group: str) -> None:
"""Initialize the LoeOutagesApi."""
self.group = group
self.schedule = []
self.schedules = []

async def async_fetch_latest_json(self) -> dict:
"""Fetch outages from the async API endpoint."""
Expand All @@ -43,36 +43,34 @@ async def async_fetch_all_json(self) -> dict:
LOGGER.error(f"Failed to fetch schedule: {response.status}")
return None

async def async_fetch_schedule(self) -> None:
async def async_fetch_schedules(self) -> None:
"""Fetch outages from the JSON response."""
if len(self.schedule) == 0:
schedule_data = await self.async_fetch_all_json()
schedules = OutageSchedule.from_list(schedule_data)
if len(self.schedules) == 0:
schedules_data = await self.async_fetch_all_json()
schedules = OutageSchedule.from_list(schedules_data)
for schedule in sorted(schedules, key=lambda s: s.date):
self.schedule.append(schedule)
self.schedules.append(schedule)
return
else:
schedule_data = await self.async_fetch_latest_json()
schedule = OutageSchedule.from_dict(schedule_data)

new_schedule = OutageSchedule.from_dict(schedule_data)
self.schedule = [
self.schedules = [
item
for item in self.schedule
for item in self.schedules
if item.dateString != new_schedule.dateString
]
self.schedule.append(new_schedule)
self.schedule.sort(key=lambda item: item.dateString)

self.schedule = sorted(self.schedule, key=lambda s: s.date)
self.schedules.append(new_schedule)
self.schedules.sort(key=lambda item: item.date)

def get_current_event(self, at: datetime) -> dict:
"""Get the current event."""
if not self.schedule:
if not self.schedules:
return None

twoDaysBefore = datetime.datetime.now() + datetime.timedelta(days=-2)
for schedule in reversed(self.schedule):
for schedule in reversed(self.schedules):
if schedule.date < twoDaysBefore.astimezone(pytz.UTC):
return None

Expand All @@ -87,12 +85,12 @@ def get_events(
end_date: datetime.datetime,
) -> list[dict]:
"""Get all events."""
if not self.schedule:
if not self.schedules:
return []

result = []
twoDaysBeforeStart = start_date + datetime.timedelta(days=-2)
for schedule in reversed(self.schedule):
for schedule in reversed(self.schedules):
if schedule.date < twoDaysBeforeStart:
break

Expand Down
2 changes: 1 addition & 1 deletion custom_components/loe_outages/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def _async_update_data(self) -> None:
"""Fetch data from API."""
try:
await self.async_fetch_translations()
return await self.api.async_fetch_schedule()
return await self.api.async_fetch_schedules()
except FileNotFoundError as err:
LOGGER.exception("Cannot read file for group %s", self.group)
msg = f"File not found: {err}"
Expand Down
9 changes: 7 additions & 2 deletions custom_components/loe_outages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class Interval:
def __init__(self, state: str, startTime: str, endTime: str):
def __init__(self, state: str, startTime: datetime, endTime: datetime):
self.state = state
self.startTime = startTime
self.endTime = endTime
Expand Down Expand Up @@ -49,7 +49,12 @@ def to_dict(self) -> dict:

class OutageSchedule:
def __init__(
self, id: str, date: str, dateString: str, imageUrl: str, groups: List[Group]
self,
id: str,
date: datetime,
dateString: str,
imageUrl: str,
groups: List[Group],
):
self.id = id
self.date = date
Expand Down

0 comments on commit 573123f

Please sign in to comment.