Skip to content

Commit

Permalink
Add pickup start/end and picture attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxWinterstein committed Jan 16, 2021
1 parent 79706c3 commit ab5db63
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Create some settings file called `settings.local.json`:

`every_n_minutes` sets the polling intervall. A value of e.g. 10 would fetch data every 10 minutes.

`timezone` (optional) as TooGoodToGo provides its times as UTC we format it to local time. See [Wikipedia](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) for valid values.

`locale` (optional) to format pickup times like _in 2 hours_. E.g. `de` for german, `en_us` for american english.

And start with the mounted settings file, e.g. for Mac OS:

```bash
Expand Down
32 changes: 31 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dynaconf = "^3.1.2"
tgtg = "^0.4.0"
coloredlogs = "^15.0"
tenacity = "^6.3.1"
arrow = "^0.17.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
2 changes: 1 addition & 1 deletion toogoodtogo_ha_mqtt_bridge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

settings = Dynaconf(
envvar_prefix="DYNACONF",
settings_files=["settings.local.json", ".secrets.toml"],
settings_files=["settings.json", "settings.local.json", ".secrets.toml"],
)

# `envvar_prefix` = export envvars with `export DYNACONF_FOO=bar`.
Expand Down
29 changes: 26 additions & 3 deletions toogoodtogo_ha_mqtt_bridge/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import threading

import arrow
import coloredlogs
import paho.mqtt.client as mqtt
import watchdog
Expand Down Expand Up @@ -54,15 +55,37 @@ def check():
json.dumps({"stock": stock}),
)

# prepare attrs
price = shop["item"]["price"]["minor_units"] / pow(10, shop["item"]["price"]["decimals"])
pickup_start_date = (
None if not stock else arrow.get(shop["pickup_interval"]["start"]).to(tz=settings.timezone)
)
pickup_end_date = (
None if not stock else arrow.get(shop["pickup_interval"]["end"]).to(tz=settings.timezone)
)
pickup_start_str = ("Unknown" if stock == 0 else pickup_start_date.to(tz=settings.timezone).format(),)
pickup_end_str = ("Unknown" if stock == 0 else pickup_end_date.to(tz=settings.timezone).format(),)
pickup_start_human = (
"Unknown"
if stock == 0
else pickup_start_date.humanize(only_distance=False, locale=settings.locale)
)
pickup_end_human = (
"Unknown" if stock == 0 else pickup_end_date.humanize(only_distance=False, locale=settings.locale)
)
picture = shop["store"]["logo_picture"]["current_url"]
mqtt_client.publish(
f"homeassistant/sensor/toogoodtogo_{item_id}/attr",
json.dumps(
{
"price": (
shop["item"]["price"]["minor_units"] / pow(10, shop["item"]["price"]["decimals"])
),
"price": price,
"stock_available": True if stock > 0 else False,
"url": f"http://share.toogoodtogo.com/item/{item_id}",
"pickup_start": pickup_start_str,
"pickup_start_human": pickup_start_human,
"pickup_end": pickup_end_str,
"pickup_end_human": pickup_end_human,
"picture": picture,
}
),
)
Expand Down
4 changes: 3 additions & 1 deletion toogoodtogo_ha_mqtt_bridge/settings.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"email": "[email protected]",
"password": "iliketurtles",
"every_n_minutes": 10
}
},
"timezone": "Europe/Berlin",
"locale": "en_us"
}
4 changes: 4 additions & 0 deletions toogoodtogo_ha_mqtt_bridge/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"timezone": "Europe/Berlin",
"locale": "en_us"
}

0 comments on commit ab5db63

Please sign in to comment.