diff --git a/.HA_VERSION b/.HA_VERSION index cb90fee..420080f 100755 --- a/.HA_VERSION +++ b/.HA_VERSION @@ -1 +1 @@ -2026.4.1 \ No newline at end of file +2026.4.3 \ No newline at end of file diff --git a/include/automations/mealie.yaml b/include/automations/mealie.yaml new file mode 100644 index 0000000..29f31b7 --- /dev/null +++ b/include/automations/mealie.yaml @@ -0,0 +1,9 @@ +- id: mealie_update_mealplan + alias: "Mealie opdater madplan" + trigger: + - platform: homeassistant + event: start + - platform: time_pattern + minutes: "/30" + action: + - service: shell_command.mealie_update diff --git a/include/sensors/mealie.yaml b/include/sensors/mealie.yaml index 3b76133..13b3b88 100644 --- a/include/sensors/mealie.yaml +++ b/include/sensors/mealie.yaml @@ -1,13 +1,7 @@ - platform: rest name: mealie_madplan_ugen - resource_template: >- - http://10.0.0.142:9925/api/households/mealplans?start_date={{ now().strftime('%Y-%m-%d') }}&end_date={{ (now() + timedelta(days=6)).strftime('%Y-%m-%d') }} - headers: - Authorization: !secret mealie_bearer_token - Content-Type: application/json - method: GET + resource: "http://localhost:8123/local/mealie.json" scan_interval: 1800 - value_template: "{{ value_json['items'] | default([]) | length }}" - json_attributes_path: "$" + value_template: "{{ value_json.count }}" json_attributes: - items diff --git a/include/shell_commands/mealie.yaml b/include/shell_commands/mealie.yaml new file mode 100644 index 0000000..e56b84a --- /dev/null +++ b/include/shell_commands/mealie.yaml @@ -0,0 +1 @@ +mealie_update: "python3 /config/python_scripts/mealie_mealplan.py" diff --git a/python_scripts/mealie_mealplan.py b/python_scripts/mealie_mealplan.py new file mode 100644 index 0000000..5abb666 --- /dev/null +++ b/python_scripts/mealie_mealplan.py @@ -0,0 +1,41 @@ +#!/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") diff --git a/www/mealie.json b/www/mealie.json new file mode 100644 index 0000000..9d66042 --- /dev/null +++ b/www/mealie.json @@ -0,0 +1 @@ +{"count": 7, "items": [{"date": "2026-04-19", "recipe": {"name": "Cheeseburger Tacos", "slug": "cheeseburger-tacos"}}, {"date": "2026-04-20", "recipe": {"name": "Lasagne", "slug": "lasagne"}}, {"date": "2026-04-24", "recipe": {"name": "Fiskefrikadeller med remoulade og r\u00e5kost", "slug": "fiskefrikadeller-med-remoulade-og-rakost"}}, {"date": "2026-04-25", "recipe": {"name": "Cheeseburger Tacos", "slug": "cheeseburger-tacos"}}, {"date": "2026-04-23", "recipe": {"name": "K\u00e5lfad med hakket oksek\u00f8d", "slug": "kalfad-med-hakket-oksekod"}}, {"date": "2026-04-22", "recipe": {"name": "Frikadeller", "slug": "frikadeller"}}, {"date": "2026-04-21", "recipe": {"name": "Kylling i cremet sennepssauce", "slug": "kylling-i-cremet-sennepssauce"}}]} \ No newline at end of file