wake-alarm/wake_alarm/_ring_probe.py
Krzysztof kuhy Rudnicki be45e5969a Log the probe's skipped mornings once a day
A morning with no ring_probe line could mean kuhy was logged in or the
probe never ran. Also: the morning ring meets the greeter-only state,
not a masked lightdm; correct the docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 19:13:55 +01:00

49 lines
2.0 KiB
Python

"""The silent monitor probe: once, at A - 5, on a morning with no session.
On 2026-11-22 the fallback rang 30 minutes into the analog speakers, which
kuhy switches off at the knob for the night, so nothing was heard. The ring
now plays to every connected monitor too (``scripts/pc/ring_alsa.sh``).
Whether a monitor really takes the stream with no graphical session can be
known only in that state, and kuhy asked for no test sound, so this starts
``wake-alarm-ring-probe.service`` once: digital silence into each monitor,
one ``ring_probe`` journal line per sink. The first real fallback morning's
``ring_sink`` lines are the follow-up check.
"""
from __future__ import annotations
from datetime import timedelta
from typing import TYPE_CHECKING
from wake_alarm._constants import LOG_STATE_DIR
from wake_alarm._events import emit, emit_on_change
from wake_alarm._ring_player import _ok, graphical_session
if TYPE_CHECKING:
from datetime import datetime
PROBE_UNIT = "wake-alarm-ring-probe.service"
PROBE_DONE_FILE = LOG_STATE_DIR / "ring_probe_done"
PROBE_LEAD = timedelta(minutes=5)
def maybe_probe(now: datetime, alarm_at: datetime) -> str | None:
"""Start the probe if it is due; what happened, or None when not due."""
if PROBE_DONE_FILE.exists() or not alarm_at - PROBE_LEAD <= now < alarm_at:
return None
graphical, why = graphical_session()
if graphical:
# Not the state a fallback ring meets; try again tomorrow. Logged
# once a day, so a morning with no ring_probe line is a bug.
reason = "waiting_for_no_session"
key = f"{now.date().isoformat()}|{reason}"
emit_on_change("ring_probe", key, reason, session=why)
return reason
started, out = _ok(["sudo", "-n", "systemctl", "start", PROBE_UNIT])
emit("ring_probe", "started" if started else "failed", session=why, output=out)
if not started:
return "failed"
PROBE_DONE_FILE.parent.mkdir(parents=True, exist_ok=True)
PROBE_DONE_FILE.write_text(now.isoformat(), encoding="utf-8")
return "started"