46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from app.api.v1.entities import router as entities_router
|
|
from app.config import load_settings
|
|
from app.ha.client import HaClient, HaClientSettings
|
|
from app.ha.reader import HaReader
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
|
settings = load_settings()
|
|
app.state.settings = settings
|
|
if settings.ha_configured:
|
|
client = HaClient(
|
|
settings=HaClientSettings(
|
|
url=settings.ha_url or "",
|
|
token=settings.ha_token or "",
|
|
)
|
|
)
|
|
app.state.ha_reader = HaReader(client=client)
|
|
yield
|
|
|
|
|
|
app = FastAPI(
|
|
title="SillyHome Next API",
|
|
description="Lokales Smart-Home-Intelligenzsystem für Home Assistant.",
|
|
version="0.1.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
|
|
app.include_router(entities_router)
|
|
|
|
|
|
@app.get("/health")
|
|
def health() -> dict[str, str]:
|
|
return {"status": "ok"}
|
|
|
|
|
|
@app.get("/")
|
|
def root() -> dict[str, str]:
|
|
return {"service": "sillyhome-next", "docs": "/docs"}
|