From 9494eaa657a54f79c5daadf3ded7ab79c83cfefb Mon Sep 17 00:00:00 2001 From: Denys Dovhan Date: Thu, 25 Jul 2024 11:37:02 +0000 Subject: [PATCH 1/2] Attempt to fix regression with next_events --- custom_components/yasno_outages/api.py | 2 +- custom_components/yasno_outages/coordinator.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/custom_components/yasno_outages/api.py b/custom_components/yasno_outages/api.py index 6775a75..9b68b7d 100644 --- a/custom_components/yasno_outages/api.py +++ b/custom_components/yasno_outages/api.py @@ -75,7 +75,7 @@ def get_group_schedule(self, city: str, group: str) -> list: def get_current_event(self, at: datetime.datetime) -> dict | None: """Get the current event.""" - for event in self.get_events(at, at + datetime.timedelta(hours=1)): + for event in self.get_events(at, at + datetime.timedelta(days=1)): if event["start"] <= at < event["end"]: return event return None diff --git a/custom_components/yasno_outages/coordinator.py b/custom_components/yasno_outages/coordinator.py index 4b8fdbf..51f51a9 100644 --- a/custom_components/yasno_outages/coordinator.py +++ b/custom_components/yasno_outages/coordinator.py @@ -140,11 +140,9 @@ def next_connectivity(self) -> datetime.date | datetime.datetime | None: """Get next connectivity time.""" now = dt_utils.now() current_event = self.get_event_at(now) - if not current_event: - return None # If current event is maybe, return the end time if self._event_to_state(current_event) == STATE_MAYBE: - return current_event.end + return current_event.end if current_event else None # Otherwise, return the next maybe event's end event = self._get_next_event_of_type(STATE_MAYBE) LOGGER.debug("Next connectivity: %s", event) From b44192a0725517347c6d20fb065f6fb9408ed03f Mon Sep 17 00:00:00 2001 From: Denys Dovhan Date: Thu, 25 Jul 2024 12:41:45 +0000 Subject: [PATCH 2/2] Do not return None early --- .../yasno_outages/coordinator.py | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/custom_components/yasno_outages/coordinator.py b/custom_components/yasno_outages/coordinator.py index 51f51a9..726afcf 100644 --- a/custom_components/yasno_outages/coordinator.py +++ b/custom_components/yasno_outages/coordinator.py @@ -158,8 +158,6 @@ def current_state(self) -> str: def get_event_at(self, at: datetime.datetime) -> CalendarEvent | None: """Get the current event.""" event = self.api.get_current_event(at) - if not event: - return None return self._get_calendar_event(event, translate=False) def get_events_between( @@ -173,19 +171,22 @@ def get_events_between( events = self.api.get_events(start_date, end_date) return [ self._get_calendar_event(event, translate=translate) for event in events - ] + ] # type: ignore[return-type] def _get_calendar_event( self, - event: dict, + event: dict | None, *, translate: bool = True, - ) -> CalendarEvent: + ) -> CalendarEvent | None: + if not event: + return None + """Transform an event into a CalendarEvent.""" - event_summary = event.get("summary") - event_start = event.get("start") - event_end = event.get("end") - translated_summary = self.event_name_map.get(event_summary) + event_summary = event.get("summary", None) + event_start = event.get("start", None) + event_end = event.get("end", None) + translated_summary = self.event_name_map.get(event_summary, None) LOGGER.debug( "Transforming event: %s (%s -> %s)", @@ -195,9 +196,9 @@ def _get_calendar_event( ) return CalendarEvent( - summary=translated_summary if translate else event_summary, # type: ignore[assignment] - start=event_start, # type: ignore[assignment] - end=event_end, # type: ignore[assignment] + summary=translated_summary if translate else event_summary, + start=event_start, + end=event_end, description=event_summary, )