Make the Right button toggle the living room lights

This commit is contained in:
Sven van Heugten 2026-03-15 11:49:27 +01:00
parent 1dc1faa16a
commit 48c58b610c
3 changed files with 17 additions and 6 deletions

View file

@ -96,11 +96,13 @@ type NightLightTests() =
let newDayStartedSinceBedroomRemote = let newDayStartedSinceBedroomRemote =
hasNewDayStartedSince interactions lastBedroomControllingRemoteInteraction hasNewDayStartedSince interactions lastBedroomControllingRemoteInteraction
let hasPressedRight = let livingRoomLightsToggledOn =
interactions interactions
|> Seq.exists (function |> Seq.filter (function
| Interaction.LivingRoomControllingRemoteInteraction RemotePressedRightButton -> true | Interaction.LivingRoomControllingRemoteInteraction RemotePressedRightButton -> true
| _ -> false) | _ -> false)
|> Seq.length
|> fun rightPresses -> rightPresses % 2 = 0
let isExpectedOn light = let isExpectedOn light =
match light with match light with
@ -115,15 +117,15 @@ type NightLightTests() =
| Some(_, RemotePressedOnButton) -> true | Some(_, RemotePressedOnButton) -> true
| None -> true | None -> true
| LivingRoomWallLamp | LivingRoomWallLamp
| LivingRoomFloorLamp -> not hasPressedRight | LivingRoomFloorLamp -> livingRoomLightsToggledOn
| BathroomCeilingLamp -> true | BathroomCeilingLamp -> true
lightsWithPower lightsWithPower
|> Seq.forall (fun (light, state) -> state.IsOn = isExpectedOn light) |> Seq.forall (fun (light, state) -> state.IsOn = isExpectedOn light)
|> Prop.collect |> Prop.collect
$"last bedroom controlling remote interaction is {lastBedroomControllingRemoteInteraction |> Option.map snd}" $"last bedroom controlling remote interaction is {lastBedroomControllingRemoteInteraction |> Option.map snd}"
|> Prop.collect $"pressed right: {hasPressedRight}"
|> Prop.collect $"{lightsWithPower.Length} light(s) with power" |> 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.classify newDayStartedSinceBedroomRemote "new day since bedroom remote"
|> Prop.label fakeHome.Label |> Prop.label fakeHome.Label
|> Prop.trivial (lightsWithPower.Length = 0) |> Prop.trivial (lightsWithPower.Length = 0)

View file

@ -87,6 +87,11 @@ type internal State =
| On | On
| Off | Off
member this.Invert() =
match this with
| On -> Off
| Off -> On
type internal Brightness = type internal Brightness =
| Brightness of int | Brightness of int

View file

@ -49,6 +49,10 @@ let internal withStateFor (light: Light) (state: State) (oldNightLightState: Nig
{ oldNightLightState with { oldNightLightState with
LightToManualState = Map.add light state oldNightLightState.LightToManualState } 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) = let internal withAlarmOff (oldNightLightState: NightLightState) =
{ oldNightLightState with { oldNightLightState with
Alarm = false } Alarm = false }
@ -105,8 +109,8 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
|> withStateFor LeftBedroomLamp On |> withStateFor LeftBedroomLamp On
| PressedRight -> | PressedRight ->
currentState currentState
|> withStateFor LivingRoomWallLamp Off |> withInvertedStateFor LivingRoomWallLamp
|> withStateFor LivingRoomFloorLamp Off |> withInvertedStateFor LivingRoomFloorLamp
NightLightStateMachine(Some newNightLightState), NightLightStateMachine(Some newNightLightState),
generateZigbeeCommandsForDifference (Some currentState) newNightLightState generateZigbeeCommandsForDifference (Some currentState) newNightLightState