Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit fields returned for the list events service #95506

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions homeassistant/components/calendar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
EVENT_TIME_FIELDS,
EVENT_TYPES,
EVENT_UID,
LIST_EVENT_FIELDS,
CalendarEntityFeature,
)

Expand Down Expand Up @@ -415,6 +416,17 @@ def _api_event_dict_factory(obj: Iterable[tuple[str, Any]]) -> dict[str, Any]:
return result


def _list_events_dict_factory(
obj: Iterable[tuple[str, Any]]
) -> dict[str, JsonValueType]:
"""Convert CalendarEvent dataclass items to dictionary of attributes."""
return {
name: value
for name, value in obj
if name in LIST_EVENT_FIELDS and value is not None
}


def _get_datetime_local(
dt_or_d: datetime.datetime | datetime.date,
) -> datetime.datetime:
Expand Down Expand Up @@ -782,9 +794,9 @@ async def async_list_events_service(
else:
end = service_call.data[EVENT_END_DATETIME]
calendar_event_list = await calendar.async_get_events(calendar.hass, start, end)
events: list[JsonValueType] = [
dataclasses.asdict(event) for event in calendar_event_list
]
return {
"events": events,
"events": [
dataclasses.asdict(event, dict_factory=_list_events_dict_factory)
for event in calendar_event_list
]
}
9 changes: 9 additions & 0 deletions homeassistant/components/calendar/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ class CalendarEntityFeature(IntFlag):
}
EVENT_TYPES = "event_types"
EVENT_DURATION = "duration"

# Fields for the list events service
LIST_EVENT_FIELDS = {
"start",
"end",
EVENT_SUMMARY,
EVENT_DESCRIPTION,
EVENT_LOCATION,
}
18 changes: 12 additions & 6 deletions tests/components/calendar/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import timedelta
from http import HTTPStatus
from typing import Any
from unittest.mock import patch
from unittest.mock import ANY, patch

import pytest
import voluptuous as vol
Expand Down Expand Up @@ -405,11 +405,17 @@ async def test_list_events_service(hass: HomeAssistant) -> None:
blocking=True,
return_response=True,
)
assert response
assert "events" in response
events = response["events"]
assert len(events) == 1
assert events[0]["summary"] == "Future Event"
assert response == {
"events": [
{
"start": ANY,
"end": ANY,
"summary": "Future Event",
"description": "Future Description",
"location": "Future Location",
}
]
}


@pytest.mark.parametrize(
Expand Down
3 changes: 0 additions & 3 deletions tests/components/websocket_api/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,9 +1769,6 @@ async def test_execute_script_complex_response(
"summary": "Future Event",
"description": "Future Description",
"location": "Future Location",
"uid": None,
"recurrence_id": None,
"rrule": None,
}
]
}
Expand Down