Fix: Wetter-Alarm standortbezogen statt 5-Städte-Maximum — Abonnenten nach Standort clustern, lokale Tagesprognose, nur lokal warnen
This commit is contained in:
parent
cad34711b7
commit
8c2bc0c445
2 changed files with 82 additions and 87 deletions
|
|
@ -1,6 +1,6 @@
|
|||
"""
|
||||
BAN YARO — Wetter via Open-Meteo
|
||||
- get_weather_summary(): Push-Job, prüft 5 deutsche Städte
|
||||
- get_day_alert(): standortbezogener Wetter-Alarm-Push (Hitze/Gewitter)
|
||||
- get_weather_for_location(): API-Endpoint, beliebiger Standort mit TTL-Cache
|
||||
"""
|
||||
|
||||
|
|
@ -158,67 +158,37 @@ async def get_weather_for_location(lat: float, lon: float) -> dict:
|
|||
_location_cache[key] = (now, data)
|
||||
return data
|
||||
|
||||
# Wichtige deutsche Städte als Stichprobe
|
||||
CITIES = [
|
||||
{"name": "Berlin", "lat": 52.52, "lon": 13.41},
|
||||
{"name": "München", "lat": 48.14, "lon": 11.58},
|
||||
{"name": "Hamburg", "lat": 53.55, "lon": 10.00},
|
||||
{"name": "Frankfurt", "lat": 50.11, "lon": 8.68},
|
||||
{"name": "Köln", "lat": 50.94, "lon": 6.96},
|
||||
]
|
||||
|
||||
# WMO-Wettercodes 95–99 = Gewitter
|
||||
THUNDERSTORM_CODES = {95, 96, 99}
|
||||
|
||||
|
||||
async def get_weather_summary() -> dict:
|
||||
async def get_day_alert(lat: float, lon: float) -> dict:
|
||||
"""
|
||||
Holt Tagesprognose für mehrere deutsche Städte von Open-Meteo.
|
||||
Gibt zurück: {"max_temp_c": float, "thunderstorm": bool}
|
||||
Tagesprognose (heute) für EINEN Standort — Grundlage für den
|
||||
standortbezogenen Wetter-Alarm-Push (Hitze / Gewitter).
|
||||
Gibt zurück: {"max_temp_c": float|None, "thunderstorm": bool}
|
||||
"""
|
||||
max_temp = -999.0
|
||||
thunderstorm = False
|
||||
|
||||
url = (
|
||||
"https://api.open-meteo.com/v1/forecast"
|
||||
f"?latitude={lat}&longitude={lon}"
|
||||
"&daily=temperature_2m_max,precipitation_probability_max,weathercode"
|
||||
"&timezone=Europe%2FBerlin&forecast_days=1"
|
||||
)
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
for city in CITIES:
|
||||
url = (
|
||||
"https://api.open-meteo.com/v1/forecast"
|
||||
f"?latitude={city['lat']}&longitude={city['lon']}"
|
||||
"&daily=temperature_2m_max,precipitation_probability_max,weathercode"
|
||||
"&timezone=Europe%2FBerlin&forecast_days=1"
|
||||
)
|
||||
try:
|
||||
resp = await client.get(url)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
resp = await client.get(url)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
daily = data.get("daily", {})
|
||||
temps = daily.get("temperature_2m_max", [None])
|
||||
precip_probs = daily.get("precipitation_probability_max", [None])
|
||||
weathercodes = daily.get("weathercode", [None])
|
||||
daily = data.get("daily", {})
|
||||
temp = (daily.get("temperature_2m_max") or [None])[0]
|
||||
precip_prob = (daily.get("precipitation_probability_max") or [None])[0]
|
||||
weathercode = (daily.get("weathercode") or [None])[0]
|
||||
|
||||
temp = temps[0] if temps else None
|
||||
precip_prob = precip_probs[0] if precip_probs else None
|
||||
weathercode = weathercodes[0] if weathercodes else None
|
||||
|
||||
if temp is not None and temp > max_temp:
|
||||
max_temp = temp
|
||||
|
||||
if (
|
||||
precip_prob is not None and precip_prob > 50
|
||||
and weathercode is not None and int(weathercode) in THUNDERSTORM_CODES
|
||||
):
|
||||
thunderstorm = True
|
||||
logger.info(f"Gewitter erkannt: {city['name']} (Code {weathercode}, {precip_prob}% Niederschlag)")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Wetter-Abruf für {city['name']} fehlgeschlagen: {e}")
|
||||
|
||||
if max_temp == -999.0:
|
||||
max_temp = 0.0
|
||||
|
||||
logger.info(f"Wetter-Zusammenfassung: max_temp={max_temp}°C, thunderstorm={thunderstorm}")
|
||||
return {"max_temp_c": max_temp, "thunderstorm": thunderstorm}
|
||||
thunderstorm = (
|
||||
precip_prob is not None and precip_prob > 50
|
||||
and weathercode is not None and int(weathercode) in THUNDERSTORM_CODES
|
||||
)
|
||||
return {"max_temp_c": temp, "thunderstorm": thunderstorm}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue