33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from dashboard_data.card_utils import average, safe_number, weather_condition
|
|
|
|
|
|
def build_farm_weather_card(sensor_id: str, context: dict | None = None, ai_bundle: dict | None = None) -> dict:
|
|
forecasts = (context or {}).get("forecasts", [])
|
|
if not forecasts:
|
|
return {
|
|
"condition": "نامشخص",
|
|
"temperature": 0,
|
|
"unit": "°C",
|
|
"humidity": 0,
|
|
"windSpeed": 0,
|
|
"windUnit": "km/h",
|
|
"chartData": {"labels": [], "series": [[]]},
|
|
}
|
|
|
|
current_forecast = forecasts[0]
|
|
labels = [str(forecast.forecast_date) for forecast in forecasts[:7]]
|
|
series = [[round(safe_number(forecast.temperature_mean, 0)) for forecast in forecasts[:7]]]
|
|
|
|
return {
|
|
"condition": weather_condition(current_forecast.weather_code),
|
|
"temperature": round(safe_number(current_forecast.temperature_mean, current_forecast.temperature_max)),
|
|
"unit": "°C",
|
|
"humidity": round(average([current_forecast.humidity_mean], default=0)),
|
|
"windSpeed": round(safe_number(current_forecast.wind_speed_max, 0)),
|
|
"windUnit": "km/h",
|
|
"chartData": {
|
|
"labels": labels,
|
|
"series": series,
|
|
},
|
|
}
|