api: implement operating locks

This commit is contained in:
lilly 2026-05-14 15:47:59 +02:00
commit 60c4770280
Signed by: lilly
SSH key fingerprint: SHA256:y9T5GFw2A20WVklhetIxG1+kcg/Ce0shnQmbu1LQ37g
3 changed files with 45 additions and 12 deletions

View file

@ -240,9 +240,37 @@ async def list_locks(
@app.patch(
"/api/locks/{name}",
"/api/locks/{lock_id}",
tags=["locks"],
responses={status.HTTP_401_UNAUTHORIZED: {"model": models.HttpProblemDetail}},
responses={
status.HTTP_401_UNAUTHORIZED: {"model": models.HttpProblemDetail},
status.HTTP_404_NOT_FOUND: {"model": models.HttpProblemDetail},
},
)
async def operate_lock(name: str):
pass
async def operate_lock(req: Request, lock_id: str, requested_op: models.LockOperation, ccujack: deps.CCUJackClient, _current_user: deps.CurrentUser) -> None:
# TODO: Validate that the user is authorized
# find appropriate lock from ccujack
for i_lock, lock_channels in ccujack.locks:
if i_lock.identifier == lock_id:
for i_channel, channel_params in lock_channels:
if i_channel.type == "DOOR_LOCK_STATE_TRANSMITTER":
for i_param in channel_params:
if i_param.id == "LOCK_TARGET_LEVEL":
addr = f"{i_lock.address}/{i_channel.index}/{i_param.id}"
# match readable request parameter to ccujack value
match requested_op.desired_state:
case "closed":
ccujack_value = 0
case "open":
ccujack_value = 1
# write to ccujack
await ccujack.set_param_value(addr, ccujack_value)
return
else:
raise exceptions.HttpProblemException(
models.HttpProblemDetail.new_lock_not_found(lock_id, req.url)
)