Apply all todoist custom project filters for calendar events (#117454)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
Aaron Godfrey 2024-06-25 06:34:54 -07:00 committed by GitHub
parent bcd1243686
commit f934fea754
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 76 additions and 5 deletions

View file

@ -436,7 +436,7 @@ class TodoistProjectData:
self._coordinator = coordinator
self._name = project_data[CONF_NAME]
# If no ID is defined, fetch all tasks.
# If no ID is defined, this is a custom project.
self._id = project_data.get(CONF_ID)
# All labels the user has defined, for easy lookup.
@ -497,6 +497,13 @@ class TodoistProjectData:
SUMMARY: data.content,
}
if (
self._project_id_whitelist
and data.project_id not in self._project_id_whitelist
):
# Project isn't in `include_projects` filter.
return None
# All task Labels (optional parameter).
task[LABELS] = [
label.name for label in self._labels if label.name in data.labels
@ -625,10 +632,7 @@ class TodoistProjectData:
tasks = self._coordinator.data
if self._id is None:
project_task_data = [
task
for task in tasks
if not self._project_id_whitelist
or task.project_id in self._project_id_whitelist
task for task in tasks if self.create_todoist_task(task) is not None
]
else:
project_task_data = [task for task in tasks if task.project_id == self._id]

View file

@ -366,6 +366,73 @@ async def test_task_due_datetime(
assert await response.json() == []
@pytest.mark.parametrize(
("todoist_config", "due", "start", "end", "expected_response"),
[
(
{"custom_projects": [{"name": "Test", "labels": ["Label1"]}]},
Due(date="2023-03-30", is_recurring=False, string="Mar 30"),
"2023-03-28T00:00:00.000Z",
"2023-04-01T00:00:00.000Z",
[get_events_response({"date": "2023-03-30"}, {"date": "2023-03-31"})],
),
(
{"custom_projects": [{"name": "Test", "labels": ["custom"]}]},
Due(date="2023-03-30", is_recurring=False, string="Mar 30"),
"2023-03-28T00:00:00.000Z",
"2023-04-01T00:00:00.000Z",
[],
),
(
{"custom_projects": [{"name": "Test", "include_projects": ["Name"]}]},
Due(date="2023-03-30", is_recurring=False, string="Mar 30"),
"2023-03-28T00:00:00.000Z",
"2023-04-01T00:00:00.000Z",
[get_events_response({"date": "2023-03-30"}, {"date": "2023-03-31"})],
),
(
{"custom_projects": [{"name": "Test", "due_date_days": 1}]},
Due(date="2023-03-30", is_recurring=False, string="Mar 30"),
"2023-03-28T00:00:00.000Z",
"2023-04-01T00:00:00.000Z",
[get_events_response({"date": "2023-03-30"}, {"date": "2023-03-31"})],
),
(
{"custom_projects": [{"name": "Test", "due_date_days": 1}]},
Due(
date=(dt_util.now() + timedelta(days=2)).strftime("%Y-%m-%d"),
is_recurring=False,
string="Mar 30",
),
dt_util.now().isoformat(),
(dt_util.now() + timedelta(days=5)).isoformat(),
[],
),
],
ids=[
"in_labels_whitelist",
"not_in_labels_whitelist",
"in_include_projects",
"in_due_date_days",
"not_in_due_date_days",
],
)
async def test_events_filtered_for_custom_projects(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
start: str,
end: str,
expected_response: dict[str, Any],
) -> None:
"""Test we filter out tasks from custom projects based on their config."""
client = await hass_client()
response = await client.get(
get_events_url("calendar.test", start, end),
)
assert response.status == HTTPStatus.OK
assert await response.json() == expected_response
@pytest.mark.parametrize(
("due", "setup_platform"),
[