fibaro.sleep(ms) — design note

Status: implemented. This documents the mechanics and the per-message-type delivery decisions.

Goal

fibaro.sleep(ms) suspends the calling QA for ms of virtual time — other QAs keep running. While a QA sleeps, messages destined for it (timer callbacks, device actions, UI events, network results, refreshState events, custom events) are queued and delivered — in arrival order — after the sleeping callback resumes. A restart drops the queue (the deferred messages belong to the old code instance).

Why coroutines

All QAs share one Lua VM; a QA callback runs to completion inside _PY.dispatch(batch) on the pump thread (the single-entry discipline). A blocking sleep would freeze every QA. Instead, each QA callback runs in a small coroutine (runInQa): fibaro.sleep(ms) is coroutine.yield(ms), and the runner schedules a QA-attributed wake timer through the existing timer machinery. The wake resumes the coroutine; the single-entry discipline still holds (resume happens inside dispatch). Because the wake is an ordinary timer, sleep rides the virtual clock: --speed accelerates it, --instant collapses it, a paused debugger freezes it.

Which message types defer (are queued) where

Every Python→Lua message funnels through _FLUA.dispatch(batch) → handlers[msg.type](msg). Each handler knows its QA:

message QA from while that QA sleeps
timerExpired callback_qas[id] (registered in _FLUA.setTimeout) queued — except wake timers (wakeTimers[id]), which end the sleep; the callback stays registered and runs from the queue
deviceAction device id → qaForDevice (children resolve to the parent — they share its code) queued
uiEvent msg.deviceId → qaForDevice queued
httpResult/tcpResult/udpResult/wsEvent msg.qa queued
mqttEvent msg.qa queued
customEvent broadcast over qaInstances per-QA: one queue entry per sleeping QA
refreshStateEvent broadcast over _FLUA.refreshStateListeners (maps handle → qaId) per-QA: one queue entry per sleeping QA (deduped across its subscriber handles)
startQA/restartQA msg.id not queued — restart abandons the sleep and the queue; the wake timer was already cancelled Python-side
runPreamble main state, no QA n/a

The wake resumes the sleeping callback first, then drains the queue — deferred events run in arrival order after the code that slept. If a resumed callback (or a deferred handler) sleeps again, its sleep chains through the same machinery and the queue keeps waiting. Broadcasts are delivered per-QA on drain (re-dispatching through the shared handler would hand a second copy to every QA).

Where the changes live

src/flua/lua/init.lua

src/flua/lua/fibaro.lua — fibaro.sleep(ms): __assert_type → coroutine.running() check (clear error outside a QA) → coroutine.yield(ms). The old blocking _PY.sleep stub was removed (it did not exist on the Python side and would have errored). The QA identity (a stub in qaInstances) is registered before the file executes, so sleep works at top level, in onInit, and in callbacks alike.

src/flua/lua/quickapp.lua — RefreshStateSubscriber:run() registers handle -> _FLUA.qaId so refresh delivery can be deferred per sleeping QA.

src/flua/engine.py — no changes needed: the wake timer is a normal QA-attributed timer, so --speed/--instant/has_pending_work behave unchanged, and exit()/restart_qa() cancel it via cancel_qa.

Edge cases

Tests (tests/test_sleep.py)