diff --git a/homeassistant/components/calendar/__init__.py b/homeassistant/components/calendar/__init__.py index 86f61f0ed87..b22ac98b0dc 100644 --- a/homeassistant/components/calendar/__init__.py +++ b/homeassistant/components/calendar/__init__.py @@ -422,7 +422,7 @@ def _list_events_dict_factory( """Convert CalendarEvent dataclass items to dictionary of attributes.""" return { name: value - for name, value in obj + for name, value in _event_dict_factory(obj).items() if name in LIST_EVENT_FIELDS and value is not None } diff --git a/tests/components/calendar/conftest.py b/tests/components/calendar/conftest.py index 4d6b5adfde7..5d506d67c6f 100644 --- a/tests/components/calendar/conftest.py +++ b/tests/components/calendar/conftest.py @@ -9,3 +9,11 @@ from homeassistant.setup import async_setup_component async def setup_homeassistant(hass: HomeAssistant): """Set up the homeassistant integration.""" await async_setup_component(hass, "homeassistant", {}) + + +@pytest.fixture +def set_time_zone(hass: HomeAssistant) -> None: + """Set the time zone for the tests.""" + # Set our timezone to CST/Regina so we can check calculations + # This keeps UTC-6 all year round + hass.config.set_time_zone("America/Regina") diff --git a/tests/components/calendar/test_init.py b/tests/components/calendar/test_init.py index 9fdc76abe03..463e075d169 100644 --- a/tests/components/calendar/test_init.py +++ b/tests/components/calendar/test_init.py @@ -4,8 +4,9 @@ from __future__ import annotations from datetime import timedelta from http import HTTPStatus from typing import Any -from unittest.mock import ANY, patch +from unittest.mock import patch +from freezegun import freeze_time import pytest import voluptuous as vol @@ -386,8 +387,14 @@ async def test_create_event_service_invalid_params( ) -async def test_list_events_service(hass: HomeAssistant) -> None: - """Test listing events from the service call using exlplicit start and end time.""" +@freeze_time("2023-06-22 10:30:00+00:00") +async def test_list_events_service(hass: HomeAssistant, set_time_zone: None) -> 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 + string output values. + """ + await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}}) await hass.async_block_till_done() @@ -408,8 +415,8 @@ async def test_list_events_service(hass: HomeAssistant) -> None: assert response == { "events": [ { - "start": ANY, - "end": ANY, + "start": "2023-06-22T05:00:00-06:00", + "end": "2023-06-22T06:00:00-06:00", "summary": "Future Event", "description": "Future Description", "location": "Future Location", diff --git a/tests/components/calendar/test_trigger.py b/tests/components/calendar/test_trigger.py index 05c7d95d8ad..45dd9d6afe1 100644 --- a/tests/components/calendar/test_trigger.py +++ b/tests/components/calendar/test_trigger.py @@ -121,14 +121,6 @@ class FakeSchedule: await self.fire_time(dt_util.utcnow()) -@pytest.fixture -def set_time_zone(hass: HomeAssistant) -> None: - """Set the time zone for the tests.""" - # Set our timezone to CST/Regina so we can check calculations - # This keeps UTC-6 all year round - hass.config.set_time_zone("America/Regina") - - @pytest.fixture def fake_schedule( hass: HomeAssistant, freezer: FrozenDateTimeFactory