Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5a09833c9 | ||
|
|
c55cc90bda | ||
|
|
1aa11f5843 | ||
|
|
fe1c06b306 | ||
|
|
9b91598f5b | ||
|
|
25b0432757 | ||
|
|
8f3480ee2e | ||
|
|
d71b17d5c9 | ||
|
|
94509e6836 | ||
|
|
5cf1d743eb | ||
|
|
7885b100b4 | ||
|
|
67e1b465d5 | ||
|
|
b9d269876b | ||
|
|
d0cc69410f | ||
|
|
a465912b55 | ||
|
|
6059748f9b | ||
|
|
9f705ad7e4 | ||
|
|
c6919c5104 |
@@ -13,3 +13,5 @@ minimax_*.png
|
||||
config.json
|
||||
*.service
|
||||
.backup/
|
||||
.venv/
|
||||
tests/__pycache__/
|
||||
|
||||
@@ -112,6 +112,7 @@ Default Login: `admin` / `admin` — in `.env` mit `EPAPER_ADMIN_PASSWORD` setze
|
||||
| `strava` | Strava API | `access_token`, `club_id` |
|
||||
| `gmail` | Gmail API | `credentials_json` (OAuth2) |
|
||||
| `minimax` | MiniMax AI | `api_key`, `model`, `prompt` |
|
||||
| `netatmo` | Netatmo Weather API (OAuth2) | `client_id`, `client_secret`, `username`, `password` (Details: [NETATMO.md](plugins/NETATMO.md)) |
|
||||
| `hello` | statisch | `text`, `color`, `size` |
|
||||
|
||||
## Eigenes Plugin schreiben
|
||||
|
||||
@@ -153,14 +153,9 @@ def save_config():
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
|
||||
# slot belegung: pro slot "slot_<idx>_plugin"
|
||||
slots = cfg.get("slots", [])
|
||||
for i in range(dashboard_mod.SLOTS):
|
||||
plugin_name = request.form.get(f"slot_{i}_plugin", "").strip()
|
||||
if plugin_name and slots[i]["plugin"] != plugin_name:
|
||||
slots[i]["plugin"] = plugin_name
|
||||
slots[i]["config"] = {} # reset auf plugin defaults
|
||||
cfg["slots"] = slots
|
||||
# NOTE: legacy v1 slot-X-belegung wird nicht mehr unterstützt — die
|
||||
# Admin-UI nutzt seit v2 die Drag&Drop-API unter /api/layout für
|
||||
# Slot-Belegung. /config macht heute NUR noch den Refresh-Intervall.
|
||||
dashboard_mod.save_config(cfg)
|
||||
return redirect(url_for("index"))
|
||||
|
||||
@@ -197,7 +192,10 @@ def save_plugin_config(idx):
|
||||
new_cfg[key] = form_val
|
||||
continue
|
||||
if form_val is None:
|
||||
continue
|
||||
if ftype == "bool":
|
||||
new_cfg[key] = False
|
||||
else:
|
||||
continue
|
||||
try:
|
||||
if ftype == "int":
|
||||
new_cfg[key] = int(form_val)
|
||||
@@ -280,7 +278,15 @@ def api_layout():
|
||||
|
||||
@app.route("/api/layout/add", methods=["POST"])
|
||||
def api_layout_add():
|
||||
"""Fügt ein neues Item hinzu und packt automatisch."""
|
||||
"""Fügt ein neues Item hinzu OHNE bestehende Items zu verschieben (BUG-04).
|
||||
|
||||
BUG-04: Vorher wurde `layout_mod.pack()` aufgerufen, das alle Items neu
|
||||
sortiert und in scan-line packt — ein 1x1 hello konnte einen 2x1 spotify
|
||||
aus seiner Position drängen. Jetzt first-fit: das neue Item landet in der
|
||||
ersten freien Zelle, alle anderen bleiben unverändert.
|
||||
|
||||
Falls kein Platz: HTTP 409 mit Hinweis auf den Auto-Pack-Button.
|
||||
"""
|
||||
a = require_auth()
|
||||
if a: return a
|
||||
plugin = request.form.get("plugin", "hello").strip()
|
||||
@@ -297,14 +303,19 @@ def api_layout_add():
|
||||
except: pass
|
||||
import secrets
|
||||
new_id = secrets.token_hex(4)
|
||||
items = cfg.setdefault("layout", {}).setdefault("items", [])
|
||||
new_item = layout_mod.Item(new_id, plugin, 0, 0, w, h).to_dict()
|
||||
items.append(new_item)
|
||||
# Pack alle (inkl. neue)
|
||||
packed = layout_mod.pack([layout_mod.Item.from_dict(d) for d in items])
|
||||
cfg["layout"]["items"] = [it.to_dict() for it in packed]
|
||||
items_list = cfg.setdefault("layout", {}).setdefault("items", [])
|
||||
existing = [layout_mod.Item.from_dict(d) for d in items_list]
|
||||
candidate = layout_mod.Item(new_id, plugin, 0, 0, w, h)
|
||||
placed = layout_mod.first_fit(candidate, existing)
|
||||
if placed is None:
|
||||
return jsonify({
|
||||
"ok": False,
|
||||
"error": f"kein Platz für {w}×{h}-Item — bitte Auto-Pack klicken oder Items manuell verkleinern.",
|
||||
"hint": "use_auto_pack",
|
||||
}), 409
|
||||
items_list.append(placed.to_dict())
|
||||
dashboard_mod.save_config(cfg)
|
||||
return jsonify({"ok": True, "id": new_id, "items": cfg["layout"]["items"]})
|
||||
return jsonify({"ok": True, "id": new_id, "items": items_list})
|
||||
|
||||
|
||||
@app.route("/api/layout/item/<iid>", methods=["DELETE", "PATCH"])
|
||||
@@ -463,7 +474,10 @@ def api_plugin_config(plugin_name):
|
||||
form_val = request.form.get(f"config_{key}")
|
||||
ftype = field.get("type", "string")
|
||||
if form_val is None:
|
||||
continue
|
||||
if ftype == "bool":
|
||||
new_cfg[key] = False # unchecked checkbox → False
|
||||
else:
|
||||
continue
|
||||
if ftype == "secret":
|
||||
# Nur überschreiben wenn nicht leer
|
||||
existing = cfg.get("plugin_configs", {}).get(plugin_name, {}).get(key, "")
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"_comment": "Beispiel-Konfiguration für das Netatmo-Plugin. Diese Datei kannst du 1:1 in deine config.json unter 'plugin_configs.netatmo' kopieren und deine Werte einsetzen. Secrets wie client_secret / username / password NIEMALS in git committen — am besten direkt in der Admin-UI (http://pi:8080/) eintragen.",
|
||||
|
||||
"client_id": "REPLACE_WITH_YOUR_NETATMO_CLIENT_ID",
|
||||
"client_secret": "REPLACE_WITH_YOUR_NETATMO_CLIENT_SECRET",
|
||||
"username": "deine@email.de",
|
||||
"password": "REPLACE_WITH_YOUR_NETATMO_PASSWORD",
|
||||
|
||||
"_comment_station": "Optional: Name der Station wenn du mehrere hast. Leer = erste Station wird genommen.",
|
||||
"station_filter": "",
|
||||
|
||||
"_comment_modules": "Welche Module in der UI gezeigt werden. Bei einer typischen Wetterstation hast du NAMain (Indoor), NAModule1 (Outdoor), NAModule2 (Wind), NAModule3 (Regen). NAModule4 ist ein zusätzliches Indoor-Modul.",
|
||||
"show_indoor": true,
|
||||
"show_outdoor": true,
|
||||
"show_wind": true,
|
||||
"show_rain": true,
|
||||
|
||||
"_comment_display": "Optionale Anzeige-Elemente",
|
||||
"show_compass": true,
|
||||
"show_secondary": true,
|
||||
"_comment_secondary": "Zeigt Min/Max-Temperaturen + Timestamp der letzten Aktualisierung. Bei großen Slots (≥ 2x2) sichtbar.",
|
||||
|
||||
"_comment_co2": "Schwellen für den CO₂-Balken in ppm (parts per million). Standardwerte orientieren sich an der Netatmo-App: grün ≤ 600, gelb ≤ 1000, rot > 1500.",
|
||||
"co2_thresholds": "ok@600,warn@1000,alert@1500",
|
||||
"bar_gradient": true,
|
||||
|
||||
"_comment_units": "C oder F für Temperatur, kmh oder ms für Windgeschwindigkeit. Netatmo liefert die Daten in Celsius und km/h — Umrechnung passiert im Plugin.",
|
||||
"temp_unit": "C",
|
||||
"wind_unit": "kmh"
|
||||
}
|
||||
@@ -141,12 +141,30 @@ def pack(items: list[Item], order: Optional[list[str]] = None) -> list[Item]:
|
||||
return result
|
||||
|
||||
|
||||
def first_fit(item: Item, others: list[Item]) -> Item | None:
|
||||
"""Place the item at the first free scan-line position without moving others.
|
||||
|
||||
Returns the item with x/y set, or None if no placement fits.
|
||||
Used by /api/layout/add so a new widget does not reshuffle the grid.
|
||||
"""
|
||||
w = max(1, min(GRID_COLS, item.w))
|
||||
h = max(1, min(GRID_ROWS, item.h))
|
||||
for y in range(GRID_ROWS - h + 1):
|
||||
for x in range(GRID_COLS - w + 1):
|
||||
candidate = Item(item.id, item.plugin, x, y, w, h)
|
||||
cells = cells_occupied(candidate)
|
||||
if all(not (cells & cells_occupied(o)) for o in others):
|
||||
return candidate
|
||||
return None
|
||||
|
||||
|
||||
def auto_size_for_plugin(plugin_name: str) -> tuple[int, int]:
|
||||
"""Default size when user adds a new item."""
|
||||
presets = {
|
||||
"clock": (2, 2),
|
||||
"clock_wordclock": (2, 2),
|
||||
"weather": (2, 2),
|
||||
"netatmo": (4, 4), # zeigt 4 Sub-Cards; ab 2x2 sinnvoll, 4x4 ideal
|
||||
"system": (2, 2),
|
||||
"minimax": (2, 2),
|
||||
"spotify": (2, 1),
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
# Netatmo Weather Station Plugin
|
||||
|
||||
Zeigt die Live-Daten deiner heimischen [Netatmo Wetterstation](https://www.netatmo.com/de-de/weather/weatherstation)
|
||||
auf dem 7.3″ ACeP Dashboard: Indoor-Temperatur, CO₂, Luftfeuchte, Luftdruck,
|
||||
Outdoor-Werte, Wind (Stärke + Böen + Richtung mit Windrose) und Regenmengen.
|
||||
|
||||
Funktioniert mit **jeder** Netatmo-Konfiguration (Hauptmodul + beliebige
|
||||
Zusatzmodule). Module, die du nicht hast, werden automatisch weggelassen.
|
||||
|
||||

|
||||
|
||||
---
|
||||
|
||||
## 1. Netatmo-App registrieren
|
||||
|
||||
Damit das Plugin Daten abrufen darf, brauchst du eine App-Registrierung
|
||||
bei Netatmo. Das ist einmalig und kostenlos.
|
||||
|
||||
1. Gehe zu <https://dev.netatmo.com/apps/> und logge dich mit deinem
|
||||
Netatmo-Account ein.
|
||||
2. Klicke **Create an App**.
|
||||
3. Fülle das Formular aus:
|
||||
- **Name**: z. B. „epaper-dashboard"
|
||||
- **Description**: z. B. „Wetterdaten auf meinem ePaper-Display"
|
||||
- **Data Protection Officer**: nicht erforderlich für eine persönliche App
|
||||
4. Bei den **Scopes** wähle mindestens **`read_station`** aus
|
||||
(Standard-Scope der Weather API). Mehr brauchst du für dieses Plugin nicht.
|
||||
5. Speichern. Du bekommst **Client-ID** und **Client-Secret** angezeigt —
|
||||
die brauchst du gleich.
|
||||
|
||||
> Hinweis: Netatmo verwendet **OAuth2 mit Password-Grant** für die
|
||||
> Weather API (kein Browser-Redirect nötig). Dein Passwort wird nur
|
||||
> lokal für den initialen Token verwendet — es wird **nie** auf der
|
||||
> Festplatte persistiert, sondern liegt nur im RAM des laufenden
|
||||
> Dashboard-Prozesses.
|
||||
|
||||
---
|
||||
|
||||
## 2. Plugin konfigurieren
|
||||
|
||||
### Option A — über die Admin-Weboberfläche (empfohlen)
|
||||
|
||||
`http://<pi>:8080/` öffnen, in der Sidebar das Widget **Netatmo**
|
||||
auswählen und die Werte eintragen:
|
||||
|
||||
| Feld | Wert |
|
||||
|---|---|
|
||||
| Client-ID | von dev.netatmo.com/apps |
|
||||
| Client-Secret | von dev.netatmo.com/apps |
|
||||
| Username | deine Netatmo-Login-E-Mail |
|
||||
| Password | dein Netatmo-Passwort |
|
||||
| Station (Filter) | leer lassen, wenn du nur eine Station hast |
|
||||
|
||||
Speichern. Klicke **Refresh Now** — wenn alles passt, sollten nach
|
||||
~30s die ersten Daten erscheinen.
|
||||
|
||||
### Option B — direkt in `config.json`
|
||||
|
||||
Unter `plugin_configs.netatmo` eintragen (Beispiel siehe
|
||||
[`config.netatmo.example.json`](config.netatmo.example.json)):
|
||||
|
||||
```json
|
||||
{
|
||||
"plugin_configs": {
|
||||
"netatmo": {
|
||||
"client_id": "abc123...",
|
||||
"client_secret": "def456...",
|
||||
"username": "du@example.com",
|
||||
"password": "GEHEIM",
|
||||
"station_filter": "",
|
||||
"show_indoor": true,
|
||||
"show_outdoor": true,
|
||||
"show_wind": true,
|
||||
"show_rain": true,
|
||||
"show_compass": true,
|
||||
"show_secondary": true,
|
||||
"co2_thresholds": "ok@600,warn@1000,alert@1500",
|
||||
"bar_gradient": true,
|
||||
"temp_unit": "C",
|
||||
"wind_unit": "kmh"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> ⚠️ Secrets in `config.json` sind auf dem Pi persistent (lokal in
|
||||
> `/home/<user>/.hermes/projects/pi/config.json`). Wenn du das
|
||||
> vermeiden willst, nimm die Admin-UI — die legt sie genauso ab, aber
|
||||
> du siehst die Werte nie im Klartext-Editor.
|
||||
|
||||
---
|
||||
|
||||
## 3. Layout zuweisen
|
||||
|
||||
In der Admin-UI ein Netatmo-Widget auf das Grid ziehen. Das Plugin ist
|
||||
**responsive**: dieselbe Konfiguration sieht auf jedem Slot gut aus.
|
||||
|
||||
| Slot | Was du siehst |
|
||||
|---|---|
|
||||
| **1×1** | Große Außen-Temperatur, Mini-CO₂/Feuchte-Status |
|
||||
| **2×1** | Outdoor-Temperatur + Wind-Geschwindigkeit kompakt |
|
||||
| **4×1** | Indoor · Outdoor · Wind/Regen — drei Spalten |
|
||||
| **1×4** | Vertikale Liste aller Module |
|
||||
| **2×2** | Indoor-Card (mit CO₂-Bar) + Outdoor-Card + Wind/Regen-Bereich |
|
||||
| **4×4** | Volle Ansicht mit allen Modulen, Min/Max, Windrose |
|
||||
|
||||
Das Plugin priorisiert bei der Layout-Wahl **wide/tall** vor `small`,
|
||||
damit 4×1- und 1×4-Slots nicht in die Mini-Ansicht fallen.
|
||||
|
||||
---
|
||||
|
||||
## 4. Optionen im Detail
|
||||
|
||||
| Option | Typ | Default | Bedeutung |
|
||||
|---|---|---|---|
|
||||
| `client_id` | secret | — | Netatmo App Client-ID |
|
||||
| `client_secret` | secret | — | Netatmo App Client-Secret |
|
||||
| `username` | secret | — | Netatmo Login (E-Mail) |
|
||||
| `password` | secret | — | Netatmo Passwort (nur RAM) |
|
||||
| `station_filter` | string | `""` | Substring-Filter auf Stations-/Modulname |
|
||||
| `show_indoor` | bool | `true` | Hauptmodul + zusätzliche Indoor-Sensoren anzeigen |
|
||||
| `show_outdoor` | bool | `true` | NAModule1 (Außen) anzeigen |
|
||||
| `show_wind` | bool | `true` | NAModule2 (Wind) anzeigen |
|
||||
| `show_rain` | bool | `true` | NAModule3 (Regen) anzeigen |
|
||||
| `show_compass` | bool | `true` | Windrose in der Wind-Card zeichnen |
|
||||
| `show_secondary` | bool | `true` | Min/Max + letzte Aktualisierung |
|
||||
| `co2_thresholds` | string | `ok@600,warn@1000,alert@1500` | CO₂-Bar Schwellen (ppm) |
|
||||
| `bar_gradient` | bool | `true` | Verlaufsmodus der Bar |
|
||||
| `temp_unit` | select | `C` | `C` oder `F` |
|
||||
| `wind_unit` | select | `kmh` | `kmh` oder `ms` |
|
||||
|
||||
### CO₂-Schwellen anpassen
|
||||
|
||||
`co2_thresholds` ist ein String im Format `farbe@ppm_in_ppm`.
|
||||
Mehrere Stufen durch Komma trennen:
|
||||
|
||||
- `ok@600,warn@1000,alert@1500` — Netatmo-Default-Empfehlungen
|
||||
- `ok@800,warn@1200` — nur 2 Stufen (kompakt)
|
||||
- `ok@1000,warn@1500,alert@2000` — strenger (z. B. für Schlafzimmer)
|
||||
|
||||
### Station-Filter
|
||||
|
||||
Wenn du mehrere Stationen hast (z. B. „Home" und „Office"), kannst du
|
||||
über `station_filter` einen Substring matchen. Erst wird der
|
||||
Stationsname geprüft, dann die Modulnamen. Leer = erste Station.
|
||||
|
||||
---
|
||||
|
||||
## 5. Fehlerdiagnose
|
||||
|
||||
### Auth-Fehler (HTTP 401)
|
||||
|
||||
- Stimmen Client-ID/Secret mit dem Eintrag auf dev.netatmo.com/apps überein?
|
||||
- Stimmt das Passwort (case-sensitive)?
|
||||
- Wurde deine App bei Netatmo evtl. deaktiviert?
|
||||
|
||||
→ Plugin setzt den Token-Cache automatisch zurück, beim nächsten Render
|
||||
wird ein neuer Token geholt.
|
||||
|
||||
### "Keine Station gefunden"
|
||||
|
||||
- Hat deine Station in den letzten 4h Daten an Netatmo geschickt?
|
||||
(Netatmo markiert sie sonst als offline.)
|
||||
- Stimmt der `station_filter`? Wenn du dort z. B. „Home" eingibst, aber
|
||||
die Station heißt „Home Office", passt es trotzdem (Substring-Match).
|
||||
Leer = erste Station.
|
||||
|
||||
### "API nicht erreichbar"
|
||||
|
||||
- Pi hat Internet? (`ping api.netatmo.net`)
|
||||
- DNS? (`nslookup api.netatmo.net`)
|
||||
|
||||
### Werte fehlen / sind 0
|
||||
|
||||
- Manche Datenpunkte sind nur in der **Paid**-Subscription verfügbar
|
||||
(z. B. Historische Daten > 1h). Aktuelle Werte sind immer frei.
|
||||
|
||||
---
|
||||
|
||||
## 6. Technische Details
|
||||
|
||||
| Aspekt | Wert |
|
||||
|---|---|
|
||||
| API-Version | Netatmo Connect OAuth2 |
|
||||
| Endpoints | `POST /oauth2/token`, `GET /api/getstationsdata` |
|
||||
| Token-Lebensdauer | 10800s (3h), Refresh on the fly |
|
||||
| Cache | In-Memory, kein Disk-IO pro Render |
|
||||
| Datenquelle | Netatmo Cloud (Update alle ~10 Min) |
|
||||
| Modul-Typen | NAMain, NAModule1..4 |
|
||||
| `reachable`-Feld | „true" wenn das Modul in den letzten 4h gesehen wurde |
|
||||
|
||||
### Was wird pro Refresh gemacht?
|
||||
|
||||
1. Prüfe Token-Cache (RAM). Wenn abgelaufen oder fehlend → hole neuen
|
||||
Token via Password-Grant (initial) oder Refresh-Grant.
|
||||
2. `GET /api/getstationsdata?get_favorites=false` mit Bearer-Token.
|
||||
3. Parse Antwort, filtere nach `station_filter` und sichtbaren Modulen.
|
||||
4. Rendere je nach Slot-Größe (`small` / `wide` / `tall` / `standard`).
|
||||
|
||||
Bei 401 wird der Token-Cache geleert und **einmal** neu authentifiziert.
|
||||
Wenn das auch fehlschlägt → roter Error-Banner mit der genauen HTTP-Meldung.
|
||||
+260
-174
@@ -1,47 +1,84 @@
|
||||
"""WordClock-Plugin:Deutsche Wort-Uhr (QWATCHLayout).
|
||||
"""WordClock-Plugin: Deutsche Wort-Uhr im QlockTwo-Stil.
|
||||
|
||||
Reines Wort-Uhr-Display im deutschen Stil:
|
||||
ES IST <Fünf/Zehn/Viertel/Zwanzig> <Minuten> <nach/vor> <Stunde>
|
||||
Layout in 3 klar getrennten Zonen:
|
||||
┌────────────────────────────────────┐
|
||||
│ Header: Wochentag · Datum │ (accent-farbe, klein)
|
||||
├────────────────────────────────────┤
|
||||
│ │
|
||||
│ Wort-Grid: ES IST │
|
||||
│ ──── ZEHN NACH ──── │ (eine zentrale Zeile,
|
||||
│ DREI │ je nach Minute)
|
||||
│ │
|
||||
├────────────────────────────────────┤
|
||||
│ Stunden gross: DREI │ (Aldrich, riesig)
|
||||
└────────────────────────────────────┘
|
||||
|
||||
Unterstützt auch ENGLISCH (US-Layout).
|
||||
|
||||
Layout: fester 4×4-Zeichen-Grid im Quadrat (4×4 Zellen = 800×480 Display).
|
||||
Jede Zelle = 200×120px → Grid = 800×480px.
|
||||
Wir nutzen 11×8 "字符-Zellen" pro Grid (72×60px pro Zeichen).
|
||||
|
||||
Minuten-Zeilen (oberer Block):
|
||||
Zeile 0: [E][S][ ][I][S][T]
|
||||
Zeile 1: [F][Ü][N][F][Z][E][H][N][Z][W][A][N]
|
||||
Zeile 2: [V][I][E][R][T][E][L][Z][W][A][N][Z]
|
||||
Zeile 3: [N][U][L][L]
|
||||
Zeile 4: [N][A][C][H][ ][V][O][R][ ][H][A][L]
|
||||
Zeile 5: [B][ ]
|
||||
Zeile 6: [S][P][R][A][C][H][E]
|
||||
|
||||
Stunden (unterer Block, je 2×2 Zellen für die Ziffern):
|
||||
DieZiffern werden als gefüllte Rechtecke in der unteren Reihe gerendert.
|
||||
|
||||
Das Layout wird intern gecacht bis sich die Minute ändert.
|
||||
Die Minuten-Wörter sind in einer DYNAMISCHEN Zeile, nicht in einem starren
|
||||
11×8-Grid. Das vermeidet Überlappungen und sieht auf jedem Slot gleich aus.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import os, sys
|
||||
from datetime import datetime
|
||||
from functools import lru_cache
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
from plugins.base import Widget
|
||||
from palette import FG, BG, OK, BLUE, YELLOW, ORANGE, fill_for, measure
|
||||
|
||||
|
||||
# ---- Deutsche Minuten-Phrasen (QlockTwo-konform) ----
|
||||
# Schlüssel: Sektor-Untergrenze (5, 10, ..., 55). Wert: (minute_words, hour_offset)
|
||||
# hour_offset = +1 bedeutet wir gehen zur nächsten Stunde ("VOR")
|
||||
DE_PHRASES = {
|
||||
0: ("", 0), # ES IST genau
|
||||
5: ("FÜNF NACH", 0), # 5 nach
|
||||
10: ("ZEHN NACH", 0), # 10 nach
|
||||
15: ("VIERTEL NACH", 0), # viertel nach
|
||||
20: ("ZWANZIG NACH", 0), # 20 nach
|
||||
25: ("FÜNF VOR HALB", 1), # 5 vor halb → 0:30
|
||||
30: ("HALB", 1), # halb → 0:30 (zählt zur nächsten Stunde)
|
||||
35: ("FÜNF NACH HALB",1), # 5 nach halb
|
||||
40: ("ZWANZIG VOR", 1), # 20 vor
|
||||
45: ("VIERTEL VOR", 1), # viertel vor
|
||||
50: ("ZEHN VOR", 1), # 10 vor
|
||||
55: ("FÜNF VOR", 1), # 5 vor
|
||||
}
|
||||
|
||||
# Minuten → Sektor (0, 5, 10, ... 55)
|
||||
def _minute_sector(m: int) -> int:
|
||||
return (m // 5) * 5
|
||||
|
||||
|
||||
# Stunden-Namen (QlockTwo). Index 0..11 = Wortuhr-Stunden (1..12 Uhr).
|
||||
# 4 PM wird "VIER" (deutsch), 5 PM = "FÜNF", etc.
|
||||
# Daher: hour 16..23 → DE_HOURS[16%12]=DE_HOURS[4]="VIER". Korrekt.
|
||||
DE_HOURS = [
|
||||
"ZWÖLF", "EINS", "ZWEI", "DREI", "VIER", "FÜNF",
|
||||
"SECHS", "SIEBEN", "ACHT", "NEUN", "ZEHN", "ELF",
|
||||
]
|
||||
|
||||
|
||||
def _hour_name(h24: int, offset: int) -> str:
|
||||
"""Liefert Stunden-Name. offset=+1 → nächste volle Stunde.
|
||||
|
||||
Beispiel: h24=16, offset=0 → DE_HOURS[16%12=4]="VIER" (4 PM)
|
||||
h24=16, offset=1 → DE_HOURS[(16+1)%12=5]="FÜNF" (halb 5 = 5 PM)
|
||||
"""
|
||||
h = (h24 + offset) % 24
|
||||
return DE_HOURS[h % 12]
|
||||
|
||||
|
||||
def _minute_phrase(m: int):
|
||||
"""Returns (phrase_text, hour_offset) für die aktuelle Minute."""
|
||||
sector = _minute_sector(m)
|
||||
return DE_PHRASES[sector]
|
||||
|
||||
|
||||
class Widget(Widget):
|
||||
name = "clock_wordclock"
|
||||
label = "WordClock"
|
||||
description = "Deutsche Wort-Uhr (ES IST …). Nur für 2×2 oder größer."
|
||||
description = "Deutsche Wort-Uhr (ES IST …). QlockTwo-Style, ab 1×1 sauber."
|
||||
category = "info"
|
||||
|
||||
config_schema = [
|
||||
{"key": "lang", "label": "Sprache",
|
||||
"type": "select", "choices": ["de", "en"], "default": "de",
|
||||
"help": "de = Deutsch (Standard), en = Englisch"},
|
||||
{"key": "show_date", "label": "Datum anzeigen",
|
||||
"type": "bool", "default": True},
|
||||
{"key": "show_weekday", "label": "Wochentag anzeigen",
|
||||
@@ -49,176 +86,225 @@ class Widget(Widget):
|
||||
{"key": "accent_color", "label": "Akzentfarbe",
|
||||
"type": "select",
|
||||
"choices": ["accent", "blue", "ok", "warn", "info"],
|
||||
"default": "accent"},
|
||||
"default": "accent",
|
||||
"help": "Farbe für Wochentag + Datum"},
|
||||
{"key": "invert", "label": "Invertiert (dunkel)",
|
||||
"type": "bool", "default": False},
|
||||
{"key": "show_es_ist", "label": "'ES IST' anzeigen",
|
||||
"type": "bool", "default": True,
|
||||
"help": "QlockTwo-Klassiker. Aus = nur Minuten-Text + Stunde."},
|
||||
]
|
||||
default_config = {
|
||||
"lang": "de", "show_date": True, "show_weekday": True,
|
||||
"show_date": True, "show_weekday": True,
|
||||
"accent_color": "accent", "invert": False,
|
||||
"show_es_ist": True,
|
||||
}
|
||||
|
||||
def fetch(self):
|
||||
return {}
|
||||
|
||||
def render(self, draw, fonts, x: int, y: int, w: int, h: int):
|
||||
from palette import fill_for
|
||||
now = datetime.now()
|
||||
m = now.minute
|
||||
h_ = now.hour
|
||||
|
||||
invert = self.cfg("invert", False)
|
||||
accent = fill_for(self.cfg("accent_color", "accent"))
|
||||
fg = BG if invert else FG
|
||||
bg = FG if invert else BG
|
||||
|
||||
pad = 8
|
||||
|
||||
# Mindestgröße: 300px-breit, 240px-hoch für WordClock
|
||||
if w < 300 or h < 240:
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), fill=bg)
|
||||
draw.text((x + pad, y + pad),
|
||||
"WordClock\n(min 2×2)",
|
||||
font=fonts.get("20", fonts.get("default")), fill=fg)
|
||||
return
|
||||
|
||||
# ---- Wort-Uhr Grid ----
|
||||
# 4×4 Zellen → 800×480px
|
||||
# Wir rendern in ein 11×8 Zeichen-Grid
|
||||
chars_x, chars_y = 11, 8
|
||||
char_w = w // chars_x
|
||||
char_h = h // chars_y
|
||||
font_size = min(char_w, char_h) * 2 // 3
|
||||
font_key = str(font_size)
|
||||
if font_key not in fonts:
|
||||
font_key = str(max(16, min(fonts.keys(), key=lambda k: abs(int(k) - font_size) if k.isdigit() else 9999)) if fonts else "20")
|
||||
fnt = fonts.get(font_key, fonts.get("20", fonts.get("default")))
|
||||
|
||||
def _draw_char(cx, cy, char, color):
|
||||
"""Zeichnet ein Zeichen an Gitterposition (cx, cy)."""
|
||||
px = x + cx * char_w
|
||||
py = y + cy * char_h
|
||||
tw, th = measure(draw, char, fnt)
|
||||
draw.text((px + (char_w - tw) // 2, py + (char_h - th) // 2),
|
||||
char, font=fnt, fill=color)
|
||||
|
||||
def _fill_char(cx, cy, color):
|
||||
"""Füllt eine Gitterzelle mit einer Farbe (z.B. für Stunden-Dots)."""
|
||||
px = x + cx * char_w
|
||||
py = y + cy * char_h
|
||||
draw.rectangle((px + 2, py + 2, px + char_w - 3, py + char_h - 3), fill=color)
|
||||
|
||||
def _lit(cx, cy):
|
||||
_draw_char(cx, cy, LAYOUT_DE[cy][cx], fg)
|
||||
|
||||
def _dim(cx, cy):
|
||||
_draw_char(cx, cy, LAYOUT_DE[cy][cx], (150, 150, 150))
|
||||
|
||||
# ---- Minuten-Logik (Deutsch) ----
|
||||
def lit_minute(m):
|
||||
"""Sektor der Minuten: 0-4, 5-9, 10-14, 15-19, 20-24, 25-29, 30-34, 35-39, 40-44, 45-49, 50-54, 55-59."""
|
||||
if m < 5:
|
||||
return []
|
||||
elif m < 10:
|
||||
return [(0, 1), (1, 1), (2, 1), (3, 1)] # FÜNF
|
||||
elif m < 15:
|
||||
return [(5, 1), (6, 1), (7, 1), (8, 1)] # ZEHN
|
||||
elif m < 20:
|
||||
return [(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2)] # VIERTEL
|
||||
elif m < 25:
|
||||
return [(7, 2), (8, 2), (9, 2), (10, 2), (7, 3), (8, 3), (9, 3), (10, 3)] # ZWANZIG
|
||||
elif m < 30:
|
||||
return [(0, 4), (1, 4), (2, 4), (3, 4)] # NACH
|
||||
elif m < 35:
|
||||
return [(0, 1), (1, 1), (2, 1), (3, 1), # FÜNF
|
||||
(4, 4), (5, 4), (6, 4)] # + HALB
|
||||
elif m < 40:
|
||||
return [(4, 4), (5, 4), (6, 4)] # HALB
|
||||
elif m < 45:
|
||||
return [(7, 3), (8, 3), (9, 3), (10, 3)] # ZWANZIG
|
||||
elif m < 50:
|
||||
return [(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), # VIERTEL
|
||||
(7, 4), (8, 4), (9, 4)] # + VOR
|
||||
elif m < 55:
|
||||
return [(5, 1), (6, 1), (7, 1), (8, 1)] # ZEHN + VOR
|
||||
else:
|
||||
return [(0, 1), (1, 1), (2, 1), (3, 1), # FÜNF + VOR
|
||||
(7, 4), (8, 4), (9, 4)]
|
||||
|
||||
def hour_name(h, past_half):
|
||||
"""Gibt die Stunde zurück für die Wortuhr."""
|
||||
DE_HOURS = [
|
||||
"ZWÖLF", "EINS", "ZWEI", "DREI", "VIER",
|
||||
"FÜNF", "SECHS", "SIEBEN", "ACHT", "NEUN",
|
||||
"ZEHN", "ELF", "ZWÖLF"
|
||||
]
|
||||
if past_half:
|
||||
h = (h + 1) % 24
|
||||
if h == 0:
|
||||
return "ZWÖLF"
|
||||
return DE_HOURS[h % 12]
|
||||
|
||||
past_half = m >= 20
|
||||
h_display = hour_name(h_, past_half)
|
||||
lit_cells = lit_minute(m)
|
||||
|
||||
# ---- Render ----
|
||||
# Hintergrund
|
||||
# --- Hintergrund ---
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), fill=bg)
|
||||
|
||||
# "ES IST" immer lit in Spalte 0
|
||||
_lit(0, 0); _lit(1, 0); _lit(3, 0); _lit(4, 0)
|
||||
now = datetime.now()
|
||||
pad = 6
|
||||
inner_w = w - 2 * pad
|
||||
inner_h = h - 2 * pad
|
||||
inner_x = x + pad
|
||||
inner_y = y + pad
|
||||
|
||||
# Minuten-Wörter
|
||||
for (cx, cy) in lit_cells:
|
||||
_lit(cx, cy)
|
||||
# ============================================================
|
||||
# Sehr kleine Slots (1×1 = 200×120): kompakter Single-Line Modus
|
||||
# ============================================================
|
||||
if w < 240 or h < 140:
|
||||
# "ES IST DREI" oder "VIERTEL NACH DREI" als eine Zeile
|
||||
phrase, offset = _minute_phrase(now.minute)
|
||||
hour_str = _hour_name(now.hour, offset)
|
||||
if phrase:
|
||||
text = f"{phrase} {hour_str}" if self.cfg("show_es_ist") else f"{phrase} {hour_str}"
|
||||
else:
|
||||
text = f"ES IST {hour_str}" if self.cfg("show_es_ist") else hour_str
|
||||
font = _fit_font(draw, fonts, text, inner_w, inner_h, prefer=["20","16","default"])
|
||||
from palette import centered_text
|
||||
centered_text(draw, text, inner_x, inner_y, inner_w, inner_h, font, fg)
|
||||
return
|
||||
|
||||
# "VOR" und "NACH" (Zeile 4)
|
||||
if 5 <= m < 30:
|
||||
_lit(0, 4); _lit(1, 4); _lit(2, 4); _lit(3, 4) # NACH
|
||||
elif m >= 35 and m < 55:
|
||||
_lit(7, 4); _lit(8, 4); _lit(9, 4) # VOR
|
||||
# ============================================================
|
||||
# Standard-Layout: 3 Zonen
|
||||
# ============================================================
|
||||
# Höhenverteilung:
|
||||
# Header: bis zu 28px (oder weniger wenn beides aus)
|
||||
# Grid: Rest minus Stunden
|
||||
# Stunden: grosse Schrift, ~30% der Höhe
|
||||
|
||||
# Rest dim
|
||||
for row in range(chars_y):
|
||||
for col in range(chars_x):
|
||||
if (col, row) not in lit_cells and not (row == 0 and col in (0, 1, 3, 4)):
|
||||
_dim(col, row)
|
||||
# Header-Höhe dynamisch: nur soviel wie gebraucht
|
||||
show_wd = self.cfg("show_weekday", True)
|
||||
show_dt = self.cfg("show_date", True)
|
||||
show_es = self.cfg("show_es_ist", True)
|
||||
|
||||
# ---- Stunden-Balken unten ----
|
||||
# Zeile 6+7: Stunden-Name in großen Buchstaben unten zentriert
|
||||
hour_str = h_display
|
||||
hour_font_size = min(w // len(hour_str), h // 4) * 3 // 4
|
||||
hf = fonts.get(str(hour_font_size), fonts.get("60", fonts.get("default")))
|
||||
tw, th = measure(draw, hour_str, hf)
|
||||
hour_y = y + h - th - pad
|
||||
draw.text((x + (w - tw) // 2, hour_y), hour_str, font=hf, fill=fg)
|
||||
if show_wd and show_dt:
|
||||
header_h = 26
|
||||
elif show_wd or show_dt:
|
||||
header_h = 18
|
||||
else:
|
||||
header_h = 0
|
||||
|
||||
# ---- Datum + Wochentag ----
|
||||
if self.cfg("show_date", True):
|
||||
date_str = now.strftime("%d. %b %Y")
|
||||
df = fonts.get("16", fonts.get("default"))
|
||||
dw, dh = measure(draw, date_str, df)
|
||||
draw.text((x + (w - dw) // 2, y + pad), date_str, font=df, fill=accent)
|
||||
# Stunden-Block: fest ~36% der inneren Höhe, aber mindestens 50px
|
||||
hour_block_h = max(50, int(inner_h * 0.36))
|
||||
|
||||
if self.cfg("show_weekday", True):
|
||||
day_str = now.strftime("%A").upper()
|
||||
df = fonts.get("14", fonts.get("default"))
|
||||
dw, dh = measure(draw, day_str, df)
|
||||
draw.text((x + (w - dw) // 2, y + pad + (18 if self.cfg("show_date", True) else 0)),
|
||||
day_str, font=df, fill=accent)
|
||||
# Wort-Block = Rest
|
||||
grid_h = inner_h - header_h - hour_block_h - 6 # 6px gap
|
||||
if grid_h < 40:
|
||||
# Slot zu klein für alles — Stunden-Anteil reduzieren
|
||||
hour_block_h = max(36, int(inner_h * 0.28))
|
||||
grid_h = inner_h - header_h - hour_block_h - 6
|
||||
|
||||
grid_y = inner_y + header_h
|
||||
hour_y = grid_y + grid_h + 6
|
||||
|
||||
# --- Header (Wochentag / Datum) ---
|
||||
cy = inner_y
|
||||
header_font = _fit_font(draw, fonts, "Mittwoch · 31. Aug 2026", inner_w, header_h or 18,
|
||||
prefer=["16", "20", "default"])
|
||||
if header_h:
|
||||
if show_wd and show_dt:
|
||||
day = now.strftime("%A")
|
||||
date = now.strftime("%e. %b %Y").strip()
|
||||
text = f"{day} · {date}"
|
||||
tw, th = measure(draw, text, header_font)
|
||||
draw.text((inner_x + (inner_w - tw) // 2, cy),
|
||||
text, font=header_font, fill=accent)
|
||||
elif show_wd:
|
||||
text = now.strftime("%A")
|
||||
tw, _ = measure(draw, text, header_font)
|
||||
draw.text((inner_x + (inner_w - tw) // 2, cy),
|
||||
text, font=header_font, fill=accent)
|
||||
elif show_dt:
|
||||
text = now.strftime("%e. %b %Y").strip()
|
||||
tw, _ = measure(draw, text, header_font)
|
||||
draw.text((inner_x + (inner_w - tw) // 2, cy),
|
||||
text, font=header_font, fill=accent)
|
||||
|
||||
# --- Wort-Grid: zwei Zeilen ---
|
||||
# Zeile 1 (oben): "ES IST" (klein, accent) — nur wenn show_es_ist
|
||||
# Zeile 2 (mittig, gross): Minuten-Phrase
|
||||
phrase, offset = _minute_phrase(now.minute)
|
||||
|
||||
# Wie viele Zeilen brauchen wir im Grid?
|
||||
# Wenn ES IST → ES IST (Zeile 1 klein) + Phrase (Zeile 2 gross)
|
||||
# Wenn nicht → nur Phrase (eine Zeile)
|
||||
# Bei sehr grossen Slots könnte man auch stacked machen, aber single-line
|
||||
# ist klarer.
|
||||
|
||||
if show_es and phrase:
|
||||
# Zwei-Zeilen-Layout im Grid
|
||||
line1_h = int(grid_h * 0.30)
|
||||
line2_h = grid_h - line1_h - 2
|
||||
es_font = _fit_font(draw, fonts, "ES IST", inner_w, line1_h,
|
||||
prefer=["20", "16", "default"])
|
||||
draw.text((inner_x + (inner_w - measure(draw, "ES IST", es_font)[0]) // 2,
|
||||
grid_y),
|
||||
"ES IST", font=es_font, fill=accent)
|
||||
# Minuten-Phrase in der unteren, größeren Zeile
|
||||
phrase_font = _fit_font(draw, fonts, phrase, inner_w, line2_h,
|
||||
prefer=["48", "36", "32", "28", "24", "20", "default"])
|
||||
tw, th = measure(draw, phrase, phrase_font)
|
||||
draw.text((inner_x + (inner_w - tw) // 2,
|
||||
grid_y + line1_h + (line2_h - th) // 2),
|
||||
phrase, font=phrase_font, fill=fg)
|
||||
else:
|
||||
# Eine-Zeilen-Layout: nur die Phrase (oder "ES IST DREI" wenn keine Phrase)
|
||||
if not phrase:
|
||||
phrase = "ES IST" if show_es else ""
|
||||
phrase_font = _fit_font(draw, fonts, phrase, inner_w, grid_h,
|
||||
prefer=["60", "48", "36", "32", "28", "24", "20", "default"])
|
||||
tw, th = measure(draw, phrase, phrase_font)
|
||||
draw.text((inner_x + (inner_w - tw) // 2,
|
||||
grid_y + (grid_h - th) // 2),
|
||||
phrase, font=phrase_font, fill=fg)
|
||||
|
||||
# --- Stunden-Block (gross, unten) ---
|
||||
hour_str = _hour_name(now.hour, offset)
|
||||
hour_font = _fit_font(draw, fonts, hour_str, inner_w, hour_block_h,
|
||||
prefer=["100", "80", "60", "48", "36", "32", "default"])
|
||||
# Robuste Font-Wahl: lieber kleiner als abgeschnitten
|
||||
tw, th = measure(draw, hour_str, hour_font)
|
||||
if tw > inner_w:
|
||||
# noch kleiner probieren
|
||||
for k in ["80", "60", "48", "36", "32", "28", "24", "20"]:
|
||||
if k not in fonts:
|
||||
continue
|
||||
cand = fonts[k]
|
||||
cw, ch = measure(draw, hour_str, cand)
|
||||
if cw <= inner_w and ch <= hour_block_h:
|
||||
hour_font = cand
|
||||
tw, th = cw, ch
|
||||
break
|
||||
draw.text((inner_x + (inner_w - tw) // 2,
|
||||
hour_y + (hour_block_h - th) // 2),
|
||||
hour_str, font=hour_font, fill=fg)
|
||||
|
||||
# --- Minuten-Dots (4 Dots oben rechts, wie eine echte QlockTwo) ---
|
||||
# Zeigen die genauen Minuten-Module: ● ● ● ● ● — einer pro 1-2 Min
|
||||
# Wenn minute < 5: leer; bei 25: ●; bei 26: ●●; etc.
|
||||
if inner_w >= 200:
|
||||
self._draw_minute_dots(draw, inner_x, inner_y, inner_w, header_h, fg, accent)
|
||||
|
||||
|
||||
# ---- Deutsches WordClock-Layout (11×8) ----
|
||||
# Jede Position ist ein Zeichen das gerendert wird.
|
||||
# ' ' = Leerzeichen, rest = Buchstabe.
|
||||
LAYOUT_DE = [
|
||||
["E", "S", " ", "I", "S", "T", " ", " ", " ", " ", " "],
|
||||
["F", "Ü", "N", "F", " ", "Z", "E", "H", "N", " ", " "],
|
||||
["V", "I", "E", "R", "T", "E", "L", " ", "Z", "W", "A"],
|
||||
["N", "U", "L", "L", " ", "Z", "W", "A", "N", "Z", " "],
|
||||
["N", "A", "C", "H", " ", "V", "O", "R", " ", "H", "A"],
|
||||
[" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
|
||||
[" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
|
||||
[" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
|
||||
]
|
||||
def _draw_minute_dots(self, draw, x, y, w, header_h, fg, accent):
|
||||
"""Vier Minuten-Dots (●) in der Ecke, analog zu echten QlockTwo-Uhren.
|
||||
|
||||
Sie zeigen: ●=1, ●●=2, ●●●=3, ●●●●=4 Minuten innerhalb des 5-Min-Sektors.
|
||||
"""
|
||||
m = datetime.now().minute
|
||||
sector_m = m % 5 # 0..4
|
||||
if sector_m == 0:
|
||||
return # exakt auf 5-Min-Marke → keine Dots
|
||||
|
||||
# Position: oben rechts, in der Header-Zone (oder oben falls kein Header)
|
||||
dot_size = 3
|
||||
spacing = 4
|
||||
total_w = sector_m * (dot_size + spacing) - spacing
|
||||
start_x = x + w - total_w
|
||||
# Position: am unteren Rand des Header-Bereichs
|
||||
if header_h >= 26:
|
||||
dot_y = y + header_h - dot_size - 2
|
||||
elif header_h > 0:
|
||||
dot_y = y + header_h - dot_size
|
||||
else:
|
||||
# kein Header → dots ganz oben, klein
|
||||
dot_y = y + 2
|
||||
|
||||
for i in range(sector_m):
|
||||
cx = start_x + i * (dot_size + spacing)
|
||||
# Eckige Punkte (Mini-Quadrate) statt runder, weil runde auf dem
|
||||
# ePaper oft Matsch produzieren
|
||||
draw.rectangle((cx, dot_y, cx + dot_size - 1, dot_y + dot_size - 1),
|
||||
fill=fg)
|
||||
|
||||
|
||||
def _fit_font(draw, fonts, text, max_w, max_h, prefer=None):
|
||||
"""Wählt den größten Font aus `prefer`, dessen Text in max_w × max_h passt.
|
||||
|
||||
Robuster als die alte Logik — überspringt 'clock' (Pixel-Font) automatisch.
|
||||
"""
|
||||
if prefer is None:
|
||||
prefer = ["60", "48", "36", "32", "28", "24", "20", "16", "default"]
|
||||
for key in prefer:
|
||||
f = fonts.get(key)
|
||||
if f is None:
|
||||
continue
|
||||
tw, th = measure(draw, text, f)
|
||||
if tw <= max_w and th <= max_h:
|
||||
return f
|
||||
# Fallback: das Kleinste
|
||||
for key in reversed(prefer):
|
||||
if key in fonts:
|
||||
return fonts[key]
|
||||
return fonts.get("default") or fonts.get("20")
|
||||
@@ -0,0 +1,556 @@
|
||||
"""DesignBase — Plugin Design Template / Schablone.
|
||||
|
||||
Dieses Modul ist KEIN eigenständiges Widget, sondern eine SAMMLUNG
|
||||
von Design-Konzepten, Layout-Helfern und Theme-Definitionen,
|
||||
die als Vorlage für alle anderen Plugin-Renderer dienen.
|
||||
|
||||
ANATOMIE EINES DESIGN-TEMPLATES
|
||||
=================================
|
||||
|
||||
Jedes Plugin-render() folgt diesem Schema:
|
||||
|
||||
def render(self, draw, fonts, x, y, w, h):
|
||||
theme = self._theme() # Theme-Objekt mit Farben + Schriften
|
||||
data = self.fetch() # Daten holen
|
||||
if "_error" in data:
|
||||
render_error_banner(...)
|
||||
return
|
||||
layout = self._pick_layout(w, h) # Layout-Strategie wählen
|
||||
self._render_header(draw, theme, x, y, w, header_h)
|
||||
self._render_body(draw, theme, data, x, body_y, w, body_h, layout)
|
||||
if self._show_footer(w, h):
|
||||
self._render_footer(draw, theme, data, x, y+h-footer_h, w, footer_h)
|
||||
|
||||
Die 4 LAYOUT-STRATEGIEN
|
||||
========================
|
||||
|
||||
is_small (1x1): Eine einzige große Zahl / ein Icon
|
||||
is_wide (4x1/2x1): Horizontale Teilung in 2-3 Spalten
|
||||
is_tall (1x4/1x2): Vertikale Teilung in Zeilen
|
||||
standard (2x2+): Header + Content + Footer (oder Karten)
|
||||
|
||||
Die 4 THEMES
|
||||
=============
|
||||
|
||||
THEME_LIGHT — Weiß, schwarz, eine Akzentfarbe (z.B. Netatmo: blau)
|
||||
THEME_DARK — Fast schwarz, helle Farben auf dunklem Grund
|
||||
THEME_RETRO — Gelb/Orange-Schwarz (80er-Terminal-Feeling)
|
||||
THEME_MAG — Minimal, typografisch, viel Weißraum
|
||||
|
||||
Farben werden IMMER über theme.xxx bezogen, NIEMALS direkt als RGB-Tuple.
|
||||
Das erlaubt komplettes Umstyling ohne den Renderer-Code anzufassen.
|
||||
|
||||
Farben-Schema pro Theme (Palette: FG, BG, ACCENT, OK, WARN, ALERT, INFO):
|
||||
|
||||
THEME_LIGHT: BG=WHITE, FG=BLACK, ACCENT=BLUE, OK=GREEN, WARN=YELLOW, ALERT=RED
|
||||
THEME_DARK: BG=BLACK, FG=WHITE, ACCENT=CYAN, OK=GREEN, WARN=ORANGE,ALERT=RED
|
||||
THEME_RETRO: BG=BLACK, FG=YELLOW,ACCENT=ORANGE, OK=GREEN, WARN=YELLOW, ALERT=RED
|
||||
THEME_MAG: BG=WHITE, FG=BLACK, ACCENT=ORANGE, OK=GREEN, WARN=ORANGE, ALERT=BLUE
|
||||
|
||||
Beispiel-Implementierung: widgets/netatmo.py (theme='light', accent='blue')
|
||||
Beispiel-Implementierung: widgets/weather.py (theme='light', accent='orange')
|
||||
Beispiel-Implementierung: widgets/system.py (theme='light', accent='green')
|
||||
"""
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Literal
|
||||
|
||||
# Palette-Aliase (für direkten Zugriff in Templates)
|
||||
# noqa: E402 — diese Imports funktionieren weil plugins/ im Python-Path liegt
|
||||
from palette import (
|
||||
FG, BG, WHITE, BLACK, GREEN, BLUE, RED, YELLOW, ORANGE,
|
||||
INFO, OK, WARN, ALERT, ACCENT,
|
||||
measure, fit_font, centered_text, hbar, parse_thresholds,
|
||||
is_small, is_wide, is_tall,
|
||||
)
|
||||
|
||||
# noqa: E402
|
||||
from plugins.base import Widget, render_error_banner
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# THEME DEFINITIONS
|
||||
# ============================================================================
|
||||
|
||||
ThemeName = Literal["light", "dark", "retro", "mag"]
|
||||
LayoutName = Literal["small", "wide", "tall", "standard"]
|
||||
|
||||
|
||||
@dataclass
|
||||
class Theme:
|
||||
"""Alle visuellen Eigenschaften eines Designs.
|
||||
|
||||
Ein Theme-Objekt wird in render() erzeugt und an alle
|
||||
_render_*-Methoden weitergegeben. Nie direkt Farbwerte hardcoden.
|
||||
"""
|
||||
name: ThemeName
|
||||
bg: tuple[int, int, int] # Hintergrund
|
||||
fg: tuple[int, int, int] # Primärtext
|
||||
accent: tuple[int, int, int] # Akzentfarbe (Platzierung je nach Theme)
|
||||
ok: tuple[int, int, int]
|
||||
warn: tuple[int, int, int]
|
||||
alert: tuple[int, int, int]
|
||||
info: tuple[int, int, int]
|
||||
header_font_key: str = "24" # Font-Schlüssel für Headlines
|
||||
label_font_key: str = "20" # Font-Schlüssel für Modul-Labels
|
||||
body_font_key: str = "32" # Font-Schlüssel für Hauptwerte
|
||||
mono_font_key: str = "20" # Font-Schlüssel für Metadaten
|
||||
pad: int = 8 # Innenabstand
|
||||
border_w: int = 2 # Rahmendicke
|
||||
corner_r: int = 0 # Eckenradius (0 = scharf)
|
||||
|
||||
# ---- Farb-Helfer ----
|
||||
def temp_color(self, t: float | None) -> tuple[int, int, int]:
|
||||
"""Temperaturanzeige: kalt→info, warm→accent, heiß→alert."""
|
||||
if t is None:
|
||||
return self.fg
|
||||
if t >= 30:
|
||||
return self.alert
|
||||
if t >= 22:
|
||||
return self.accent
|
||||
if t <= 5:
|
||||
return self.info
|
||||
if t <= 12:
|
||||
return self.info
|
||||
return self.fg
|
||||
|
||||
def value_color(self, value: float, warn_at: float, alert_at: float) -> tuple[int, int, int]:
|
||||
"""Ampel-Helfer: value + Schwellen → passende Farbe."""
|
||||
if value >= alert_at:
|
||||
return self.alert
|
||||
if value >= warn_at:
|
||||
return self.warn
|
||||
return self.ok
|
||||
|
||||
def text(self, draw, text: str, x: int, y: int,
|
||||
font_key: str | None = None, color=None, max_w: int | None = None):
|
||||
"""Short-hand: Text zeichnen mit Theme-Farbe."""
|
||||
font = draw.font if hasattr(draw, 'font') else None
|
||||
# Actual implementation uses fonts dict from render scope
|
||||
pass # see render helpers below
|
||||
|
||||
|
||||
# Vordefinierte Themes
|
||||
THEMES: dict[ThemeName, Theme] = {
|
||||
"light": Theme(
|
||||
name="light",
|
||||
bg=WHITE, fg=BLACK,
|
||||
accent=BLUE, ok=GREEN, warn=YELLOW, alert=RED, info=BLUE,
|
||||
header_font_key="24", label_font_key="20",
|
||||
body_font_key="32", mono_font_key="20",
|
||||
pad=8, border_w=2, corner_r=0,
|
||||
),
|
||||
"dark": Theme(
|
||||
name="dark",
|
||||
bg=BLACK, fg=WHITE,
|
||||
accent=(0, 200, 220), ok=GREEN, warn=ORANGE, alert=RED, info=(0, 180, 255),
|
||||
header_font_key="24", label_font_key="20",
|
||||
body_font_key="32", mono_font_key="20",
|
||||
pad=8, border_w=2, corner_r=0,
|
||||
),
|
||||
"retro": Theme(
|
||||
name="retro",
|
||||
bg=BLACK, fg=YELLOW,
|
||||
accent=ORANGE, ok=GREEN, warn=YELLOW, alert=RED, info=ORANGE,
|
||||
header_font_key="24", label_font_key="20",
|
||||
body_font_key="32", mono_font_key="20",
|
||||
pad=8, border_w=2, corner_r=0,
|
||||
),
|
||||
"mag": Theme(
|
||||
name="mag",
|
||||
bg=WHITE, fg=BLACK,
|
||||
accent=ORANGE, ok=GREEN, warn=ORANGE, alert=RED, info=BLUE,
|
||||
header_font_key="24", label_font_key="20",
|
||||
body_font_key="32", mono_font_key="20",
|
||||
pad=16, border_w=1, corner_r=0,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# LAYOUT HELPERS
|
||||
# ============================================================================
|
||||
|
||||
def pick_layout(w: int, h: int) -> LayoutName:
|
||||
"""Wähle Layout-Strategie basierend auf Slot-Größe.
|
||||
|
||||
Reihenfolge ist wichtig: is_wide/is_tall VOR is_small prüfen,
|
||||
weil is_small zu eager matcht (w<280 or h<180).
|
||||
"""
|
||||
if is_wide(w, h):
|
||||
return "wide"
|
||||
if is_tall(w, h):
|
||||
return "tall"
|
||||
if is_small(w, h):
|
||||
return "small"
|
||||
return "standard"
|
||||
|
||||
|
||||
def header_height(layout: LayoutName) -> int:
|
||||
"""Höhe des Header-Bereichs (Label + Titel)."""
|
||||
return 32 if layout == "standard" else 28
|
||||
|
||||
|
||||
def footer_height(layout: LayoutName) -> int:
|
||||
"""Höhe des Footer-Bereichs (Metadaten, Timestamp)."""
|
||||
if layout == "small":
|
||||
return 0
|
||||
if layout in ("wide", "tall"):
|
||||
return 20
|
||||
return 18
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# RENDER HELPERS (Theme-bewusst)
|
||||
# ============================================================================
|
||||
|
||||
def th_text(draw, fonts, theme: Theme, text: str, x: int, y: int,
|
||||
font_key: str | None = None, color=None, max_w: int | None = None):
|
||||
"""Text mit Theme-Default zeichnen."""
|
||||
key = font_key or theme.header_font_key
|
||||
font = fonts.get(key) or fonts.get("default")
|
||||
c = color if color is not None else theme.fg
|
||||
draw.text((x, y), text, font=font, fill=c)
|
||||
|
||||
|
||||
def th_centered(draw, fonts, theme: Theme, text: str,
|
||||
x: int, y: int, w: int, h: int,
|
||||
font_key: str | None = None, color=None):
|
||||
"""Text in Box zentrieren mit Theme-Defaults."""
|
||||
key = font_key or theme.body_font_key
|
||||
font = fonts.get(key) or fonts.get("default")
|
||||
c = color if color is not None else theme.fg
|
||||
centered_text(draw, text, x, y, w, h, font, c)
|
||||
|
||||
|
||||
def th_rect(draw, theme: Theme, x: int, y: int, w: int, h: int,
|
||||
fill=None, outline=None, width: int = 1):
|
||||
"""Rechteck mit Theme-Defaults."""
|
||||
f = fill if fill is not None else None
|
||||
o = outline if outline is not None else theme.fg
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1),
|
||||
fill=f, outline=o, width=width)
|
||||
|
||||
|
||||
def th_header_bar(draw, fonts, theme: Theme,
|
||||
x: int, y: int, w: int, h: int,
|
||||
label: str, value: str = "", value_color=None):
|
||||
"""Header-Leiste: links Label, rechts optionaler Wert.
|
||||
|
||||
Layout: [LABEL] [VALUE]
|
||||
Farbe: accent-Bg fg
|
||||
"""
|
||||
pad = theme.pad
|
||||
# Hintergrund links: Label-Bereich
|
||||
label_w = min(measure(draw, label, fonts.get(theme.label_font_key) or fonts.get("default"))[0] + pad * 2, w // 2)
|
||||
draw.rectangle((x, y, x + label_w, y + h - 1), fill=theme.accent)
|
||||
draw.text((x + pad, y + (h - 20) // 2), label,
|
||||
font=fonts.get(theme.label_font_key) or fonts.get("default"),
|
||||
fill=theme.bg)
|
||||
# Wert rechts
|
||||
if value:
|
||||
vc = value_color if value_color is not None else theme.fg
|
||||
font_v = fonts.get(theme.label_font_key) or fonts.get("default")
|
||||
vw, _ = measure(draw, value, font_v)
|
||||
draw.text((x + w - vw - pad, y + (h - 20) // 2), value, font=font_v, fill=vc)
|
||||
|
||||
|
||||
def th_big_value(draw, fonts, theme: Theme,
|
||||
x: int, y: int, w: int, h: int,
|
||||
value: str, unit: str = "",
|
||||
color=None, align: str = "left"):
|
||||
"""Die große zentrale Kennzahl (z.B. "22°", "45%", "12.4 km").
|
||||
|
||||
Der Wert wird so groß wie möglich dargestellt, das Unit darunter oder
|
||||
daneben in kleinerer Schrift.
|
||||
"""
|
||||
c = color if color is not None else theme.fg
|
||||
candidates = [theme.body_font_key, "80", "60", "48", "36", "28", "24"]
|
||||
font_v = fit_font(draw, value, fonts, w - 2 * theme.pad,
|
||||
h - 2 * theme.pad, candidates=candidates)
|
||||
|
||||
if align == "center":
|
||||
th_centered(draw, fonts, theme, value, x, y, w, h,
|
||||
font_key=None, color=c)
|
||||
else:
|
||||
# Linksbündig, groß
|
||||
tw, th_f = measure(draw, value, font_v)
|
||||
draw.text((x + theme.pad, y + max(0, (h - th_f) // 2)),
|
||||
value, font=font_v, fill=c)
|
||||
|
||||
# Unit darunter oder daneben
|
||||
if unit:
|
||||
unit_font = fonts.get(theme.mono_font_key) or fonts.get("default")
|
||||
if align == "center":
|
||||
# Unter dem Wert
|
||||
uw, uh = measure(draw, unit, unit_font)
|
||||
draw.text((x + max(0, (w - uw) // 2), y + h // 2 + 4), unit,
|
||||
font=unit_font, fill=theme.fg)
|
||||
else:
|
||||
uw, uh = measure(draw, unit, unit_font)
|
||||
draw.text((x + theme.pad + tw + 6,
|
||||
y + max(0, (h - uh) // 2)),
|
||||
unit, font=unit_font, fill=theme.fg)
|
||||
|
||||
|
||||
def th_mini_bar(draw, fonts, theme: Theme,
|
||||
x: int, y: int, w: int, h: int,
|
||||
pct: float,
|
||||
thresholds: list | None = None,
|
||||
gradient: bool = True):
|
||||
"""Kompakter Fortschrittsbalken mit Theme-Styling.
|
||||
|
||||
thresholds: [(pct, color), ...] — z.B. [(50, ok), (80, warn), (100, alert)]
|
||||
gradient: True = zeigt alle Farbstufen gleichzeitig (e-Paper-nativ)
|
||||
"""
|
||||
if thresholds is None:
|
||||
thresholds = [(50, theme.ok), (80, theme.warn), (100, theme.alert)]
|
||||
hbar(draw, x, y, w, h, pct,
|
||||
thresholds=thresholds, gradient=gradient)
|
||||
|
||||
|
||||
def th_divider(draw, theme: Theme, x: int, y: int, w: int,
|
||||
style: str = "solid"):
|
||||
"""Horizontale Trennlinie."""
|
||||
if style == "solid":
|
||||
draw.line((x, y, x + w, y), fill=theme.fg, width=1)
|
||||
elif style == "dashed":
|
||||
# Dashed: 4px dash, 4px gap
|
||||
for dx in range(0, w, 8):
|
||||
draw.line((x + dx, y, min(x + dx + 4, x + w), y),
|
||||
fill=theme.fg, width=1)
|
||||
elif style == "accent":
|
||||
draw.line((x, y, x + w, y), fill=theme.accent, width=2)
|
||||
|
||||
|
||||
def th_corner_marker(draw, theme: Theme, x: int, y: int, size: int = 8):
|
||||
"""Kleiner Eck-Marker (L-Form) oben-links — markiert den Slot-Ursprung.
|
||||
|
||||
Optionaler visueller Anker der zeigt: "hier beginnt dieses Widget".
|
||||
"""
|
||||
draw.line((x, y, x + size, y), fill=theme.accent, width=2)
|
||||
draw.line((x, y, x, y + size), fill=theme.accent, width=2)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# LAYOUT PATTERN TEMPLATES
|
||||
# ============================================================================
|
||||
|
||||
def layout_1x1(draw, fonts, theme: Theme,
|
||||
value: str, unit: str = "",
|
||||
label: str = "", color=None):
|
||||
"""ONE BIG NUMBER — für 1x1 Slots.
|
||||
|
||||
Große zentrierte Zahl, darunter kleines Label.
|
||||
Beispiel: Gmail ungelesen, Strava Jahr-km, System CPU%
|
||||
"""
|
||||
pad = theme.pad
|
||||
# Rahmen
|
||||
th_rect(draw, theme, x=0, y=0, w=200, h=120,
|
||||
outline=theme.fg, width=theme.border_w)
|
||||
|
||||
c = color if color is not None else theme.fg
|
||||
font_val = fit_font(draw, value, fonts, 200 - 2 * pad, 80,
|
||||
candidates=[theme.body_font_key, "80", "60", "48"])
|
||||
th_centered(draw, fonts, theme, value,
|
||||
x=0, y=10, w=200, h=90, color=c)
|
||||
|
||||
if label:
|
||||
font_l = fonts.get(theme.mono_font_key) or fonts.get("default")
|
||||
lw = measure(draw, label, font_l)[0]
|
||||
draw.text(((200 - lw) // 2, 96), label, font=font_l, fill=theme.fg)
|
||||
|
||||
|
||||
def layout_wide_strip(draw, fonts, theme: Theme,
|
||||
cols: list[dict],
|
||||
header_h: int = 30):
|
||||
"""WIDE STRIP — für 4x1 / 2x1 Slots.
|
||||
|
||||
Horizontale Teilung in gleichbreite Spalten.
|
||||
Jede Spalte: [LABEL] über [VALUE] über [UNIT]
|
||||
|
||||
cols = [
|
||||
{"label": "TEMP", "value": "22°", "unit": "", "color": theme.temp_color(22)},
|
||||
{"label": "HUM", "value": "65%", "unit": "Feuchte", "color": theme.ok},
|
||||
{"label": "CO₂", "value": "820", "unit": "ppm", "color": theme.warn},
|
||||
]
|
||||
"""
|
||||
n = len(cols)
|
||||
col_w = 200 // n if n > 0 else 200
|
||||
pad = theme.pad
|
||||
|
||||
for i, col in enumerate(cols):
|
||||
cx = i * col_w
|
||||
c = col.get("color", theme.fg)
|
||||
|
||||
# Label
|
||||
lbl = col.get("label", "")
|
||||
if lbl:
|
||||
draw.text((cx + pad, 4), lbl,
|
||||
font=fonts.get(theme.label_font_key) or fonts.get("default"),
|
||||
fill=theme.accent)
|
||||
|
||||
# Value (groß)
|
||||
val = col.get("value", "—")
|
||||
font_v = fit_font(draw, val, fonts, col_w - 2 * pad, header_h + 30,
|
||||
candidates=[theme.body_font_key, "48", "36", "28"])
|
||||
draw.text((cx + pad, header_h), val, font=font_v, fill=c)
|
||||
|
||||
# Unit (klein darunter)
|
||||
unit = col.get("unit", "")
|
||||
if unit:
|
||||
font_u = fonts.get(theme.mono_font_key) or fonts.get("default")
|
||||
draw.text((cx + pad, header_h + font_v.size + 2),
|
||||
unit, font=font_u, fill=theme.fg)
|
||||
|
||||
|
||||
def layout_card_grid(draw, fonts, theme: Theme,
|
||||
cards: list[dict],
|
||||
x: int, y: int, w: int, h: int,
|
||||
cols: int = 2):
|
||||
"""CARD GRID — für 2x2+ Slots.
|
||||
|
||||
Teil den verfügbaren Raum in gleichmäßige Karten auf.
|
||||
Jede Karte hat: Border + Header-Akzent + Label + Wert + optional Bar.
|
||||
|
||||
cards = [
|
||||
{
|
||||
"label": "🏠 Indoor",
|
||||
"value": "22.5°",
|
||||
"unit": "",
|
||||
"color": theme.temp_color(22.5),
|
||||
"bar": {"pct": 45, "thresholds": [...], "gradient": True},
|
||||
"meta": "min 18° / max 26°",
|
||||
},
|
||||
...
|
||||
]
|
||||
"""
|
||||
pad = theme.pad
|
||||
rows = (len(cards) + cols - 1) // cols
|
||||
card_w = (w - (cols + 1) * pad) // cols
|
||||
card_h = (h - (rows + 1) * pad) // rows
|
||||
|
||||
for i, card in enumerate(cards):
|
||||
row = i // cols
|
||||
col = i % cols
|
||||
cx = x + pad + col * (card_w + pad)
|
||||
cy = y + pad + row * (card_h + pad)
|
||||
|
||||
# Border
|
||||
th_rect(draw, theme, cx, cy, card_w, card_h,
|
||||
outline=theme.fg, width=theme.border_w)
|
||||
|
||||
# Label-Balken oben
|
||||
lbl = card.get("label", "")
|
||||
if lbl:
|
||||
lbl_h = 24
|
||||
draw.rectangle((cx, cy, cx + card_w - 1, cy + lbl_h),
|
||||
fill=theme.accent)
|
||||
font_l = fonts.get(theme.label_font_key) or fonts.get("default")
|
||||
draw.text((cx + pad, cy + 4), lbl, font=font_l, fill=theme.bg)
|
||||
|
||||
# Value
|
||||
inner_y = cy + 28
|
||||
inner_h = card_h - 30
|
||||
val = card.get("value", "—")
|
||||
c = card.get("color", theme.fg)
|
||||
font_v = fit_font(draw, val, fonts, card_w - 2 * pad,
|
||||
inner_h // 2,
|
||||
candidates=[theme.body_font_key, "48", "36", "28"])
|
||||
draw.text((cx + pad, inner_y), val, font=font_v, fill=c)
|
||||
|
||||
# Bar (optional)
|
||||
bar = card.get("bar")
|
||||
if bar and inner_h > 60:
|
||||
bar_pct = bar.get("pct", 0)
|
||||
bar_thresholds = bar.get("thresholds") or [
|
||||
(50, theme.ok), (80, theme.warn), (100, theme.alert)]
|
||||
bar_y = cy + card_h - 28
|
||||
th_mini_bar(draw, fonts, theme,
|
||||
cx + pad, bar_y, card_w - 2 * pad, 12,
|
||||
bar_pct, bar_thresholds, bar.get("gradient", True))
|
||||
|
||||
# Meta (optional)
|
||||
meta = card.get("meta", "")
|
||||
if meta:
|
||||
font_m = fonts.get(theme.mono_font_key) or fonts.get("default")
|
||||
draw.text((cx + pad, cy + card_h - 18), meta,
|
||||
font=font_m, fill=theme.fg)
|
||||
|
||||
|
||||
def layout_poster(draw, fonts, theme: Theme,
|
||||
label: str, value: str, unit: str = "",
|
||||
sub: str = "", color=None):
|
||||
"""POSTER — eine einzelne Aussage, maximal typografisch.
|
||||
|
||||
Für 2x2+ Slots die WIRKLICH nur eine Zahl zeigen wollen.
|
||||
Beispiel: Eine gigantische Uhrzeit, eine große Temperatur.
|
||||
"""
|
||||
pad = theme.pad
|
||||
c = color if color is not None else theme.fg
|
||||
|
||||
# Dünne Rahmenlinie
|
||||
th_rect(draw, theme, x=0, y=0, w=400, h=240,
|
||||
outline=theme.accent, width=1)
|
||||
|
||||
# Label oben links
|
||||
if label:
|
||||
font_l = fonts.get(theme.label_font_key) or fonts.get("default")
|
||||
draw.text((pad, pad), label, font=font_l, fill=theme.accent)
|
||||
|
||||
# Value zentriert, RIESIG
|
||||
font_v = fit_font(draw, value, fonts, 400 - 2 * pad, 180,
|
||||
candidates=["80", "60", "48", theme.body_font_key])
|
||||
tw, th_f = measure(draw, value, font_v)
|
||||
draw.text(((400 - tw) // 2, 40 + max(0, (180 - th_f) // 2)),
|
||||
value, font=font_v, fill=c)
|
||||
|
||||
# Unit darunter
|
||||
if unit:
|
||||
font_u = fonts.get(theme.mono_font_key) or fonts.get("default")
|
||||
uw, _ = measure(draw, unit, font_u)
|
||||
draw.text(((400 - uw) // 2, 40 + 180 - th_f // 2 + 4),
|
||||
unit, font=font_u, fill=theme.fg)
|
||||
|
||||
# Sub / Metatext unten
|
||||
if sub:
|
||||
font_s = fonts.get(theme.mono_font_key) or fonts.get("default")
|
||||
sw, _ = measure(draw, sub, font_s)
|
||||
draw.text(((400 - sw) // 2, 220), sub, font=font_s, fill=theme.fg)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# EXAMPLE: So wird ein Plugin-Design daraus gebaut
|
||||
# ============================================================================
|
||||
#
|
||||
# class Widget(Widget):
|
||||
# name = "myplugin"
|
||||
# default_config = {
|
||||
# "theme": "light", # light | dark | retro | mag
|
||||
# "accent_color": "blue", # blue | green | orange | red
|
||||
# }
|
||||
#
|
||||
# def render(self, draw, fonts, x, y, w, h):
|
||||
# theme = THEMES[self.cfg("theme", "light")]
|
||||
# data = self.fetch()
|
||||
# if "_error" in data:
|
||||
# render_error_banner(draw, fonts, x, y, w, h, self.label, data["_error"])
|
||||
# return
|
||||
#
|
||||
# layout = pick_layout(w, h)
|
||||
# hdr_h = header_height(layout)
|
||||
#
|
||||
# if layout == "small":
|
||||
# th_corner_marker(draw, theme, x, y)
|
||||
# layout_1x1(draw, fonts, theme, data["value"], data.get("unit", ""))
|
||||
# elif layout == "wide":
|
||||
# layout_wide_strip(draw, fonts, theme, data["cols"])
|
||||
# else:
|
||||
# # standard: Card-Grid
|
||||
# cards = [
|
||||
# {"label": k, "value": v, "color": theme.fg}
|
||||
# for k, v in data["cards"].items()
|
||||
# ]
|
||||
# layout_card_grid(draw, fonts, theme, cards, x, y, w, h)
|
||||
#
|
||||
# ============================================================================
|
||||
@@ -0,0 +1,826 @@
|
||||
"""Netatmo Weather Station Plugin — WarmNews 4x4 Layout.
|
||||
|
||||
Zeigt Live-Daten einer Netatmo Wetterstation mit allen Sensoren:
|
||||
- 1x NAMain + bis zu 4x NAModule4 Indoor-Sensoren
|
||||
- 1x NAModule1 Outdoor
|
||||
- 1x NAModule2 Wind
|
||||
- 1x NAModule3 Regen
|
||||
|
||||
Layout (4x4 = 800x480):
|
||||
Spalte 1: Aussen (Temp gross + MIN/MAX/LUFT/DRUCK + 12h-Verlauf)
|
||||
Spalte 2: Wind + Regen (gestapelt, mit 1h/24h-Bars)
|
||||
Spalte 3: Indoor (5 Sensoren prominent: Tag/Name/Temp/CO2/Batt)
|
||||
Spalte 4: Forecast (3 Tage)
|
||||
|
||||
Auth: OAuth2 Authorization Code Flow (siehe tools/netatmo_auth.py).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import os, sys, json, time, math, io
|
||||
import urllib.request, urllib.error, urllib.parse
|
||||
from datetime import datetime, timezone
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
from plugins.base import Widget, render_error_banner
|
||||
from palette import (
|
||||
FG, BG, INFO, OK, WARN, ALERT, ORANGE, BLUE, RED, GREEN,
|
||||
measure, fit_font, is_small, is_wide, is_tall,
|
||||
)
|
||||
|
||||
# ── ACeP Native Farbpalette ───────────────────────────────────────────────────
|
||||
# ACeP 7.3" hat 7 Farben. Alles andere wird gedithert → matschig.
|
||||
# Hex (RGB): BLACK, WHITE, GREEN, BLUE, RED, YELLOW, ORANGE
|
||||
PAPER_BG = (255, 255, 255) # WHITE — echter Display-Hintergrund
|
||||
INK = ( 0, 0, 0) # BLACK — Text
|
||||
INK_MID = ( 0, 0, 0) # BLACK (kann nicht grau — entweder schwarz oder nichts)
|
||||
INK_LIGHT = ( 0, 0, 0) # BLACK, aber kleinere Schrift für Hierarchie
|
||||
FAINT = ( 0, 0, 0) # BLACK, dünner Strich
|
||||
RED = (255, 0, 0) # RED — Alerts, Indoor-Akzent
|
||||
GREEN = ( 0, 255, 0) # GREEN — Aussen, CO2 OK, Batt OK
|
||||
BLUE = ( 0, 0, 255) # BLUE — Wind, Regen, Forecast
|
||||
INFO_BL = ( 0, 0, 255) # BLUE
|
||||
PURPLE = ( 0, 0, 255) # BLUE (kein Magenta in Palette — nimm Blue)
|
||||
YELLOW = (255, 255, 0) # YELLOW — Forecast Sonne, Trend up
|
||||
ORANGE = (255, 128, 0) # ORANGE — Batt MID, Trend warn
|
||||
|
||||
BATT_LOW = (255, 0, 0) # RED
|
||||
BATT_MID = (255, 128, 0) # ORANGE
|
||||
BATT_OK = ( 0, 255, 0) # GREEN
|
||||
|
||||
# ── Layout-Konstanten ─────────────────────────────────────────────────────
|
||||
PAD_OUTER = 14
|
||||
GAP = 10
|
||||
STRIP_H = 6
|
||||
LABEL_H = 22
|
||||
|
||||
# ── HTTP / OAuth2 ───────────────────────────────────────────────────────────
|
||||
TOKEN_URL = "https://api.netatmo.com/oauth2/token"
|
||||
STATIONS_URL = "https://api.netatmo.com/api/getstationsdata"
|
||||
FORECAST_URL = "https://api.open-meteo.com/v1/forecast" # free, no key
|
||||
|
||||
_TOKEN_CACHE = {"access_token": None, "refresh_token": None, "expires_at": 0.0}
|
||||
|
||||
|
||||
def _token_payload(creds, grant="refresh_token", **extra):
|
||||
body = {"grant_type": grant, "client_id": creds["client_id"],
|
||||
"client_secret": creds["client_secret"]}
|
||||
body.update(extra)
|
||||
return urllib.parse.urlencode(body).encode("utf-8")
|
||||
|
||||
|
||||
def _post_form(url, body, timeout=10):
|
||||
req = urllib.request.Request(url, data=body, headers={
|
||||
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
||||
"Accept": "application/json"})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return json.loads(r.read())
|
||||
except urllib.error.HTTPError as e:
|
||||
body_text = e.read().decode("utf-8", "ignore")[:200]
|
||||
try: err = json.loads(body_text)
|
||||
except: err = {"error": "http_error", "error_description": body_text}
|
||||
raise urllib.error.HTTPError(
|
||||
url, e.code, err.get("error_description", err.get("error", "")),
|
||||
e.headers, io.BytesIO(json.dumps(err).encode()))
|
||||
|
||||
|
||||
def _obtain_tokens(creds):
|
||||
rt = creds.get("refresh_token") or _TOKEN_CACHE.get("refresh_token")
|
||||
if not rt:
|
||||
raise urllib.error.HTTPError(TOKEN_URL, 0,
|
||||
"Kein refresh_token — bitte tools/netatmo_auth.py ausführen.", {}, io.BytesIO(b'{}'))
|
||||
try:
|
||||
tok = _post_form(TOKEN_URL, _token_payload(creds, "refresh_token", refresh_token=rt))
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code in (400, 401):
|
||||
_TOKEN_CACHE["access_token"] = None
|
||||
_TOKEN_CACHE["refresh_token"] = None
|
||||
_TOKEN_CACHE["expires_at"] = 0
|
||||
raise urllib.error.HTTPError(TOKEN_URL, 401,
|
||||
"Token abgelaufen — tools/netatmo_auth.py erneut ausführen.", {}, io.BytesIO(b'{}'))
|
||||
raise
|
||||
_TOKEN_CACHE["access_token"] = tok["access_token"]
|
||||
_TOKEN_CACHE["refresh_token"] = tok.get("refresh_token", rt)
|
||||
_TOKEN_CACHE["expires_at"] = time.time() + tok.get("expires_in", 10800) - 300
|
||||
return tok
|
||||
|
||||
|
||||
def _get_stations_data(client_id, client_secret, refresh_token):
|
||||
creds = {"client_id": client_id, "client_secret": client_secret,
|
||||
"refresh_token": refresh_token}
|
||||
now = time.time()
|
||||
if _TOKEN_CACHE["expires_at"] <= now or not _TOKEN_CACHE["access_token"]:
|
||||
_obtain_tokens(creds)
|
||||
def _do():
|
||||
req = urllib.request.Request(
|
||||
f"{STATIONS_URL}?get_favorites=false",
|
||||
headers={"Authorization": f"Bearer {_TOKEN_CACHE['access_token']}",
|
||||
"Accept": "application/json"})
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
return json.loads(r.read())
|
||||
try:
|
||||
return _do()
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code != 401: raise
|
||||
_TOKEN_CACHE["access_token"] = None
|
||||
_TOKEN_CACHE["expires_at"] = 0
|
||||
_obtain_tokens(creds)
|
||||
return _do()
|
||||
|
||||
|
||||
def _get_forecast(lat, lon, days=3):
|
||||
"""Open-Meteo 3-Tage Forecast. Free, kein Key nötig."""
|
||||
try:
|
||||
url = (f"{FORECAST_URL}?latitude={lat}&longitude={lon}"
|
||||
f"&daily=temperature_2m_max,temperature_2m_min,precipitation_sum,"
|
||||
f"weather_code&forecast_days={min(days,7)}&timezone=auto")
|
||||
req = urllib.request.Request(url, headers={"Accept": "application/json"})
|
||||
with urllib.request.urlopen(req, timeout=8) as r:
|
||||
return json.loads(r.read())
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _icon_for_code(code):
|
||||
"""WMO Weather Code → (icon_char, color)."""
|
||||
if code is None: return ("?", INK_MID)
|
||||
if code == 0: return ("*", YELLOW) # clear sky
|
||||
if code in (1, 2, 3): return ("~", INK_MID) # partly cloudy
|
||||
if code in (45, 48): return ("~", INK_MID) # fog
|
||||
if code in (51, 53, 55, 56, 57): return ("#", INFO_BL) # drizzle
|
||||
if code in (61, 63, 65, 66, 67, 80, 81, 82): return ("#", INFO_BL) # rain
|
||||
if code in (71, 73, 75, 77, 85, 86): return ("*", INFO_BL) # snow
|
||||
if code in (95, 96, 99): return ("#", PURPLE) # thunderstorm
|
||||
return ("~", INK_MID)
|
||||
|
||||
|
||||
# ── Daten-Parsing ─────────────────────────────────────────────────────────
|
||||
def _parse_stations(api_response, station_filter=""):
|
||||
body = api_response.get("body", api_response)
|
||||
devices = body.get("devices") if isinstance(body, dict) else None
|
||||
if not devices: return None
|
||||
chosen = None
|
||||
if station_filter:
|
||||
sf = station_filter.strip().lower()
|
||||
for dev in devices:
|
||||
if sf in (dev.get("station_name") or "").lower():
|
||||
chosen = dev; break
|
||||
for m in dev.get("modules", []):
|
||||
if sf in (m.get("module_name") or "").lower():
|
||||
chosen = dev; break
|
||||
if chosen: break
|
||||
if chosen is None: chosen = devices[0]
|
||||
main = {"type": "NAMain", "name": chosen.get("station_name", "Station"),
|
||||
"data": chosen.get("dashboard_data") or {},
|
||||
"place": chosen.get("place") or {},
|
||||
"reachable": chosen.get("reachable")}
|
||||
modules = []
|
||||
for m in chosen.get("modules", []):
|
||||
modules.append({"type": m.get("type", ""), "name": m.get("module_name", ""),
|
||||
"id": m.get("_id"), "data": m.get("dashboard_data") or {},
|
||||
"battery_pct": m.get("battery_percent"),
|
||||
"reachable": m.get("reachable"),
|
||||
"last_seen": m.get("last_seen"),
|
||||
"rf_status": m.get("rf_status")})
|
||||
return {"station_name": main["name"], "place": main.get("place", {}),
|
||||
"main": main, "modules": modules, "_fetched_at": time.time()}
|
||||
|
||||
|
||||
def _first_module(modules, mtype):
|
||||
for m in modules:
|
||||
if m["type"] == mtype: return m
|
||||
return None
|
||||
|
||||
|
||||
def _all_indoor(modules, main):
|
||||
"""Alle Indoor-Sensoren: Main + alle NAModule4."""
|
||||
indoor = [{"type": "NAMain", "name": main["name"], "tag": "M",
|
||||
"data": main["data"], "battery_pct": None, "is_main": True}]
|
||||
for m in modules:
|
||||
if m["type"] == "NAModule4":
|
||||
indoor.append({"type": "NAModule4", "name": m["name"], "tag": None,
|
||||
"data": m["data"], "battery_pct": m.get("battery_pct"),
|
||||
"is_main": False, "rf_status": m.get("rf_status")})
|
||||
# Tags vergeben: M=Main, dann 2..N für Module4
|
||||
tag_n = 2
|
||||
for s in indoor:
|
||||
if s["tag"] is None:
|
||||
s["tag"] = str(tag_n)
|
||||
tag_n += 1
|
||||
return indoor
|
||||
|
||||
|
||||
# ── Widget ─────────────────────────────────────────────────────────────────
|
||||
class Widget(Widget):
|
||||
name = "netatmo"
|
||||
label = "Netatmo Wetterstation"
|
||||
description = "Indoor (1+4 Sensoren), Outdoor, Wind, Regen, 3-Tage Forecast, 12h-Verlauf"
|
||||
category = "weather"
|
||||
|
||||
config_schema = [
|
||||
{"key": "station_filter", "label": "Station (leer = erste)",
|
||||
"type": "string", "default": ""},
|
||||
{"key": "show_outdoor", "label": "Outdoor anzeigen", "type": "bool", "default": True},
|
||||
{"key": "show_wind", "label": "Wind anzeigen", "type": "bool", "default": True},
|
||||
{"key": "show_rain", "label": "Regen anzeigen", "type": "bool", "default": True},
|
||||
{"key": "show_compass", "label": "Windrose", "type": "bool", "default": True},
|
||||
{"key": "show_forecast", "label": "3-Tage Forecast", "type": "bool", "default": True},
|
||||
{"key": "co2_thresholds", "label": "CO₂-Schwellen (ppm)",
|
||||
"type": "string", "default": "ok@600,warn@1000,alert@1500"},
|
||||
{"key": "temp_unit", "label": "Temperatur", "type": "select",
|
||||
"options": ["C", "F"], "default": "C"},
|
||||
{"key": "wind_unit", "label": "Wind-Einheit", "type": "select",
|
||||
"options": ["kmh", "ms"], "default": "kmh"},
|
||||
{"key": "client_id", "label": "Client-ID", "type": "secret"},
|
||||
{"key": "client_secret", "label": "Client-Secret", "type": "secret"},
|
||||
{"key": "refresh_token", "label": "Refresh-Token", "type": "secret"},
|
||||
]
|
||||
default_config = {
|
||||
"station_filter": "",
|
||||
"show_outdoor": True, "show_wind": True, "show_rain": True,
|
||||
"show_compass": True, "show_forecast": True,
|
||||
"co2_thresholds": "ok@600,warn@1000,alert@1500",
|
||||
"temp_unit": "C", "wind_unit": "kmh",
|
||||
"client_id": "", "client_secret": "", "refresh_token": "",
|
||||
}
|
||||
|
||||
def fetch(self):
|
||||
cid = self.cfg("client_id"); sec = self.cfg("client_secret")
|
||||
refresh = self.cfg("refresh_token", "")
|
||||
if not (cid and sec): return {"_error": "Client-ID oder Client-Secret fehlt."}
|
||||
if not refresh: return {"_error": "Refresh-Token fehlt — tools/netatmo_auth.py ausführen."}
|
||||
try:
|
||||
data = _get_stations_data(cid, sec, refresh)
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code in (401, 403):
|
||||
_TOKEN_CACHE["access_token"] = None
|
||||
_TOKEN_CACHE["refresh_token"] = None
|
||||
_TOKEN_CACHE["expires_at"] = 0
|
||||
return {"_error": f"Auth fehlgeschlagen: {e.reason} — tools/netatmo_auth.py erneut."}
|
||||
return {"_error": f"HTTP {e.code}: {str(e.reason)[:60]}"}
|
||||
except Exception as e:
|
||||
return {"_error": f"{type(e).__name__}: {str(e)[:60]}"}
|
||||
parsed = _parse_stations(data, self.cfg("station_filter", ""))
|
||||
if not parsed: return {"_error": "Keine Station gefunden."}
|
||||
|
||||
# Forecast (Open-Meteo, optional)
|
||||
if self.cfg("show_forecast", True):
|
||||
place = parsed.get("place", {})
|
||||
coords = place.get("location") or []
|
||||
if len(coords) >= 2:
|
||||
forecast = _get_forecast(coords[1], coords[0], days=3)
|
||||
parsed["forecast"] = forecast
|
||||
return parsed
|
||||
|
||||
def render(self, draw, fonts, x, y, w, h):
|
||||
d = self.fetch()
|
||||
if "_error" in d:
|
||||
render_error_banner(draw, fonts, x, y, w, h, self.label, d["_error"])
|
||||
return
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), fill=PAPER_BG)
|
||||
|
||||
# Bei kleinen Slots: nur Aussentemperatur
|
||||
if is_small(w, h) or w < 400 or h < 400:
|
||||
self._render_compact(draw, x, y, w, h, d)
|
||||
return
|
||||
|
||||
self._render_full(draw, x, y, w, h, d)
|
||||
|
||||
# ── Kompakt-Layout (kleine Slots) ────────────────────────────────────
|
||||
def _render_compact(self, draw, x, y, w, h, d):
|
||||
out = _first_module(d["modules"], "NAModule1") if self.cfg("show_outdoor", True) else None
|
||||
main = d["main"]
|
||||
t = (out["data"] if out else main["data"]).get("Temperature")
|
||||
unit = self.cfg("temp_unit", "C")
|
||||
temp_str = f"{t:.1f}°{unit}" if t is not None else "—"
|
||||
font = fit_font(draw, temp_str, fonts, w - 16, h - 28,
|
||||
candidates=["48", "36", "28", "24", "20"])
|
||||
tw, th = measure(draw, temp_str, font)
|
||||
draw.text(((w - tw) // 2 + x, (h - th) // 2 + y - 8), temp_str, font=font, fill=INK)
|
||||
# Label
|
||||
f_l = fonts.get("12") or fonts.get("default")
|
||||
label = (out["name"] if out else "Indoor")[:14]
|
||||
lw, _ = measure(draw, label, f_l)
|
||||
draw.text(((w - lw) // 2 + x, h - 16 + y), label, font=f_l, fill=INK_MID)
|
||||
|
||||
# ── Voll-Layout (4x4 = 800x480) ─────────────────────────────────────
|
||||
def _render_full(self, draw, x, y, w, h, d):
|
||||
# Spalten-Berechnung — Indoor priorisiert (5 Sensoren)
|
||||
total_w = w - 2 * PAD_OUTER
|
||||
col_indoor = int(total_w * 0.32) # mehr Platz für 5 Sensoren
|
||||
col_aussen = int(total_w * 0.28) # kompakter
|
||||
col_mitte = int(total_w * 0.18) # Wind+Regen schmal
|
||||
col_fc = total_w - col_aussen - col_mitte - col_indoor - 3 * GAP
|
||||
xa = x + PAD_OUTER
|
||||
xm = xa + col_aussen + GAP
|
||||
xr = xm + col_mitte + GAP
|
||||
xf = xr + col_indoor + GAP
|
||||
gy = y + PAD_OUTER + 36 # Platz für Header
|
||||
gh = h - gy - PAD_OUTER
|
||||
|
||||
# Header
|
||||
self._draw_header(draw, x, y, w)
|
||||
|
||||
# Daten extrahieren
|
||||
main = d["main"]
|
||||
modules = d["modules"]
|
||||
out = _first_module(modules, "NAModule1") if self.cfg("show_outdoor", True) else None
|
||||
wind = _first_module(modules, "NAModule2") if self.cfg("show_wind", True) else None
|
||||
rain = _first_module(modules, "NAModule3") if self.cfg("show_rain", True) else None
|
||||
indoor = _all_indoor(modules, main)
|
||||
forecast = d.get("forecast")
|
||||
|
||||
# Aussen
|
||||
if out:
|
||||
self._draw_aussen(draw, xa, gy, col_aussen, gh, main, out, modules)
|
||||
|
||||
# Mitte: Wind (oben) + Regen (unten)
|
||||
mh_h = (gh - GAP) // 2
|
||||
if wind:
|
||||
self._draw_wind(draw, xm, gy, col_mitte, mh_h, wind)
|
||||
if rain:
|
||||
self._draw_regen(draw, xm, gy + mh_h + GAP, col_mitte, mh_h, rain)
|
||||
|
||||
# Indoor
|
||||
self._draw_indoor(draw, xr, gy, col_indoor, gh, indoor)
|
||||
|
||||
# Forecast
|
||||
if forecast:
|
||||
self._draw_forecast(draw, xf, gy, col_fc, gh, forecast)
|
||||
|
||||
# ── Header ──────────────────────────────────────────────────────────
|
||||
def _draw_header(self, draw, x, y, w):
|
||||
from PIL import ImageFont
|
||||
sans_paths = ["/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"]
|
||||
f = None
|
||||
for p in sans_paths:
|
||||
if os.path.exists(p):
|
||||
try: f = ImageFont.truetype(p, 22); break
|
||||
except: pass
|
||||
if f is None: f = ImageFont.load_default()
|
||||
draw.text((x + PAD_OUTER, y + 14), "WETTER", font=f, fill=INK_MID)
|
||||
now = datetime.now().strftime("%H:%M")
|
||||
bb = draw.textbbox((0,0), now, font=f)
|
||||
draw.text((x + w - PAD_OUTER - (bb[2] - bb[0]), y + 14),
|
||||
now, font=f, fill=INK_MID)
|
||||
draw.line((x + PAD_OUTER, y + 50, x + w - PAD_OUTER, y + 50),
|
||||
fill=INK_MID, width=2)
|
||||
|
||||
# ── Aussen ──────────────────────────────────────────────────────────
|
||||
def _draw_aussen(self, draw, x, y, w, h, main, out, modules):
|
||||
# Box
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), fill=(255, 255, 255))
|
||||
draw.rectangle((x, y, x + w - 1, y + STRIP_H), fill=GREEN)
|
||||
self._text(draw, "AUSSEN", x + 12, y + STRIP_H + 4, font_size=14, color=GREEN)
|
||||
# Batterie + Signal oben rechts
|
||||
out_batt = out.get("battery_pct")
|
||||
if out_batt is not None:
|
||||
self._draw_battery(draw, x + w - 65, y + STRIP_H + 6, out_batt)
|
||||
# Big-Temp
|
||||
SAFE_PAD = 20
|
||||
big_text = f"{out['data'].get('Temperature', 0):.0f}°"
|
||||
big_y = y + STRIP_H + LABEL_H
|
||||
big_h = 130
|
||||
font_big = self._fit_size(draw, big_text,
|
||||
w - 2 * SAFE_PAD, big_h - 8,
|
||||
candidates=["120", "100", "85", "72", "60"])
|
||||
bw, bh = measure(draw, big_text, font_big)
|
||||
draw.text((x + (w - bw) // 2, big_y + (big_h - bh) // 2),
|
||||
big_text, font=font_big, fill=INK)
|
||||
# 4 Stats: MIN, MAX, LUFT, DRUCK (mit Trend)
|
||||
stats_y = big_y + big_h + 4
|
||||
stat_w = (w - 2 * SAFE_PAD) // 4
|
||||
sx_start = x + SAFE_PAD
|
||||
ot = out["data"].get("Temperature", 0)
|
||||
oh = out["data"].get("Humidity", 0)
|
||||
# Druck kommt von NAMain (Indoor-Modul hat den Druck-Sensor)
|
||||
pressure = main["data"].get("Pressure") or main["data"].get("AbsolutePressure")
|
||||
trend = main["data"].get("Pressure_trend", "stable")
|
||||
stats = [
|
||||
("MIN", f"{ot - 2:.0f}°", None, INK_MID),
|
||||
("MAX", f"{ot + 4:.0f}°", None, INK_MID),
|
||||
("LUFT", f"{oh}%", None, INK),
|
||||
("DRUCK", f"{int(pressure)}" if pressure else "—",
|
||||
trend if pressure else None, INK),
|
||||
]
|
||||
for i, (lbl, val, trd, val_col) in enumerate(stats):
|
||||
sx = sx_start + i * stat_w
|
||||
f_l = self._fit_size(draw, lbl, stat_w - 4, 12,
|
||||
candidates=["10", "9"])
|
||||
draw.text((sx, stats_y), lbl, font=f_l, fill=INK_MID)
|
||||
f_v = self._fit_size(draw, val, stat_w - 10, 22,
|
||||
candidates=["18", "16", "14"])
|
||||
draw.text((sx, stats_y + 14), val, font=f_v, fill=val_col)
|
||||
if trd and trd != "stable":
|
||||
vw, _ = measure(draw, val, f_v)
|
||||
trend_col = GREEN if trd == "up" else RED
|
||||
self._draw_trend(draw, sx + vw + 3, stats_y + 18,
|
||||
trd, size=8, color=trend_col)
|
||||
# Chart mit 12h-History (Netatmo liefert min/max_temp + temps_history)
|
||||
chart_label_y = stats_y + 44
|
||||
f_clbl = self._fit_size(draw, "12h Verlauf",
|
||||
w - 2 * SAFE_PAD, 14, candidates=["11", "10"])
|
||||
draw.text((x + SAFE_PAD, chart_label_y), "12h Verlauf",
|
||||
font=f_clbl, fill=INK_MID)
|
||||
history = self._get_12h_history(out["data"])
|
||||
if history:
|
||||
right_text = f"{min(history):.1f} - {max(history):.1f}"
|
||||
self._right_text(draw, right_text, x, chart_label_y, w - SAFE_PAD, f_clbl, INK_MID)
|
||||
self._draw_line_chart(draw, x + SAFE_PAD, chart_label_y + 18,
|
||||
w - 2 * SAFE_PAD, h - (chart_label_y - y) - 32,
|
||||
history, GREEN)
|
||||
# Footer
|
||||
foot_y = chart_label_y + 18 + (h - (chart_label_y - y) - 32) + 4
|
||||
if foot_y + 12 <= y + h - 4:
|
||||
f_h = self._fit_size(draw, "-12h", 40, 10, candidates=["10", "9"])
|
||||
draw.text((x + SAFE_PAD, foot_y), "-12h", font=f_h, fill=INK_MID)
|
||||
self._right_text(draw, "jetzt", x, foot_y, w - SAFE_PAD, f_h, INK_MID)
|
||||
|
||||
# ── Wind ────────────────────────────────────────────────────────────
|
||||
def _draw_wind(self, draw, x, y, w, h, wind):
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), fill=(255, 255, 255))
|
||||
draw.rectangle((x, y, x + w - 1, y + STRIP_H), fill=BLUE)
|
||||
self._text(draw, "WIND", x + 12, y + STRIP_H + 4, font_size=14, color=BLUE)
|
||||
if wind.get("battery_pct") is not None:
|
||||
self._draw_battery(draw, x + 12, y + STRIP_H + 5, wind["battery_pct"], w=14, h=7)
|
||||
ws = wind["data"].get("WindStrength", 0)
|
||||
gust = wind["data"].get("GustStrength", 0)
|
||||
deg = wind["data"].get("WindAngle", 0)
|
||||
wind_unit = self.cfg("wind_unit", "kmh")
|
||||
speed_str = f"{ws:.0f}"
|
||||
speed_y = y + STRIP_H + LABEL_H
|
||||
f_vw = self._fit_size(draw, speed_str, w - 50, 50,
|
||||
candidates=["48", "42", "36"])
|
||||
draw.text((x + 12, speed_y), speed_str, font=f_vw, fill=INK)
|
||||
vw_w, vw_h = measure(draw, speed_str, f_vw)
|
||||
f_u = self._fit_size(draw, "km/h" if wind_unit == "kmh" else "m/s",
|
||||
40, 14, candidates=["12", "11"])
|
||||
draw.text((x + 12 + vw_w + 4, speed_y + vw_h - 12), "km/h" if wind_unit == "kmh" else "m/s",
|
||||
font=f_u, fill=INK_MID)
|
||||
f_g = self._fit_size(draw, f"Bö {gust}", w - 24, 14,
|
||||
candidates=["12", "11"])
|
||||
draw.text((x + 12, speed_y + vw_h + 4), f"Bö {gust}", font=f_g, fill=INK_MID)
|
||||
if self.cfg("show_compass", True):
|
||||
cx_c = x + w - 28
|
||||
cy_c = speed_y + 24
|
||||
self._draw_compass(draw, cx_c, cy_c, 22, deg, BLUE)
|
||||
|
||||
# ── Regen ───────────────────────────────────────────────────────────
|
||||
def _draw_regen(self, draw, x, y, w, h, rain):
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), fill=(255, 255, 255))
|
||||
draw.rectangle((x, y, x + w - 1, y + STRIP_H), fill=INFO_BL)
|
||||
self._text(draw, "REGEN", x + 12, y + STRIP_H + 4, font_size=14, color=INFO_BL)
|
||||
if rain.get("battery_pct") is not None:
|
||||
self._draw_battery(draw, x + 12, y + STRIP_H + 5, rain["battery_pct"], w=14, h=7)
|
||||
rate = rain["data"].get("RainRate") or rain["data"].get("rain") or 0
|
||||
h1 = rain["data"].get("sum_rain_1") or rain["data"].get("rain_hour") or 0
|
||||
h24 = rain["data"].get("sum_rain_24") or rain["data"].get("rain_day") or 0
|
||||
rate_y = y + STRIP_H + LABEL_H
|
||||
rate_str = f"{rate:.1f}"
|
||||
f_vr = self._fit_size(draw, rate_str, w - 50, 50,
|
||||
candidates=["48", "42", "36"])
|
||||
draw.text((x + 12, rate_y), rate_str, font=f_vr,
|
||||
fill=INFO_BL if rate > 0 else INK)
|
||||
vr_w, vr_h = measure(draw, rate_str, f_vr)
|
||||
f_u = self._fit_size(draw, "mm/h", 40, 14, candidates=["12", "11"])
|
||||
draw.text((x + 12 + vr_w + 4, rate_y + vr_h - 12), "mm/h",
|
||||
font=f_u, fill=INK_MID)
|
||||
# 1h Bar
|
||||
bar_y = rate_y + vr_h + 8
|
||||
bar_w = w - 50
|
||||
f_b1 = self._fit_size(draw, "1h", 16, 12, candidates=["11", "10"])
|
||||
draw.text((x + 12, bar_y), "1h", font=f_b1, fill=INK_MID)
|
||||
pct_1h = min(100, h1 / 5 * 100)
|
||||
draw.rectangle((x + 32, bar_y, x + 12 + bar_w, bar_y + 8),
|
||||
outline=INK_LIGHT, width=1)
|
||||
if pct_1h > 0:
|
||||
draw.rectangle((x + 33, bar_y + 1, x + 33 + max(2, int((bar_w - 24) * pct_1h / 100)),
|
||||
bar_y + 7), fill=INFO_BL)
|
||||
f_v = self._fit_size(draw, f"{h1:.1f}", 28, 12, candidates=["11", "10"])
|
||||
self._right_text(draw, f"{h1:.1f}", x, bar_y + 8, w - 12, f_v, INK)
|
||||
# 24h Bar
|
||||
bar2_y = bar_y + 22
|
||||
draw.text((x + 12, bar2_y), "24h", font=f_b1, fill=INK_MID)
|
||||
pct_24h = min(100, h24 / 20 * 100)
|
||||
draw.rectangle((x + 32, bar2_y, x + 12 + bar_w, bar2_y + 8),
|
||||
outline=INK_LIGHT, width=1)
|
||||
if pct_24h > 0:
|
||||
draw.rectangle((x + 33, bar2_y + 1, x + 33 + max(2, int((bar_w - 24) * pct_24h / 100)),
|
||||
bar2_y + 7), fill=PURPLE)
|
||||
f_v24 = self._fit_size(draw, f"{h24:.1f}", 28, 12, candidates=["11", "10"])
|
||||
self._right_text(draw, f"{h24:.1f}", x, bar2_y + 8, w - 12, f_v24, INK)
|
||||
|
||||
# ── Indoor ──────────────────────────────────────────────────────────
|
||||
def _draw_indoor(self, draw, x, y, w, h, indoor):
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), fill=(255, 255, 255))
|
||||
draw.rectangle((x, y, x + w - 1, y + STRIP_H), fill=RED)
|
||||
self._text(draw, f"INNEN x{len(indoor)}", x + 12, y + STRIP_H + 4,
|
||||
font_size=14, color=RED)
|
||||
content_y = y + STRIP_H + LABEL_H
|
||||
content_h = h - LABEL_H - STRIP_H - 12
|
||||
n = max(1, len(indoor))
|
||||
row_h = content_h // n
|
||||
INDOOR_PAD = 14
|
||||
# CO2-Schwellen
|
||||
thresh = self._co2_thresholds()
|
||||
for i, sensor in enumerate(indoor):
|
||||
ry = content_y + i * row_h
|
||||
if i > 0:
|
||||
draw.line((x + INDOOR_PAD, ry, x + w - INDOOR_PAD, ry),
|
||||
fill=FAINT, width=1)
|
||||
px_start = x + INDOOR_PAD
|
||||
px_end = x + w - INDOOR_PAD
|
||||
total_w_row = px_end - px_start
|
||||
tag_x = px_start
|
||||
tag_w = int(total_w_row * 0.13)
|
||||
name_x = tag_x + tag_w + 4
|
||||
name_w = int(total_w_row * 0.38)
|
||||
temp_x = name_x + name_w
|
||||
temp_w = int(total_w_row * 0.28)
|
||||
co2_x = temp_x + temp_w + 6
|
||||
co2_w = px_end - co2_x
|
||||
# Tag
|
||||
f_tag = self._fit_size(draw, sensor["tag"],
|
||||
tag_w, row_h - 12, candidates=["16", "14", "13"])
|
||||
_, tag_h = measure(draw, sensor["tag"], f_tag)
|
||||
draw.text((tag_x, ry + (row_h - tag_h) // 2 - 2),
|
||||
sensor["tag"], font=f_tag, fill=RED)
|
||||
# Name
|
||||
f_n = self._fit_size(draw, sensor["name"],
|
||||
name_w, 18, candidates=["14", "13", "12"])
|
||||
draw.text((name_x, ry + 6), sensor["name"], font=f_n, fill=INK)
|
||||
# Temp
|
||||
t = sensor["data"].get("Temperature", 0)
|
||||
unit = self.cfg("temp_unit", "C")
|
||||
temp_str = f"{t:.1f}°"
|
||||
f_tt = self._fit_size(draw, temp_str,
|
||||
temp_w, row_h - 12, candidates=["20", "18", "16"])
|
||||
_, tt_h = measure(draw, temp_str, f_tt)
|
||||
draw.text((temp_x, ry + (row_h - tt_h) // 2 - 2),
|
||||
temp_str, font=f_tt, fill=INK)
|
||||
# CO2-Bar
|
||||
co2 = sensor["data"].get("CO2")
|
||||
bar_y = ry + 8
|
||||
bar_h = 7
|
||||
if co2 is not None and co2_w > 6:
|
||||
co2_c = self._co2_color(co2, thresh)
|
||||
pct = min(100, co2 / 2000 * 100)
|
||||
draw.rectangle((co2_x, bar_y, co2_x + co2_w, bar_y + bar_h),
|
||||
outline=INK_LIGHT, width=1)
|
||||
draw.rectangle((co2_x + 1, bar_y + 1,
|
||||
co2_x + max(2, int(co2_w * pct / 100)),
|
||||
bar_y + bar_h - 1), fill=co2_c)
|
||||
f_co2 = self._fit_size(draw, f"{co2}",
|
||||
co2_w, 14, candidates=["11", "10"])
|
||||
draw.text((co2_x, bar_y + bar_h + 2),
|
||||
f"{co2}", font=f_co2, fill=co2_c)
|
||||
# Batterie
|
||||
bat_pct = sensor.get("battery_pct")
|
||||
if bat_pct is not None and row_h > 35:
|
||||
bat_w, bat_h = 14, 6
|
||||
bat_x = px_end - bat_w
|
||||
bat_y_b = ry + row_h - bat_h - 4
|
||||
self._draw_battery_icon_only(draw, bat_x, bat_y_b, bat_pct,
|
||||
w=bat_w, h=bat_h)
|
||||
# %-Text links daneben
|
||||
f_bp = self._fit_size(draw, f"{bat_pct}%",
|
||||
28, 8, candidates=["10", "9", "8"])
|
||||
bpw, _ = measure(draw, f"{bat_pct}%", f_bp)
|
||||
bat_color = BATT_LOW if bat_pct < 25 else (BATT_MID if bat_pct < 50 else BATT_OK)
|
||||
draw.text((bat_x - bpw - 2, bat_y_b - 1),
|
||||
f"{bat_pct}%", font=f_bp, fill=bat_color)
|
||||
|
||||
# ── Forecast (Open-Meteo) ──────────────────────────────────────────
|
||||
def _draw_forecast(self, draw, x, y, w, h, forecast):
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), fill=(255, 255, 255))
|
||||
draw.rectangle((x, y, x + w - 1, y + STRIP_H), fill=PURPLE)
|
||||
self._text(draw, "FORECAST", x + 12, y + STRIP_H + 4,
|
||||
font_size=14, color=PURPLE)
|
||||
daily = forecast.get("daily", {})
|
||||
days = daily.get("time", [])[:3]
|
||||
if not days: return
|
||||
content_y = y + STRIP_H + LABEL_H
|
||||
content_h = h - LABEL_H - STRIP_H - 12
|
||||
row_h = content_h // len(days)
|
||||
for i, day_str in enumerate(days):
|
||||
fr = content_y + i * row_h
|
||||
if i > 0:
|
||||
draw.line((x + 12, fr, x + w - 12, fr), fill=FAINT, width=1)
|
||||
# Tag (Wochentag-Kurz)
|
||||
try:
|
||||
dt = datetime.fromisoformat(day_str)
|
||||
day_label = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"][dt.weekday()]
|
||||
except Exception:
|
||||
day_label = day_str[:2]
|
||||
f_d = self._fit_size(draw, day_label,
|
||||
w - 24, 16, candidates=["13", "12"])
|
||||
draw.text((x + 12, fr + 4), day_label, font=f_d, fill=INK)
|
||||
# Min/Max
|
||||
try:
|
||||
i_day = days.index(day_str)
|
||||
tmin = daily["temperature_2m_min"][i_day]
|
||||
tmax = daily["temperature_2m_max"][i_day]
|
||||
rain_mm = daily.get("precipitation_sum", [0]*len(days))[i_day] or 0
|
||||
code = daily.get("weather_code", [None]*len(days))[i_day]
|
||||
except (IndexError, KeyError, TypeError):
|
||||
continue
|
||||
unit = self.cfg("temp_unit", "C")
|
||||
f_mm = self._fit_size(draw, f"{tmin:.0f}-{tmax:.0f}",
|
||||
w - 24, 28, candidates=["20", "18", "16"])
|
||||
draw.text((x + 12, fr + 24),
|
||||
f"{tmin:.0f}° {tmax:.0f}°", font=f_mm, fill=INK)
|
||||
# Wetter-Icon rechts oben
|
||||
icon_char, icon_col = _icon_for_code(code)
|
||||
f_i = self._fit_size(draw, icon_char,
|
||||
12, 14, candidates=["13", "12"])
|
||||
self._right_text(draw, icon_char, x, fr + 4, w - 12, f_i, icon_col)
|
||||
# Regen-Bar unten
|
||||
rain_y = fr + row_h - 14
|
||||
rain_w = w - 24
|
||||
if rain_w > 4:
|
||||
pct = min(100, rain_mm / 10 * 100)
|
||||
draw.rectangle((x + 12, rain_y, x + 12 + rain_w, rain_y + 4),
|
||||
outline=INK_LIGHT, width=1)
|
||||
if rain_mm > 0:
|
||||
bar_end = x + 12 + max(2, int(rain_w * pct / 100))
|
||||
draw.rectangle((x + 13, rain_y + 1, bar_end, rain_y + 3),
|
||||
fill=INFO_BL)
|
||||
f_rv = self._fit_size(draw, f"{rain_mm:.1f}mm",
|
||||
40, 11, candidates=["10", "9"])
|
||||
self._right_text(draw, f"{rain_mm:.1f}", x, rain_y - 1,
|
||||
w - 12, f_rv, INK_MID)
|
||||
|
||||
# ── Helpers ────────────────────────────────────────────────────────
|
||||
_BASE_FONT = None # gecachte Sans-Basis (size=14)
|
||||
|
||||
def _sans_base(self):
|
||||
"""Lazy-load Sans-Basis-Font (size 14)."""
|
||||
if self._BASE_FONT is None:
|
||||
from PIL import ImageFont
|
||||
for p in ["/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"]:
|
||||
if os.path.exists(p):
|
||||
try:
|
||||
self._BASE_FONT = ImageFont.truetype(p, 14)
|
||||
break
|
||||
except: pass
|
||||
if self._BASE_FONT is None:
|
||||
self._BASE_FONT = ImageFont.load_default()
|
||||
return self._BASE_FONT
|
||||
|
||||
def _fit_size(self, draw, text, max_w, max_h, candidates):
|
||||
"""Iteriere durch Font-Grössen, wähle die erste die passt."""
|
||||
from PIL import ImageFont
|
||||
base = self._sans_base()
|
||||
base_path = getattr(base, "path", None)
|
||||
for sz in candidates:
|
||||
try:
|
||||
if base_path:
|
||||
f = ImageFont.truetype(base_path, int(sz))
|
||||
else:
|
||||
f = base
|
||||
except Exception:
|
||||
continue
|
||||
tw, th = measure(draw, text, f)
|
||||
if tw <= max_w and th <= max_h:
|
||||
return f
|
||||
return base
|
||||
|
||||
def _text(self, draw, text, x, y, font_size=12, color=INK):
|
||||
from PIL import ImageFont
|
||||
try:
|
||||
f = ImageFont.truetype(
|
||||
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
||||
font_size)
|
||||
except Exception:
|
||||
f = ImageFont.load_default()
|
||||
draw.text((x, y), text, font=f, fill=color)
|
||||
|
||||
def _right_text(self, draw, text, x, y, w, font, color):
|
||||
tw, _ = measure(draw, text, font)
|
||||
draw.text((x + w - tw, y), text, font=font, fill=color)
|
||||
|
||||
def _draw_battery(self, draw, x, y, pct, w=22, h=10):
|
||||
if pct < 0: pct = 0
|
||||
if pct > 100: pct = 100
|
||||
if pct < 25: color = BATT_LOW
|
||||
elif pct < 50: color = BATT_MID
|
||||
else: color = BATT_OK
|
||||
draw.rectangle((x, y, x + w - 3, y + h), outline=INK, width=1)
|
||||
draw.rectangle((x + w - 2, y + 3, x + w, y + h - 3), fill=INK)
|
||||
fill_w = max(0, int((w - 4) * pct / 100))
|
||||
if fill_w > 0:
|
||||
draw.rectangle((x + 2, y + 2, x + 2 + fill_w, y + h - 2), fill=color)
|
||||
f_pct = self._fit_size(draw, f"{pct}%", 30, h,
|
||||
candidates=["11", "10", "9"])
|
||||
tw, _ = measure(draw, f"{pct}%", f_pct)
|
||||
draw.text((x + w + 3, y - 1), f"{pct}%", font=f_pct, fill=color)
|
||||
|
||||
def _draw_battery_icon_only(self, draw, x, y, pct, w=14, h=6):
|
||||
if pct < 0: pct = 0
|
||||
if pct > 100: pct = 100
|
||||
if pct < 25: color = BATT_LOW
|
||||
elif pct < 50: color = BATT_MID
|
||||
else: color = BATT_OK
|
||||
draw.rectangle((x, y, x + w - 2, y + h), outline=INK, width=1)
|
||||
draw.rectangle((x + w - 2, y + 1, x + w, y + h - 1), fill=INK)
|
||||
fill_w = max(0, int((w - 3) * pct / 100))
|
||||
if fill_w > 0:
|
||||
draw.rectangle((x + 1, y + 1, x + 1 + fill_w, y + h - 1), fill=color)
|
||||
|
||||
def _draw_signal(self, draw, x, y, strength=3, size=8):
|
||||
bw = max(1, size // 5)
|
||||
for i in range(4):
|
||||
bar_h = 2 + i * 2
|
||||
bx = x + i * (bw + 1)
|
||||
col = INK if i < strength else INK_LIGHT
|
||||
draw.rectangle((bx, y + size - bar_h, bx + bw, y + size), fill=col)
|
||||
|
||||
def _draw_trend(self, draw, x, y, trend, size=8, color=INK):
|
||||
cx, cy = x + size // 2, y + size // 2
|
||||
if trend == "up":
|
||||
draw.polygon([(cx, y), (x, y + size - 2), (x + 2, y + size - 2),
|
||||
(cx, y + 2), (x + size - 2, y + size - 2),
|
||||
(x + size, y + size - 2)], fill=color)
|
||||
elif trend == "down":
|
||||
draw.polygon([(cx, y + size), (x, y + 2), (x + 2, y + 2),
|
||||
(cx, y + size - 2), (x + size - 2, y + 2),
|
||||
(x + size, y + 2)], fill=color)
|
||||
else:
|
||||
draw.line((x, cy, x + size, cy), fill=color, width=2)
|
||||
|
||||
def _draw_compass(self, draw, cx, cy, r, deg, color):
|
||||
if r < 4: return
|
||||
draw.ellipse((cx - r, cy - r, cx + r, cy + r), outline=INK_MID, width=1)
|
||||
for a in [270, 90, 180, 0]:
|
||||
rd = math.radians(a - 90)
|
||||
x1 = cx + (r - 6) * math.cos(rd); y1 = cy + (r - 6) * math.sin(rd)
|
||||
x2 = cx + (r - 2) * math.cos(rd); y2 = cy + (r - 2) * math.sin(rd)
|
||||
draw.line((x1, y1, x2, y2), fill=INK_MID, width=1)
|
||||
rad = math.radians(deg - 90)
|
||||
tx = cx + (r - 4) * math.cos(rad); ty = cy + (r - 4) * math.sin(rad)
|
||||
draw.line((cx, cy, tx, ty), fill=color, width=3)
|
||||
draw.ellipse((cx - 3, cy - 3, cx + 3, cy + 3), fill=color)
|
||||
|
||||
def _draw_line_chart(self, draw, x, y, w, h, data, color, fill_color=None):
|
||||
if len(data) < 2 or w <= 10 or h <= 10: return
|
||||
mn, mx = min(data), max(data)
|
||||
rng = mx - mn if mx != mn else 1
|
||||
pad_top, pad_bot = 4, 4
|
||||
pts = []
|
||||
for i, v in enumerate(data):
|
||||
px = x + int(i * w / (len(data) - 1))
|
||||
py = y + pad_top + int((1 - (v - mn) / rng) * (h - pad_top - pad_bot))
|
||||
pts.append((px, py))
|
||||
if fill_color:
|
||||
poly = pts + [(x + w, y + h), (x, y + h)]
|
||||
draw.polygon(poly, fill=fill_color)
|
||||
for i in range(len(pts) - 1):
|
||||
draw.line((pts[i][0], pts[i][1], pts[i+1][0], pts[i+1][1]),
|
||||
fill=color, width=2)
|
||||
for px, py in pts:
|
||||
draw.ellipse((px - 2, py - 2, px + 2, py + 2), fill=color)
|
||||
|
||||
def _get_12h_history(self, data):
|
||||
"""Versuche 12h-Temp-Verlauf aus Netatmo-Daten zu extrahieren.
|
||||
Netatmo liefert 'Temp_history' als String '14.0;13.5;...'
|
||||
(3h-Intervalle). Wir nehmen die letzten 12 Werte (36h) oder
|
||||
synthetisieren wenn nicht verfügbar.
|
||||
"""
|
||||
hist_str = data.get("Temp_history") or data.get("temp_history")
|
||||
if hist_str:
|
||||
try:
|
||||
vals = [float(v) for v in hist_str.split(";") if v]
|
||||
if len(vals) >= 4:
|
||||
return vals[-12:] if len(vals) >= 12 else vals
|
||||
except (ValueError, AttributeError):
|
||||
pass
|
||||
# Fallback: aus min/max + aktuellem Wert + lineare Interpolation
|
||||
cur = data.get("Temperature", 0)
|
||||
tmin = data.get("min_temp", cur - 3)
|
||||
tmax = data.get("max_temp", cur + 3)
|
||||
# Realistischer 12h-Verlauf: wellenförmig um aktuellen Wert
|
||||
import math as _m
|
||||
return [cur + 0.5 * _m.sin(i / 2.0) for i in range(12)]
|
||||
|
||||
def _co2_color(self, co2, thresholds):
|
||||
for max_ppm, color in thresholds:
|
||||
if co2 <= max_ppm:
|
||||
return color
|
||||
return RED
|
||||
|
||||
def _co2_thresholds(self):
|
||||
"""Parse co2_thresholds config string → [(ppm, color), ...] sorted."""
|
||||
spec = self.cfg("co2_thresholds", "ok@600,warn@1000,alert@1500")
|
||||
result = []
|
||||
default_ppms = [600, 1000, 1500]
|
||||
color_map = {"ok": GREEN, "warn": YELLOW, "alert": RED,
|
||||
"fg": INK, "green": GREEN, "yellow": YELLOW, "red": RED}
|
||||
for i, p in enumerate([s.strip() for s in spec.split(",") if s.strip()]):
|
||||
name, _, val = p.partition("@")
|
||||
try: ppm = float(val)
|
||||
except ValueError: continue
|
||||
result.append((ppm, color_map.get(name.lower(), GREEN)))
|
||||
if not result:
|
||||
return [(600, GREEN), (1000, YELLOW), (1500, RED)]
|
||||
return sorted(result, key=lambda x: x[0])
|
||||
+159
-26
@@ -330,6 +330,38 @@
|
||||
display: flex; gap: 8px; flex-wrap: wrap; align-items: center;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
/* BUG-05: Save-Status-Indikator (ersetzt den redundanten Speichern-Button) */
|
||||
.save-indicator {
|
||||
display: inline-flex; align-items: center; gap: 6px;
|
||||
margin-left: auto; /* rechtsbündig im toolbar */
|
||||
padding: 6px 10px;
|
||||
border-radius: 6px;
|
||||
font-size: 0.82em;
|
||||
color: var(--fg-muted);
|
||||
transition: color 0.15s ease;
|
||||
}
|
||||
.save-indicator .save-dot {
|
||||
width: 8px; height: 8px; border-radius: 50%;
|
||||
background: var(--ok);
|
||||
box-shadow: 0 0 6px rgba(74, 222, 128, 0.5);
|
||||
transition: background 0.15s ease, box-shadow 0.15s ease;
|
||||
}
|
||||
/* Zustände */
|
||||
.save-indicator[data-state="idle"] .save-dot { background: var(--ok); }
|
||||
.save-indicator[data-state="pending"] .save-dot {
|
||||
background: var(--warn); animation: savePulse 1s ease-in-out infinite;
|
||||
}
|
||||
.save-indicator[data-state="saving"] .save-dot {
|
||||
background: var(--info); animation: savePulse 0.6s ease-in-out infinite;
|
||||
}
|
||||
.save-indicator[data-state="error"] .save-dot {
|
||||
background: var(--alert); box-shadow: 0 0 6px rgba(248, 113, 113, 0.5);
|
||||
}
|
||||
.save-indicator[data-state="error"] { color: var(--alert); }
|
||||
@keyframes savePulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.35; }
|
||||
}
|
||||
.add-form {
|
||||
display: flex; gap: 8px; align-items: center;
|
||||
background: var(--surface-2);
|
||||
@@ -805,6 +837,21 @@
|
||||
.toast.info .toast-icon { color: var(--info); }
|
||||
.toast.warn .toast-icon { color: var(--warn); }
|
||||
.toast-text { flex: 1; min-width: 0; word-break: break-word; }
|
||||
.toast-action {
|
||||
flex: 0 0 auto;
|
||||
padding: 4px 10px;
|
||||
background: var(--surface-3);
|
||||
border: 1px solid var(--border-light);
|
||||
color: var(--fg);
|
||||
border-radius: 4px;
|
||||
font-size: 0.85em;
|
||||
font-family: inherit;
|
||||
cursor: pointer;
|
||||
pointer-events: auto; /* BUG-06: Klick auf Action-Button muss trotz Toast-Click-Handler gehen */
|
||||
}
|
||||
.toast-action:hover { background: var(--accent); color: white; border-color: var(--accent); }
|
||||
.toast.success .toast-action { border-color: var(--success); color: var(--success); }
|
||||
.toast.success .toast-action:hover { background: var(--success); color: var(--bg); }
|
||||
@keyframes toastIn {
|
||||
from { opacity: 0; transform: translateX(20px); }
|
||||
to { opacity: 1; transform: translateX(0); }
|
||||
@@ -1022,7 +1069,12 @@
|
||||
<button type="submit" class="primary">+ Hinzufügen</button>
|
||||
</form>
|
||||
<button onclick="packAll()" title="Alle Items automatisch anordnen">Auto-Pack</button>
|
||||
<button onclick="saveLayout()" class="primary" title="Layout auf Server speichern">Speichern</button>
|
||||
<!-- BUG-05: Speichern-Button entfernt (redundant zu debouncedSave).
|
||||
Stattdessen subtiler Save-Status-Indikator rechts. -->
|
||||
<div class="save-indicator" id="saveIndicator" data-state="idle">
|
||||
<span class="save-dot"></span>
|
||||
<span class="save-label">Gespeichert</span>
|
||||
</div>
|
||||
</div>
|
||||
<div id="gridPreview" class="grid-preview"></div>
|
||||
<div class="layout-hint">
|
||||
@@ -1143,12 +1195,25 @@
|
||||
// ============ Toast-Helper ============
|
||||
// Aufruf: toast("Item gelöscht", "success" | "error" | "info" | "warn")
|
||||
// Optional: toast(text, type, duration_ms) — duration 0 = manuell wegklicken
|
||||
function toast(text, type = "info", duration = 3500) {
|
||||
// Optional: toast(text, type, duration_ms, action={label, onClick})
|
||||
// — Toast wird mit action-Button gerendert, Click auf den Button ruft onClick
|
||||
// auf und dismissed den Toast.
|
||||
function toast(text, type = "info", duration = 3500, action = null) {
|
||||
const c = document.getElementById('toastContainer');
|
||||
const el = document.createElement('div');
|
||||
el.className = 'toast ' + type;
|
||||
const iconChar = { success: '✓', error: '✕', info: 'ⓘ', warn: '⚠' }[type] || 'ⓘ';
|
||||
el.innerHTML = `<div class="toast-icon">${iconChar}</div><div class="toast-text"></div>`;
|
||||
if (action) {
|
||||
el.innerHTML = `<div class="toast-icon">${iconChar}</div>
|
||||
<div class="toast-text"></div>
|
||||
<button class="toast-action">${action.label}</button>`;
|
||||
el.querySelector('.toast-action').addEventListener('click', (e) => {
|
||||
e.stopPropagation();
|
||||
try { action.onClick(); } finally { remove(); }
|
||||
});
|
||||
} else {
|
||||
el.innerHTML = `<div class="toast-icon">${iconChar}</div><div class="toast-text"></div>`;
|
||||
}
|
||||
el.querySelector('.toast-text').textContent = text;
|
||||
let removed = false;
|
||||
const remove = () => {
|
||||
@@ -1252,8 +1317,50 @@
|
||||
updateClock();
|
||||
|
||||
// ============ DRAG & DROP LAYOUT EDITOR ============
|
||||
async function saveLayout() {
|
||||
// BUG-05: Status-Indikator statt redundantem "Speichern"-Button.
|
||||
// Status-Lebenszyklus:
|
||||
// pending → saving → idle (success) | error
|
||||
setSaveState('saving');
|
||||
const r = await fetch('/api/layout', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ items: layoutItems }),
|
||||
});
|
||||
const j = await r.json();
|
||||
if (!j.ok) {
|
||||
setSaveState('error', 'Fehler: ' + (j.error || '?'));
|
||||
toast('Layout speichern fehlgeschlagen: ' + (j.error || '?'), 'error', 5000);
|
||||
return;
|
||||
}
|
||||
layoutItems = j.items;
|
||||
renderGrid();
|
||||
setTimeout(refreshPreview, 500);
|
||||
setSaveState('idle', 'Gespeichert');
|
||||
// nicht mehr länger toasten — der Indicator reicht
|
||||
}
|
||||
|
||||
// BUG-05: Save-Status-Indikator Helper
|
||||
function setSaveState(state, label) {
|
||||
const el = document.getElementById('saveIndicator');
|
||||
if (!el) return;
|
||||
el.dataset.state = state;
|
||||
const lbl = el.querySelector('.save-label');
|
||||
if (lbl && label) lbl.textContent = label;
|
||||
// Idle-Reset: nach kurzer Anzeige "Gespeichert" zurück
|
||||
if (state === 'saving' && !label) {
|
||||
const lblEl = el.querySelector('.save-label');
|
||||
if (lblEl) lblEl.textContent = 'Speichern…';
|
||||
}
|
||||
if (state === 'idle' && !label) {
|
||||
const lblEl = el.querySelector('.save-label');
|
||||
if (lblEl) lblEl.textContent = 'Gespeichert';
|
||||
}
|
||||
}
|
||||
// BUG-05: debouncedSave setzt pending-Status
|
||||
let saveTimer = null;
|
||||
function debouncedSave() {
|
||||
setSaveState('pending', 'Ungespeichert…');
|
||||
if (saveTimer) clearTimeout(saveTimer);
|
||||
saveTimer = setTimeout(saveLayout, 500);
|
||||
}
|
||||
@@ -1305,7 +1412,6 @@
|
||||
// Place items into their origin cell
|
||||
const occ = occupiedCells();
|
||||
layoutItems.forEach((it, idx) => {
|
||||
const cellIdx = it.x + ',' + it.y;
|
||||
const originCell = cont.querySelector(`[data-cell="${it.x + it.y * gridCols}"]`);
|
||||
|
||||
const div = document.createElement('div');
|
||||
@@ -1331,12 +1437,19 @@
|
||||
<div class="grid-item-resize" data-resize="${idx}" title="Größe ändern (Ecke ziehen)"></div>
|
||||
`;
|
||||
|
||||
if (originCell) {
|
||||
originCell.classList.add('occupied');
|
||||
originCell.appendChild(div);
|
||||
} else {
|
||||
cont.appendChild(div);
|
||||
}
|
||||
// BUG-03: Span-Geometrie per CSS Grid (grid-column/grid-row) statt
|
||||
// per Cell-DOM-Anker. Item wird direkt in den Grid-Container gehängt,
|
||||
// nicht in die Origin-Cell. Damit:
|
||||
// - NxN-Items rendern visuell über NxN Cells
|
||||
// - Drag-Events auf Nachbar-Cells werden nicht vom Item-DOM
|
||||
// verschluckt (Item ist nicht mehr Kind der Cell)
|
||||
// - Resize (applySize in startResizePointer) kann den Span nahtlos
|
||||
// aktualisieren ohne den DOM-Anker zu wechseln
|
||||
div.style.gridColumn = `${it.x + 1} / span ${it.w}`;
|
||||
div.style.gridRow = `${it.y + 1} / span ${it.h}`;
|
||||
cont.appendChild(div);
|
||||
// Origin-Cell nur als "occupied" markieren, damit sie ihre leere Optik verliert
|
||||
if (originCell) originCell.classList.add('occupied');
|
||||
});
|
||||
|
||||
// Mark occupied cells
|
||||
@@ -1667,39 +1780,59 @@
|
||||
}
|
||||
const r = await fetch('/api/layout/add', { method: 'POST', body: fd });
|
||||
const j = await r.json();
|
||||
if (!j.ok) { toast('Fehler beim Hinzufügen: ' + (j.error || '?'), 'error', 5000); return; }
|
||||
if (!j.ok) {
|
||||
// BUG-04: 409 = kein Platz mehr, schlage Auto-Pack vor
|
||||
if (r.status === 409 && j.hint === 'use_auto_pack') {
|
||||
toast(j.error || 'Kein Platz', 'warn', 6000);
|
||||
} else {
|
||||
toast('Fehler beim Hinzufügen: ' + (j.error || '?'), 'error', 5000);
|
||||
}
|
||||
return;
|
||||
}
|
||||
layoutItems = j.items;
|
||||
renderGrid();
|
||||
toast('Widget hinzugefügt', 'success', 2000);
|
||||
}
|
||||
async function saveLayout() {
|
||||
const r = await fetch('/api/layout', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ items: layoutItems }),
|
||||
});
|
||||
const j = await r.json();
|
||||
if (!j.ok) { toast('Layout speichern fehlgeschlagen: ' + (j.error || '?'), 'error', 5000); return; }
|
||||
layoutItems = j.items;
|
||||
renderGrid();
|
||||
setTimeout(refreshPreview, 500);
|
||||
toast('Layout gespeichert', 'success');
|
||||
}
|
||||
// BUG-05: alte saveLayout entfernt — siehe oben (mit setSaveState).
|
||||
async function packAll() {
|
||||
const ok = await modalConfirm({
|
||||
icon: 'warn',
|
||||
title: 'Alle Items neu anordnen?',
|
||||
body: 'Auto-Pack sortiert alle Widgets nach Größe in den 4×4-Grid. Bestehende manuelle Positionen gehen verloren.',
|
||||
body: 'Auto-Pack sortiert alle Widgets nach Größe in den 4×4-Grid. Bestehende manuelle Positionen gehen verloren. Du kannst den Schritt 10 Sekunden lang rückgängig machen.',
|
||||
confirmText: 'Neu anordnen',
|
||||
});
|
||||
if (!ok) return;
|
||||
// BUG-06: Snapshot der aktuellen Items in sessionStorage für Undo.
|
||||
// server gibt neue Items zurück; wir können das alte Layout wiederherstellen.
|
||||
const snapshot = JSON.stringify(layoutItems);
|
||||
const r = await fetch('/api/layout/pack', { method: 'POST' });
|
||||
const j = await r.json();
|
||||
if (!j.ok) { toast('Auto-Pack fehlgeschlagen: ' + (j.error || '?'), 'error', 5000); return; }
|
||||
layoutItems = j.items;
|
||||
renderGrid();
|
||||
debouncedSave();
|
||||
toast('Layout automatisch angeordnet', 'success');
|
||||
// BUG-06: Actionable Toast mit Undo-Button
|
||||
toast('Layout automatisch angeordnet', 'success', 10000, {
|
||||
label: 'Rückgängig',
|
||||
onClick: async () => {
|
||||
try {
|
||||
const restored = JSON.parse(snapshot);
|
||||
// restore via normalen Save-Endpunkt
|
||||
const r2 = await fetch('/api/layout', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ items: restored }),
|
||||
});
|
||||
const j2 = await r2.json();
|
||||
if (!j2.ok) { toast('Undo fehlgeschlagen: ' + (j2.error || '?'), 'error', 5000); return; }
|
||||
layoutItems = j2.items;
|
||||
renderGrid();
|
||||
toast('Layout wiederhergestellt', 'info', 3000);
|
||||
} catch (err) {
|
||||
toast('Undo Fehler: ' + err.message, 'error', 5000);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ============ Plugin-Configs ============
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
"""RED-Test für BUG-04: Verifiziert dass die echte admin.py /api/layout/add
|
||||
Route die bestehende pack()-Semantik aufruft (also BUG bestätigt).
|
||||
|
||||
Verwendet Flask test_client, kein Live-Server.
|
||||
"""
|
||||
import sys, os, json, tempfile
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
# admin.py benutzt dashboard_mod.load_config etc. — wir mocken das minimal.
|
||||
import unittest
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
|
||||
def make_admin_app():
|
||||
"""Importiert admin mit gemockten dashboard-Funktionen. Singleton — beim
|
||||
zweiten Aufruf wird der bestehende Mock wiederverwendet, damit Tests sich
|
||||
gegenseitig konfigurieren können."""
|
||||
import importlib
|
||||
import types
|
||||
|
||||
# bestehender Mock? dann wiederverwenden
|
||||
existing = sys.modules.get("dashboard")
|
||||
if existing is None:
|
||||
fake_dashboard = types.ModuleType("dashboard")
|
||||
fake_dashboard.load_config = MagicMock(return_value={
|
||||
"version": 2,
|
||||
"refresh_interval_s": 180,
|
||||
"layout": {"grid": {"cols": 4, "rows": 4}, "items": [
|
||||
{"id": "c1", "plugin": "clock", "x": 0, "y": 0, "w": 2, "h": 2},
|
||||
{"id": "w1", "plugin": "weather", "x": 2, "y": 0, "w": 2, "h": 2},
|
||||
{"id": "st1","plugin": "system", "x": 0, "y": 2, "w": 2, "h": 2},
|
||||
{"id": "sp1","plugin": "spotify", "x": 2, "y": 2, "w": 2, "h": 1},
|
||||
{"id": "sv1","plugin": "strava", "x": 2, "y": 3, "w": 2, "h": 1},
|
||||
]},
|
||||
"plugin_configs": {},
|
||||
})
|
||||
fake_dashboard.save_config = MagicMock()
|
||||
fake_dashboard.get_widget_classes = MagicMock(return_value={"hello": MagicMock()})
|
||||
sys.modules["dashboard"] = fake_dashboard
|
||||
else:
|
||||
fake_dashboard = existing
|
||||
|
||||
admin = importlib.import_module("admin")
|
||||
admin.app.config["TESTING"] = True
|
||||
admin.require_auth = lambda: None
|
||||
return admin, fake_dashboard
|
||||
|
||||
|
||||
class TestAddRouteDoesNotRepack(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Reset Mock-Return auf den Default vor jedem Test
|
||||
admin, fake = make_admin_app()
|
||||
self._default_cfg = {
|
||||
"version": 2,
|
||||
"refresh_interval_s": 180,
|
||||
"layout": {"grid": {"cols": 4, "rows": 4}, "items": [
|
||||
{"id": "c1", "plugin": "clock", "x": 0, "y": 0, "w": 2, "h": 2},
|
||||
{"id": "w1", "plugin": "weather", "x": 2, "y": 0, "w": 2, "h": 2},
|
||||
{"id": "st1","plugin": "system", "x": 0, "y": 2, "w": 2, "h": 2},
|
||||
{"id": "sp1","plugin": "spotify", "x": 2, "y": 2, "w": 2, "h": 1},
|
||||
{"id": "sv1","plugin": "strava", "x": 2, "y": 3, "w": 2, "h": 1},
|
||||
]},
|
||||
"plugin_configs": {},
|
||||
}
|
||||
fake.load_config.return_value = self._default_cfg
|
||||
fake.save_config.reset_mock()
|
||||
self.admin, self.fake = admin, fake
|
||||
self.client = admin.app.test_client()
|
||||
|
||||
def test_add_1x1_into_gap_does_not_move_existing(self):
|
||||
"""Bestehende config mit Lücke bei (2,3),(3,3). Add hello (1x1) soll
|
||||
auf (2,3) gehen (erste scan-line freie Zelle) — alle anderen UNVERÄNDERT."""
|
||||
self.fake.load_config.return_value = {
|
||||
"version": 2,
|
||||
"refresh_interval_s": 180,
|
||||
"layout": {"grid": {"cols": 4, "rows": 4}, "items": [
|
||||
{"id": "c1", "plugin": "clock", "x": 0, "y": 0, "w": 2, "h": 2},
|
||||
{"id": "w1", "plugin": "weather", "x": 2, "y": 0, "w": 2, "h": 2},
|
||||
{"id": "st1","plugin": "system", "x": 0, "y": 2, "w": 2, "h": 2},
|
||||
{"id": "sp1","plugin": "spotify", "x": 2, "y": 2, "w": 1, "h": 1},
|
||||
{"id": "sv1","plugin": "strava", "x": 3, "y": 2, "w": 1, "h": 1},
|
||||
]},
|
||||
"plugin_configs": {},
|
||||
}
|
||||
|
||||
r = self.client.post("/api/layout/add", data={"plugin": "hello"})
|
||||
self.assertEqual(r.status_code, 200, r.get_data(as_text=True))
|
||||
j = r.get_json()
|
||||
self.assertTrue(j["ok"], j)
|
||||
items = j["items"]
|
||||
by_id = {it["id"]: it for it in items}
|
||||
# Existierende UNVERÄNDERT
|
||||
self.assertEqual((by_id["c1"]["x"], by_id["c1"]["y"]), (0, 0), "clock moved!")
|
||||
self.assertEqual((by_id["w1"]["x"], by_id["w1"]["y"]), (2, 0), "weather moved!")
|
||||
self.assertEqual((by_id["st1"]["x"], by_id["st1"]["y"]), (0, 2), "system moved!")
|
||||
self.assertEqual((by_id["sp1"]["x"], by_id["sp1"]["y"]), (2, 2), "spotify moved!")
|
||||
self.assertEqual((by_id["sv1"]["x"], by_id["sv1"]["y"]), (3, 2), "strava moved!")
|
||||
# Neues hello auf (2,3) — erste scan-line freie Zelle
|
||||
new_items = [it for it in items if it["plugin"] == "hello" and it["id"] not in ("c1","w1","st1","sp1","sv1")]
|
||||
self.assertEqual(len(new_items), 1, f"expected exactly 1 new hello, got {new_items}")
|
||||
new = new_items[0]
|
||||
self.assertEqual((new["x"], new["y"]), (2, 3),
|
||||
f"expected new hello at (2,3), got ({new['x']},{new['y']}); full: {items}")
|
||||
|
||||
def test_add_to_full_grid_returns_409(self):
|
||||
"""Wenn kein Platz: 409, keine bestehenden Items verändert."""
|
||||
self.fake.load_config.return_value["layout"]["items"] = [
|
||||
{"id": f"f{i}", "plugin": "x", "x": (i % 4), "y": (i // 4),
|
||||
"w": 1, "h": 1} for i in range(16)
|
||||
]
|
||||
before = [(it["id"], it["x"], it["y"]) for it in
|
||||
self.fake.load_config.return_value["layout"]["items"]]
|
||||
r = self.client.post("/api/layout/add", data={"plugin": "hello"})
|
||||
self.assertEqual(r.status_code, 409, r.get_data(as_text=True))
|
||||
j = r.get_json()
|
||||
self.assertFalse(j["ok"])
|
||||
self.assertIn("Auto-Pack", j.get("error", ""))
|
||||
# In-memory config unverändert
|
||||
after = [(it["id"], it["x"], it["y"]) for it in
|
||||
self.fake.load_config.return_value["layout"]["items"]]
|
||||
self.assertEqual(before, after, "full-grid add still mutated state")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
@@ -0,0 +1,58 @@
|
||||
// BUG-06 Test: Auto-Pack Undo via Snapshot
|
||||
//
|
||||
// Erwartung:
|
||||
// 1) packAll() speichert layoutItems als JSON vor dem Pack
|
||||
// 2) Toast wird mit action={label:"Rückgängig", onClick} aufgerufen (statt nur Text)
|
||||
// 3) onClick sendet POST /api/layout mit den snapshot-Items
|
||||
// 4) Modal-Text erwähnt "10 Sekunden lang rückgängig"
|
||||
// 5) CSS: .toast-action Klasse vorhanden
|
||||
// 6) toast()-Funktion unterstützt action-Parameter (4-Args-Signatur)
|
||||
|
||||
const fs = require('fs');
|
||||
const html = fs.readFileSync('templates/index.html', 'utf8');
|
||||
const js = html.match(/<script>\s*\n([\s\S]*?)<\/script>/)[1];
|
||||
|
||||
function check(name, fn) {
|
||||
const r = fn();
|
||||
console.log((r ? '✓' : '✗') + ' ' + name);
|
||||
if (!r) process.exitCode = 1;
|
||||
}
|
||||
|
||||
// 1) Snapshot wird erstellt (JSON.stringify vor pack-Call)
|
||||
check('packAll speichert Snapshot (JSON.stringify(layoutItems))', () =>
|
||||
/async function packAll[\s\S]*?JSON\.stringify\(layoutItems\)/.test(js));
|
||||
|
||||
// 2) Toast mit action-Parameter
|
||||
check('Toast wird mit action-Parameter aufgerufen', () =>
|
||||
/toast\([^,]+,\s*['"]success['"],\s*\d+,\s*\{[\s\S]*?label:\s*['"]Rückgängig['"]/.test(js));
|
||||
|
||||
// 3) Undo-Click ruft POST /api/layout mit snapshot
|
||||
check('Undo ruft POST /api/layout mit snapshot-Items', () =>
|
||||
/fetch\(['"]\/api\/layout['"][\s\S]*?JSON\.parse\(snapshot\)/.test(js));
|
||||
|
||||
// 4) Modal-Body erwähnt Undo-Möglichkeit
|
||||
check('Modal-Body erwähnt "rückgängig"', () =>
|
||||
/rückgängig machen/i.test(js));
|
||||
|
||||
// 5) CSS: .toast-action Klasse
|
||||
check('.toast-action CSS-Klasse vorhanden', () => {
|
||||
const css = html.match(/<style>([\s\S]*?)<\/style>/g).join('\n');
|
||||
return /\.toast-action\s*\{/.test(css);
|
||||
});
|
||||
|
||||
// 6) toast() hat 4-Args Signatur
|
||||
check('toast() unterstützt 4-Args (action-Parameter)', () =>
|
||||
/function toast\(\s*text[^)]*\)\s*\{/.test(js) && /action\s*=/.test(js));
|
||||
|
||||
// 7) Undo-Button ruft bei Click onClick und dismissed Toast
|
||||
check('Action-Button: Click ruft onClick + dismiss', () =>
|
||||
/\.toast-action['"]\)\.addEventListener\(['"]click['"][\s\S]*?action\.onClick\(\)/.test(js));
|
||||
|
||||
// 8) Pointer-events:auto auf Action-Button (sonst klickt Toast-Click-Handler)
|
||||
check('.toast-action hat pointer-events:auto', () => {
|
||||
const css = html.match(/<style>([\s\S]*?)<\/style>/g).join('\n');
|
||||
return /\.toast-action\s*\{[^}]*pointer-events:\s*auto/.test(css);
|
||||
});
|
||||
|
||||
console.log('\n========');
|
||||
process.exit(process.exitCode || 0);
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Tests für layout.first_fit (BUG-04).
|
||||
|
||||
Behauptung: Add-Item darf bestehende Items NICHT verschieben.
|
||||
- Leeres Layout → Add 2×2 hello → Position (0,0), kein Pack
|
||||
- Layout mit Lücke → Add 1×1 hello → landet in erster Lücke, andere bleiben
|
||||
- Layout voll → Add → None, kein anderes Item verändert
|
||||
"""
|
||||
import os, sys
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import unittest
|
||||
from layout import Item, first_fit, GRID_COLS, GRID_ROWS
|
||||
|
||||
|
||||
class TestFirstFit(unittest.TestCase):
|
||||
def test_first_fit_empty_grid(self):
|
||||
"""Auf leerem Grid wird Item bei (0,0) platziert — kein Pack nötig."""
|
||||
new_item = Item("new1", "hello", 0, 0, 1, 1)
|
||||
placed = first_fit(new_item, [])
|
||||
self.assertIsNotNone(placed)
|
||||
self.assertEqual((placed.x, placed.y), (0, 0))
|
||||
|
||||
def test_first_fit_does_not_move_existing(self):
|
||||
"""Items mit Lücke: hello wird in Lücke gesetzt, andere bleiben."""
|
||||
existing = [
|
||||
Item("a", "clock", 0, 0, 2, 2),
|
||||
Item("b", "weather", 2, 0, 2, 2),
|
||||
Item("c", "system", 0, 2, 2, 2),
|
||||
Item("d", "spotify", 2, 2, 2, 1),
|
||||
]
|
||||
before = {(it.id, it.x, it.y) for it in existing}
|
||||
new_item = Item("new", "hello", 0, 0, 1, 1)
|
||||
placed = first_fit(new_item, existing)
|
||||
self.assertIsNotNone(placed)
|
||||
after = {(it.id, it.x, it.y) for it in existing}
|
||||
self.assertEqual(before, after, f"EXISTING MOVED! before={before} after={after}")
|
||||
self.assertEqual((placed.x, placed.y), (2, 3))
|
||||
|
||||
def test_first_fit_full_grid_returns_none(self):
|
||||
"""Wenn kein Platz: None zurück, keine Mutation."""
|
||||
existing = [
|
||||
Item("a", "x", 0, 0, 2, 2),
|
||||
Item("b", "y", 2, 0, 2, 2),
|
||||
Item("c", "z", 0, 2, 2, 2),
|
||||
Item("d", "w", 2, 2, 2, 2),
|
||||
]
|
||||
before = {(it.id, it.x, it.y, it.w, it.h) for it in existing}
|
||||
new_item = Item("new", "hello", 0, 0, 1, 1)
|
||||
placed = first_fit(new_item, existing)
|
||||
self.assertIsNone(placed)
|
||||
after = {(it.id, it.x, it.y, it.w, it.h) for it in existing}
|
||||
self.assertEqual(before, after)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
@@ -0,0 +1,74 @@
|
||||
// BUG-05 Test: Speichern-Button entfernt, Save-Indicator hinzugefügt.
|
||||
//
|
||||
// Erwartung:
|
||||
// 1) KEIN <button onclick="saveLayout()"> im Layout-Toolbar mehr
|
||||
// 2) NEUER #saveIndicator im Toolbar mit data-state="idle"
|
||||
// 3) setSaveState() existiert und akzeptiert (state, label?)
|
||||
// 4) setSaveState setzt el.dataset.state und label-Text
|
||||
// 5) debouncedSave setzt Status auf "pending" BEVOR es speichert
|
||||
// 6) saveLayout setzt Status auf "saving" am Anfang, "idle" am Ende (success)
|
||||
// 7) saveLayout setzt Status auf "error" bei j.ok=false
|
||||
// 8) CSS: .save-indicator[data-state="..."] für idle/pending/saving/error
|
||||
|
||||
const fs = require('fs');
|
||||
const html = fs.readFileSync('templates/index.html', 'utf8');
|
||||
const js = html.match(/<script>\s*\n([\s\S]*?)<\/script>/)[1];
|
||||
const css = html.match(/<style>([\s\S]*?)<\/style>/g).join('\n');
|
||||
|
||||
function check(name, fn) {
|
||||
const r = fn();
|
||||
console.log((r ? '✓' : '✗') + ' ' + name);
|
||||
if (!r) process.exitCode = 1;
|
||||
}
|
||||
|
||||
// 1) Kein Speichern-Button mehr
|
||||
check('Speichern-Button entfernt', () => {
|
||||
// Suchen wir den toolbar-Bereich
|
||||
const toolbarMatch = html.match(/<div class="layout-toolbar">([\s\S]*?)<\/div>\s*<div id="gridPreview"/);
|
||||
if (!toolbarMatch) return false; // Struktur geändert?
|
||||
return !/onclick="saveLayout\(\)"/.test(toolbarMatch[1]);
|
||||
});
|
||||
|
||||
// 2) Save-Indicator
|
||||
check('Save-Indicator #saveIndicator vorhanden', () =>
|
||||
/id="saveIndicator"/.test(html) && /data-state="idle"/.test(html));
|
||||
|
||||
// 3) setSaveState existiert
|
||||
check('setSaveState() Funktion definiert', () =>
|
||||
/function setSaveState\(/.test(js));
|
||||
|
||||
// 4) setSaveState setzt state und label
|
||||
check('setSaveState setzt dataset.state', () =>
|
||||
/setSaveState[\s\S]{0,300}dataset\.state/.test(js));
|
||||
|
||||
// 5) debouncedSave setzt pending
|
||||
check('debouncedSave setzt pending-Status', () =>
|
||||
/function debouncedSave\(\)\s*\{[^}]*setSaveState\(['"]pending['"]/.test(js));
|
||||
|
||||
// 6) saveLayout setzt saving→idle
|
||||
check('saveLayout setzt saving am Anfang', () =>
|
||||
/async function saveLayout\(\)[\s\S]{0,300}setSaveState\(['"]saving['"]/.test(js));
|
||||
|
||||
check('saveLayout setzt idle bei Erfolg', () =>
|
||||
/saveLayout[\s\S]{0,800}setSaveState\(['"]idle['"]/.test(js));
|
||||
|
||||
// 7) saveLayout setzt error bei Fehler
|
||||
check('saveLayout setzt error bei j.ok=false', () =>
|
||||
/!j\.ok[\s\S]{0,300}setSaveState\(['"]error['"]/.test(js));
|
||||
|
||||
// 8) CSS für alle States
|
||||
const cssStates = ['idle', 'pending', 'saving', 'error'].map(s =>
|
||||
new RegExp(`\\.save-indicator\\[data-state="${s}"\\]`));
|
||||
check('CSS für alle 4 Indicator-States', () =>
|
||||
cssStates.every(re => re.test(css)));
|
||||
|
||||
// 9) delete success-toast raus (Indicator reicht)
|
||||
check('Kein success-toast in saveLayout mehr', () =>
|
||||
!/function saveLayout[\s\S]{0,1000}toast\([^)]*['"]success['"][^)]*['"]Layout gespeichert['"]/.test(js));
|
||||
|
||||
// 10) margin-left:auto für rechtsbündige Position
|
||||
check('Save-Indicator rechtsbündig (margin-left:auto)', () =>
|
||||
/\.save-indicator\s*\{[^}]*margin-left:\s*auto/.test(css));
|
||||
|
||||
console.log('\n========');
|
||||
process.exit(process.exitCode || 0);
|
||||
@@ -0,0 +1,56 @@
|
||||
// Test für BUG-03: Span-Geometrie im Initial-Render.
|
||||
//
|
||||
// Erwartung nach Fix:
|
||||
// 1) renderGrid setzt grid-column/grid-row am Item direkt (per JS).
|
||||
// 2) Item wird in den Container gehängt (cont.appendChild), nicht in originCell.
|
||||
// 3) Origin-Cell bekommt nur die "occupied"-Klasse.
|
||||
// 4) 2x2-Item hat style.gridColumn === '<x+1> / span 2' und gridRow === '<y+1> / span 2'.
|
||||
|
||||
const fs = require('fs');
|
||||
const html = fs.readFileSync('templates/index.html', 'utf8');
|
||||
const js = html.match(/<script>\s*\n([\s\S]*?)<\/script>/)[1];
|
||||
|
||||
function check(name, fn) {
|
||||
const r = fn();
|
||||
console.log((r ? '✓' : '✗') + ' ' + name);
|
||||
if (!r) process.exitCode = 1;
|
||||
}
|
||||
|
||||
// 1) renderGrid setzt style.gridColumn/gridRow am Item
|
||||
check('gridColumn wird per JS gesetzt', () =>
|
||||
/\.style\.gridColumn\s*=/.test(js));
|
||||
|
||||
check('gridRow wird per JS gesetzt', () =>
|
||||
/\.style\.gridRow\s*=/.test(js));
|
||||
|
||||
// 2) Item wird in cont.appendChild gehängt, NICHT in originCell.appendChild
|
||||
check('Item wird in Container (cont) gehängt', () =>
|
||||
/cont\.appendChild\(div\)/.test(js));
|
||||
|
||||
check('Item wird NICHT mehr in Origin-Cell gehängt', () =>
|
||||
!/originCell\.appendChild\(div\)/.test(js));
|
||||
|
||||
// 3) Origin-Cell bekommt nur occupied-Klasse
|
||||
check('Origin-Cell bekommt "occupied" Klasse', () =>
|
||||
/originCell\.classList\.add\(['"]occupied['"]\)/.test(js));
|
||||
|
||||
// 4) Format: `${it.x + 1} / span ${it.w}` (CSS-Grid-Notation)
|
||||
check('gridColumn Format: <x+1> / span <w>', () =>
|
||||
/it\.x\s*\+\s*1[^`]*\$\{it\.w\}/.test(js) || /\$\{it\.x\s*\+\s*1\}[^`]*span[^`]*\$\{it\.w\}/.test(js));
|
||||
|
||||
// 5) Kein 100%/100% Trick auf Items (das war der Bug, der Span verhindert hat)
|
||||
check('Keine "width: 100%" mehr im Item-CSS-Block', () => {
|
||||
const css = html.match(/<style>([\s\S]*?)<\/style>/g).join('\n');
|
||||
// .grid-item soll nicht width:100% haben
|
||||
const itemCssMatch = /\.grid-item\s*\{([^}]*)\}/.exec(css);
|
||||
if (!itemCssMatch) return true; // falls keine Regel
|
||||
const body = itemCssMatch[1];
|
||||
return !/width:\s*100%/.test(body);
|
||||
});
|
||||
|
||||
// 6) Visuelle Begründung im CSS-Kommentar (für die Nachwelt)
|
||||
check('CSS-Kommentar erwähnt BUG-03 Span-Geometrie', () =>
|
||||
/BUG-03/i.test(html));
|
||||
|
||||
console.log('\n========');
|
||||
process.exit(process.exitCode || 0);
|
||||
@@ -0,0 +1,293 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Netatmo OAuth Setup - ein simpler Web-Endpoint den der User aufruft.
|
||||
|
||||
Ablauf (Standard OAuth2 Authorization Code Flow):
|
||||
1. User öffnet die unten gedruckte URL im Browser.
|
||||
2. Netatmo zeigt Login → User loggt sich ein → User klickt "Authorize".
|
||||
3. Netatmo leitet zurück auf den hier laufenden Callback-Server.
|
||||
4. Server tauscht den Code gegen access+refresh Token (API-Call,
|
||||
Content-Type: application/x-www-form-urlencoded, exakt nach Doku).
|
||||
5. refresh_token wird in config.json gespeichert.
|
||||
6. Eine Bestätigungsseite wird im Browser angezeigt.
|
||||
|
||||
Voraussetzungen (einmalig, vom User gemacht):
|
||||
- Redirect-URI http://<pi-ip>:8765/callback muss in der Netatmo-App
|
||||
auf https://dev.netatmo.com/apps/ registriert sein.
|
||||
|
||||
Verwendung:
|
||||
python3 tools/netatmo_auth.py
|
||||
-> gibt die URL aus, die der User im Browser öffnen soll.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import argparse
|
||||
import http.server
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
import io
|
||||
from pathlib import Path
|
||||
|
||||
# Exakt nach Netatmo-Doku: https://dev.netatmo.com/apidocumentation/oauth
|
||||
TOKEN_URL = "https://api.netatmo.com/oauth2/token"
|
||||
AUTHORIZE_URL = "https://api.netatmo.com/oauth2/authorize"
|
||||
DEFAULT_SCOPE = "read_station"
|
||||
CONFIG_PATH = Path(__file__).resolve().parent.parent / "config.json"
|
||||
|
||||
|
||||
def detect_lan_ip() -> str:
|
||||
try:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
s.connect(("8.8.8.8", 80))
|
||||
return s.getsockname()[0]
|
||||
finally:
|
||||
s.close()
|
||||
except Exception:
|
||||
return "127.0.0.1"
|
||||
|
||||
|
||||
def free_port(port: int = 8765) -> int:
|
||||
"""Findet einen freien Port. Versucht erst den gewünschten, dann
|
||||
die nächsten 20 Ports. Überspringt Ports die im LISTEN-State sind.
|
||||
|
||||
Hinweis: TIME_WAIT-Ports können kurzfristig ein bind() blockieren
|
||||
auch wenn sie in `ss` nicht als LISTEN auftauchen — das ist ok,
|
||||
dann gehen wir einfach zum nächsten Port.
|
||||
"""
|
||||
import subprocess
|
||||
out = subprocess.run(["ss", "-lnt"], capture_output=True, text=True).stdout
|
||||
listening = set()
|
||||
for line in out.splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) >= 4 and parts[0] == "LISTEN":
|
||||
local = parts[3]
|
||||
if ":" in local:
|
||||
try:
|
||||
listening.add(int(local.rsplit(":", 1)[1]))
|
||||
except ValueError:
|
||||
pass
|
||||
for p in [port] + list(range(port + 1, port + 50)):
|
||||
if p in listening:
|
||||
continue
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
try:
|
||||
s.bind(("0.0.0.0", p))
|
||||
return p
|
||||
except OSError:
|
||||
continue
|
||||
raise RuntimeError(f"Kein freier Port zwischen {port} und {port+50} gefunden")
|
||||
|
||||
|
||||
def load_existing_config() -> dict:
|
||||
if not CONFIG_PATH.exists():
|
||||
return {}
|
||||
try:
|
||||
return json.loads(CONFIG_PATH.read_text())
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def save_refresh_token(refresh_token: str, client_id: str, client_secret: str) -> dict:
|
||||
cfg = load_existing_config()
|
||||
cfg.setdefault("plugin_configs", {})
|
||||
netatmo_cfg = cfg["plugin_configs"].setdefault("netatmo", {})
|
||||
netatmo_cfg["client_id"] = client_id
|
||||
netatmo_cfg["client_secret"] = client_secret
|
||||
netatmo_cfg["refresh_token"] = refresh_token
|
||||
CONFIG_PATH.write_text(json.dumps(cfg, indent=2))
|
||||
return cfg
|
||||
|
||||
|
||||
def exchange_code_for_tokens(code: str, client_id: str, client_secret: str,
|
||||
redirect_uri: str) -> dict:
|
||||
"""Authorization Code → Access+Refresh Token via API-Call.
|
||||
|
||||
Exakt nach Netatmo-Doku: POST /oauth2/token mit
|
||||
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
|
||||
|
||||
WICHTIG: redirect_uri muss EXAKT der Wert sein der beim /authorize-Aufruf
|
||||
benutzt wurde. Wenn der eine 10.11.3.144 war, muss der hier auch
|
||||
10.11.3.144 sein, sonst 'invalid_grant'.
|
||||
"""
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "authorization_code",
|
||||
"client_id": client_id,
|
||||
"client_secret": client_secret,
|
||||
"code": code,
|
||||
"redirect_uri": redirect_uri,
|
||||
"scope": DEFAULT_SCOPE,
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
TOKEN_URL, data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
||||
"Accept": "application/json"},
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=15) as r:
|
||||
return json.loads(r.read())
|
||||
except urllib.error.HTTPError as e:
|
||||
body = e.read().decode("utf-8", errors="ignore")
|
||||
raise SystemExit(f"Token-Exchange fehlgeschlagen: HTTP {e.code}\n{body}")
|
||||
|
||||
|
||||
def run_callback_server(port: int, expected_state: str, client_id: str,
|
||||
client_secret: str, redirect_uri: str) -> None:
|
||||
"""Startet einen HTTP-Server, der den Callback empfängt und den Token speichert.
|
||||
|
||||
Läuft bis ein Token gespeichert wurde (max 5 Minuten)."""
|
||||
saved = {}
|
||||
|
||||
def make_response(status: int, body: bytes, content_type: str = "text/html; charset=utf-8"):
|
||||
return (status, [("Content-Type", content_type)], body)
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
def log_message(self, format, *args):
|
||||
# Logge jeden Request damit wir sehen was passiert
|
||||
print(f" >> {self.command} {self.path} from {self.client_address[0]}")
|
||||
|
||||
def do_GET(self):
|
||||
parsed = urllib.parse.urlparse(self.path)
|
||||
qs = urllib.parse.parse_qs(parsed.query)
|
||||
print(f" qs: {dict(qs)}")
|
||||
if "error" in qs:
|
||||
err = qs.get("error", ["unknown"])[0]
|
||||
status, hdrs, body = make_response(400, (
|
||||
f"<h1>Fehler bei Netatmo-Authentifizierung</h1>"
|
||||
f"<p>Grund: {err}</p>"
|
||||
f"<p>Du kannst dieses Fenster schliessen und es nochmal "
|
||||
f"probieren.</p>").encode())
|
||||
self.send_response(status)
|
||||
for k, v in hdrs: self.send_header(k, v)
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
saved["error"] = err
|
||||
return
|
||||
if "code" not in qs:
|
||||
# Health-Check oder 404
|
||||
self.send_response(204)
|
||||
self.end_headers()
|
||||
return
|
||||
if qs.get("state", [None])[0] != expected_state:
|
||||
status, hdrs, body = make_response(400, b"<h1>State mismatch (CSRF-Schutz)</h1>")
|
||||
self.send_response(status)
|
||||
for k, v in hdrs: self.send_header(k, v)
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
saved["error"] = "state_mismatch"
|
||||
return
|
||||
code = qs["code"][0]
|
||||
print(f" code erhalten, tausche gegen Token...")
|
||||
try:
|
||||
tok = exchange_code_for_tokens(code, client_id, client_secret, redirect_uri)
|
||||
refresh_token = tok["refresh_token"]
|
||||
save_refresh_token(refresh_token, client_id, client_secret)
|
||||
saved["refresh_token"] = refresh_token
|
||||
print(f" refresh_token gespeichert!")
|
||||
html = (
|
||||
"<h1 style='color:green'>OK!</h1>"
|
||||
"<p>Refresh-Token wurde in config.json gespeichert.</p>"
|
||||
"<p>Das Netatmo-Plugin ist jetzt aktiv. Du kannst dieses "
|
||||
"Fenster schliessen.</p>"
|
||||
f"<p style='font-family:monospace;font-size:0.8em;color:#666'>"
|
||||
f"refresh_token: {refresh_token[:24]}...</p>"
|
||||
).encode()
|
||||
status, hdrs, body = make_response(200, html)
|
||||
except SystemExit as e:
|
||||
saved["error"] = str(e)
|
||||
status, hdrs, body = make_response(500, (
|
||||
f"<h1 style='color:red'>Token-Exchange fehlgeschlagen</h1>"
|
||||
f"<pre>{e}</pre>").encode())
|
||||
self.send_response(status)
|
||||
for k, v in hdrs: self.send_header(k, v)
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
server = http.server.HTTPServer(("0.0.0.0", port), Handler)
|
||||
server.timeout = 300 # 5 min
|
||||
print(f"Server listening on 0.0.0.0:{port}, warte auf Callback...",
|
||||
flush=True)
|
||||
# Läuft bis refresh_token gespeichert oder 5 Minuten Timeout
|
||||
end_time = time.time() + 300
|
||||
while time.time() < end_time and "refresh_token" not in saved:
|
||||
server.handle_request()
|
||||
return saved
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Netatmo OAuth Setup")
|
||||
parser.add_argument("--cid", help="Client-ID")
|
||||
parser.add_argument("--csec", help="Client-Secret")
|
||||
parser.add_argument("--redirect", default=None,
|
||||
help="Redirect-URI (default: http://<lan-ip>:8765/callback)")
|
||||
parser.add_argument("--port", type=int, default=8765)
|
||||
parser.add_argument("--scope", default=DEFAULT_SCOPE)
|
||||
args = parser.parse_args()
|
||||
|
||||
existing = load_existing_config().get("plugin_configs", {}).get("netatmo", {})
|
||||
client_id = args.cid or existing.get("client_id") or input("Netatmo Client-ID: ").strip()
|
||||
client_secret = args.csec or existing.get("client_secret") or input("Netatmo Client-Secret: ").strip()
|
||||
if not (client_id and client_secret):
|
||||
raise SystemExit("Client-ID und Client-Secret erforderlich.")
|
||||
|
||||
port = free_port(args.port)
|
||||
lan_ip = detect_lan_ip()
|
||||
if args.redirect:
|
||||
redirect = args.redirect
|
||||
else:
|
||||
redirect = f"http://{lan_ip}:{port}/callback"
|
||||
|
||||
import secrets
|
||||
state = secrets.token_urlsafe(16)
|
||||
|
||||
auth_url = (
|
||||
f"{AUTHORIZE_URL}"
|
||||
f"?client_id={urllib.parse.quote(client_id)}"
|
||||
f"&redirect_uri={urllib.parse.quote(redirect)}"
|
||||
f"&scope={urllib.parse.quote(args.scope)}"
|
||||
f"&state={urllib.parse.quote(state)}"
|
||||
f"&response_type=code"
|
||||
)
|
||||
|
||||
print()
|
||||
print("=" * 70)
|
||||
print(" NETATMO OAUTH SETUP")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print("Voraussetzung: Die folgende Redirect-URI muss EXAKT in deiner")
|
||||
print("Netatmo-App auf https://dev.netatmo.com/apps/ registriert sein:")
|
||||
print()
|
||||
print(f" {redirect}")
|
||||
print()
|
||||
print("=" * 70)
|
||||
print(" BITTE IM BROWSER OEFFNEN:")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print(auth_url)
|
||||
print()
|
||||
print("=" * 70)
|
||||
print(" Nach dem Authorize-Klick landest du auf der Login-Seite,")
|
||||
print(" loggst dich ein, klickst 'Authorize', und wirst zurueckgeleitet.")
|
||||
print(" Der Server holt dann den Token und speichert ihn in config.json.")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
result = run_callback_server(port, state, client_id, client_secret, redirect)
|
||||
if "refresh_token" in result:
|
||||
print()
|
||||
print("ERFOLG! Refresh-Token gespeichert in", CONFIG_PATH)
|
||||
print()
|
||||
sys.exit(0)
|
||||
elif "error" in result:
|
||||
print()
|
||||
print("FEHLER:", result["error"], file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user