From 52e0ec85e92880461bc77fc41b09e7465a3844db Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Mon, 5 Jan 2026 22:00:14 +0100 Subject: [PATCH] Introduce the remote into FakeHome --- NightLight.Core.Tests/FakeHome.fs | 26 ++++++++++++++++++- .../InteractionListGenerators.fs | 14 ++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/NightLight.Core.Tests/FakeHome.fs b/NightLight.Core.Tests/FakeHome.fs index d01c935..c3a7c4c 100644 --- a/NightLight.Core.Tests/FakeHome.fs +++ b/NightLight.Core.Tests/FakeHome.fs @@ -9,6 +9,8 @@ open FSharp.Data type HumanInteraction = | LightPoweredOn of Light | LightPoweredOff of Light + | RemotePressedOnButton + | RemotePressedOffButton type Interaction = | HumanInteraction of HumanInteraction @@ -25,15 +27,21 @@ type LightState = type FakeLight(light: Light) = let mutable hasPower = false + let mutable state = true let mutable brightness: byte = 255uy let mutable color: Color = White - member _.LightWithState = light, if hasPower then On(brightness, color) else Off + member _.LightWithState = + light, if hasPower && state then On(brightness, color) else Off member _.PowerOn() = hasPower <- true member _.PowerOff() = hasPower <- false + member _.SetState(newState: bool) = + if hasPower then + state <- newState + member _.SetBrightness(newBrightness: byte) = if hasPower then brightness <- newBrightness @@ -69,6 +77,12 @@ type FakeHome() = let parsedPayload = JsonValue.Parse command.Payload + match parsedPayload.TryGetProperty "state" with + | Some(JsonValue.String "ON") -> fakeLight.SetState true + | Some(JsonValue.String "OFF") -> fakeLight.SetState false + | None -> () + | value -> failwith $"Unexpected state value {value}" + match parsedPayload.TryGetProperty "brightness" with | Some(JsonValue.Number newBrightness) -> fakeLight.SetBrightness(byte newBrightness) | None -> () @@ -103,6 +117,16 @@ type FakeHome() = |> ReceivedZigbeeEvent |> onEventPublished.Trigger | HumanInteraction(LightPoweredOff light) -> friendlyNameToFakeLight[light.FriendlyName].PowerOff() + | HumanInteraction RemotePressedOnButton -> + { Topic = $"zigbee2mqtt/{remoteControlFriendlyName.Get}" + Payload = @"{ ""action"": ""on"" }" } + |> ReceivedZigbeeEvent + |> onEventPublished.Trigger + | HumanInteraction RemotePressedOffButton -> + { Topic = $"zigbee2mqtt/{remoteControlFriendlyName.Get}" + Payload = @"{ ""action"": ""off"" }" } + |> ReceivedZigbeeEvent + |> onEventPublished.Trigger | TimeChanged newTime -> newTime |> Event.TimeChanged |> onEventPublished.Trigger type FakeHome with diff --git a/NightLight.Core.Tests/InteractionListGenerators.fs b/NightLight.Core.Tests/InteractionListGenerators.fs index 17fd21e..7289a32 100644 --- a/NightLight.Core.Tests/InteractionListGenerators.fs +++ b/NightLight.Core.Tests/InteractionListGenerators.fs @@ -8,11 +8,15 @@ let private genTimeChangedInteraction = ArbMap.defaults |> ArbMap.generate |> Gen.map Interaction.TimeChanged let private genHumanInteraction = - Gen.elements lights - |> Gen.bind (fun light -> - [ LightPoweredOn light; LightPoweredOff light ] - |> Gen.elements - |> Gen.map Interaction.HumanInteraction) + let genLightInteraction = + Gen.elements lights + |> Gen.bind (fun light -> Gen.elements [ LightPoweredOn light; LightPoweredOff light ]) + + let genRemoteInteraction = + Gen.elements [ RemotePressedOnButton; RemotePressedOffButton ] + + Gen.oneof [ genLightInteraction; genRemoteInteraction ] + |> Gen.map Interaction.HumanInteraction let private genInteraction = Gen.oneof [ genTimeChangedInteraction; genHumanInteraction ]