from datetime import date def load_farm_context(sensor_id: str) -> dict | None: from irrigation.models import IrrigationMethod from location_data.satellite_snapshot import build_location_block_satellite_snapshots from farm_data.models import SensorData from farm_data.services import get_farm_plant_snapshots from weather.models import WeatherForecast try: sensor = SensorData.objects.select_related("center_location").prefetch_related("plant_assignments__plant").get( farm_uuid=sensor_id ) except SensorData.DoesNotExist: return None location = sensor.center_location satellite_snapshots = build_location_block_satellite_snapshots(location) forecasts = list( WeatherForecast.objects.filter(location=location, forecast_date__gte=date.today()).order_by("forecast_date")[:7] ) plants = get_farm_plant_snapshots(sensor) irrigation_methods = list(IrrigationMethod.objects.all()[:5]) return { "sensor": sensor, "location": location, "satellite_snapshots": satellite_snapshots, "forecasts": forecasts, "history": [], "plants": plants, "irrigation_methods": irrigation_methods, }