Refactor AlarmClock to remove relay functionality and add Relay class for GPIO control

This commit is contained in:
2026-02-18 16:41:03 +02:00
parent 4324160014
commit 0985b953a4
3 changed files with 55 additions and 12 deletions

View File

@@ -17,7 +17,6 @@ class AlarmClock:
wakeup_actions: list[WakeUpAction] = [] wakeup_actions: list[WakeUpAction] = []
led_gpio: GPIO led_gpio: GPIO
button_gpio: GPIO button_gpio: GPIO
relay_gpio: GPIO
last_known_button_state: bool = False last_known_button_state: bool = False
lcd: JHD1802 lcd: JHD1802
last_button_press_time: float = 0.0 last_button_press_time: float = 0.0
@@ -36,7 +35,6 @@ class AlarmClock:
def __init__(self, pins: dict = {}): def __init__(self, pins: dict = {}):
self.led_gpio = GPIO(pins.get("led", 5), GPIO.OUT) self.led_gpio = GPIO(pins.get("led", 5), GPIO.OUT)
self.button_gpio = GPIO(pins.get("button", 6), GPIO.IN) self.button_gpio = GPIO(pins.get("button", 6), GPIO.IN)
self.relay_gpio = GPIO(pins.get("relay", 16), GPIO.OUT)
self.lcd = JHD1802() self.lcd = JHD1802()
self.wakeup_actions = [] self.wakeup_actions = []
self.add_wakeup_action(0, lambda: self.trigger_alarm()) # Activate alarm state self.add_wakeup_action(0, lambda: self.trigger_alarm()) # Activate alarm state
@@ -121,10 +119,6 @@ class AlarmClock:
while True: while True:
self.loop() self.loop()
def set_relay_state(self, state: bool):
"""Set the relay GPIO state."""
self.relay_gpio.write(1 if state else 0)
def loop(self): def loop(self):
"""Main loop iteration - handles display and button input.""" """Main loop iteration - handles display and button input."""
current_button_state = not self.button_gpio.read() current_button_state = not self.button_gpio.read()
@@ -185,9 +179,7 @@ class AlarmClock:
# Dismiss active alarm # Dismiss active alarm
self.dismiss_alarm() self.dismiss_alarm()
else: else:
# Normal mode: toggle relay print("pressed in normal mode - no active alarm to dismiss")
self.set_relay_state(not self.relay_gpio.read())
set_shelly_plug_state(SHELLY_DEVICE_ID, self.relay_gpio.read() == 0)
elif self.config_mode == "set_hour": elif self.config_mode == "set_hour":
# Increment hour # Increment hour
self.alarm_time = self.alarm_time.replace(hour=(self.alarm_time.hour + 1) % 24) self.alarm_time = self.alarm_time.replace(hour=(self.alarm_time.hour + 1) % 24)

View File

@@ -2,16 +2,27 @@ from .config import SHELLY_DEVICE_ID
from .shelly import ShellyDevice from .shelly import ShellyDevice
from .audio import AudioPlayer from .audio import AudioPlayer
from .alarm_clock import AlarmClock from .alarm_clock import AlarmClock
from .relay import Relay
from os import path from os import path
def main():
"""Main entry point for the alarm clock application."""
shellyDevice = ShellyDevice(SHELLY_DEVICE_ID) shellyDevice = ShellyDevice(SHELLY_DEVICE_ID)
relay = Relay(16)
relay.off()
audioFileAbsolutePath = path.abspath('./boxing_bell_multiple.wav') audioFileAbsolutePath = path.abspath('./boxing_bell_multiple.wav')
audio_player = AudioPlayer(audioFileAbsolutePath, loop=True) audio_player = AudioPlayer(audioFileAbsolutePath, loop=True)
def main():
"""Main entry point for the alarm clock application."""
alarm_clock = AlarmClock() alarm_clock = AlarmClock()
alarm_clock.add_wakeup_action( alarm_clock.add_wakeup_action(
@@ -36,6 +47,11 @@ def main():
dismiss_action=lambda: audio_player.stop() dismiss_action=lambda: audio_player.stop()
) )
alarm_clock.add_wakeup_action(
0,
action=lambda: relay.on(),
)
alarm_clock.start() alarm_clock.start()

35
src/relay.py Normal file
View File

@@ -0,0 +1,35 @@
from grove.gpio import GPIO
class Relay:
"""Controller for a GPIO relay."""
_gpio: GPIO
_state: bool = False
def __init__(self, pin: int = 16):
self._gpio = GPIO(pin, GPIO.OUT)
self._state = False
@property
def state(self) -> bool:
"""Get the current relay state."""
return self._state
def set_state(self, state: bool):
"""Set the relay state."""
self._state = state
self._gpio.write(1 if state else 0)
def toggle(self) -> bool:
"""Toggle the relay state and return the new state."""
self.set_state(not self._state)
return self._state
def on(self):
"""Turn the relay on."""
self.set_state(True)
def off(self):
"""Turn the relay off."""
self.set_state(False)