Skip to content

Commit 66ff9d6

Browse files
gjohansson-STfrenck
authored andcommitted
Fix next event in workday calendar (home-assistant#153465)
1 parent b2a63d4 commit 66ff9d6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

homeassistant/components/workday/calendar.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from homeassistant.const import CONF_NAME
1111
from homeassistant.core import HomeAssistant
1212
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
13+
from homeassistant.util import dt as dt_util
1314

1415
from . import WorkdayConfigEntry
1516
from .const import CONF_EXCLUDES, CONF_OFFSET, CONF_WORKDAYS
@@ -87,11 +88,12 @@ def update_data(self, now: datetime) -> None:
8788
@property
8889
def event(self) -> CalendarEvent | None:
8990
"""Return the next upcoming event."""
90-
return (
91-
sorted(self.event_list, key=lambda e: e.start)[0]
92-
if self.event_list
93-
else None
91+
sorted_list: list[CalendarEvent] | None = (
92+
sorted(self.event_list, key=lambda e: e.start) if self.event_list else None
9493
)
94+
if not sorted_list:
95+
return None
96+
return [d for d in sorted_list if d.start >= dt_util.utcnow().date()][0]
9597

9698
async def async_get_events(
9799
self, hass: HomeAssistant, start_date: datetime, end_date: datetime

tests/components/workday/test_calendar.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ async def test_holiday_calendar_entity(
7070
async_fire_time_changed(hass)
7171
await hass.async_block_till_done()
7272

73+
# Binary sensor added to ensure same state for both entities
74+
state = hass.states.get("binary_sensor.workday_sensor")
75+
assert state is not None
76+
assert state.state == "on"
77+
7378
state = hass.states.get("calendar.workday_sensor_calendar")
7479
assert state is not None
7580
assert state.state == "on"
@@ -78,6 +83,10 @@ async def test_holiday_calendar_entity(
7883
async_fire_time_changed(hass)
7984
await hass.async_block_till_done()
8085

86+
state = hass.states.get("binary_sensor.workday_sensor")
87+
assert state is not None
88+
assert state.state == "off"
89+
8190
state = hass.states.get("calendar.workday_sensor_calendar")
8291
assert state is not None
8392
assert state.state == "off"

0 commit comments

Comments
 (0)