3 Commits

Author SHA1 Message Date
0ca5ffaca0 device state fix 3 2025-11-19 09:40:47 +01:00
ec193a92f8 device state fix 2 2025-11-19 09:38:03 +01:00
c473ea341e device state fix 2025-11-19 09:35:27 +01:00

View File

@@ -1,146 +1,3 @@
"""API main entry point.
API-Analyse für HomeKit-Bridge Kompatibilität
==============================================
1) GET /devices
Status: ✅ VORHANDEN (Zeile 325-343)
Aktuelles Response-Modell (DeviceInfo, Zeile 189-194):
{
"device_id": str, ✅ OK
"type": str, ✅ OK
"name": str, ⚠️ ABWEICHUNG: Erwartet wurde "short_name" (optional)
"features": dict ✅ OK
}
Bewertung:
- ✅ Liefert device_id, type, features wie erwartet
- ⚠️ Verwendet "name" statt "short_name"
- ✅ Fallback auf device_id wenn name nicht vorhanden
- Kompatibilität: HOCH - einfach "name" als "short_name" verwenden
2) GET /layout
Status: ✅ VORHANDEN (Zeile 354-387)
Aktuelles Response-Format:
{
"rooms": [
{
"name": "Schlafzimmer",
"devices": [
{
"device_id": "thermostat_wolfgang",
"title": "Thermostat Wolfgang", ← friendly_name
"icon": "thermometer",
"rank": 1
}
]
}
]
}
Mapping device_id -> room, friendly_name:
- room: Durch Iteration über rooms[].devices[] ableitbar
- friendly_name: Im Feld "title" enthalten
Bewertung:
- ✅ Alle erforderlichen Informationen vorhanden
- ⚠️ ABWEICHUNG: Verschachtelte Struktur (rooms -> devices)
- ⚠️ ABWEICHUNG: friendly_name heißt "title"
- Kompatibilität: HOCH - einfache Transformation möglich:
```python
for room in layout["rooms"]:
for device in room["devices"]:
mapping[device["device_id"]] = {
"room": room["name"],
"friendly_name": device["title"]
}
```
3) POST /devices/{device_id}/set
Status: ✅ VORHANDEN (Zeile 406-504)
Aktuelles Request-Modell (SetDeviceRequest, Zeile 182-185):
{
"type": str, ✅ OK - muss zum Gerätetyp passen
"payload": dict ✅ OK - abstraktes Kommando
}
Beispiel Light:
POST /devices/leselampe_esszimmer/set
{"type": "light", "payload": {"power": "on", "brightness": 80}}
Beispiel Thermostat:
POST /devices/thermostat_wolfgang/set
{"type": "thermostat", "payload": {"target": 21.0}}
Validierung:
- ✅ Type-spezifische Payload-Validierung (Zeile 437-487)
- ✅ Read-only Check → 405 METHOD_NOT_ALLOWED (Zeile 431-435)
- ✅ Ungültige Payload → 422 UNPROCESSABLE_ENTITY
- ✅ Device nicht gefunden → 404 NOT_FOUND
Bewertung:
- ✅ Exakt wie erwartet implementiert
- ✅ Alle geforderten Error Codes vorhanden
- Kompatibilität: PERFEKT
4) Realtime-Endpoint (SSE)
Status: ✅ VORHANDEN als GET /realtime (Zeile 608-632)
Implementierung:
- ✅ Server-Sent Events (media_type="text/event-stream")
- ✅ Redis Pub/Sub basiert (event_generator, Zeile 510-607)
- ✅ Safari-kompatibel (Heartbeats, Retry-Hints)
Aktuelles Event-Format (aus apps/abstraction/main.py:250-256):
{
"type": "state", ✅ OK
"device_id": str, ✅ OK
"payload": dict, ✅ OK - z.B. {"power":"on","brightness":80}
"ts": str ✅ OK - ISO-8601 format von datetime.now(timezone.utc)
}
Beispiel-Event:
{
"type": "state",
"device_id": "thermostat_wolfgang",
"payload": {"current": 19.5, "target": 21.0},
"ts": "2025-11-17T14:23:45.123456+00:00"
}
Bewertung:
- ✅ Alle geforderten Felder vorhanden
- ✅ Timestamp im korrekten Format
- ✅ SSE mit proper headers und error handling
- Kompatibilität: PERFEKT
ZUSAMMENFASSUNG
===============
Alle 4 geforderten Endpunkte sind implementiert!
Kompatibilität mit HomeKit-Bridge Anforderungen:
- GET /devices: HOCH (nur Name-Feld unterschiedlich)
- GET /layout: HOCH (Struktur-Transformation nötig)
- POST /devices/{id}/set: PERFEKT (1:1 wie gefordert)
- GET /realtime (SSE): PERFEKT (1:1 wie gefordert)
Erforderliche Anpassungen für Bridge:
1. GET /devices: "name" als "short_name" interpretieren ✓ trivial
2. GET /layout: Verschachtelte Struktur zu flat mapping umwandeln ✓ einfach
Keine Code-Änderungen in der API erforderlich!
Die Bridge kann die bestehenden Endpoints direkt nutzen.
"""
import asyncio import asyncio
import json import json
import logging import logging
@@ -231,21 +88,12 @@ async def get_device_layout(device_id: str):
@app.get("/devices/{device_id}/state") @app.get("/devices/{device_id}/state")
async def get_device_state(device_id: str): async def get_device_state(device_id: str):
"""Gibt den aktuellen State für ein einzelnes Gerät zurück.""" try:
state_path = Path(__file__).parent.parent.parent / "config" / "devices.yaml" logger.debug(f"Fetching state for device {device_id}")
if not state_path.exists(): state = device_states[device_id]
raise HTTPException(status_code=500, detail="State file not found")
with open(state_path, "r") as f:
config = yaml.safe_load(f)
states = config.get("states", {})
state = states.get(device_id)
if state is None:
raise HTTPException(status_code=404, detail="Device state not found")
return state return state
# --- Minimal-invasive: Einzelgerät-Layout-Endpunkt --- except KeyError:
from fastapi import Query raise HTTPException(status_code=404, detail="Device state not found")
# --- Minimal-invasive: Einzelgerät-Layout-Endpunkt --- # --- Minimal-invasive: Einzelgerät-Layout-Endpunkt ---
@app.get("/devices/{device_id}/layout") @app.get("/devices/{device_id}/layout")
@@ -265,22 +113,6 @@ async def get_device_layout(device_id: str):
} }
raise HTTPException(status_code=404, detail="Device layout not found") raise HTTPException(status_code=404, detail="Device layout not found")
# --- Minimal-invasive: Einzelgerät-State-Endpunkt ---
@app.get("/devices/{device_id}/state")
async def get_device_state(device_id: str):
"""Gibt den aktuellen State für ein einzelnes Gerät zurück."""
# States werden wie im Bulk-Endpoint geladen
state_path = Path(__file__).parent.parent.parent / "config" / "devices.yaml"
if not state_path.exists():
raise HTTPException(status_code=500, detail="State file not found")
with open(state_path, "r") as f:
config = yaml.safe_load(f)
states = config.get("states", {})
state = states.get(device_id)
if state is None:
raise HTTPException(status_code=404, detail="Device state not found")
return state
@app.on_event("startup") @app.on_event("startup")
async def startup_event(): async def startup_event():
"""Include routers after app is initialized to avoid circular imports.""" """Include routers after app is initialized to avoid circular imports."""