Skip to content

Commit

Permalink
Merge pull request #5 from denysdovhan/fix-next-connectivity
Browse files Browse the repository at this point in the history
fix next connectivity sensor
  • Loading branch information
denysdovhan authored Jul 11, 2024
2 parents 6406c02 + 5d4beec commit 0fdf2c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
28 changes: 22 additions & 6 deletions custom_components/yasno_outages/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,37 @@ async def async_fetch_translations(self) -> None:
LOGGER.debug("Translations loaded: %s", self.translations)

@property
def next_outage(self) -> CalendarEvent:
"""Get the next outage."""
def next_outage(self) -> datetime.datetime | None:
"""Get the next outage time."""
next_events = self.get_next_events()
for event in next_events:
if self._event_to_state(event) == STATE_OFF:
return event
return event.start
return None

@property
def next_possible_outage(self) -> CalendarEvent:
"""Get the next outage."""
def next_possible_outage(self) -> datetime.datetime | None:
"""Get the next outage time."""
next_events = self.get_next_events()
for event in next_events:
if self._event_to_state(event) == STATE_MAYBE:
return event
return event.start
return None

@property
def next_connectivity(self) -> datetime.datetime | None:
"""Get next connectivity time."""
now = dt_utils.now()
current_event = self.get_event_at(now)
# If current event is maybe, return the end time
if self._event_to_state(current_event) == STATE_MAYBE:
return current_event.end

# Otherwise, return the next maybe event's end
next_events = self.get_next_events()
for event in next_events:
if self._event_to_state(event) == STATE_MAYBE:
return event.end
return None

@property
Expand Down
6 changes: 3 additions & 3 deletions custom_components/yasno_outages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ def get_next_outage(coordinator: YasnoOutagesCoordinator) -> str:
translation_key="next_outage",
icon="mdi:calendar-remove",
device_class=SensorDeviceClass.TIMESTAMP,
val_func=lambda coordinator: coordinator.next_outage.start,
val_func=lambda coordinator: coordinator.next_outage,
),
YasnoOutagesSensorDescription(
key="next_possible_outage",
translation_key="next_possible_outage",
icon="mdi:calendar-question",
device_class=SensorDeviceClass.TIMESTAMP,
val_func=lambda coordinator: coordinator.next_possible_outage.start,
val_func=lambda coordinator: coordinator.next_possible_outage,
),
YasnoOutagesSensorDescription(
key="next_connectivity",
translation_key="next_connectivity",
icon="mdi:calendar-check",
device_class=SensorDeviceClass.TIMESTAMP,
val_func=lambda coordinator: coordinator.next_possible_outage.end,
val_func=lambda coordinator: coordinator.next_connectivity,
),
)

Expand Down

0 comments on commit 0fdf2c2

Please sign in to comment.