Fix timezones used in list events (#95804)

* Fix timezones used in list events

* Add additional tests that catch floating vs timezone datetime comparisons
This commit is contained in:
Allen Porter 2023-07-04 23:25:03 -07:00 committed by GitHub
parent 60e2ee86b2
commit 26f2fabd85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 15 deletions

View file

@ -793,7 +793,9 @@ async def async_list_events_service(
end = start + service_call.data[EVENT_DURATION]
else:
end = service_call.data[EVENT_END_DATETIME]
calendar_event_list = await calendar.async_get_events(calendar.hass, start, end)
calendar_event_list = await calendar.async_get_events(
calendar.hass, dt_util.as_local(start), dt_util.as_local(end)
)
return {
"events": [
dataclasses.asdict(event, dict_factory=_list_events_dict_factory)

View file

@ -67,14 +67,6 @@ class DemoCalendar(CalendarEntity):
end_date: datetime.datetime,
) -> list[CalendarEvent]:
"""Return calendar events within a datetime range."""
if start_date.tzinfo is None:
start_date = start_date.replace(
tzinfo=dt_util.get_time_zone(hass.config.time_zone)
)
if end_date.tzinfo is None:
end_date = end_date.replace(
tzinfo=dt_util.get_time_zone(hass.config.time_zone)
)
assert start_date < end_date
if self._event.start_datetime_local >= end_date:
return []

View file

@ -388,7 +388,17 @@ async def test_create_event_service_invalid_params(
@freeze_time("2023-06-22 10:30:00+00:00")
async def test_list_events_service(hass: HomeAssistant, set_time_zone: None) -> None:
@pytest.mark.parametrize(
("start_time", "end_time"),
[
("2023-06-22T04:30:00-06:00", "2023-06-22T06:30:00-06:00"),
("2023-06-22T04:30:00", "2023-06-22T06:30:00"),
("2023-06-22T10:30:00Z", "2023-06-22T12:30:00Z"),
],
)
async def test_list_events_service(
hass: HomeAssistant, set_time_zone: None, start_time: str, end_time: str
) -> None:
"""Test listing events from the service call using exlplicit start and end time.
This test uses a fixed date/time so that it can deterministically test the
@ -398,16 +408,13 @@ async def test_list_events_service(hass: HomeAssistant, set_time_zone: None) ->
await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}})
await hass.async_block_till_done()
start = dt_util.now()
end = start + timedelta(days=1)
response = await hass.services.async_call(
DOMAIN,
SERVICE_LIST_EVENTS,
{
"entity_id": "calendar.calendar_1",
"start_date_time": start,
"end_date_time": end,
"start_date_time": start_time,
"end_date_time": end_time,
},
blocking=True,
return_response=True,