firmament: report when the player starts and stops
Assisted-by: Claude Opus 5.5 via Claude Code
change ynrprrknxsypzwtmsmwpxozvnwkrmmrt
commit ca93e73922d9db618ab8a2f4fd65649d0e436c43
author Alpha Chen <alpha@kejadlen.dev>
date
parent kwvlruyq
diff --git a/firmament/src/firmament/cli.py b/firmament/src/firmament/cli.py
index bace3e4..8a48347 100644
--- a/firmament/src/firmament/cli.py
+++ b/firmament/src/firmament/cli.py
@@ -66,6 +66,10 @@ def main(argv: list[str] | None = None) -> int:
         return 1
 
     with sender:
+        print(
+            f"firmament: {_activity(args)} on {args.host} ({sender.ip}); press Ctrl-C to stop",
+            file=sys.stderr,
+        )
         try:
             if args.command == "play":
                 _play(frames, sender, args.log)
@@ -79,10 +83,24 @@ def main(argv: list[str] | None = None) -> int:
         try:
             sender.send(slot_frame(ALL_OFF, args.leds, args.level))
         except OSError:
-            pass  # WLED goes back to its own effect after its realtime timeout anyway.
+            # WLED goes back to its own effect after its realtime timeout anyway.
+            print("firmament: stopped, but couldn't turn the LEDs off", file=sys.stderr)
+        else:
+            print("firmament: stopped and turned the LEDs off", file=sys.stderr)
     return 0
 
 
+def _activity(args: argparse.Namespace) -> str:
+    if args.command == "play":
+        cycle = SLOT_COUNT * SLOT_SECONDS
+        return f"playing the ID pattern ({args.leds} LEDs, {cycle:.1f} s per cycle)"
+    if args.index is not None:
+        return f"lighting LED {args.index}"
+    first = args.strand * args.strand_length
+    last = min(first + args.strand_length, args.leds) - 1
+    return f"lighting strand {args.strand} (LEDs {first} to {last})"
+
+
 def _frames(args: argparse.Namespace) -> list[bytes]:
     if args.command == "play":
         return [slot_frame(slot, args.leds, args.level) for slot in range(SLOT_COUNT)]
diff --git a/firmament/src/firmament/ddp.py b/firmament/src/firmament/ddp.py
index bc3d800..8b17d89 100644
--- a/firmament/src/firmament/ddp.py
+++ b/firmament/src/firmament/ddp.py
@@ -61,10 +61,13 @@ def packets(frame: bytes, sequence: int) -> list[bytes]:
 
 class DdpSender:
     def __init__(self, host: str, port: int = PORT) -> None:
-        family, kind, proto, _, address = socket.getaddrinfo(host, port, type=socket.SOCK_DGRAM)[0]
+        results = socket.getaddrinfo(host, port, type=socket.SOCK_DGRAM)
+        # Prefer IPv4: a hostname can resolve to IPv6 first, and WLED may not listen there.
+        family, kind, proto, _, address = min(results, key=lambda r: r[0] != socket.AF_INET)
         self._socket = socket.socket(family, kind, proto)
         self._socket.connect(address)
         self._sequence = 0
+        self.ip = str(address[0])
 
     def send(self, frame: bytes) -> None:
         self._sequence = self._sequence % 15 + 1
diff --git a/firmament/tests/test_cli.py b/firmament/tests/test_cli.py
index fbd6509..41c294b 100644
--- a/firmament/tests/test_cli.py
+++ b/firmament/tests/test_cli.py
@@ -1,9 +1,41 @@
+import socket
+from collections.abc import Iterator
+
 import pytest
 
 from firmament.cli import main, play
+from firmament.ddp import Header
 from firmament.pattern import SLOT_COUNT, SLOT_SECONDS
 
 
+@pytest.fixture
+def receiver() -> Iterator[socket.socket]:
+    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
+        s.bind(("127.0.0.1", 0))
+        s.settimeout(1)
+        yield s
+
+
+def _interrupt(_: float) -> None:
+    raise KeyboardInterrupt
+
+
+def test_light_reports_start_and_stop_and_turns_leds_off(receiver, monkeypatch, capsys):
+    monkeypatch.setattr("firmament.cli.time.sleep", _interrupt)
+    port = str(receiver.getsockname()[1])
+
+    assert (
+        main(["light", "--host", "localhost", "--port", port, "--leds", "4", "--index", "1"]) == 0
+    )
+
+    lit, off = (receiver.recv(2048)[Header.SIZE :] for _ in range(2))
+    assert lit == bytes(3) + bytes([32] * 3) + bytes(6)
+    assert off == bytes(12)
+    err = capsys.readouterr().err
+    assert "lighting LED 1 on localhost (127.0.0.1); press Ctrl-C to stop" in err
+    assert "stopped and turned the LEDs off" in err
+
+
 class FakeClock:
     def __init__(self) -> None:
         self.now = 0.0