Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """DataUpdateCoordinator for the Todoist component."""
2 
3 from datetime import timedelta
4 import logging
5 
6 from todoist_api_python.api_async import TodoistAPIAsync
7 from todoist_api_python.models import Label, Project, Section, Task
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
12 
13 
15  """Coordinator for updating task data from Todoist."""
16 
17  def __init__(
18  self,
19  hass: HomeAssistant,
20  logger: logging.Logger,
21  entry: ConfigEntry | None,
22  update_interval: timedelta,
23  api: TodoistAPIAsync,
24  token: str,
25  ) -> None:
26  """Initialize the Todoist coordinator."""
27  super().__init__(
28  hass,
29  logger,
30  config_entry=entry,
31  name="Todoist",
32  update_interval=update_interval,
33  )
34  self.apiapi = api
35  self._projects_projects: list[Project] | None = None
36  self._labels_labels: list[Label] | None = None
37  self.tokentoken = token
38 
39  async def _async_update_data(self) -> list[Task]:
40  """Fetch tasks from the Todoist API."""
41  try:
42  return await self.apiapi.get_tasks()
43  except Exception as err:
44  raise UpdateFailed(f"Error communicating with API: {err}") from err
45 
46  async def async_get_projects(self) -> list[Project]:
47  """Return todoist projects fetched at most once."""
48  if self._projects_projects is None:
49  self._projects_projects = await self.apiapi.get_projects()
50  return self._projects_projects
51 
52  async def async_get_sections(self, project_id: str) -> list[Section]:
53  """Return todoist sections for a given project ID."""
54  return await self.apiapi.get_sections(project_id=project_id)
55 
56  async def async_get_labels(self) -> list[Label]:
57  """Return todoist labels fetched at most once."""
58  if self._labels_labels is None:
59  self._labels_labels = await self.apiapi.get_labels()
60  return self._labels_labels
list[Section] async_get_sections(self, str project_id)
Definition: coordinator.py:52
None __init__(self, HomeAssistant hass, logging.Logger logger, ConfigEntry|None entry, timedelta update_interval, TodoistAPIAsync api, str token)
Definition: coordinator.py:25