diff --git a/custom_components/mealie/api.py b/custom_components/mealie/api.py index 373b39f..2450408 100644 --- a/custom_components/mealie/api.py +++ b/custom_components/mealie/api.py @@ -49,15 +49,29 @@ async def async_get_shopping_list_items( return await self.api_wrapper("get", "/api/groups/shopping/items", data=params) async def async_add_shopping_list_item( - self, shopping_list_id: str, summary: str + self, shopping_list_id: str, summary: str, position: int ) -> dict: data = {"isFood": "False", "checked": "False"} data["note"] = summary data["shoppingListId"] = shopping_list_id + data["position"] = position return await self.api_wrapper("post", "/api/groups/shopping/items", data=data) + async def async_update_shopping_list_item( + self, shopping_list_id: str, item_id: str, summary: str, checked: bool + ) -> dict: + + data = {"isFood": "False", "checked": checked} + data["note"] = summary + data["item_id"] = item_id + data["shoppingListId"] = shopping_list_id + + return await self.api_wrapper( + "put", f"/api/groups/shopping/items/{item_id}", data=data + ) + async def api_wrapper(self, method: str, service: str, data: dict = {}) -> any: """Get information from the API.""" @@ -89,7 +103,11 @@ async def api_wrapper(self, method: str, service: str, data: dict = {}) -> any: response = await self._session.put( url=url, json=data, - headers={"Authorization": f"bearer {self._token}"}, + headers={ + "accept": "application/json", + "Content-Type": "application/json", + "Authorization": f"bearer {self._token}", + }, ) if response.status >= 200 and response.status < 300: diff --git a/custom_components/mealie/todo.py b/custom_components/mealie/todo.py index 79989d6..049aafe 100644 --- a/custom_components/mealie/todo.py +++ b/custom_components/mealie/todo.py @@ -122,10 +122,37 @@ async def async_added_to_hass(self) -> None: async def async_create_todo_item(self, item: TodoItem) -> None: """Add an item to the To-do list.""" + position = ( + self.coordinator.shopping_list_items[self._shopping_list_id][-1].get( + "position" + ) + + 1 + ) + await self.coordinator.api.async_add_shopping_list_item( - self._shopping_list_id, item.summary + self._shopping_list_id, item.summary, position + ) + await self.coordinator.async_refresh() + + async def async_update_todo_item(self, item: TodoItem) -> None: + """Update an item to the To-do list.""" + + # try: + await self.coordinator.api.async_update_shopping_list_item( + self._shopping_list_id, + item.uid, + item.summary, + item.status == TodoItemStatus.COMPLETED, ) await self.coordinator.async_refresh() + # except NoMatchingShoppingListItem as err: + # raise HomeAssistantError( + # f"Shopping list item '{item.uid}' was not found" + # ) from err + + # async def async_delete_todo_items(self, uids: list[str]) -> None: + # """Delete an item to the To-do list.""" + # await self.coordinator.api(set(uids)) @callback def _handle_coordinator_update(self) -> None: @@ -187,10 +214,6 @@ def _handle_coordinator_update(self) -> None: # f"Shopping list item '{item.uid}' was not found" # ) from err - # async def async_delete_todo_items(self, uids: list[str]) -> None: - # """Add an item to the To-do list.""" - # await self._data.async_remove_items(set(uids)) - # async def async_move_todo_item( # self, uid: str, previous_uid: str | None = None # ) -> None: