Home Assistant Unofficial Reference 2024.12.1
todo.py
Go to the documentation of this file.
1 """A shopping list todo platform."""
2 
3 from typing import cast
4 
6  TodoItem,
7  TodoItemStatus,
8  TodoListEntity,
9  TodoListEntityFeature,
10 )
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import HomeAssistantError
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import NoMatchingShoppingListItem, ShoppingData
17 from .const import DOMAIN
18 
19 
21  hass: HomeAssistant,
22  config_entry: ConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Set up the shopping_list todo platform."""
26  shopping_data = hass.data[DOMAIN]
27  entity = ShoppingTodoListEntity(shopping_data, unique_id=config_entry.entry_id)
28  async_add_entities([entity], True)
29 
30 
32  """A To-do List representation of the Shopping List."""
33 
34  _attr_has_entity_name = True
35  _attr_translation_key = "shopping_list"
36  _attr_should_poll = False
37  _attr_supported_features = (
38  TodoListEntityFeature.CREATE_TODO_ITEM
39  | TodoListEntityFeature.DELETE_TODO_ITEM
40  | TodoListEntityFeature.UPDATE_TODO_ITEM
41  | TodoListEntityFeature.MOVE_TODO_ITEM
42  )
43 
44  def __init__(self, data: ShoppingData, unique_id: str) -> None:
45  """Initialize ShoppingTodoListEntity."""
46  self._attr_unique_id_attr_unique_id = unique_id
47  self._data_data = data
48 
49  async def async_create_todo_item(self, item: TodoItem) -> None:
50  """Add an item to the To-do list."""
51  await self._data_data.async_add(
52  item.summary, complete=(item.status == TodoItemStatus.COMPLETED)
53  )
54 
55  async def async_update_todo_item(self, item: TodoItem) -> None:
56  """Update an item to the To-do list."""
57  data = {
58  "name": item.summary,
59  "complete": item.status == TodoItemStatus.COMPLETED,
60  }
61  try:
62  await self._data_data.async_update(item.uid, data)
63  except NoMatchingShoppingListItem as err:
64  raise HomeAssistantError(
65  f"Shopping list item '{item.uid}' was not found"
66  ) from err
67 
68  async def async_delete_todo_items(self, uids: list[str]) -> None:
69  """Add an item to the To-do list."""
70  await self._data_data.async_remove_items(set(uids))
71 
73  self, uid: str, previous_uid: str | None = None
74  ) -> None:
75  """Re-order an item to the To-do list."""
76 
77  try:
78  await self._data_data.async_move_item(uid, previous_uid)
79  except NoMatchingShoppingListItem as err:
80  raise HomeAssistantError(
81  f"Shopping list item '{uid}' could not be re-ordered"
82  ) from err
83 
84  async def async_added_to_hass(self) -> None:
85  """Entity has been added to hass."""
86  # Shopping list integration doesn't currently support config entry unload
87  # so this code may not be used in practice, however it is here in case
88  # this changes in the future.
89  self.async_on_removeasync_on_remove(self._data_data.async_add_listener(self.async_write_ha_stateasync_write_ha_state))
90 
91  @property
92  def todo_items(self) -> list[TodoItem]:
93  """Get items in the To-do list."""
94  results = []
95  for item in self._data_data.items:
96  if cast(bool, item["complete"]):
97  status = TodoItemStatus.COMPLETED
98  else:
99  status = TodoItemStatus.NEEDS_ACTION
100  results.append(
101  TodoItem(
102  summary=cast(str, item["name"]),
103  uid=cast(str, item["id"]),
104  status=status,
105  )
106  )
107  return results
None __init__(self, ShoppingData data, str unique_id)
Definition: todo.py:44
None async_move_todo_item(self, str uid, str|None previous_uid=None)
Definition: todo.py:74
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_add_listener(HomeAssistant hass, Callable[[], None] listener)
Definition: __init__.py:82
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: todo.py:24