Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
List stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codechimp committed May 29, 2024
1 parent 9ebc4d4 commit 3d9b242
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
22 changes: 20 additions & 2 deletions custom_components/mealie/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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:
Expand Down
33 changes: 28 additions & 5 deletions custom_components/mealie/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3d9b242

Please sign in to comment.