1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 13:38:04 +00:00

Fix next event in workday calendar (#153465)

This commit is contained in:
G Johansson
2025-10-02 22:17:11 +02:00
committed by GitHub
parent 2169ce1722
commit 3bf995eb71
2 changed files with 15 additions and 4 deletions

View File

@@ -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

View File

@@ -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"