1
mirror of https://github.com/home-assistant/core synced 2024-08-28 03:36:46 +02:00

Limit fields returned for the list events service (#95506)

* Limit fields returned for the list events service

* Update websocket tests and fix bugs in response fields

* Omit 'None' fields in the list events response
This commit is contained in:
Allen Porter 2023-06-29 10:25:25 -07:00 committed by GitHub
parent 63218adb65
commit 7252c33df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 13 deletions

View File

@ -60,6 +60,7 @@ from .const import (
EVENT_TIME_FIELDS,
EVENT_TYPES,
EVENT_UID,
LIST_EVENT_FIELDS,
CalendarEntityFeature,
)
@ -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:
@ -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
]
}

View File

@ -41,3 +41,12 @@ EVENT_TIME_FIELDS = {
}
EVENT_TYPES = "event_types"
EVENT_DURATION = "duration"
# Fields for the list events service
LIST_EVENT_FIELDS = {
"start",
"end",
EVENT_SUMMARY,
EVENT_DESCRIPTION,
EVENT_LOCATION,
}

View File

@ -4,7 +4,7 @@ from __future__ import annotations
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
@ -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(

View File

@ -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,
}
]
}