Skip to content

Commit

Permalink
Merge pull request #1 from sh00t2kill/add-specification-handling
Browse files Browse the repository at this point in the history
Add specification (ie sub item) handling
  • Loading branch information
sh00t2kill authored Nov 19, 2023
2 parents d458d38 + b605504 commit ad58339
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
8 changes: 6 additions & 2 deletions custom_components/bring/bring.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,23 @@ async def purchase_item(self, item, specification: str = None):
)

# add/move something to the recent items
async def recent_item(self, item):
async def recent_item(self, item, specification: str = None):
item = await self.reverse_translate(item)
params = {"recently": item}
if specification:
params["specification"] = specification
await self.__put(
f"bringlists/{self.bringListUUID}",
params=params,
headers=self.addheaders,
)

# remove an item completely (from recent and purchase)
async def remove_item(self, item):
async def remove_item(self, item, specification: str = None):
item = await self.reverse_translate(item)
params = {"remove": item}
if specification:
params["specification"] = specification
await self.__put(
f"bringlists/{self.bringListUUID}",
params=params,
Expand Down
66 changes: 51 additions & 15 deletions custom_components/bring/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def state(self):
all_items = self.coordinator.data[self.uuid]
for item in all_items["purchase"]:
bring_item = BringTodoItem(self.coordinator.bring_api, item["name"], self.uuid)
if item["specification"] and item["specification"] != "":
bring_item.set_specification(item["specification"])
#These are active items -- lets do our thing
if item['name'] not in self._processed_items:
_LOGGER.debug(f"Found new item {item['name']}")
Expand All @@ -81,6 +83,8 @@ def state(self):
for item in all_items["recently"]:
bring_item = BringTodoItem(self.coordinator.bring_api, item["name"], self.uuid)
bring_item.set_status(TodoItemStatus.COMPLETED)
if item["specification"] and item["specification"] != "":
bring_item.set_specification(item["specification"])
# These are completed items
if item['name'] not in self._processed_items:
self._items.append(bring_item)
Expand All @@ -95,30 +99,49 @@ def state(self):
self.remove_outdated_list_items()
return len(all_items["purchase"])

async def async_remove_outdated_list_items(self):
#await self.remove_outdated_list_items()
await asyncio.to_thread(self.remove_outdated_list_items)

def remove_outdated_list_items(self):
bring_todo_items = []
all_items = self.coordinator.data[self.uuid]
for api_item in all_items["purchase"]:
bring_todo_items.append(BringTodoItem(self.coordinator.bring_api, api_item["name"], self.uuid))
todo_item = BringTodoItem(self.coordinator.bring_api, api_item["name"], self.uuid)
if api_item["specification"] and api_item["specification"] != "":
todo_item.set_specification(api_item["specification"])
bring_todo_items.append(todo_item)
for api_item in all_items["recently"]:
todo_item = BringTodoItem(self.coordinator.bring_api, api_item["name"], self.uuid)
if api_item["specification"] and api_item["specification"] != "":
todo_item.set_specification(api_item["specification"])
todo_item.set_status(TodoItemStatus.COMPLETED)
bring_todo_items.append(todo_item)

for existing_ha_item in self._items:
_LOGGER.debug(f"Checking if {existing_ha_item} no longer exists in Bring!")
#_LOGGER.debug(f"Checking if {existing_ha_item} no longer exists in Bring!")
_LOGGER.debug(f"Checking if {existing_ha_item.get_summary()} {existing_ha_item.get_specification()} no longer exists in Bring!")
if existing_ha_item not in bring_todo_items:
_LOGGER.debug(f"Removing {existing_ha_item.get_summary()} from list")
self._items.remove(existing_ha_item)
self._processed_items.remove(existing_ha_item.get_summary())

async def async_create_todo_item(self, item):
_LOGGER.debug(f"Creating new item {item.summary}")
#_LOGGER.debug(f"Creating new item {item.summary}")
await self.coordinator.bring_api.set_list_by_uuid(self.uuid)
await self.coordinator.bring_api.purchase_item(item.summary)
bring_item = BringTodoItem(self.coordinator.bring_api, item.summary, self.uuid)
item_summary = item.summary
item_specification = None
if ":" in item.summary:
split = item.summary.split(":")
item_summary = split[0]
item_specification = split[1]
_LOGGER.debug(f"Creating new item with Summary: {item_summary} Specification:{item_specification}")
await self.coordinator.bring_api.purchase_item(item_summary, item_specification)
bring_item = BringTodoItem(self.coordinator.bring_api, item_summary, self.uuid)
if item_specification:
bring_item.set_specification(item_specification)
self._items.append(bring_item)
self._processed_items.append(item.summary)
self._processed_items.append(bring_item.get_summary())
await self.coordinator.async_request_refresh()

# Note: This removes it from the API, and lets the List state remove it from the lst
Expand All @@ -127,12 +150,14 @@ async def async_delete_todo_items(self, uids: list[str]) -> None:
for item in self._items:
item_uid = item.get_uid()
if item_uid == uid:
item_name = item.get_summary()
item_summary = item.get_summary()
item_specification = item.get_specification()
break
await self.coordinator.bring_api.set_list_by_uuid(self.uuid)
await self.coordinator.bring_api.remove_item(item_name)
await self.coordinator.bring_api.remove_item(item_summary, item_specification)
await self.coordinator.async_request_refresh()
self.remove_outdated_list_items()
_LOGGER.debug("Syncing removed items")
await self.async_remove_outdated_list_items()

async def async_update_todo_item(self, item: TodoItem) -> None:
_LOGGER.debug(f"Updating item {item.summary}")
Expand All @@ -150,7 +175,8 @@ async def async_update_todo_item(self, item: TodoItem) -> None:

class BringTodoItem(TodoItem):
def __init__(self, api, summary, list_uuid):
self.summary = summary
self._summary = summary
self.specification = None
self.status = TodoItemStatus.NEEDS_ACTION
self.bring_api = api
self.list_uuid = list_uuid
Expand All @@ -159,8 +185,18 @@ def __init__(self, api, summary, list_uuid):
def uid(self):
return f"{self.list_uuid}_item_{self.summary.replace(' ', '_')}"

@property
def summary(self):
return f"{self._summary}:{self.specification}" if self.specification else self._summary

def set_summary(self, summary):
self.summary = summary
self._summary = summary

def set_specification(self, specification):
self.specification = specification

def get_specification(self):
return self.specification

def set_status(self, status):
self.status = status
Expand All @@ -169,7 +205,7 @@ def get_uid(self):
return self.uid

def get_summary(self):
return self.summary
return self._summary

def update_local_status(self):
if self.status == TodoItemStatus.NEEDS_ACTION:
Expand All @@ -179,14 +215,14 @@ def update_local_status(self):

async def update_status(self):
if self.status == TodoItemStatus.NEEDS_ACTION:
await self.bring_api.recent_item(self.summary)
await self.bring_api.recent_item(self._summary, self.specification)
self.status = TodoItemStatus.COMPLETED
elif self.status == TodoItemStatus.COMPLETED:
self.status = TodoItemStatus.NEEDS_ACTION
await self.bring_api.purchase_item(self.summary)
await self.bring_api.purchase_item(self._summary, self.specification)

async def purchase_item(self):
self.bring_api.purchase_item(self.summary)
self.bring_api.purchase_item(self._summary, self.specification)

@property
def state(self):
Expand Down

0 comments on commit ad58339

Please # to comment.