diff --git a/app/main.py b/app/main.py index de0f8ff..6826c1c 100644 --- a/app/main.py +++ b/app/main.py @@ -1,12 +1,46 @@ -from fastapi import FastAPI +from contextlib import asynccontextmanager + +from fastapi import FastAPI, Depends + +from app.api.v1.entities import router as entities_router +from app.ha.client import HaClient, HaClientSettings +from app.ha.reader import HaReader + + +@asynccontextmanager +async def lifespan(app: FastAPI): + settings = HaClientSettings( + url=app.state.settings.ha_url, + token=app.state.settings.ha_token, + ) + client = HaClient(settings=settings) + 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, ) +class Settings: + ha_url: str + ha_token: str + + +app.state.settings = Settings() + + +def get_ha_reader() -> HaReader: + return app.state.ha_reader + + +app.include_router(entities_router, dependencies=[Depends(get_ha_reader)]) + + @app.get("/health") def health() -> dict[str, str]: return {"status": "ok"} @@ -14,4 +48,4 @@ def health() -> dict[str, str]: @app.get("/") def root() -> dict[str, str]: - return {"service": "sillyhome-next", "docs": "/docs"} + return {"service": "sillyhome-next", "docs": "/docs"} \ No newline at end of file