-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
251 lines (202 loc) · 8.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import json
import logging
import os
import threading
from pathlib import Path
from time import sleep
import arrow
import coloredlogs
import paho.mqtt.client as mqtt
import watchdog
from config import settings
from tgtg import TgtgClient
from watchdog import Watchdog
logger = logging.getLogger(__name__)
coloredlogs.install(level="DEBUG", logger=logger) # pretty logging is pretty
mqtt_client = None
first_run = True
tgtg_client = TgtgClient(email=settings.tgtg.email, language=settings.tgtg.language, timeout=30)
watchdog: Watchdog = None
def check():
global first_run
if not first_run:
tgtg_client.login()
write_token_file()
first_run = False
shops = tgtg_client.get_items(page_size=400)
for shop in shops:
stock = shop["items_available"]
item_id = shop["item"]["item_id"]
logger.debug(f"Pushing message for {shop['display_name']} // {item_id}")
# Autodiscover
result_ad = mqtt_client.publish(
f"homeassistant/sensor/toogoodtogo_{item_id}/config",
json.dumps(
{
"name": f"TooGoodToGo - {shop['display_name']}",
"icon": "mdi:food" if stock > 0 else "mdi:food-off",
"state_topic": f"homeassistant/sensor/toogoodtogo_{item_id}/state",
"json_attributes_topic": f"homeassistant/sensor/toogoodtogo_{item_id}/attr",
"unit_of_measurement": "portions",
"value_template": "{{ value_json.stock }}",
"unique_id": f"toogoodtogo_{item_id}",
}
),
)
result_state = mqtt_client.publish(
f"homeassistant/sensor/toogoodtogo_{item_id}/state",
json.dumps({"stock": stock}),
)
# prepare attrs
if shop["item"].get("price"):
price = shop["item"]["price"]["minor_units"] / pow(10, shop["item"]["price"]["decimals"])
elif shop["item"].get("price_including_taxes"):
price = shop["item"]["price_including_taxes"]["minor_units"] / pow(
10, shop["item"]["price_including_taxes"]["decimals"]
)
else:
logger.error("Can't find price")
price = 0
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)
)
# get company logo
try:
picture = shop["store"]["logo_picture"]["current_url"] # this location fits for the most
except KeyError:
try:
picture = shop["item"]["logo_picture"]["current_url"] # fits some longer existing ones
except KeyError:
# okay, i give up. Take TGTG brand logo.
picture = "https://toogoodtogo.com/images/logo/econ-textless.svg"
result_attrs = mqtt_client.publish(
f"homeassistant/sensor/toogoodtogo_{item_id}/attr",
json.dumps(
{
"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,
}
),
)
logger.debug(
f"Message published: Autodiscover: {bool(result_ad.rc == mqtt.MQTT_ERR_SUCCESS)}, "
f"State: {bool(result_state.rc == mqtt.MQTT_ERR_SUCCESS)}, "
f"Attributes: {bool(result_attrs.rc == mqtt.MQTT_ERR_SUCCESS)}"
)
if not result_ad.rc == result_state.rc == result_attrs.rc == mqtt.MQTT_ERR_SUCCESS:
logger.warning("Seems like some message was not transferred successfully.")
return False
if settings.get("cleanup"):
check_for_removed_stores(shops)
return True
def write_token_file():
tokens = {
"access_token": tgtg_client.access_token,
"access_token_lifetime": tgtg_client.access_token_lifetime,
"refresh_token": tgtg_client.refresh_token,
"user_id": tgtg_client.user_id,
"last_time_token_refreshed": str(tgtg_client.last_time_token_refreshed),
}
with open(settings.get("data_dir") + "/tokens.json", "w") as json_file:
json.dump(tokens, json_file)
logger.debug("Written tokens.json file to filesystem")
def check_existing_token_file():
if os.path.isfile(settings.get("data_dir") + "/tokens.json"):
read_token_file()
return True
else:
logger.debug("Logging in with credentials")
return False
def read_token_file():
with open(settings.get("data_dir") + "/tokens.json") as f:
tokens = json.load(f)
if tokens:
logger.debug("Loaded tokens form tokenfile. Logging in with tokens.")
rebuild_tgtg_client(tokens)
def rebuild_tgtg_client(tokens):
global tgtg_client
tgtg_client = TgtgClient(
access_token=tokens["access_token"],
refresh_token=tokens["refresh_token"],
user_id=tokens["user_id"],
language=settings.tgtg.language,
timeout=30,
)
def check_for_removed_stores(shops: []):
path = settings.get("data_dir") + "/known_shops.json"
checked_items = [shop["item"]["item_id"] for shop in shops]
if os.path.isfile(path):
logger.debug(f"known_shops.json exists at {path}")
try:
known_items = json.load(open(path, "r"))
except:
logger.error("Error happened when reading known_shops file")
return
deprecated_items = [x for x in known_items if x not in checked_items]
for deprecated_item in deprecated_items:
logger.info(f"Shop {deprecated_item} was not checked, will send remove message")
result = mqtt_client.publish(f"homeassistant/sensor/toogoodtogo_{deprecated_item}/config")
logger.debug(f"Message published: Removal: {bool(result.rc == mqtt.MQTT_ERR_SUCCESS)}")
Path(settings.get("data_dir")).mkdir(parents=True, exist_ok=True)
json.dump(checked_items, open(path, "w"))
pass
def loop(event):
logger.info("Starting loop")
token_exits = check_existing_token_file()
tgtg_client.login()
if not token_exits and tgtg_client.access_token:
write_token_file()
while True:
logger.debug("Loop run started")
if not check():
logger.error("Loop was not successfully.")
else:
logger.debug("Loop run finished")
watchdog.reset()
event.wait(settings.tgtg.every_n_minutes * 60)
def watchdog_handler():
logger.error(f"Watchdog handler fired! No pull in the last {settings.tgtg.every_n_minutes} minutes!")
os._exit(1) # easy way to die from within a thread
def on_disconnect(client, userdata, rc):
if rc != 0:
logger.error("Wow, mqtt client lost connection. Will try to reconnect once in 30s.")
sleep(30)
logger.debug("Trying to reconnect")
client.reconnect()
def start():
global watchdog, mqtt_client
watchdog = Watchdog(
timeout=settings.tgtg.every_n_minutes * 60 * 3 + 30, # 3 pull intervals + 1 timeout
user_handler=watchdog_handler,
)
logger.info("Connecting mqtt")
mqtt_client = mqtt.Client("toogoodtogo-ha-mqtt-bridge")
if settings.mqtt.username:
mqtt_client.username_pw_set(username=settings.mqtt.username, password=settings.mqtt.password)
mqtt_client.connect(host=settings.mqtt.host, port=int(settings.mqtt.port))
mqtt_client.on_disconnect = on_disconnect
mqtt_client.loop_start()
event = threading.Event()
thread = threading.Thread(target=loop, args=(event,))
thread.start()
if __name__ == "__main__":
start()