Files
Home_assistant/backend/app/api/routes/homelab.py
T
2026-06-16 04:38:23 +00:00

57 lines
2.0 KiB
Python

import httpx
from fastapi import APIRouter, Depends
from app.auth.deps import get_current_user
from app.config import get_settings
from app.db.models import User
from app.homelab.comfyui import _use_anima
from app.homelab.openmeteo import build_weather_dashboard
router = APIRouter(prefix="/homelab", tags=["homelab"])
def _probe(url: str, *, timeout: float = 10.0) -> dict:
try:
with httpx.Client(timeout=timeout) as client:
response = client.get(url)
body = response.text[:500]
return {
"ok": response.status_code < 400,
"status_code": response.status_code,
"preview": body,
}
except Exception as exc:
return {"ok": False, "error": str(exc)}
@router.get("/status")
def homelab_status() -> dict:
settings = get_settings()
comfy_backend = "anima" if _use_anima(settings) else "checkpoint"
return {
"openmeteo": _probe(f"{settings.openmeteo_base_url.rstrip('/')}/v1/forecast?latitude=0&longitude=0&current=temperature_2m"),
"comfyui": _probe(f"{settings.comfyui_base_url.rstrip('/')}/system_stats"),
"netdata": _probe(f"{settings.netdata_base_url.rstrip('/')}/api/v1/info"),
"rp_chat": _probe(f"{settings.rp_chat_base_url.rstrip('/')}/health"),
"config": {
"openmeteo_base_url": settings.openmeteo_base_url,
"comfyui_base_url": settings.comfyui_base_url,
"comfyui_backend": comfy_backend,
"comfyui_unet": settings.comfyui_unet,
"netdata_base_url": settings.netdata_base_url,
"rp_chat_base_url": settings.rp_chat_base_url,
"rp_chat_enabled": settings.rp_chat_enabled,
},
}
@router.get("/weather")
def weather_dashboard(
hours_ahead: int = 12,
days_ahead: int = 7,
_: User = Depends(get_current_user),
) -> dict:
hours = max(1, min(int(hours_ahead), 168))
days = max(1, min(int(days_ahead), 16))
return build_weather_dashboard(hours_ahead=hours, days_ahead=days)