From 998e3a94fc2b91698f1d1d767eb3c5bfc4a06213 Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Sat, 17 Jan 2026 16:31:34 +0100 Subject: [PATCH] Remember if the alarm is on --- NightLight.Core/NightLightStateMachine.fs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/NightLight.Core/NightLightStateMachine.fs b/NightLight.Core/NightLightStateMachine.fs index 4a8e77f..8622f49 100644 --- a/NightLight.Core/NightLightStateMachine.fs +++ b/NightLight.Core/NightLightStateMachine.fs @@ -25,6 +25,7 @@ let internal generateZigbeeCommandsToFixLight state (light: Light) = type internal NightLightState = { Time: DateTime + Alarm: bool LightToState: Map } let internal withStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) = @@ -37,6 +38,10 @@ let internal withStateForRemoteControlledLights (state: State) (oldNightLightSta remoteControlledLights |> Seq.fold (fun acc light -> acc |> withStateFor light state) oldNightLightState +let internal withAlarmOff (oldNightLightState: NightLightState) = + { oldNightLightState with + Alarm = false } + let internal generateZigbeeCommandsForDifference (maybeBefore: NightLightState option) (after: NightLightState) = after.LightToState |> Seq.collect (fun (KeyValue(light, newState)) -> @@ -78,6 +83,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) = currentState |> withStateForRemoteControlledLights Off |> withStateFor lightThatShouldBeOn On + |> withAlarmOff NightLightStateMachine(Some newNightLightState), generateZigbeeCommandsForDifference (Some currentState) newNightLightState @@ -90,6 +96,10 @@ type NightLightStateMachine private (maybeState: NightLightState option) = maybePreviousPartOfDay <> Some Day && newPartOfDay = Day + let alarm = + maybeCurrentState |> Option.map _.Alarm |> Option.defaultValue false + || newDayStarted + let newLightToState = lights |> Seq.map (fun light -> @@ -102,7 +112,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) = |> Option.map _.LightToState[light].State |> Option.defaultValue On - let newState = if newDayStarted then On else previousState + let newState = if alarm then On else previousState light, { Color = color @@ -112,6 +122,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) = let newNightLightState = { Time = newTime + Alarm = alarm LightToState = newLightToState } return