Coverage for custom_components/appliance_autonotify/todo.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-28 12:19 +0000

1"""The one to-do list of every appliance's maintenance that's due, held by the entry for automatic discovery: ticking 

2an item off says it's done, and deleting it puts it off for a month""" 

3 

4from __future__ import annotations 

5 

6from typing import TYPE_CHECKING 

7 

8from homeassistant.components.todo import TodoItem, TodoItemStatus, TodoListEntity, TodoListEntityFeature 

9 

10from .maintenance import DATA_MAINTENANCE, MaintenanceTracker 

11 

12if TYPE_CHECKING: 

13 from homeassistant.config_entries import ConfigEntry 

14 from homeassistant.core import HomeAssistant 

15 from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback 

16 

17# the list only reflects what the tracker holds, with nothing to fetch 

18PARALLEL_UPDATES = 0 

19 

20# between the entry id of the appliance and the key of its item, neither of which has it 

21UID_SEPARATOR = "|" 

22 

23 

24async def async_setup_entry( 

25 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback 

26) -> None: 

27 async_add_entities([MaintenanceList(hass.data[DATA_MAINTENANCE])]) 

28 

29 

30class MaintenanceList(TodoListEntity): 

31 _attr_has_entity_name = True 

32 _attr_translation_key = "maintenance" 

33 _attr_unique_id = "maintenance" 

34 _attr_supported_features = TodoListEntityFeature.UPDATE_TODO_ITEM | TodoListEntityFeature.DELETE_TODO_ITEM 

35 

36 def __init__(self, tracker: MaintenanceTracker) -> None: 

37 self._tracker = tracker 

38 

39 async def async_added_to_hass(self) -> None: 

40 self.async_on_remove(self._tracker.async_add_listener(self.async_write_ha_state)) 

41 

42 @property 

43 def todo_items(self) -> list[TodoItem]: # ty: ignore[incompatible-property-override] 

44 return [ 

45 TodoItem( 

46 uid=f"{entry_id}{UID_SEPARATOR}{item.key}", 

47 summary=f"{name}: {self._tracker.name(entry_id, item)}", 

48 status=TodoItemStatus.NEEDS_ACTION, 

49 description=self._tracker.message(entry_id, item), 

50 ) 

51 for entry_id, name, item in self._tracker.due_items() 

52 ] 

53 

54 async def async_update_todo_item(self, item: TodoItem) -> None: 

55 if item.uid and item.status == TodoItemStatus.COMPLETED: 

56 entry_id, _, key = item.uid.partition(UID_SEPARATOR) 

57 await self._tracker.async_done(entry_id, key, self._context) 

58 

59 async def async_delete_todo_items(self, uids: list[str]) -> None: 

60 for uid in uids: 

61 entry_id, _, key = uid.partition(UID_SEPARATOR) 

62 self._tracker.async_snooze(entry_id, key)