Skip to content

Commit

Permalink
Merge pull request #20 from Chouffy/dev_attributes
Browse files Browse the repository at this point in the history
Add attributes to sensor
  • Loading branch information
Chouffy committed Jan 28, 2022
2 parents 550ef98 + 13fbc34 commit 2604a68
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 13 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions custom_components/tgtg/manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"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"],
"requirements": ["tgtg>=0.10.0"],
"requirements": ["tgtg>=0.11.0"],
"iot_class": "cloud_polling"
}
94 changes: 84 additions & 10 deletions custom_components/tgtg/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)

Expand All @@ -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 != [""]:
Expand All @@ -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
Expand Down Expand Up @@ -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"]

0 comments on commit 2604a68

Please sign in to comment.