Fix todoist end time for tasks with due date in the future (#91874)

Fix end time for tasks with due date in the future.

Co-authored-by: Allen Porter <allen@thebends.org>
This commit is contained in:
Aaron Godfrey 2023-05-27 10:09:11 -07:00 committed by GitHub
parent c5e425d075
commit bb170a2bbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -120,7 +120,7 @@ async def async_setup_platform(
api = TodoistAPIAsync(token) api = TodoistAPIAsync(token)
coordinator = TodoistCoordinator(hass, _LOGGER, SCAN_INTERVAL, api) coordinator = TodoistCoordinator(hass, _LOGGER, SCAN_INTERVAL, api)
await coordinator.async_config_entry_first_refresh() await coordinator.async_refresh()
async def _shutdown_coordinator(_: Event) -> None: async def _shutdown_coordinator(_: Event) -> None:
await coordinator.async_shutdown() await coordinator.async_shutdown()
@ -477,16 +477,16 @@ class TodoistProjectData:
end = dt.parse_datetime( end = dt.parse_datetime(
data.due.datetime if data.due.datetime else data.due.date 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 task[END] is not None:
if self._due_date_days is not None and ( 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; # This task is out of range of our due date;
# it shouldn't be counted. # it shouldn't be counted.
return None 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. # Special case: Task is overdue.
if task[END] <= task[START]: if task[END] <= task[START]:

View File

@ -1,4 +1,5 @@
"""Unit tests for the Todoist calendar platform.""" """Unit tests for the Todoist calendar platform."""
from datetime import timedelta
from http import HTTPStatus from http import HTTPStatus
from typing import Any from typing import Any
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
@ -182,6 +183,30 @@ async def test_update_entity_for_custom_project_no_due_date_on(
assert state.state == "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]) @pytest.mark.parametrize("setup_integration", [None])
async def test_failed_coordinator_update(hass: HomeAssistant, api: AsyncMock) -> None: async def test_failed_coordinator_update(hass: HomeAssistant, api: AsyncMock) -> None:
"""Test a failed data coordinator update is handled correctly.""" """Test a failed data coordinator update is handled correctly."""