Support Bring! recently list (#109854)

* support bring recently list

* fix keyerror

---------

Co-authored-by: tr4nt0r <manni@zapto.de>
This commit is contained in:
Cyrill Raccaud 2024-02-21 16:27:59 +01:00 committed by GitHub
parent 6f1cc7b3a4
commit 6e20cc8700
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 15 deletions

View File

@ -20,7 +20,8 @@ _LOGGER = logging.getLogger(__name__)
class BringData(BringList): class BringData(BringList):
"""Coordinator data class.""" """Coordinator data class."""
items: list[BringItemsResponse] purchase_items: list[BringItemsResponse]
recently_items: list[BringItemsResponse]
class BringDataUpdateCoordinator(DataUpdateCoordinator[dict[str, BringData]]): class BringDataUpdateCoordinator(DataUpdateCoordinator[dict[str, BringData]]):
@ -56,7 +57,8 @@ class BringDataUpdateCoordinator(DataUpdateCoordinator[dict[str, BringData]]):
) from e ) from e
except BringParseException as e: except BringParseException as e:
raise UpdateFailed("Unable to parse response from bring") from 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 list_dict[lst["listUuid"]] = lst
return list_dict return list_dict

View File

@ -74,13 +74,24 @@ class BringTodoListEntity(
def todo_items(self) -> list[TodoItem]: def todo_items(self) -> list[TodoItem]:
"""Return the todo items.""" """Return the todo items."""
return [ return [
*(
TodoItem( TodoItem(
uid=item["itemId"], uid=item["itemId"],
summary=item["itemId"], summary=item["itemId"],
description=item["specification"] or "", description=item["specification"] or "",
status=TodoItemStatus.NEEDS_ACTION, status=TodoItemStatus.NEEDS_ACTION,
) )
for item in self.bring_list["items"] 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 @property
@ -103,27 +114,42 @@ class BringTodoListEntity(
"""Update an item to the To-do list. """Update an item to the To-do list.
Bring has an internal 'recent' list which we want to use instead of a todo 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: This results in following behaviour:
- Completed items will move to the "completed" section in home assistant todo - 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 list and get moved to the recently list in bring
assistant todo list completely after a short delay
- Bring items do not have unique identifiers and are using the - 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 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 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 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_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: if TYPE_CHECKING:
assert item.uid assert item.uid
if item.status == TodoItemStatus.COMPLETED: if item.status == TodoItemStatus.COMPLETED and bring_purchase_item:
await self.coordinator.bring.remove_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"], bring_list["listUuid"],
item.uid, item.uid,
) )