fix: complete websocket delivery for v0.7.1
Some checks failed
quality / test (3.11) (push) Has been cancelled
quality / test (3.13) (push) Has been cancelled

This commit is contained in:
2026-06-14 22:30:29 +02:00
parent 51d23e0a9a
commit 2ae5576b8f
5 changed files with 129 additions and 49 deletions

View File

@@ -29,7 +29,7 @@ class _WsStatus:
"""Einfacher Status-Tracker für den WebSocket-Listener.
Wird als Attribut an app.state gehängt und enthält:
- status: "disconnected" | "connecting" | "connected" | "error"
- status: "disconnected" | "connecting" | "connected" | "reconnecting" | "error"
- error: str | None (Fehlermeldung bei status=error)
"""
def __init__(self) -> None:
@@ -100,7 +100,7 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
app = FastAPI(
title="SillyHome Next API",
description="Lokales Smart-Home-Intelligenzsystem für Home Assistant.",
version="0.7.0",
version="0.7.1",
lifespan=lifespan,
)
app.state.settings = load_settings()
@@ -122,7 +122,7 @@ def websocket_health() -> dict[str, object]:
"""Gibt den aktuellen Status des WebSocket-Listeners zurück.
Antwort:
- status: "disconnected" | "connecting" | "connected" | "error"
- status: "disconnected" | "connecting" | "connected" | "reconnecting" | "error"
- error: str | None (nur bei status=error)
"""
ws_status = getattr(app.state, "ws_status", None)
@@ -167,29 +167,39 @@ async def _ha_event_listener(app: FastAPI, client: HaClient) -> None:
ws_url = ha_url.replace("http://", "ws://").replace("https://", "wss://") + "/api/websocket"
auth_token = cast(str, settings.ha_token)
ws_status = getattr(app.state, "ws_status", None)
if ws_status is not None:
ws_status.status = "connecting"
while True:
if ws_status is not None:
ws_status.status = "connecting"
try:
async with websockets.connect(ws_url, extra_headers={
"Authorization": f"Bearer {auth_token}"
}) as websocket:
logger.info("WebSocket-Verbindung zu Home Assistant hergestellt")
if ws_status is not None:
ws_status.status = "connected"
ws_status.error = None
# Authentifizierung durchführen
auth_msg = await websocket.recv()
auth_data = json.loads(auth_msg)
if auth_data.get("type") != "auth_ok":
async with websockets.connect(ws_url) as websocket:
auth_required_msg = await websocket.recv()
auth_required_data = json.loads(auth_required_msg)
if auth_required_data.get("type") != "auth_required":
logger.error("Unerwartete WebSocket-Authentifizierungsaufforderung")
if ws_status is not None:
ws_status.status = "error"
ws_status.error = "Unerwartete Authentifizierungsaufforderung"
await asyncio.sleep(5)
continue
await websocket.send(json.dumps({"type": "auth", "access_token": auth_token}))
auth_result_msg = await websocket.recv()
auth_result_data = json.loads(auth_result_msg)
if auth_result_data.get("type") != "auth_ok":
logger.error("WebSocket-Authentifizierung fehlgeschlagen")
if ws_status is not None:
ws_status.status = "error"
ws_status.error = "Authentifizierung fehlgeschlagen"
await asyncio.sleep(5)
continue
logger.info("WebSocket-Verbindung zu Home Assistant hergestellt")
if ws_status is not None:
ws_status.status = "connected"
ws_status.error = None
# Auf alle State Changes subscriben
subscribe_msg = {
"id": 1,
"type": "subscribe_events",
"event_type": "state_changed"
}