From 3bf995eb71ffcf78d5d20d3e4685b0d0ccca2bcd Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 2 Oct 2025 22:17:11 +0200 Subject: [PATCH] Fix next event in workday calendar (#153465) --- homeassistant/components/workday/calendar.py | 10 ++++++---- tests/components/workday/test_calendar.py | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/workday/calendar.py b/homeassistant/components/workday/calendar.py index 82f2942d1f9..e631ebb6e6a 100644 --- a/homeassistant/components/workday/calendar.py +++ b/homeassistant/components/workday/calendar.py @@ -10,6 +10,7 @@ from homeassistant.components.calendar import CalendarEntity, CalendarEvent from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.util import dt as dt_util from . import WorkdayConfigEntry from .const import CONF_EXCLUDES, CONF_OFFSET, CONF_WORKDAYS @@ -87,11 +88,12 @@ class WorkdayCalendarEntity(BaseWorkdayEntity, CalendarEntity): @property def event(self) -> CalendarEvent | None: """Return the next upcoming event.""" - return ( - sorted(self.event_list, key=lambda e: e.start)[0] - if self.event_list - else None + sorted_list: list[CalendarEvent] | None = ( + sorted(self.event_list, key=lambda e: e.start) if self.event_list else None ) + if not sorted_list: + return None + return [d for d in sorted_list if d.start >= dt_util.utcnow().date()][0] async def async_get_events( self, hass: HomeAssistant, start_date: datetime, end_date: datetime diff --git a/tests/components/workday/test_calendar.py b/tests/components/workday/test_calendar.py index 5e5417362a3..6aa454c860f 100644 --- a/tests/components/workday/test_calendar.py +++ b/tests/components/workday/test_calendar.py @@ -70,6 +70,11 @@ async def test_holiday_calendar_entity( async_fire_time_changed(hass) await hass.async_block_till_done() + # Binary sensor added to ensure same state for both entities + state = hass.states.get("binary_sensor.workday_sensor") + assert state is not None + assert state.state == "on" + state = hass.states.get("calendar.workday_sensor_calendar") assert state is not None assert state.state == "on" @@ -78,6 +83,10 @@ async def test_holiday_calendar_entity( async_fire_time_changed(hass) await hass.async_block_till_done() + state = hass.states.get("binary_sensor.workday_sensor") + assert state is not None + assert state.state == "off" + state = hass.states.get("calendar.workday_sensor_calendar") assert state is not None assert state.state == "off"