1 """Google Tasks todo platform."""
3 from __future__
import annotations
5 from datetime
import UTC, date, datetime, timedelta
6 from typing
import Any, cast
12 TodoListEntityFeature,
20 from .api
import AsyncConfigEntryAuth
21 from .const
import DOMAIN
22 from .coordinator
import TaskUpdateCoordinator
27 "needsAction": TodoItemStatus.NEEDS_ACTION,
28 "completed": TodoItemStatus.COMPLETED,
30 TODO_STATUS_MAP_INV = {v: k
for k, v
in TODO_STATUS_MAP.items()}
34 """Convert TodoItem dataclass items to dictionary of attributes the tasks API."""
35 result: dict[str, str |
None] = {}
36 result[
"title"] = item.summary
37 if item.status
is not None:
38 result[
"status"] = TODO_STATUS_MAP_INV[item.status]
40 result[
"status"] = TodoItemStatus.NEEDS_ACTION
41 if (due := item.due)
is not None:
45 result[
"due"] = dt_util.start_of_local_day(due).replace(tzinfo=UTC).isoformat()
48 result[
"notes"] = item.description
53 """Convert tasks API items into a TodoItem."""
54 due: date |
None =
None
55 if (due_str := item.get(
"due"))
is not None:
58 due = datetime.fromisoformat(due_str).
date()
60 summary=item[
"title"],
62 status=TODO_STATUS_MAP.get(
63 item.get(
"status",
""),
64 TodoItemStatus.NEEDS_ACTION,
67 description=item.get(
"notes"),
72 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
74 """Set up the Google Tasks todo platform."""
75 api: AsyncConfigEntryAuth = hass.data[DOMAIN][entry.entry_id]
76 task_lists = await api.list_task_lists()
85 for task_list
in task_lists
92 CoordinatorEntity[TaskUpdateCoordinator], TodoListEntity
94 """A To-do List representation of the Shopping List."""
96 _attr_has_entity_name =
True
97 _attr_supported_features = (
98 TodoListEntityFeature.CREATE_TODO_ITEM
99 | TodoListEntityFeature.UPDATE_TODO_ITEM
100 | TodoListEntityFeature.DELETE_TODO_ITEM
101 | TodoListEntityFeature.MOVE_TODO_ITEM
102 | TodoListEntityFeature.SET_DUE_DATE_ON_ITEM
103 | TodoListEntityFeature.SET_DESCRIPTION_ON_ITEM
108 coordinator: TaskUpdateCoordinator,
110 config_entry_id: str,
113 """Initialize GoogleTaskTodoListEntity."""
121 """Get the current set of To-do items."""
122 if self.coordinator.data
is None:
127 """Add an item to the To-do list."""
128 await self.coordinator.api.insert(
135 """Update a To-do item."""
136 uid: str = cast(str, item.uid)
137 await self.coordinator.api.patch(
145 """Delete To-do items."""
150 self, uid: str, previous_uid: str |
None =
None
152 """Re-order a To-do item."""
158 """Order the task items response.
160 All tasks have an order amongst their siblings based on position.
162 Home Assistant To-do items do not support the Google Task parent/sibling
163 relationships and the desired behavior is for them to be filtered.
165 parents = [task
for task
in tasks
if task.get(
"parent")
is None]
166 parents.sort(key=
lambda task: task[
"position"])
None async_delete_todo_items(self, list[str] uids)
None async_create_todo_item(self, TodoItem item)
list[TodoItem]|None todo_items(self)
None async_update_todo_item(self, TodoItem item)
None async_move_todo_item(self, str uid, str|None previous_uid=None)
None __init__(self, TaskUpdateCoordinator coordinator, str name, str config_entry_id, str task_list_id)
TodoItem _convert_api_item(dict[str, str] item)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
list[dict[str, Any]] _order_tasks(list[dict[str, Any]] tasks)
dict[str, str|None] _convert_todo_item(TodoItem item)