From 971fdbbbf9a59481b80543f3b202069366c2e5d0 Mon Sep 17 00:00:00 2001 From: Chouffy Date: Fri, 28 Jan 2022 10:48:57 +0100 Subject: [PATCH 1/4] Bump module to 4.0.0 --- custom_components/tgtg/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/tgtg/manifest.json b/custom_components/tgtg/manifest.json index a92fedf..4fbe2df 100644 --- a/custom_components/tgtg/manifest.json +++ b/custom_components/tgtg/manifest.json @@ -1,7 +1,7 @@ { "domain": "tgtg", "name": "TooGooToGo", - "version": "3.0.1", + "version": "4.0.0", "documentation": "https://github.com/Chouffy/home_assistant_tgtg", "issue_tracker": "https://github.com/Chouffy/home_assistant_tgtg/issues", "codeowners": ["@chouffy"], From e401407d6ce2303cd89a1dfd846bf1a51627b4d6 Mon Sep 17 00:00:00 2001 From: Chouffy Date: Fri, 28 Jan 2022 10:49:15 +0100 Subject: [PATCH 2/4] Bump dependency to 0.11.0 --- custom_components/tgtg/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/tgtg/manifest.json b/custom_components/tgtg/manifest.json index 4fbe2df..745ff04 100644 --- a/custom_components/tgtg/manifest.json +++ b/custom_components/tgtg/manifest.json @@ -5,6 +5,6 @@ "documentation": "https://github.com/Chouffy/home_assistant_tgtg", "issue_tracker": "https://github.com/Chouffy/home_assistant_tgtg/issues", "codeowners": ["@chouffy"], - "requirements": ["tgtg>=0.10.0"], + "requirements": ["tgtg>=0.11.0"], "iot_class": "cloud_polling" } From 9409011487b56f125f299875006aaef7f259c9dc Mon Sep 17 00:00:00 2001 From: Chouffy Date: Fri, 28 Jan 2022 10:49:29 +0100 Subject: [PATCH 3/4] Implement few attributes --- custom_components/tgtg/sensor.py | 94 ++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/custom_components/tgtg/sensor.py b/custom_components/tgtg/sensor.py index 4e65196..89c78a3 100644 --- a/custom_components/tgtg/sensor.py +++ b/custom_components/tgtg/sensor.py @@ -17,16 +17,23 @@ CONF_ACCESS_TOKEN = "access_token" CONF_REFRESH_TOKEN = "refresh_token" CONF_USER_ID = "user_id" +ATTR_ITEM_ID = "Item ID" +ATTR_ITEM_ID_URL = "Item URL" +ATTR_PRICE = "TGTG Price" +ATTR_VALUE = "Original value" +ATTR_PICKUP_START = "Pickup start" +ATTR_PICKUP_STOP = "Pickup stop" +ATTR_SOLDOUT_DATE = "Soldout date" _LOGGER = logging.getLogger(DOMAIN) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { - vol.Optional(CONF_USERNAME): cv.string, + vol.Required(CONF_USERNAME): cv.string, vol.Optional(CONF_ITEM, default=""): cv.ensure_list, - vol.Required(CONF_ACCESS_TOKEN, default=""): cv.string, - vol.Required(CONF_REFRESH_TOKEN, default=""): cv.string, - vol.Required(CONF_USER_ID, default=""): cv.string, + vol.Optional(CONF_ACCESS_TOKEN, default=""): cv.string, + vol.Optional(CONF_REFRESH_TOKEN, default=""): cv.string, + vol.Optional(CONF_USER_ID, default=""): cv.string, } ) @@ -50,7 +57,9 @@ def setup_platform( global tgtg_client # Log in with tokens - tgtg_client = TgtgClient(access_token=access_token, refresh_token=refresh_token, user_id=user_id) + tgtg_client = TgtgClient( + access_token=access_token, refresh_token=refresh_token, user_id=user_id + ) # If item: isn't defined, use favorites - otherwise use defined items if item != [""]: @@ -59,13 +68,15 @@ def setup_platform( else: tgtgReply = tgtg_client.get_items() for item in tgtgReply: - add_entities([TGTGSensor(item['item']['item_id'])]) + add_entities([TGTGSensor(item["item"]["item_id"])]) class TGTGSensor(SensorEntity): """Representation of a Sensor.""" global tgtg_client + + tgtg_answer = None item_id = None store_name = None item_qty = None @@ -94,17 +105,80 @@ def icon(self): def unit_of_measurement(self): """Return the unit of measurement.""" return "pcs" - + @property def native_value(self) -> str: """Return the state of the sensor.""" return self.item_qty + @property + def extra_state_attributes(self) -> dict | None: + """Return the optional state attributes.""" + if not self.tgtg_answer: + return None + data = {} + # if self.tgtg_answer["item"]["item_id"]: + if "item" in self.tgtg_answer: + if "item_id" in self.tgtg_answer["item"]: + data[ATTR_ITEM_ID] = self.tgtg_answer["item"]["item_id"] + data[ATTR_ITEM_ID_URL] = "https://share.toogoodtogo.com/item/" + str( + self.tgtg_answer["item"]["item_id"] + ) + if "price_including_taxes" in self.tgtg_answer["item"]: + data[ATTR_PRICE] = ( + str( + int( + self.tgtg_answer["item"]["price_including_taxes"][ + "minor_units" + ] + ) + / pow( + 10, + int( + self.tgtg_answer["item"]["price_including_taxes"][ + "decimals" + ] + ), + ) + ) + + " " + + self.tgtg_answer["item"]["price_including_taxes"]["code"] + ) + if "value_including_taxes" in self.tgtg_answer["item"]: + data[ATTR_VALUE] = ( + str( + int( + self.tgtg_answer["item"]["value_including_taxes"][ + "minor_units" + ] + ) + / pow( + 10, + int( + self.tgtg_answer["item"]["value_including_taxes"][ + "decimals" + ] + ), + ) + ) + + " " + + self.tgtg_answer["item"]["value_including_taxes"]["code"] + ) + if "pickup_interval" in self.tgtg_answer: + if "start" in self.tgtg_answer["pickup_interval"]: + data[ATTR_PICKUP_START] = self.tgtg_answer["pickup_interval"]["start"] + if "end" in self.tgtg_answer["pickup_interval"]: + data[ATTR_PICKUP_STOP] = self.tgtg_answer["pickup_interval"]["end"] + if "sold_out_at" in self.tgtg_answer: + data[ATTR_SOLDOUT_DATE] = self.tgtg_answer["sold_out_at"] + return data + def update(self) -> None: """Fetch new state data for the sensor. This is the only method that should fetch new data for Home Assistant. """ global tgtg_client - tgtg_answer = tgtg_client.get_item(item_id=self.item_id) - self.store_name = tgtg_answer["display_name"] - self.item_qty = tgtg_answer["items_available"] + self.tgtg_answer = tgtg_client.get_item(item_id=self.item_id) + + self.store_name = self.tgtg_answer["display_name"] + self.item_qty = self.tgtg_answer["items_available"] From 13fbc34d85872e8c58f16e03a30c54841f52a5b3 Mon Sep 17 00:00:00 2001 From: Chouffy Date: Fri, 28 Jan 2022 10:50:53 +0100 Subject: [PATCH 4/4] Update Readme with Attributes --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3392e48..3520c12 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Sensor data can be used afterward to generate notifications, history graphs, ... 1. Click *Install* 1. Install required packages on your local PC: * [Python >=3.8](https://www.python.org/downloads/) - * [tgtg-python](https://github.com/ahivert/tgtg-python) library: In a command line, type `pip install tgtg>=0.10.0` or `pip install --upgrade tgtg` if you already have it. + * [tgtg-python](https://github.com/ahivert/tgtg-python) library: In a command line, type `pip install tgtg>=0.11.0` or `pip install --upgrade tgtg` if you already have it. 1. Run the [tgtg_get_tokens](./tgtg_get_tokens.py) script on your local PC: 1. Paste the result in your `/config/configuration.yaml`. 1. Restart the Home Assistant server @@ -58,6 +58,11 @@ Check the [tgtg_get_favorites_item_id](./tgtg_get_favorites_item_id.py) script! * Fetch each item stock defined * Authenticate using tokens * Retrieve all favorites instead of a manual list of item_id if no `item:` are defined +* Retrieve additional information as attributes, if available: + * Item ID + * TooGoodToGo price and original value + * Pick-up start and end + * Sold-out date ## Q&A