Skip to content

Commit

Permalink
fix: multi-day spanning events
Browse files Browse the repository at this point in the history
  • Loading branch information
denysdovhan committed Jul 13, 2024
1 parent e7e2d8b commit 84fc2f7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions custom_components/yasno_outages/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ async def async_fetch_translations(self) -> None:
def _get_next_event_of_type(self, state_type: str) -> CalendarEvent | None:
"""Get the next event of a specific type."""
now = dt_utils.now()
next_events = self.get_events_between(
now,
now + TIMEFRAME_TO_CHECK,
translate=False,
# Sort events to handle multi-day spanning events correctly
next_events = sorted(
self.get_events_between(
now,
now + TIMEFRAME_TO_CHECK,
translate=False,
),
key=lambda event: event.start,
)
LOGGER.debug("Next events: %s", next_events)
for event in next_events:
if self._event_to_state(event) == state_type and event.start > now:
return event
Expand All @@ -110,12 +115,14 @@ def _get_next_event_of_type(self, state_type: str) -> CalendarEvent | None:
def next_outage(self) -> datetime.datetime | None:
"""Get the next outage time."""
event = self._get_next_event_of_type(STATE_OFF)
LOGGER.debug("Next outage: %s", event)
return event.start if event else None

@property
def next_possible_outage(self) -> datetime.datetime | None:
"""Get the next possible outage time."""
event = self._get_next_event_of_type(STATE_MAYBE)
LOGGER.debug("Next possible outage: %s", event)
return event.start if event else None

@property
Expand All @@ -129,6 +136,7 @@ def next_connectivity(self) -> datetime.datetime | None:

# Otherwise, return the next maybe event's end
event = self._get_next_event_of_type(STATE_MAYBE)
LOGGER.debug("Next connectivity: %s", event)
return event.end if event else None

@property
Expand Down

0 comments on commit 84fc2f7

Please sign in to comment.