refactor: keep style-specific config off the base class via **kwargs

The base AlarmStyle contract carried music_file, forcing SimpleStyle to
accept a parameter it never uses. Move style-specific config to the
subclass: the base __init__ takes only set_led (**kwargs swallows the rest);
each style declares the keyword args it actually uses (BlinkStyle: music_file;
SimpleStyle: none). The runner passes config as keyword args and each style
keeps only what it needs, so the base contract never grows as styles are
added — a future 'talk' style adds talk_file to its own signature, not to the
base. README plugin contract updated.
This commit is contained in:
2026-08-02 22:09:46 +02:00
parent 3a7b6ac7a3
commit 000b204215
7 changed files with 32 additions and 19 deletions
+10 -7
View File
@@ -12,16 +12,19 @@ class AlarmStyle(ABC):
import-safe in the API process — ``import pygame`` does not initialise
audio, so the API can import styles just to validate names.
Lifecycle: construct once with ``set_led``/``music_file``; call ``start()``
to begin ringing (kick off the tone and initial LED); then call ``update``
every tick. Return ``False`` from ``update`` to stop the alarm.
Lifecycle: construct once with ``set_led`` (plus any style-specific
keyword config the runner passes); call ``start()`` to begin ringing (kick
off the tone and initial LED); then call ``update`` every tick. Return
``False`` from ``update`` to stop the alarm.
Only ``set_led`` is universal. Style-specific config (e.g. ``music_file``)
belongs on the subclass, not here, so the base contract never grows as
styles are added; the runner passes config as keyword arguments and each
style declares only the ones it uses (extra kwargs are swallowed).
"""
def __init__(
self, set_led: Callable[[bool], None], music_file: str | None = None
):
def __init__(self, set_led: Callable[[bool], None], **kwargs):
self.set_led = set_led
self.music_file = music_file
def start(self) -> None:
"""Begin ringing. Override to start the style's tone / initial LED."""
+3 -2
View File
@@ -23,8 +23,9 @@ class BlinkStyle(AlarmStyle):
exactly that many times. A wrong count starts a fresh sequence.
"""
def __init__(self, set_led, music_file=None):
super().__init__(set_led, music_file)
def __init__(self, set_led, music_file=None, **kwargs):
super().__init__(set_led, **kwargs)
self.music_file = music_file
self.state = STATE_RINGING
self.target_blinks = 0
self.user_presses = 0
+2 -2
View File
@@ -37,8 +37,8 @@ class SimpleStyle(AlarmStyle):
wave played through ``pygame.mixer.Sound``.
"""
def __init__(self, set_led, music_file=None):
super().__init__(set_led, music_file)
def __init__(self, set_led, **kwargs):
super().__init__(set_led, **kwargs)
self._next_beep = 0.0
self._beep = None