1 """CalDAV todo platform."""
3 from __future__
import annotations
6 from datetime
import date, datetime, timedelta
7 from functools
import partial
9 from typing
import Any, cast
12 from caldav.lib.error
import DAVError, NotFoundError
19 TodoListEntityFeature,
26 from .
import CalDavConfigEntry
27 from .api
import async_get_calendars, get_attr_value
29 _LOGGER = logging.getLogger(__name__)
33 SUPPORTED_COMPONENT =
"VTODO"
35 "NEEDS-ACTION": TodoItemStatus.NEEDS_ACTION,
36 "IN-PROCESS": TodoItemStatus.NEEDS_ACTION,
37 "COMPLETED": TodoItemStatus.COMPLETED,
38 "CANCELLED": TodoItemStatus.COMPLETED,
40 TODO_STATUS_MAP_INV: dict[TodoItemStatus, str] = {
41 TodoItemStatus.NEEDS_ACTION:
"NEEDS-ACTION",
42 TodoItemStatus.COMPLETED:
"COMPLETED",
48 entry: CalDavConfigEntry,
49 async_add_entities: AddEntitiesCallback,
51 """Set up the CalDav todo platform for a config entry."""
59 for calendar
in calendars
65 def _todo_item(resource: caldav.CalendarObjectResource) -> TodoItem |
None:
66 """Convert a caldav Todo into a TodoItem."""
68 not hasattr(resource.instance,
"vtodo")
69 or not (todo := resource.instance.vtodo)
74 due: date | datetime |
None =
None
76 if isinstance(due_value, datetime):
77 due = dt_util.as_local(due_value)
78 elif isinstance(due_value, date):
83 status=TODO_STATUS_MAP.get(
85 TodoItemStatus.NEEDS_ACTION,
93 """CalDAV To-do list entity."""
95 _attr_has_entity_name =
True
96 _attr_supported_features = (
97 TodoListEntityFeature.CREATE_TODO_ITEM
98 | TodoListEntityFeature.UPDATE_TODO_ITEM
99 | TodoListEntityFeature.DELETE_TODO_ITEM
100 | TodoListEntityFeature.SET_DUE_DATE_ON_ITEM
101 | TodoListEntityFeature.SET_DUE_DATETIME_ON_ITEM
102 | TodoListEntityFeature.SET_DESCRIPTION_ON_ITEM
105 def __init__(self, calendar: caldav.Calendar, config_entry_id: str) ->
None:
106 """Initialize WebDavTodoListEntity."""
108 self.
_attr_name_attr_name = (calendar.name
or "Unknown").capitalize()
112 """Update To-do list entity state."""
113 results = await self.
hasshass.async_add_executor_job(
117 include_completed=
True,
122 for resource
in results
123 if (todo_item :=
_todo_item(resource))
is not None
127 """Add an item to the To-do list."""
128 item_data: dict[str, Any] = {}
129 if summary := item.summary:
130 item_data[
"summary"] = summary
131 if status := item.status:
132 item_data[
"status"] = TODO_STATUS_MAP_INV.get(status,
"NEEDS-ACTION")
134 item_data[
"due"] = due
135 if description := item.description:
136 item_data[
"description"] = description
138 await self.
hasshass.async_add_executor_job(
139 partial(self.
_calendar_calendar.save_todo, **item_data),
141 except (requests.ConnectionError, DAVError)
as err:
145 """Update a To-do item."""
146 uid: str = cast(str, item.uid)
148 todo = await self.
hasshass.async_add_executor_job(
151 except NotFoundError
as err:
153 except (requests.ConnectionError, DAVError)
as err:
155 vtodo = todo.icalendar_component
156 vtodo[
"SUMMARY"] = item.summary
or ""
157 if status := item.status:
158 vtodo[
"STATUS"] = TODO_STATUS_MAP_INV.get(status,
"NEEDS-ACTION")
162 vtodo.pop(
"DUE",
None)
163 if description := item.description:
164 vtodo[
"DESCRIPTION"] = description
166 vtodo.pop(
"DESCRIPTION",
None)
168 await self.
hasshass.async_add_executor_job(
175 except (requests.ConnectionError, DAVError)
as err:
179 """Delete To-do items."""
181 self.
hasshass.async_add_executor_job(self.
_calendar_calendar.todo_by_uid, uid)
186 items = await asyncio.gather(*tasks)
187 except NotFoundError
as err:
189 except (requests.ConnectionError, DAVError)
as err:
195 await self.
hasshass.async_add_executor_job(item.delete)
196 except (requests.ConnectionError, DAVError)
as err:
None async_create_todo_item(self, TodoItem item)
None async_update_todo_item(self, TodoItem item)
None async_delete_todo_items(self, list[str] uids)
None __init__(self, caldav.Calendar calendar, str config_entry_id)
list[caldav.Calendar] async_get_calendars(HomeAssistant hass, caldav.DAVClient client, str component)
str|None get_attr_value(caldav.CalendarObjectResource obj, str attribute)
None async_setup_entry(HomeAssistant hass, CalDavConfigEntry entry, AddEntitiesCallback async_add_entities)
TodoItem|None _todo_item(caldav.CalendarObjectResource resource)