This commit is contained in:
Aaro Varis
2026-02-05 09:45:57 +02:00
parent 989c831732
commit 00e973cf42

33
init.py
View File

@@ -88,14 +88,16 @@ class AlarmClock:
longPressThreshold: float = 1.0 # 1 second for long press longPressThreshold: float = 1.0 # 1 second for long press
buttonPressStartTime: float = 0.0 buttonPressStartTime: float = 0.0
configMode: str = "normal" # modes: normal, set_hour, set_minute configMode: str = "normal" # modes: normal, set_hour, set_minute
alarmTime: datetime = datetime.now(ZoneInfo("Europe/Helsinki")).replace(hour=9, minute=32, second=0, microsecond=0) alarmTime: datetime = datetime.now(ZoneInfo("Europe/Helsinki")).replace(hour=9, minute=48, second=0, microsecond=0)
actionsResetForToday: bool = False actionsResetForToday: bool = False
alarmActive: bool = False
def __init__(self, pins: dict = {}): def __init__(self, pins: dict = {}):
self.ledGpio = GPIO(pins.get("led", 5), GPIO.OUT) self.ledGpio = GPIO(pins.get("led", 5), GPIO.OUT)
self.buttonGpio = GPIO(pins.get("button", 6), GPIO.IN) self.buttonGpio = GPIO(pins.get("button", 6), GPIO.IN)
self.relayGpio = GPIO(pins.get("relay", 16), GPIO.OUT) self.relayGpio = GPIO(pins.get("relay", 16), GPIO.OUT)
self.lcd = JHD1802() self.lcd = JHD1802()
self.wakeupActions = [] self.wakeupActions = []
self.addWakeUpAction(0, lambda: self.triggerAlarm()) # Activate alarm state
self.setLcdText("AlarmClock Init") self.setLcdText("AlarmClock Init")
def addWakeUpAction(self, offsetSeconds: int, action: Callable[[], None]): def addWakeUpAction(self, offsetSeconds: int, action: Callable[[], None]):
@@ -103,12 +105,23 @@ class AlarmClock:
self.wakeupActions.append(WakeUpAction(offsetSeconds, action)) self.wakeupActions.append(WakeUpAction(offsetSeconds, action))
def resetWakeUpActions(self): def resetWakeUpActions(self):
"""Reset all wakeup actions so they can trigger again"""
for action in self.wakeupActions: for action in self.wakeupActions:
action.triggered = False action.triggered = False
self.actionsResetForToday = True self.actionsResetForToday = True
print("Wakeup actions reset") print("Wakeup actions reset")
def triggerAlarm(self):
if self.alarmActive:
return
self.alarmActive = True
print("Alarm is now active!")
def dismissAlarm(self):
if not self.alarmActive:
return
self.alarmActive = False
print("Alarm dismissed")
def checkWakeUpActions(self): def checkWakeUpActions(self):
"""Check and trigger any wakeup actions that are due""" """Check and trigger any wakeup actions that are due"""
if not self.wakeupActions: if not self.wakeupActions:
@@ -163,7 +176,10 @@ class AlarmClock:
currentHour = datetime.now(ZoneInfo(city.timezone)).hour currentHour = datetime.now(ZoneInfo(city.timezone)).hour
currrentSecond = datetime.now(ZoneInfo(city.timezone)).second currrentSecond = datetime.now(ZoneInfo(city.timezone)).second
self.setLcdText(f"Time {currentHour:02d}:{currentMinute:02d}:{currrentSecond:02d}\nAlarm {self.alarmTime.hour:02d}:{self.alarmTime.minute:02d}") if self.alarmActive:
self.setLcdText(f"!! ALARM !!\nPress to dismiss")
else:
self.setLcdText(f"Time {currentHour:02d}:{currentMinute:02d}:{currrentSecond:02d}\nAlarm {self.alarmTime.hour:02d}:{self.alarmTime.minute:02d}")
# Check and trigger wakeup actions # Check and trigger wakeup actions
self.checkWakeUpActions() self.checkWakeUpActions()
@@ -201,9 +217,13 @@ class AlarmClock:
def onShortPress(self): def onShortPress(self):
"""Handle short button press based on current mode""" """Handle short button press based on current mode"""
if self.configMode == "normal": if self.configMode == "normal":
# Normal mode: toggle relay if self.alarmActive:
self.setRelayState(not self.relayGpio.read()) # Dismiss active alarm
setShellyPlugState(shelly_deviceId, self.relayGpio.read() == 0) self.dismissAlarm()
else:
# Normal mode: toggle relay
self.setRelayState(not self.relayGpio.read())
setShellyPlugState(shelly_deviceId, self.relayGpio.read() == 0)
elif self.configMode == "set_hour": elif self.configMode == "set_hour":
# Increment hour # Increment hour
self.alarmTime = self.alarmTime.replace(hour=(self.alarmTime.hour + 1) % 24) self.alarmTime = self.alarmTime.replace(hour=(self.alarmTime.hour + 1) % 24)
@@ -260,6 +280,7 @@ alarm_clock.addWakeUpAction(120, lambda: print("2 minutes to alarm!")) # 2 minu
alarm_clock.addWakeUpAction(90, lambda: setShellyPlugState(shelly_deviceId, True)) # activate plug 90s before alarm alarm_clock.addWakeUpAction(90, lambda: setShellyPlugState(shelly_deviceId, True)) # activate plug 90s before alarm
alarm_clock.addWakeUpAction(30, lambda: print("30 seconds to alarm!")) # 30 seconds before alarm alarm_clock.addWakeUpAction(30, lambda: print("30 seconds to alarm!")) # 30 seconds before alarm
alarm_clock.addWakeUpAction(0, lambda: print("Alarm triggered!")) # At alarm time alarm_clock.addWakeUpAction(0, lambda: print("Alarm triggered!")) # At alarm time
alarm_clock.addWakeUpAction(0, lambda: playAudio("./boxing_bell_multiple.wav")) # Play sound at alarm time alarm_clock.addWakeUpAction(0, lambda: playAudio("./boxing_bell_multiple.wav")) # Play sound at alarm time
alarm_clock.start() alarm_clock.start()