Skip to content

Commit

Permalink
Fix todoist end time for tasks with due date in the future (#91874)
Browse files Browse the repository at this point in the history
Fix end time for tasks with due date in the future.

Co-authored-by: Allen Porter <allen@thebends.org>
  • Loading branch information
boralyl and allenporter committed May 27, 2023
1 parent c5e425d commit bb170a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions homeassistant/components/todoist/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async def async_setup_platform(

api = TodoistAPIAsync(token)
coordinator = TodoistCoordinator(hass, _LOGGER, SCAN_INTERVAL, api)
await coordinator.async_config_entry_first_refresh()
await coordinator.async_refresh()

async def _shutdown_coordinator(_: Event) -> None:
await coordinator.async_shutdown()
Expand Down Expand Up @@ -477,16 +477,16 @@ def create_todoist_task(self, data: Task):
end = dt.parse_datetime(
data.due.datetime if data.due.datetime else data.due.date
)
task[END] = dt.as_utc(end) if end is not None else end
task[END] = dt.as_local(end) if end is not None else end
if task[END] is not None:
if self._due_date_days is not None and (
task[END] > dt.utcnow() + self._due_date_days
task[END] > dt.now() + self._due_date_days
):
# This task is out of range of our due date;
# it shouldn't be counted.
return None

task[DUE_TODAY] = task[END].date() == dt.utcnow().date()
task[DUE_TODAY] = task[END].date() == dt.now().date()

# Special case: Task is overdue.
if task[END] <= task[START]:
Expand Down
25 changes: 25 additions & 0 deletions tests/components/todoist/test_calendar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Unit tests for the Todoist calendar platform."""
from datetime import timedelta
from http import HTTPStatus
from typing import Any
from unittest.mock import AsyncMock, patch
Expand Down Expand Up @@ -182,6 +183,30 @@ async def test_update_entity_for_custom_project_no_due_date_on(
assert state.state == "on"


@pytest.mark.parametrize(
"due",
[
Due(
date=(dt.now() + timedelta(days=3)).strftime("%Y-%m-%d"),
is_recurring=False,
string="3 days from today",
)
],
)
async def test_update_entity_for_calendar_with_due_date_in_the_future(
hass: HomeAssistant,
api: AsyncMock,
) -> None:
"""Test that a task with a due date in the future has on state and correct end_time."""
await async_update_entity(hass, "calendar.name")
state = hass.states.get("calendar.name")
assert state.state == "on"

# The end time should be in the user's timezone
expected_end_time = (dt.now() + timedelta(days=3)).strftime("%Y-%m-%d 00:00:00")
assert state.attributes["end_time"] == expected_end_time


@pytest.mark.parametrize("setup_integration", [None])
async def test_failed_coordinator_update(hass: HomeAssistant, api: AsyncMock) -> None:
"""Test a failed data coordinator update is handled correctly."""
Expand Down

0 comments on commit bb170a2

Please sign in to comment.