From 2f6e46960f626f3378f9dc8ba813f878bea0442c Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Sat, 10 Jan 2026 21:04:20 +0100 Subject: [PATCH] Spam a bit less --- NightLight.Core/NightLightStateMachine.fs | 10 +++++++--- NightLight.Core/ZigbeeCommands.fs | 11 ++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/NightLight.Core/NightLightStateMachine.fs b/NightLight.Core/NightLightStateMachine.fs index 94eef76..4de831d 100644 --- a/NightLight.Core/NightLightStateMachine.fs +++ b/NightLight.Core/NightLightStateMachine.fs @@ -11,15 +11,19 @@ open FsToolkit.ErrorHandling let internal tryFindLight friendlyName = Seq.tryFind (fun light -> light.FriendlyName = friendlyName) lights -let internal generateZigbeeCommandsToFixLight state partOfDay light = +let internal generateZigbeeCommandsToFixLight state partOfDay (light: Light) = seq { - yield generateStateCommand state light + match light.ControlledWithRemote, state with + | true, _ -> yield generateStateCommand state light + | false, On -> () + | false, Off -> failwith $"Unexpectly trying to turn off {light}. It's not remote-controlled." if state = On then let color, brightness = getDesiredMood light.Room partOfDay |> getDesiredColorAndBrightness light.Bulb - yield generateZigbeeCommand color brightness light + yield generateColorCommand light color + yield generateBrightnessCommand light brightness } type NightLightStateMachine private (maybeTime: DateTime option, lightToState: Map) = diff --git a/NightLight.Core/ZigbeeCommands.fs b/NightLight.Core/ZigbeeCommands.fs index 955c041..0e16a6e 100644 --- a/NightLight.Core/ZigbeeCommands.fs +++ b/NightLight.Core/ZigbeeCommands.fs @@ -25,10 +25,10 @@ let generateStateCommand state light = commandObj.ToJsonString() |> toZigbeeCommand light -let generateZigbeeCommand targetColor targetBrightness light = +let internal generateColorCommand light color = let commandObj = JsonObject() - match targetColor with + match color with | ColorByCoordinates(x, y) -> let colorObj = JsonObject() colorObj["x"] <- x @@ -36,8 +36,13 @@ let generateZigbeeCommand targetColor targetBrightness light = commandObj["color"] <- colorObj | ColorByTemperature t -> commandObj["color_temp"] <- t + commandObj.ToJsonString() |> toZigbeeCommand light + +let internal generateBrightnessCommand light brightness = + let commandObj = JsonObject() + commandObj["brightness"] <- - match targetBrightness with + match brightness with | Brightness b -> b commandObj.ToJsonString() |> toZigbeeCommand light