Skip to content

Commit

Permalink
api: use pydantic models for notify
Browse files Browse the repository at this point in the history
  • Loading branch information
stintel committed Oct 31, 2023
1 parent ec2fa63 commit b2f84e9
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit b2f84e9

Please sign in to comment.