window contact first try

This commit is contained in:
2025-11-10 19:41:08 +01:00
parent e728dd58e4
commit 19a6a603d5
8 changed files with 360 additions and 14 deletions

View File

@@ -15,7 +15,14 @@ from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, ValidationError
from packages.home_capabilities import LIGHT_VERSION, THERMOSTAT_VERSION, LightState, ThermostatState
from packages.home_capabilities import (
LIGHT_VERSION,
THERMOSTAT_VERSION,
CONTACT_SENSOR_VERSION,
LightState,
ThermostatState,
ContactState
)
logger = logging.getLogger(__name__)
@@ -137,7 +144,8 @@ async def spec() -> dict[str, dict[str, str]]:
return {
"capabilities": {
"light": LIGHT_VERSION,
"thermostat": THERMOSTAT_VERSION
"thermostat": THERMOSTAT_VERSION,
"contact": CONTACT_SENSOR_VERSION
}
}
@@ -331,6 +339,13 @@ async def set_device(device_id: str, request: SetDeviceRequest) -> dict[str, str
detail=f"Device {device_id} not found"
)
# Check if device is read-only (contact sensors, etc.)
if "topics" in device and "set" not in device["topics"]:
raise HTTPException(
status_code=status.HTTP_405_METHOD_NOT_ALLOWED,
detail="Device is read-only"
)
# Validate payload based on device type
if request.type == "light":
try:
@@ -356,6 +371,12 @@ async def set_device(device_id: str, request: SetDeviceRequest) -> dict[str, str
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"Invalid payload for thermostat: {e}"
)
elif request.type in {"contact", "contact_sensor"}:
# Contact sensors are read-only
raise HTTPException(
status_code=status.HTTP_405_METHOD_NOT_ALLOWED,
detail="Contact sensors are read-only devices"
)
else:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,