From 34d25cf9e6711f44910bda0480890c28b268bdaf Mon Sep 17 00:00:00 2001 From: "Mr. Bubbles" Date: Mon, 4 Mar 2024 15:57:37 +0100 Subject: [PATCH] Bump bring-api to 0.5.4 (#111654) --- homeassistant/components/bring/coordinator.py | 6 +- homeassistant/components/bring/manifest.json | 2 +- homeassistant/components/bring/todo.py | 103 +++++++++++------- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 69 insertions(+), 46 deletions(-) diff --git a/homeassistant/components/bring/coordinator.py b/homeassistant/components/bring/coordinator.py index dbb6905473d..550c589aa4e 100644 --- a/homeassistant/components/bring/coordinator.py +++ b/homeassistant/components/bring/coordinator.py @@ -6,7 +6,7 @@ import logging from bring_api.bring import Bring from bring_api.exceptions import BringParseException, BringRequestException -from bring_api.types import BringItemsResponse, BringList +from bring_api.types import BringList, BringPurchase from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -20,8 +20,8 @@ _LOGGER = logging.getLogger(__name__) class BringData(BringList): """Coordinator data class.""" - purchase_items: list[BringItemsResponse] - recently_items: list[BringItemsResponse] + purchase_items: list[BringPurchase] + recently_items: list[BringPurchase] class BringDataUpdateCoordinator(DataUpdateCoordinator[dict[str, BringData]]): diff --git a/homeassistant/components/bring/manifest.json b/homeassistant/components/bring/manifest.json index 604b9d9c750..0e425ec3eee 100644 --- a/homeassistant/components/bring/manifest.json +++ b/homeassistant/components/bring/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/bring", "integration_type": "service", "iot_class": "cloud_polling", - "requirements": ["bring-api==0.4.1"] + "requirements": ["bring-api==0.5.4"] } diff --git a/homeassistant/components/bring/todo.py b/homeassistant/components/bring/todo.py index 3e4e3137aaa..5d3fc5bbf68 100644 --- a/homeassistant/components/bring/todo.py +++ b/homeassistant/components/bring/todo.py @@ -2,8 +2,10 @@ from __future__ import annotations from typing import TYPE_CHECKING +import uuid from bring_api.exceptions import BringRequestException +from bring_api.types import BringItem, BringItemOperation from homeassistant.components.todo import ( TodoItem, @@ -76,7 +78,7 @@ class BringTodoListEntity( return [ *( TodoItem( - uid=item["itemId"], + uid=item["uuid"], summary=item["itemId"], description=item["specification"] or "", status=TodoItemStatus.NEEDS_ACTION, @@ -85,7 +87,7 @@ class BringTodoListEntity( ), *( TodoItem( - uid=item["itemId"], + uid=item["uuid"], summary=item["itemId"], description=item["specification"] or "", status=TodoItemStatus.COMPLETED, @@ -103,7 +105,10 @@ class BringTodoListEntity( """Add an item to the To-do list.""" try: await self.coordinator.bring.save_item( - self.bring_list["listUuid"], item.summary, item.description or "" + self.bring_list["listUuid"], + item.summary, + item.description or "", + str(uuid.uuid4()), ) except BringRequestException as e: raise HomeAssistantError("Unable to save todo item for bring") from e @@ -121,60 +126,69 @@ class BringTodoListEntity( - Completed items will move to the "completed" section in home assistant todo 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 and replaced seamlessly + - Bring shows some odd behaviour when renaming items. This is because Bring + did not have unique identifiers for items in the past and this is still + a relic from it. Therefore the name is not to be changed! Should a name + be changed anyway, the item will be deleted and 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 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), + (i for i in bring_list["purchase_items"] if i["uuid"] == item.uid), None, ) bring_recently_item = next( - (i for i in bring_list["recently_items"] if i["itemId"] == item.uid), + (i for i in bring_list["recently_items"] if i["uuid"] == item.uid), None, ) + current_item = bring_purchase_item or bring_recently_item + if TYPE_CHECKING: assert item.uid + assert current_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, - ) - - elif item.summary == item.uid: + if item.summary == current_item["itemId"]: try: - await self.coordinator.bring.update_item( + await self.coordinator.bring.batch_update_list( bring_list["listUuid"], - item.uid, - item.description or "", + BringItem( + itemId=item.summary, + spec=item.description, + uuid=item.uid, + ), + BringItemOperation.ADD + if item.status == TodoItemStatus.NEEDS_ACTION + else BringItemOperation.COMPLETE, ) except BringRequestException as e: raise HomeAssistantError("Unable to update todo item for bring") from e else: try: - await self.coordinator.bring.remove_item( + await self.coordinator.bring.batch_update_list( bring_list["listUuid"], - item.uid, - ) - await self.coordinator.bring.save_tem( - bring_list["listUuid"], - item.summary, - item.description or "", + [ + BringItem( + itemId=current_item["itemId"], + spec=item.description, + uuid=item.uid, + operation=BringItemOperation.REMOVE, + ), + BringItem( + itemId=item.summary, + spec=item.description, + uuid=str(uuid.uuid4()), + operation=BringItemOperation.ADD + if item.status == TodoItemStatus.NEEDS_ACTION + else BringItemOperation.COMPLETE, + ), + ], ) + except BringRequestException as e: raise HomeAssistantError("Unable to replace todo item for bring") from e @@ -182,12 +196,21 @@ class BringTodoListEntity( async def async_delete_todo_items(self, uids: list[str]) -> None: """Delete an item from the To-do list.""" - for uid in uids: - try: - await self.coordinator.bring.remove_item( - self.bring_list["listUuid"], uid - ) - except BringRequestException as e: - raise HomeAssistantError("Unable to delete todo item for bring") from e + + try: + await self.coordinator.bring.batch_update_list( + self.bring_list["listUuid"], + [ + BringItem( + itemId=uid, + spec="", + uuid=uid, + ) + for uid in uids + ], + BringItemOperation.REMOVE, + ) + except BringRequestException as e: + raise HomeAssistantError("Unable to delete todo item for bring") from e await self.coordinator.async_refresh() diff --git a/requirements_all.txt b/requirements_all.txt index 8c13e868478..47e4dcf5971 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -603,7 +603,7 @@ boschshcpy==0.2.75 boto3==1.33.13 # homeassistant.components.bring -bring-api==0.4.1 +bring-api==0.5.4 # homeassistant.components.broadlink broadlink==0.18.3 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ed3b1da72d4..18f8208ae26 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -514,7 +514,7 @@ bond-async==0.2.1 boschshcpy==0.2.75 # homeassistant.components.bring -bring-api==0.4.1 +bring-api==0.5.4 # homeassistant.components.broadlink broadlink==0.18.3