Skip to content

Commit

Permalink
Fix google tasks due date timezone handling (#132498)
Browse files Browse the repository at this point in the history
  • Loading branch information
allenporter authored and frenck committed Dec 6, 2024
1 parent 8827454 commit 30504fc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
10 changes: 7 additions & 3 deletions homeassistant/components/google_tasks/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from datetime import date, datetime, timedelta
from datetime import UTC, date, datetime, timedelta
from typing import Any, cast

from homeassistant.components.todo import (
Expand Down Expand Up @@ -39,8 +39,10 @@ def _convert_todo_item(item: TodoItem) -> dict[str, str | None]:
else:
result["status"] = TodoItemStatus.NEEDS_ACTION
if (due := item.due) is not None:
# due API field is a timestamp string, but with only date resolution
result["due"] = dt_util.start_of_local_day(due).isoformat()
# due API field is a timestamp string, but with only date resolution.
# The time portion of the date is always discarded by the API, so we
# always set to UTC.
result["due"] = dt_util.start_of_local_day(due).replace(tzinfo=UTC).isoformat()
else:
result["due"] = None
result["notes"] = item.description
Expand All @@ -51,6 +53,8 @@ def _convert_api_item(item: dict[str, str]) -> TodoItem:
"""Convert tasks API items into a TodoItem."""
due: date | None = None
if (due_str := item.get("due")) is not None:
# Due dates are returned always in UTC so we only need to
# parse the date portion which will be interpreted as a a local date.
due = datetime.fromisoformat(due_str).date()
return TodoItem(
summary=item["title"],
Expand Down
31 changes: 29 additions & 2 deletions tests/components/google_tasks/snapshots/test_todo.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
# ---
# name: test_create_todo_list_item[due].1
'{"title": "Soda", "status": "needsAction", "due": "2023-11-18T00:00:00-08:00", "notes": null}'
'{"title": "Soda", "status": "needsAction", "due": "2023-11-18T00:00:00+00:00", "notes": null}'
# ---
# name: test_create_todo_list_item[summary]
tuple(
Expand Down Expand Up @@ -137,7 +137,7 @@
)
# ---
# name: test_partial_update[due_date].1
'{"title": "Water", "status": "needsAction", "due": "2023-11-18T00:00:00-08:00", "notes": null}'
'{"title": "Water", "status": "needsAction", "due": "2023-11-18T00:00:00+00:00", "notes": null}'
# ---
# name: test_partial_update[empty_description]
tuple(
Expand Down Expand Up @@ -166,6 +166,33 @@
# name: test_partial_update_status[api_responses0].1
'{"title": "Water", "status": "needsAction", "due": null, "notes": null}'
# ---
# name: test_update_due_date[api_responses0-America/Regina]
tuple(
'https://tasks.googleapis.com/tasks/v1/lists/task-list-id-1/tasks/some-task-id?alt=json',
'PATCH',
)
# ---
# name: test_update_due_date[api_responses0-America/Regina].1
'{"title": "Water", "status": "needsAction", "due": "2024-12-05T00:00:00+00:00", "notes": null}'
# ---
# name: test_update_due_date[api_responses0-Asia/Tokyo]
tuple(
'https://tasks.googleapis.com/tasks/v1/lists/task-list-id-1/tasks/some-task-id?alt=json',
'PATCH',
)
# ---
# name: test_update_due_date[api_responses0-Asia/Tokyo].1
'{"title": "Water", "status": "needsAction", "due": "2024-12-05T00:00:00+00:00", "notes": null}'
# ---
# name: test_update_due_date[api_responses0-UTC]
tuple(
'https://tasks.googleapis.com/tasks/v1/lists/task-list-id-1/tasks/some-task-id?alt=json',
'PATCH',
)
# ---
# name: test_update_due_date[api_responses0-UTC].1
'{"title": "Water", "status": "needsAction", "due": "2024-12-05T00:00:00+00:00", "notes": null}'
# ---
# name: test_update_todo_list_item[api_responses0]
tuple(
'https://tasks.googleapis.com/tasks/v1/lists/task-list-id-1/tasks/some-task-id?alt=json',
Expand Down
38 changes: 37 additions & 1 deletion tests/components/google_tasks/test_todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def mock_http_response(response_handler: list | Callable) -> Mock:
yield mock_response


@pytest.mark.parametrize("timezone", ["America/Regina", "UTC", "Asia/Tokyo"])
@pytest.mark.parametrize(
"api_responses",
[
Expand All @@ -251,7 +252,7 @@ def mock_http_response(response_handler: list | Callable) -> Mock:
"title": "Task 1",
"status": "needsAction",
"position": "0000000000000001",
"due": "2023-11-18T00:00:00+00:00",
"due": "2023-11-18T00:00:00Z",
},
{
"id": "task-2",
Expand All @@ -271,8 +272,10 @@ async def test_get_items(
integration_setup: Callable[[], Awaitable[bool]],
hass_ws_client: WebSocketGenerator,
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
timezone: str,
) -> None:
"""Test getting todo list items."""
await hass.config.async_set_time_zone(timezone)

assert await integration_setup()

Expand Down Expand Up @@ -484,6 +487,39 @@ async def test_update_todo_list_item(
assert call.kwargs.get("body") == snapshot


@pytest.mark.parametrize("timezone", ["America/Regina", "UTC", "Asia/Tokyo"])
@pytest.mark.parametrize("api_responses", [UPDATE_API_RESPONSES])
async def test_update_due_date(
hass: HomeAssistant,
setup_credentials: None,
integration_setup: Callable[[], Awaitable[bool]],
mock_http_response: Any,
snapshot: SnapshotAssertion,
timezone: str,
) -> None:
"""Test for updating the due date of a To-do item and timezone."""
await hass.config.async_set_time_zone(timezone)

assert await integration_setup()

state = hass.states.get("todo.my_tasks")
assert state
assert state.state == "1"

await hass.services.async_call(
TODO_DOMAIN,
TodoServices.UPDATE_ITEM,
{ATTR_ITEM: "some-task-id", ATTR_DUE_DATE: "2024-12-5"},
target={ATTR_ENTITY_ID: "todo.my_tasks"},
blocking=True,
)
assert len(mock_http_response.call_args_list) == 4
call = mock_http_response.call_args_list[2]
assert call
assert call.args == snapshot
assert call.kwargs.get("body") == snapshot


@pytest.mark.parametrize(
"api_responses",
[
Expand Down

0 comments on commit 30504fc

Please sign in to comment.