feat(signage): the weather all the way: a week, the next hours, feels like, humidity, wind, rain chance, sunrise / sunset, UV, in words; row, column, card, list of days or hourly layouts

services/weather.py asks Open-Meteo for 7 days, the next 24 hours and the details
(mph for Fahrenheit); older answers without them still read. The widget's layout,
the details it adds and the place's name; up to 7 days. Plan 6R.18, F-116.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 15:07:03 +02:00
co-authored by Claude Opus 5.5
parent 673113aff7
commit 84c2d5b2d8
10 changed files with 301 additions and 43 deletions
+29 -9
View File
@@ -6,7 +6,10 @@ editor from a town search). `weather_worker` (a signage module worker) asks Open
key) every 30 minutes while it has internet and keeps the last answer in memory. A forecast older
than 3 hours counts as none: the screens get `weather: null` and hide the widget.
current() → {"place", "temperature", "code", "is_day", "days": [{date, max, min, code}], "units", "at"} | None
current() → {"place", "temperature", "code", "is_day", "feels_like", "humidity", "wind", "wind_dir",
"precipitation", "days": [{date, max, min, code, rain, wind, uv, sunrise, sunset}] (7),
"hours": [{time, temp, code, rain, is_day}] (the next 24), "units", "wind_unit", "at"} | None
(6R.18: everything the detailed weather widget can show; older answers lack some keys)
search(name) → places for the editor's search (name, country, region, latitude, longitude)
"""
from __future__ import annotations
@@ -68,9 +71,12 @@ def _key(where: dict) -> tuple:
async def fetch(where: dict, client: httpx.AsyncClient | None = None) -> dict:
params = {"latitude": where["latitude"], "longitude": where["longitude"], "timezone": "auto", "forecast_days": 3,
"current": "temperature_2m,weather_code,is_day", "daily": "temperature_2m_max,temperature_2m_min,weather_code",
"temperature_unit": where["units"]}
imperial = where["units"] == "fahrenheit"
params = {"latitude": where["latitude"], "longitude": where["longitude"], "timezone": "auto", "forecast_days": 7,
"current": "temperature_2m,weather_code,is_day,apparent_temperature,relative_humidity_2m,wind_speed_10m,wind_direction_10m,precipitation",
"daily": "temperature_2m_max,temperature_2m_min,weather_code,precipitation_probability_max,wind_speed_10m_max,uv_index_max,sunrise,sunset",
"hourly": "temperature_2m,weather_code,precipitation_probability,is_day",
"temperature_unit": where["units"], "wind_speed_unit": "mph" if imperial else "kmh"}
own = client is None
client = client or httpx.AsyncClient(timeout=TIMEOUT_S)
try:
@@ -80,12 +86,26 @@ async def fetch(where: dict, client: httpx.AsyncClient | None = None) -> dict:
finally:
if own:
await client.aclose()
cur, daily = j.get("current") or {}, j.get("daily") or {}
days = [{"date": d, "max": mx, "min": mn, "code": c}
for d, mx, mn, c in zip(daily.get("time", []), daily.get("temperature_2m_max", []),
daily.get("temperature_2m_min", []), daily.get("weather_code", []))]
cur, daily, hourly = j.get("current") or {}, j.get("daily") or {}, j.get("hourly") or {}
col = lambda src, key, n: (src.get(key) or [None] * n)[:n] + [None] * max(0, n - len(src.get(key) or [])) # noqa: E731
n = len(daily.get("time", []))
hm = lambda v: v[11:16] if isinstance(v, str) and len(v) >= 16 else None # noqa: E731 "2026-11-17T07:12" → "07:12"
days = [{"date": d, "max": mx, "min": mn, "code": c, "rain": rp, "wind": wm, "uv": uv, "sunrise": hm(sr), "sunset": hm(ss)}
for d, mx, mn, c, rp, wm, uv, sr, ss in zip(daily.get("time", []), col(daily, "temperature_2m_max", n), col(daily, "temperature_2m_min", n),
col(daily, "weather_code", n), col(daily, "precipitation_probability_max", n),
col(daily, "wind_speed_10m_max", n), col(daily, "uv_index_max", n),
col(daily, "sunrise", n), col(daily, "sunset", n))]
# the next 24 hours from now (the place's own time: Open-Meteo answers in it)
times = hourly.get("time", [])
start = next((i for i, x in enumerate(times) if x >= (cur.get("time") or "")[:13] + ":00"), 0)
m = len(times)
hours = [{"time": hm(t), "temp": tp, "code": c, "rain": rp, "is_day": bool(dy if dy is not None else 1)}
for t, tp, c, rp, dy in list(zip(times, col(hourly, "temperature_2m", m), col(hourly, "weather_code", m),
col(hourly, "precipitation_probability", m), col(hourly, "is_day", m)))[start:start + 24]]
return {"place": where["name"], "temperature": cur.get("temperature_2m"), "code": cur.get("weather_code"),
"is_day": bool(cur.get("is_day", 1)), "days": days, "units": where["units"], "at": cur.get("time")}
"is_day": bool(cur.get("is_day", 1)), "feels_like": cur.get("apparent_temperature"), "humidity": cur.get("relative_humidity_2m"),
"wind": cur.get("wind_speed_10m"), "wind_dir": cur.get("wind_direction_10m"), "precipitation": cur.get("precipitation"),
"days": days, "hours": hours, "units": where["units"], "wind_unit": "mph" if imperial else "km/h", "at": cur.get("time")}
async def refresh(db, client: httpx.AsyncClient | None = None) -> bool: