diff --git a/NightLight.Core.Tests/FakeHome.fs b/NightLight.Core.Tests/FakeHome.fs index fc912fe..c04ef4e 100644 --- a/NightLight.Core.Tests/FakeHome.fs +++ b/NightLight.Core.Tests/FakeHome.fs @@ -149,12 +149,15 @@ type FakeHome with member this.NonRemotelyControlledLightStates = this.LightStates - |> Seq.filter (fst >> lightProps >> _.ControlledWithRemote >> (=) NonRemote) + |> Seq.filter (fun (light, _) -> + light = VardagsrumVagglampa + || light = VardagsrumGolvlampa + || light = BadrumTaklampa) |> Seq.toList member this.RemotelyControlledLightStates = this.LightStates - |> Seq.filter (fst >> lightProps >> _.ControlledWithRemote >> (<>) NonRemote) + |> Seq.filter (fun (light, _) -> light = VardagsrumFonsterlampa || light = SovrumNattduksbordlampa) |> Seq.toList member this.Label = diff --git a/NightLight.Core.Tests/LightArbitraries.fs b/NightLight.Core.Tests/LightArbitraries.fs index 6c36773..02dc5be 100644 --- a/NightLight.Core.Tests/LightArbitraries.fs +++ b/NightLight.Core.Tests/LightArbitraries.fs @@ -9,27 +9,30 @@ type ArbitraryLight = type ArbitraryNonRemotelyControlledLight = static member Light() = lights - |> Seq.filter (fun light -> (lightProps light).ControlledWithRemote.IsNonRemote) + |> Seq.filter (fun light -> + light = VardagsrumVagglampa + || light = VardagsrumGolvlampa + || light = BadrumTaklampa) |> Gen.elements |> Arb.fromGen type ArbitraryLeftRemotelyControlledLight = static member Light() = lights - |> Seq.filter (fun light -> (lightProps light).ControlledWithRemote.IsRemoteLeft) + |> Seq.filter (fun light -> light = SovrumNattduksbordlampa) |> Gen.elements |> Arb.fromGen type ArbitraryRightRemotelyControlledLight = static member Light() = lights - |> Seq.filter (fun light -> (lightProps light).ControlledWithRemote.IsRemoteRight) + |> Seq.filter (fun light -> light = VardagsrumFonsterlampa) |> Gen.elements |> Arb.fromGen type ArbitraryRemotelyControlledLight = static member Light() = lights - |> Seq.filter (fun light -> (lightProps light).ControlledWithRemote.IsNonRemote |> not) + |> Seq.filter (fun light -> light = VardagsrumFonsterlampa || light = SovrumNattduksbordlampa) |> Gen.elements |> Arb.fromGen diff --git a/NightLight.Core.Tests/NightLightTests.fs b/NightLight.Core.Tests/NightLightTests.fs index 19e659a..8d4b42f 100644 --- a/NightLight.Core.Tests/NightLightTests.fs +++ b/NightLight.Core.Tests/NightLightTests.fs @@ -52,7 +52,7 @@ type NightLightTests() = && time <= endOfAlarm let scaledForAlarm light brightness = - if (lightProps light).ControlledWithRemote <> NonRemote && alarm then + if (light = VardagsrumFonsterlampa || light = SovrumNattduksbordlampa) && alarm then float brightness * ((time - startOfDay) / (endOfAlarm - startOfDay)) |> byte else brightness @@ -108,8 +108,11 @@ type NightLightTests() = let allOn (ls: (Light * LightState) seq) = ls |> Seq.forall (snd >> _.IsOn) let allOff (ls: (Light * LightState) seq) = ls |> Seq.forall (snd >> _.IsOff) - let controlledBy remote ls = - ls |> Seq.filter (fst >> lightProps >> _.ControlledWithRemote >> (=) remote) + let controlledByLeft ls = + ls |> Seq.filter (fun (light, _) -> light = SovrumNattduksbordlampa) + + let controlledByRight ls = + ls |> Seq.filter (fun (light, _) -> light = VardagsrumFonsterlampa) let maybeLastRemoteInteraction = tryGetLastRemoteInteraction interactions @@ -123,8 +126,8 @@ type NightLightTests() = | Some(_, RemotePressedOnButton) -> remotelyControlledLightsWithPower |> allOn | Some(_, RemotePressedOffButton) -> remotelyControlledLightsWithPower |> allOff | Some(_, RemotePressedLeftButton) -> - remotelyControlledLightsWithPower |> controlledBy RemoteLeft |> allOn - && remotelyControlledLightsWithPower |> controlledBy RemoteRight |> allOff + remotelyControlledLightsWithPower |> controlledByLeft |> allOn + && remotelyControlledLightsWithPower |> controlledByRight |> allOff | None -> remotelyControlledLightsWithPower |> allOn |> Prop.collect $"last remote interaction is {maybeLastRemoteInteraction |> Option.map snd}" |> Prop.collect $"{remotelyControlledLightsWithPower.Length} remotely controlled light(s) with power" diff --git a/NightLight.Core/Models.fs b/NightLight.Core/Models.fs index 6f260ce..52dcf48 100644 --- a/NightLight.Core/Models.fs +++ b/NightLight.Core/Models.fs @@ -40,11 +40,6 @@ type DeviceFriendlyName = match this with | DeviceFriendlyName deviceFriendlyName -> deviceFriendlyName -type LightControl = - | NonRemote - | RemoteLeft - | RemoteRight - type Light = | VardagsrumFonsterlampa | VardagsrumVagglampa @@ -55,36 +50,30 @@ type Light = type LightProps = { FriendlyName: DeviceFriendlyName Room: Room - Bulb: Bulb - ControlledWithRemote: LightControl } + Bulb: Bulb } let lightProps light = match light with | VardagsrumFonsterlampa -> { FriendlyName = DeviceFriendlyName "Vardagsrum - Fönsterlampa" Room = Bedroom - Bulb = IkeaBulb - ControlledWithRemote = RemoteRight } + Bulb = IkeaBulb } | VardagsrumVagglampa -> { FriendlyName = DeviceFriendlyName "Vardagsrum - Vägglampa" Room = LivingRoom - Bulb = PaulmannBulb - ControlledWithRemote = NonRemote } + Bulb = PaulmannBulb } | VardagsrumGolvlampa -> { FriendlyName = DeviceFriendlyName "Vardagsrum - Golvlampa" Room = LivingRoom - Bulb = PaulmannBulb - ControlledWithRemote = NonRemote } + Bulb = PaulmannBulb } | BadrumTaklampa -> { FriendlyName = DeviceFriendlyName "Badrum - Taklampa" Room = Bathroom - Bulb = IkeaBulb - ControlledWithRemote = NonRemote } + Bulb = IkeaBulb } | SovrumNattduksbordlampa -> { FriendlyName = DeviceFriendlyName "Sovrum - Nattduksbordlampa" Room = Bedroom - Bulb = IkeaBulb - ControlledWithRemote = RemoteLeft } + Bulb = IkeaBulb } let lights = [ VardagsrumFonsterlampa diff --git a/NightLight.Core/NightLightStateMachine.fs b/NightLight.Core/NightLightStateMachine.fs index 95d2b51..886af19 100644 --- a/NightLight.Core/NightLightStateMachine.fs +++ b/NightLight.Core/NightLightStateMachine.fs @@ -13,11 +13,17 @@ let internal tryFindLight friendlyName = let internal generateZigbeeCommandsToFixLight (light: Light) (desiredLightState: LightState) = seq { - match (lightProps light).ControlledWithRemote, desiredLightState.State with - | NonRemote, On -> () - | NonRemote, Off -> failwith $"Unexpectly trying to turn off {light}. It's not remote-controlled." - | _, On when (lightProps light).Bulb = IkeaBulb -> () // Rely on the brightness command for turning it on - | _, _ -> yield generateStateCommand desiredLightState.State light + let isRemoteControlled = + light = VardagsrumFonsterlampa || light = SovrumNattduksbordlampa + + if not isRemoteControlled then + match desiredLightState.State with + | On -> () + | Off -> failwith $"Unexpectly trying to turn off {light}. It's not remote-controlled." + else if desiredLightState.State = On && (lightProps light).Bulb = IkeaBulb then + () // Rely on the brightness command for turning it on + else + yield generateStateCommand desiredLightState.State light if desiredLightState.State = On then yield generateBrightnessCommand light desiredLightState.Brightness @@ -51,7 +57,7 @@ let internal createOrUpdateNightLightState light, { Color = color Brightness = - if alarm && (lightProps light).ControlledWithRemote <> NonRemote then + if alarm && (light = VardagsrumFonsterlampa || light = SovrumNattduksbordlampa) then brightness.Scale(getAlarmWeight time) else brightness @@ -73,7 +79,7 @@ let internal withStateFor (light: Light) (state: State) (oldNightLightState: Nig let internal withStateForRemoteControlledLights (state: State) (oldNightLightState: NightLightState) = lights - |> Seq.filter (fun light -> (lightProps light).ControlledWithRemote.IsNonRemote |> not) + |> Seq.filter (fun light -> light = VardagsrumFonsterlampa || light = SovrumNattduksbordlampa) |> Seq.fold (fun acc light -> acc |> withStateFor light state) oldNightLightState let internal withAlarmOff (oldNightLightState: NightLightState) = @@ -114,8 +120,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) = | PressedOff -> currentState |> withAlarmOff |> withStateForRemoteControlledLights Off | PressedLeft -> let lightThatShouldBeOn = - lights - |> Seq.find (fun light -> (lightProps light).ControlledWithRemote = RemoteLeft) + lights |> Seq.find (fun light -> light = SovrumNattduksbordlampa) currentState |> withAlarmOff