From 6e20cc87009959a3d55f53dfaa228448a512347a Mon Sep 17 00:00:00 2001 From: Cyrill Raccaud Date: Wed, 21 Feb 2024 16:27:59 +0100 Subject: [PATCH] Support Bring! recently list (#109854) * support bring recently list * fix keyerror --------- Co-authored-by: tr4nt0r --- homeassistant/components/bring/coordinator.py | 6 ++- homeassistant/components/bring/todo.py | 52 ++++++++++++++----- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/bring/coordinator.py b/homeassistant/components/bring/coordinator.py index 9005f429823..dbb6905473d 100644 --- a/homeassistant/components/bring/coordinator.py +++ b/homeassistant/components/bring/coordinator.py @@ -20,7 +20,8 @@ _LOGGER = logging.getLogger(__name__) class BringData(BringList): """Coordinator data class.""" - items: list[BringItemsResponse] + purchase_items: list[BringItemsResponse] + recently_items: list[BringItemsResponse] class BringDataUpdateCoordinator(DataUpdateCoordinator[dict[str, BringData]]): @@ -56,7 +57,8 @@ class BringDataUpdateCoordinator(DataUpdateCoordinator[dict[str, BringData]]): ) from e except BringParseException as e: raise UpdateFailed("Unable to parse response from bring") from e - lst["items"] = items["purchase"] + lst["purchase_items"] = items["purchase"] + lst["recently_items"] = items["recently"] list_dict[lst["listUuid"]] = lst return list_dict diff --git a/homeassistant/components/bring/todo.py b/homeassistant/components/bring/todo.py index 8bdd0ebaf36..68f3f7a8da5 100644 --- a/homeassistant/components/bring/todo.py +++ b/homeassistant/components/bring/todo.py @@ -74,13 +74,24 @@ class BringTodoListEntity( def todo_items(self) -> list[TodoItem]: """Return the todo items.""" return [ - TodoItem( - uid=item["itemId"], - summary=item["itemId"], - description=item["specification"] or "", - status=TodoItemStatus.NEEDS_ACTION, - ) - for item in self.bring_list["items"] + *( + TodoItem( + uid=item["itemId"], + summary=item["itemId"], + description=item["specification"] or "", + status=TodoItemStatus.NEEDS_ACTION, + ) + for item in self.bring_list["purchase_items"] + ), + *( + TodoItem( + uid=item["itemId"], + summary=item["itemId"], + description=item["specification"] or "", + status=TodoItemStatus.COMPLETED, + ) + for item in self.bring_list["recently_items"] + ), ] @property @@ -103,27 +114,42 @@ class BringTodoListEntity( """Update an item to the To-do list. Bring has an internal 'recent' list which we want to use instead of a todo list - status, therefore completed todo list items will directly be deleted + status, therefore completed todo list items are matched to the recent list and pending items to the purchase list This results in following behaviour: - Completed items will move to the "completed" section in home assistant todo - list and get deleted in bring, which will remove them from the home - assistant todo list completely after a short delay + list and get moved to the recently list in bring - Bring items do not have unique identifiers and are using the name/summery/title. Therefore the name is not to be changed! Should a name be changed anyway, a new item will be created instead and no update for this item is performed and on the next cloud pull update, it will get - cleared + cleared and replaced seamlessly """ bring_list = self.bring_list + bring_purchase_item = next( + (i for i in bring_list["purchase_items"] if i["itemId"] == item.uid), + None, + ) + + bring_recently_item = next( + (i for i in bring_list["recently_items"] if i["itemId"] == item.uid), + None, + ) + if TYPE_CHECKING: assert item.uid - if item.status == TodoItemStatus.COMPLETED: - await self.coordinator.bring.remove_item( + if item.status == TodoItemStatus.COMPLETED and bring_purchase_item: + await self.coordinator.bring.complete_item( + bring_list["listUuid"], + item.uid, + ) + + elif item.status == TodoItemStatus.NEEDS_ACTION and bring_recently_item: + await self.coordinator.bring.save_item( bring_list["listUuid"], item.uid, )