Merge otto/ha-client-errors into main
This commit is contained in:
41
app/main.py
41
app/main.py
@@ -1,8 +1,9 @@
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Depends
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.api.v1.entities import router as entities_router
|
||||
from app.core.exception_handlers import register_exception_handlers
|
||||
from app.ha.client import HaClient, HaClientSettings
|
||||
from app.ha.reader import HaReader
|
||||
from app.rules.recommender import Recommender
|
||||
@@ -11,44 +12,34 @@ from app.rules.heating import HeatingRule
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
ha_url = getattr(app.state.settings, "ha_url", None)
|
||||
ha_token = getattr(app.state.settings, "ha_token", None)
|
||||
settings = HaClientSettings(
|
||||
url=ha_url or "",
|
||||
token=ha_token or "",
|
||||
settings = app.state.settings
|
||||
ha_url = getattr(settings, "ha_url", None)
|
||||
ha_token = getattr(settings, "ha_token", None)
|
||||
client = HaClient(
|
||||
settings=HaClientSettings(
|
||||
url=ha_url or "",
|
||||
token=ha_token or "",
|
||||
)
|
||||
)
|
||||
client = HaClient(settings=settings)
|
||||
app.state.ha_reader = HaReader(client=client)
|
||||
app.state.recommender = Recommender(rules=[HeatingRule()])
|
||||
yield
|
||||
|
||||
|
||||
class Settings:
|
||||
ha_url: str = "http://localhost:8123"
|
||||
ha_token: str = ""
|
||||
|
||||
|
||||
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 = "http://localhost:8123"
|
||||
ha_token: str = ""
|
||||
|
||||
|
||||
app.state.settings = Settings()
|
||||
register_exception_handlers(app)
|
||||
|
||||
|
||||
def get_ha_reader() -> HaReader:
|
||||
return app.state.ha_reader
|
||||
|
||||
|
||||
def get_recommender() -> Recommender:
|
||||
return app.state.recommender
|
||||
|
||||
|
||||
app.include_router(entities_router, dependencies=[Depends(get_ha_reader)])
|
||||
app.include_router(entities_router)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
|
||||
Reference in New Issue
Block a user