From 573123f02dabdab25716879a2c9257a7c5311fed Mon Sep 17 00:00:00 2001 From: Jurkash Date: Tue, 16 Jul 2024 15:16:07 +0300 Subject: [PATCH] add datatypes --- custom_components/loe_outages/api.py | 34 +++++++++----------- custom_components/loe_outages/coordinator.py | 2 +- custom_components/loe_outages/models.py | 9 ++++-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/custom_components/loe_outages/api.py b/custom_components/loe_outages/api.py index 469d6fa..f2bd68c 100644 --- a/custom_components/loe_outages/api.py +++ b/custom_components/loe_outages/api.py @@ -2,9 +2,9 @@ import logging import aiohttp -from .models import OutageSchedule import datetime import pytz +from .models import OutageSchedule LOGGER = logging.getLogger(__name__) @@ -12,12 +12,12 @@ 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.""" @@ -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 @@ -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 diff --git a/custom_components/loe_outages/coordinator.py b/custom_components/loe_outages/coordinator.py index 6fe34c9..cb1e641 100644 --- a/custom_components/loe_outages/coordinator.py +++ b/custom_components/loe_outages/coordinator.py @@ -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}" diff --git a/custom_components/loe_outages/models.py b/custom_components/loe_outages/models.py index 50d2eef..fb97659 100644 --- a/custom_components/loe_outages/models.py +++ b/custom_components/loe_outages/models.py @@ -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 @@ -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