From ab5db6393681258db657de96cb7383bbd9501701 Mon Sep 17 00:00:00 2001 From: Max Winterstein Date: Sat, 16 Jan 2021 17:25:44 +0100 Subject: [PATCH] Add pickup start/end and picture attribute --- README.md | 4 +++ poetry.lock | 32 ++++++++++++++++++- pyproject.toml | 1 + toogoodtogo_ha_mqtt_bridge/config.py | 2 +- toogoodtogo_ha_mqtt_bridge/main.py | 29 +++++++++++++++-- .../settings.example.json | 4 ++- toogoodtogo_ha_mqtt_bridge/settings.json | 4 +++ 7 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 toogoodtogo_ha_mqtt_bridge/settings.json diff --git a/README.md b/README.md index 6342bcb..4fb2703 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/poetry.lock b/poetry.lock index 2143bb2..9f9dc5b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,3 +1,14 @@ +[[package]] +name = "arrow" +version = "0.17.0" +description = "Better dates & times for Python" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" + +[package.dependencies] +python-dateutil = ">=2.7.0" + [[package]] name = "certifi" version = "2020.12.5" @@ -83,6 +94,17 @@ category = "main" optional = false python-versions = "*" +[[package]] +name = "python-dateutil" +version = "2.8.1" +description = "Extensions to the standard Python datetime module" +category = "main" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" + +[package.dependencies] +six = ">=1.5" + [[package]] name = "requests" version = "2.25.1" @@ -150,9 +172,13 @@ socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] [metadata] lock-version = "1.1" python-versions = "^3.7" -content-hash = "fffe0f60113bb00fa8219201f17a3a9fcf5387dc66990048dce77e7439506961" +content-hash = "81af92b2546d48020b9c286f5fd9cdc1610b4cb7fea48777fa0fc4987f527675" [metadata.files] +arrow = [ + {file = "arrow-0.17.0-py2.py3-none-any.whl", hash = "sha256:e098abbd9af3665aea81bdd6c869e93af4feb078e98468dd351c383af187aac5"}, + {file = "arrow-0.17.0.tar.gz", hash = "sha256:ff08d10cda1d36c68657d6ad20d74fbea493d980f8b2d45344e00d6ed2bf6ed4"}, +] certifi = [ {file = "certifi-2020.12.5-py2.py3-none-any.whl", hash = "sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830"}, {file = "certifi-2020.12.5.tar.gz", hash = "sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c"}, @@ -185,6 +211,10 @@ pyreadline = [ {file = "pyreadline-2.1.win32.exe", hash = "sha256:65540c21bfe14405a3a77e4c085ecfce88724743a4ead47c66b84defcf82c32e"}, {file = "pyreadline-2.1.zip", hash = "sha256:4530592fc2e85b25b1a9f79664433da09237c1a270e4d78ea5aa3a2c7229e2d1"}, ] +python-dateutil = [ + {file = "python-dateutil-2.8.1.tar.gz", hash = "sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c"}, + {file = "python_dateutil-2.8.1-py2.py3-none-any.whl", hash = "sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a"}, +] requests = [ {file = "requests-2.25.1-py2.py3-none-any.whl", hash = "sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e"}, {file = "requests-2.25.1.tar.gz", hash = "sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804"}, diff --git a/pyproject.toml b/pyproject.toml index bf458fe..4c35943 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"] diff --git a/toogoodtogo_ha_mqtt_bridge/config.py b/toogoodtogo_ha_mqtt_bridge/config.py index 0ef0b1f..34418d9 100644 --- a/toogoodtogo_ha_mqtt_bridge/config.py +++ b/toogoodtogo_ha_mqtt_bridge/config.py @@ -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`. diff --git a/toogoodtogo_ha_mqtt_bridge/main.py b/toogoodtogo_ha_mqtt_bridge/main.py index a3212f4..7f19fe1 100644 --- a/toogoodtogo_ha_mqtt_bridge/main.py +++ b/toogoodtogo_ha_mqtt_bridge/main.py @@ -3,6 +3,7 @@ import os import threading +import arrow import coloredlogs import paho.mqtt.client as mqtt import watchdog @@ -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, } ), ) diff --git a/toogoodtogo_ha_mqtt_bridge/settings.example.json b/toogoodtogo_ha_mqtt_bridge/settings.example.json index 83c9feb..ad3368f 100644 --- a/toogoodtogo_ha_mqtt_bridge/settings.example.json +++ b/toogoodtogo_ha_mqtt_bridge/settings.example.json @@ -9,5 +9,7 @@ "email": "me@example.ocm", "password": "iliketurtles", "every_n_minutes": 10 - } + }, + "timezone": "Europe/Berlin", + "locale": "en_us" } diff --git a/toogoodtogo_ha_mqtt_bridge/settings.json b/toogoodtogo_ha_mqtt_bridge/settings.json new file mode 100644 index 0000000..8e70b45 --- /dev/null +++ b/toogoodtogo_ha_mqtt_bridge/settings.json @@ -0,0 +1,4 @@ +{ + "timezone": "Europe/Berlin", + "locale": "en_us" +}