mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Fix google tasks due date timezone handling (#132498)
This commit is contained in:
parent
8827454dbd
commit
30504fc9bd
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import date, datetime, timedelta
|
from datetime import UTC, date, datetime, timedelta
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from homeassistant.components.todo import (
|
from homeassistant.components.todo import (
|
||||||
@ -39,8 +39,10 @@ def _convert_todo_item(item: TodoItem) -> dict[str, str | None]:
|
|||||||
else:
|
else:
|
||||||
result["status"] = TodoItemStatus.NEEDS_ACTION
|
result["status"] = TodoItemStatus.NEEDS_ACTION
|
||||||
if (due := item.due) is not None:
|
if (due := item.due) is not None:
|
||||||
# due API field is a timestamp string, but with only date resolution
|
# due API field is a timestamp string, but with only date resolution.
|
||||||
result["due"] = dt_util.start_of_local_day(due).isoformat()
|
# 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:
|
else:
|
||||||
result["due"] = None
|
result["due"] = None
|
||||||
result["notes"] = item.description
|
result["notes"] = item.description
|
||||||
@ -51,6 +53,8 @@ def _convert_api_item(item: dict[str, str]) -> TodoItem:
|
|||||||
"""Convert tasks API items into a TodoItem."""
|
"""Convert tasks API items into a TodoItem."""
|
||||||
due: date | None = None
|
due: date | None = None
|
||||||
if (due_str := item.get("due")) is not 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()
|
due = datetime.fromisoformat(due_str).date()
|
||||||
return TodoItem(
|
return TodoItem(
|
||||||
summary=item["title"],
|
summary=item["title"],
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
)
|
)
|
||||||
# ---
|
# ---
|
||||||
# name: test_create_todo_list_item[due].1
|
# 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]
|
# name: test_create_todo_list_item[summary]
|
||||||
tuple(
|
tuple(
|
||||||
@ -137,7 +137,7 @@
|
|||||||
)
|
)
|
||||||
# ---
|
# ---
|
||||||
# name: test_partial_update[due_date].1
|
# 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]
|
# name: test_partial_update[empty_description]
|
||||||
tuple(
|
tuple(
|
||||||
@ -166,6 +166,33 @@
|
|||||||
# name: test_partial_update_status[api_responses0].1
|
# name: test_partial_update_status[api_responses0].1
|
||||||
'{"title": "Water", "status": "needsAction", "due": null, "notes": null}'
|
'{"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]
|
# name: test_update_todo_list_item[api_responses0]
|
||||||
tuple(
|
tuple(
|
||||||
'https://tasks.googleapis.com/tasks/v1/lists/task-list-id-1/tasks/some-task-id?alt=json',
|
'https://tasks.googleapis.com/tasks/v1/lists/task-list-id-1/tasks/some-task-id?alt=json',
|
||||||
|
@ -239,6 +239,7 @@ def mock_http_response(response_handler: list | Callable) -> Mock:
|
|||||||
yield mock_response
|
yield mock_response
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("timezone", ["America/Regina", "UTC", "Asia/Tokyo"])
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"api_responses",
|
"api_responses",
|
||||||
[
|
[
|
||||||
@ -251,7 +252,7 @@ def mock_http_response(response_handler: list | Callable) -> Mock:
|
|||||||
"title": "Task 1",
|
"title": "Task 1",
|
||||||
"status": "needsAction",
|
"status": "needsAction",
|
||||||
"position": "0000000000000001",
|
"position": "0000000000000001",
|
||||||
"due": "2023-11-18T00:00:00+00:00",
|
"due": "2023-11-18T00:00:00Z",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "task-2",
|
"id": "task-2",
|
||||||
@ -271,8 +272,10 @@ async def test_get_items(
|
|||||||
integration_setup: Callable[[], Awaitable[bool]],
|
integration_setup: Callable[[], Awaitable[bool]],
|
||||||
hass_ws_client: WebSocketGenerator,
|
hass_ws_client: WebSocketGenerator,
|
||||||
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
|
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
|
||||||
|
timezone: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test getting todo list items."""
|
"""Test getting todo list items."""
|
||||||
|
await hass.config.async_set_time_zone(timezone)
|
||||||
|
|
||||||
assert await integration_setup()
|
assert await integration_setup()
|
||||||
|
|
||||||
@ -484,6 +487,39 @@ async def test_update_todo_list_item(
|
|||||||
assert call.kwargs.get("body") == snapshot
|
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(
|
@pytest.mark.parametrize(
|
||||||
"api_responses",
|
"api_responses",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user