From b2f84e9e40dbe42f783e81ee7785cfa4e3dcc474 Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Mon, 30 Oct 2023 19:59:53 +0200 Subject: [PATCH] api: use pydantic models for notify --- api.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/api.py b/api.py index bfdd2dc..9009019 100644 --- a/api.py +++ b/api.py @@ -155,7 +155,9 @@ def get_client_by_hostname(self, hostname): async def notify(self, data): try: - msg = json.dumps(data) + msg = NotifyMsg.model_validate_json(json.dumps(data)) + msg = msg.model_dump_json(exclude={'hostname'}, exclude_none=True) + log.info(msg) if 'hostname' in data: ws = self.get_client_by_hostname(data['hostname']) await ws.send_text(msg) @@ -175,6 +177,22 @@ def update_client(self, ws, key, value): self.connected_clients[ws].set_mac_addr(value) +class NotifyData(BaseModel): + audio_url: Optional[str] = None + backlight: bool = False + backlight_max: bool = False + repeat: int = 1 + strobe_period_ms: Optional[int] = 0 + text: Optional[str] = None + volume: Optional[int] = Optional[Annotated[int, Field(ge=0, le=100)]] + + +class NotifyMsg(BaseModel): + cmd: str = "notify" + data: NotifyData + hostname: Optional[str] = None + + class WakeEvent: def __init__(self, client, volume): self.client = client