72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
from dashboard_data.card_utils import compute_trend, latest_history_value, safe_number
|
|
|
|
|
|
def build_sensor_values_list(sensor_id: str, context: dict | None = None, ai_bundle: dict | None = None) -> dict:
|
|
context = context or {}
|
|
sensor = context.get("sensor")
|
|
history = context.get("history", [])
|
|
forecasts = context.get("forecasts", [])
|
|
if sensor is None:
|
|
return {"sensors": []}
|
|
|
|
current_weather = forecasts[0] if forecasts else None
|
|
|
|
sensors = [
|
|
{
|
|
"title": f"{round(safe_number(current_weather.temperature_mean if current_weather else None, 0))}°C",
|
|
"subtitle": "دمای هوا",
|
|
**compute_trend(
|
|
current_weather.temperature_mean if current_weather else 0,
|
|
latest_history_value(history, "soil_temperature", 0),
|
|
),
|
|
"unit": "°C",
|
|
},
|
|
{
|
|
"title": f"{round(safe_number(sensor.soil_temperature, 0))}°C",
|
|
"subtitle": "دمای خاک",
|
|
**compute_trend(sensor.soil_temperature, latest_history_value(history, "soil_temperature", 0)),
|
|
"unit": "°C",
|
|
},
|
|
{
|
|
"title": f"{round(safe_number(current_weather.humidity_mean if current_weather else None, 0))}%",
|
|
"subtitle": "رطوبت هوا",
|
|
**compute_trend(current_weather.humidity_mean if current_weather else 0, 0),
|
|
"unit": "%",
|
|
},
|
|
{
|
|
"title": f"{round(safe_number(sensor.soil_moisture, 0))}%",
|
|
"subtitle": "رطوبت خاک (۱۰ سانتیمتر)",
|
|
**compute_trend(sensor.soil_moisture, latest_history_value(history, "soil_moisture", 0)),
|
|
"unit": "%",
|
|
},
|
|
{
|
|
"title": f"{safe_number(sensor.soil_ph, 0):.1f}",
|
|
"subtitle": "pH خاک",
|
|
**compute_trend(sensor.soil_ph, latest_history_value(history, "soil_ph", 0)),
|
|
"unit": "pH",
|
|
},
|
|
{
|
|
"title": f"{safe_number(sensor.electrical_conductivity, 0):.1f}",
|
|
"subtitle": "هدایت الکتریکی (dS/m)",
|
|
**compute_trend(
|
|
sensor.electrical_conductivity,
|
|
latest_history_value(history, "electrical_conductivity", 0),
|
|
),
|
|
"unit": "dS/m",
|
|
},
|
|
{
|
|
"title": "850",
|
|
"subtitle": "شدت نور (لوکس)",
|
|
"trendNumber": 0,
|
|
"trend": "positive",
|
|
"unit": "lux",
|
|
},
|
|
{
|
|
"title": f"{round(safe_number(current_weather.wind_speed_max if current_weather else None, 0))}",
|
|
"subtitle": "سرعت باد (کیلومتر/ساعت)",
|
|
**compute_trend(current_weather.wind_speed_max if current_weather else 0, 0),
|
|
"unit": "km/h",
|
|
},
|
|
]
|
|
return {"sensors": sensors}
|