From 7d552b64f7ebcf743c2c479fc3cc7c32d50650e4 Mon Sep 17 00:00:00 2001 From: "Mr. Bubbles" Date: Thu, 15 Aug 2024 20:06:23 +0200 Subject: [PATCH] Use `clearCompletedTodos` API endpoint for deleting Habitica todos (#121877) Use clearCompletedTodos endpoint for deleting multiple completed todo items --- homeassistant/components/habitica/strings.json | 5 ++++- homeassistant/components/habitica/todo.py | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/habitica/strings.json b/homeassistant/components/habitica/strings.json index 5696e6f9911..21d2622245c 100644 --- a/homeassistant/components/habitica/strings.json +++ b/homeassistant/components/habitica/strings.json @@ -100,7 +100,10 @@ }, "exceptions": { "delete_todos_failed": { - "message": "Unable to delete {count} Habitica to-do(s), please try again" + "message": "Unable to delete item from Habitica to-do list, please try again" + }, + "delete_completed_todos_failed": { + "message": "Unable to delete completed to-do items from Habitica to-do list, please try again" }, "move_todos_item_failed": { "message": "Unable to move the Habitica to-do to position {pos}, please try again" diff --git a/homeassistant/components/habitica/todo.py b/homeassistant/components/habitica/todo.py index ab458f9f59f..451db1e6806 100644 --- a/homeassistant/components/habitica/todo.py +++ b/homeassistant/components/habitica/todo.py @@ -75,14 +75,23 @@ class BaseHabiticaListEntity(HabiticaBase, TodoListEntity): async def async_delete_todo_items(self, uids: list[str]) -> None: """Delete Habitica tasks.""" - for task_id in uids: + if len(uids) > 1 and self.entity_description.key is HabiticaTodoList.TODOS: try: - await self.coordinator.api.tasks[task_id].delete() + await self.coordinator.api.tasks.clearCompletedTodos.post() except ClientResponseError as e: raise ServiceValidationError( translation_domain=DOMAIN, - translation_key=f"delete_{self.entity_description.key}_failed", + translation_key="delete_completed_todos_failed", ) from e + else: + for task_id in uids: + try: + await self.coordinator.api.tasks[task_id].delete() + except ClientResponseError as e: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key=f"delete_{self.entity_description.key}_failed", + ) from e await self.coordinator.async_refresh()