From bb170a2bbf4583784b216bf4576a7734b4152ca4 Mon Sep 17 00:00:00 2001 From: Aaron Godfrey Date: Sat, 27 May 2023 10:09:11 -0700 Subject: [PATCH] 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 --- homeassistant/components/todoist/calendar.py | 8 +++---- tests/components/todoist/test_calendar.py | 25 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/todoist/calendar.py b/homeassistant/components/todoist/calendar.py index 1c356a857a6..33e9e9c8d2c 100644 --- a/homeassistant/components/todoist/calendar.py +++ b/homeassistant/components/todoist/calendar.py @@ -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() @@ -477,16 +477,16 @@ class TodoistProjectData: 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]: diff --git a/tests/components/todoist/test_calendar.py b/tests/components/todoist/test_calendar.py index 5a6a05623c7..7cbb73c4e92 100644 --- a/tests/components/todoist/test_calendar.py +++ b/tests/components/todoist/test_calendar.py @@ -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 @@ -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."""