Skip to content

Commit

Permalink
Add order attributes (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Walsh <[email protected]>
  • Loading branch information
chris24walsh and Chris Walsh authored Dec 1, 2023
1 parent a85fc59 commit 3df817e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions custom_components/tgtg/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"documentation": "https://github.com/Chouffy/home_assistant_tgtg",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/Chouffy/home_assistant_tgtg/issues",
"requirements": ["tgtg==0.16.0"],
"version": "5.3.0"
"requirements": ["tgtg==0.16.1"],
"version": "5.4.0"
}
22 changes: 22 additions & 0 deletions custom_components/tgtg/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
ATTR_PICKUP_START = "pickup_start"
ATTR_PICKUP_END = "pickup_end"
ATTR_SOLDOUT_TIMESTAMP = "soldout_timestamp"
ATTR_ORDERS_PLACED = "orders_placed"
ATTR_TOTAL_QUANTITY_ORDERED = "total_quantity_ordered"
ATTR_PICKUP_WINDOW_CHANGED = "pickup_window_changed"
ATTR_CANCEL_UNTIL = "cancel_until"
_LOGGER = logging.getLogger(DOMAIN)


Expand Down Expand Up @@ -87,6 +91,7 @@ class TGTGSensor(SensorEntity):
global tgtg_client

tgtg_answer = None
tgtg_orders = None
item_id = None
store_name = None
item_qty = None
Expand Down Expand Up @@ -180,6 +185,22 @@ def extra_state_attributes(self) -> dict | None:
data[ATTR_PICKUP_END] = self.tgtg_answer["pickup_interval"]["end"]
if "sold_out_at" in self.tgtg_answer:
data[ATTR_SOLDOUT_TIMESTAMP] = self.tgtg_answer["sold_out_at"]

orders_placed = 0
total_quantity_ordered = 0
for order in self.tgtg_orders:
if "item_id" in order:
if order["item_id"] == self.item_id:
orders_placed += 1
if "quantity" in order:
total_quantity_ordered += order["quantity"]
if "pickup_window_changed" in order:
data[ATTR_PICKUP_WINDOW_CHANGED] = order["pickup_window_changed"]
if "cancel_until" in order:
data[ATTR_CANCEL_UNTIL] = order["cancel_until"]
data[ATTR_ORDERS_PLACED] = orders_placed
if total_quantity_ordered > 0:
data[ATTR_TOTAL_QUANTITY_ORDERED] = total_quantity_ordered
return data

def update(self) -> None:
Expand All @@ -188,6 +209,7 @@ def update(self) -> None:
"""
global tgtg_client
self.tgtg_answer = tgtg_client.get_item(item_id=self.item_id)
self.tgtg_orders = tgtg_client.get_active()["orders"]

self.store_name = self.tgtg_answer["display_name"]
self.item_qty = self.tgtg_answer["items_available"]

0 comments on commit 3df817e

Please sign in to comment.