Skip to content

Commit

Permalink
Merge pull request #14 from Jurkash/develop
Browse files Browse the repository at this point in the history
test new tz vestion
  • Loading branch information
Jurkash authored Jul 16, 2024
2 parents 48d7935 + 9569893 commit 6387f4c
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions custom_components/loe_outages/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import logging
import pytz

from .models import Interval
from homeassistant.components.calendar import CalendarEvent
Expand Down Expand Up @@ -98,7 +99,7 @@ def _get_next_event_of_type(self, state_type: str) -> Interval | None:
now = dt_utils.now()
# Sort events to handle multi-day spanning events correctly
next_events = sorted(
self.get_events_between(
self.get_intervals_between(
now,
now + TIMEFRAME_TO_CHECK,
translate=False,
Expand All @@ -122,7 +123,7 @@ def next_outage(self) -> datetime.datetime | None:
def next_connectivity(self) -> datetime.datetime | None:
"""Get next connectivity time."""
now = dt_utils.now()
current_event = self.get_event_at(now)
current_event = self.get_interval_at(now)
# If current event is OFF, return the end time
if self._event_to_state(current_event) == STATE_OFF:
return current_event.endTime
Expand All @@ -136,15 +137,15 @@ def next_connectivity(self) -> datetime.datetime | None:
def current_state(self) -> str:
"""Get the current state."""
now = dt_utils.now()
event = self.get_event_at(now)
event = self.get_interval_at(now)
return self._event_to_state(event)

def get_event_at(self, at: datetime.datetime) -> Interval:
def get_interval_at(self, at: datetime.datetime) -> Interval:
"""Get the current event."""
event = self.api.get_current_event(at)
return self._get_interval_event(event, translate=False)

def get_events_between(
def get_intervals_between(
self,
start_date: datetime.datetime,
end_date: datetime.datetime,
Expand All @@ -157,24 +158,6 @@ def get_events_between(
self._get_interval_event(event, translate=translate) for event in events
]

def get_calendar_at(self, at: datetime.datetime) -> CalendarEvent:
"""Get the current event."""
event = self.api.get_current_event(at)
return self._get_calendar_event(event, translate=False)

def get_calendar_between(
self,
start_date: datetime.datetime,
end_date: datetime.datetime,
*,
translate: bool = True,
) -> list[CalendarEvent]:
"""Get all events."""
events = self.api.get_events(start_date, end_date)
return [
self._get_calendar_event(event, translate=translate) for event in events
]

def _get_interval_event(
self,
event: dict | None,
Expand Down Expand Up @@ -203,6 +186,24 @@ def _get_interval_event(
endTime=event_end,
)

def get_calendar_at(self, at: datetime.datetime) -> CalendarEvent:
"""Get the current event."""
event = self.api.get_current_event(at)
return self._get_calendar_event(event, translate=False)

def get_calendar_between(
self,
start_date: datetime.datetime,
end_date: datetime.datetime,
*,
translate: bool = True,
) -> list[CalendarEvent]:
"""Get all events."""
events = self.api.get_events(start_date, end_date)
return [
self._get_calendar_event(event, translate=translate) for event in events
]

def _get_calendar_event(
self,
event: dict | None,
Expand All @@ -213,10 +214,11 @@ def _get_calendar_event(
if not event:
return None

local_tz = pytz.timezone("Europe/Kyiv")
event_summary = event["state"]
translated_summary = self.event_name_map.get(event_summary)
event_start = dt_utils.as_local(event["startTime"])
event_end = dt_utils.as_local(event["endTime"])
event_start = event["startTime"].astimezone(local_tz)
event_end = event["endTime"].astimezone(local_tz)

LOGGER.debug(
"Transforming event: %s (%s -> %s)",
Expand Down

0 comments on commit 6387f4c

Please sign in to comment.