Skip to content

Commit

Permalink
Merge pull request #16 from denysdovhan/fix-get-current-event
Browse files Browse the repository at this point in the history
Fix next_connectivity and current_state sensors
  • Loading branch information
denysdovhan authored Jul 25, 2024
2 parents 0278f53 + b44192a commit 5a79af2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion custom_components/yasno_outages/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 14 additions & 15 deletions custom_components/yasno_outages/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -160,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(
Expand All @@ -175,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)",
Expand All @@ -197,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,
)

Expand Down

0 comments on commit 5a79af2

Please sign in to comment.