From d0ec2abf20f6aa143233170cf5cb3df9e52fb0a7 Mon Sep 17 00:00:00 2001 From: yurii Date: Mon, 15 Jul 2024 11:32:23 +0300 Subject: [PATCH] Api sync improvements --- custom_components/loe_outages/api.py | 14 ++++++++++---- custom_components/loe_outages/calendar.py | 9 ++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/custom_components/loe_outages/api.py b/custom_components/loe_outages/api.py index af2034b..469d6fa 100644 --- a/custom_components/loe_outages/api.py +++ b/custom_components/loe_outages/api.py @@ -55,10 +55,16 @@ async def async_fetch_schedule(self) -> None: schedule_data = await self.async_fetch_latest_json() schedule = OutageSchedule.from_dict(schedule_data) - if self.schedule[-1].id != schedule.id: - if self.schedule[-1].dateString == schedule.dateString: - self.schedule.remove(self.schedule[-1]) - self.schedule.append(schedule) + new_schedule = OutageSchedule.from_dict(schedule_data) + self.schedule = [ + item + for item in self.schedule + 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) def get_current_event(self, at: datetime) -> dict: """Get the current event.""" diff --git a/custom_components/loe_outages/calendar.py b/custom_components/loe_outages/calendar.py index 0a69324..11e7dae 100644 --- a/custom_components/loe_outages/calendar.py +++ b/custom_components/loe_outages/calendar.py @@ -72,14 +72,13 @@ async def async_get_events( """Return calendar events within a datetime range.""" LOGGER.debug('Getting all events between "%s" -> "%s"', start_date, end_date) intervals = self.coordinator.get_events_between(start_date, end_date) - events = [] - for interval in intervals: - event = CalendarEvent( + return [ + CalendarEvent( summary=interval.state, start=interval.startTime, end=interval.endTime, description=interval.state, ) - events.append(event) - return events + for interval in intervals + ]