diff --git a/NightLight.Core.Tests/NightLightTests.fs b/NightLight.Core.Tests/NightLightTests.fs index 96d2ec4..f6946c7 100644 --- a/NightLight.Core.Tests/NightLightTests.fs +++ b/NightLight.Core.Tests/NightLightTests.fs @@ -96,11 +96,13 @@ type NightLightTests() = let newDayStartedSinceBedroomRemote = hasNewDayStartedSince interactions lastBedroomControllingRemoteInteraction - let hasPressedRight = + let livingRoomLightsToggledOn = interactions - |> Seq.exists (function + |> Seq.filter (function | Interaction.LivingRoomControllingRemoteInteraction RemotePressedRightButton -> true | _ -> false) + |> Seq.length + |> fun rightPresses -> rightPresses % 2 = 0 let isExpectedOn light = match light with @@ -115,15 +117,15 @@ type NightLightTests() = | Some(_, RemotePressedOnButton) -> true | None -> true | LivingRoomWallLamp - | LivingRoomFloorLamp -> not hasPressedRight + | LivingRoomFloorLamp -> livingRoomLightsToggledOn | BathroomCeilingLamp -> true lightsWithPower |> Seq.forall (fun (light, state) -> state.IsOn = isExpectedOn light) |> Prop.collect $"last bedroom controlling remote interaction is {lastBedroomControllingRemoteInteraction |> Option.map snd}" - |> Prop.collect $"pressed right: {hasPressedRight}" |> Prop.collect $"{lightsWithPower.Length} light(s) with power" + |> Prop.classify livingRoomLightsToggledOn "living room lights toggled on" |> Prop.classify newDayStartedSinceBedroomRemote "new day since bedroom remote" |> Prop.label fakeHome.Label |> Prop.trivial (lightsWithPower.Length = 0) diff --git a/NightLight.Core/Models.fs b/NightLight.Core/Models.fs index b7a31ad..122429b 100644 --- a/NightLight.Core/Models.fs +++ b/NightLight.Core/Models.fs @@ -87,6 +87,11 @@ type internal State = | On | Off + member this.Invert() = + match this with + | On -> Off + | Off -> On + type internal Brightness = | Brightness of int diff --git a/NightLight.Core/NightLightStateMachine.fs b/NightLight.Core/NightLightStateMachine.fs index bbab1d8..a09c418 100644 --- a/NightLight.Core/NightLightStateMachine.fs +++ b/NightLight.Core/NightLightStateMachine.fs @@ -49,6 +49,10 @@ let internal withStateFor (light: Light) (state: State) (oldNightLightState: Nig { oldNightLightState with LightToManualState = Map.add light state oldNightLightState.LightToManualState } +let internal withInvertedStateFor (light: Light) (oldNightLightState: NightLightState) = + oldNightLightState + |> withStateFor light (oldNightLightState.LightToManualState[light].Invert()) + let internal withAlarmOff (oldNightLightState: NightLightState) = { oldNightLightState with Alarm = false } @@ -105,8 +109,8 @@ type NightLightStateMachine private (maybeState: NightLightState option) = |> withStateFor LeftBedroomLamp On | PressedRight -> currentState - |> withStateFor LivingRoomWallLamp Off - |> withStateFor LivingRoomFloorLamp Off + |> withInvertedStateFor LivingRoomWallLamp + |> withInvertedStateFor LivingRoomFloorLamp NightLightStateMachine(Some newNightLightState), generateZigbeeCommandsForDifference (Some currentState) newNightLightState