be4f83fcb1
- Nyt Python-script henter fra Mealie API og gemmer forenklet JSON (754 bytes) til /config/www/mealie.json (kun dato, navn, slug per opskrift) - REST sensor peger nu på lokal fil via http://localhost:8123/local/mealie.json - shell_command + automation opdaterer filen hvert 30. minut og ved opstart - Løser 'Ingen planlagt' og 'buttoncardtemplateerror' i dashboard
42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Fetch Mealie meal plan and save simplified JSON for HA REST sensor."""
|
|
import json, urllib.request, datetime, os
|
|
|
|
# Read bearer token from secrets.yaml
|
|
token = None
|
|
with open('/config/secrets.yaml') as f:
|
|
for line in f:
|
|
if line.strip().startswith('mealie_bearer_token:'):
|
|
token = line.split(':', 1)[1].strip().strip('"')
|
|
break
|
|
|
|
if not token:
|
|
data = {"count": 0, "items": []}
|
|
else:
|
|
today = datetime.date.today()
|
|
end = today + datetime.timedelta(days=6)
|
|
url = f"http://10.0.0.142:9925/api/households/mealplans?start_date={today}&end_date={end}"
|
|
|
|
try:
|
|
req = urllib.request.Request(url, headers={"Authorization": token})
|
|
raw = json.loads(urllib.request.urlopen(req, timeout=10).read())
|
|
items = [
|
|
{
|
|
"date": i["date"],
|
|
"recipe": {
|
|
"name": i.get("recipe", {}).get("name", ""),
|
|
"slug": i.get("recipe", {}).get("slug", ""),
|
|
},
|
|
}
|
|
for i in raw.get("items", [])
|
|
if i.get("recipe")
|
|
]
|
|
data = {"count": len(items), "items": items}
|
|
except Exception:
|
|
data = {"count": 0, "items": []}
|
|
|
|
os.makedirs('/config/www', exist_ok=True)
|
|
with open('/config/www/mealie.json', 'w') as f:
|
|
json.dump(data, f)
|
|
print("OK")
|