Use state to steer color and brightness

This commit is contained in:
Sven van Heugten 2026-01-17 15:35:27 +01:00
parent 437e5bf25d
commit 45210c30d4

View file

@ -11,19 +11,16 @@ open FsToolkit.ErrorHandling
let internal tryFindLight friendlyName = let internal tryFindLight friendlyName =
Seq.tryFind (fun light -> light.FriendlyName = friendlyName) lights Seq.tryFind (fun light -> light.FriendlyName = friendlyName) lights
let internal generateZigbeeCommandsToFixLight state partOfDay (light: Light) = let internal generateZigbeeCommandsToFixLight state (light: Light) =
seq { seq {
match light.ControlledWithRemote, state with match light.ControlledWithRemote, state.State with
| NonRemote, On -> () | NonRemote, On -> ()
| NonRemote, Off -> failwith $"Unexpectly trying to turn off {light}. It's not remote-controlled." | NonRemote, Off -> failwith $"Unexpectly trying to turn off {light}. It's not remote-controlled."
| _, _ -> yield generateStateCommand state light | _, _ -> yield generateStateCommand state.State light
if state = On then if state.State = On then
let color, brightness = yield generateColorCommand light state.Color
getDesiredMood light.Room partOfDay |> getDesiredColorAndBrightness light.Bulb yield generateBrightnessCommand light state.Brightness
yield generateColorCommand light color
yield generateBrightnessCommand light brightness
} }
type internal NightLightState = type internal NightLightState =
@ -52,7 +49,6 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
| ReceivedZigbeeEvent payload, | ReceivedZigbeeEvent payload,
Some { Time = time Some { Time = time
LightToState = lightToState } -> LightToState = lightToState } ->
let partOfDay = time |> getPartOfDay
let! zigbeeEvent = parseZigbeeEvent payload |> Result.mapError ParseZigbeeEventError let! zigbeeEvent = parseZigbeeEvent payload |> Result.mapError ParseZigbeeEventError
return return
@ -62,7 +58,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
this, this,
match maybeLight with match maybeLight with
| Some light -> generateZigbeeCommandsToFixLight lightToState[light].State partOfDay light | Some light -> generateZigbeeCommandsToFixLight lightToState[light] light
| None -> Seq.empty | None -> Seq.empty
| ButtonPress action -> | ButtonPress action ->
let newLightToState = let newLightToState =
@ -85,8 +81,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
LightToState = newLightToState } LightToState = newLightToState }
), ),
remoteControlledLights remoteControlledLights
|> Seq.collect (fun light -> |> Seq.collect (fun light -> generateZigbeeCommandsToFixLight newLightToState[light] light)
generateZigbeeCommandsToFixLight newLightToState[light].State partOfDay light)
| TimeChanged newTime, maybeState -> | TimeChanged newTime, maybeState ->
let newPartOfDay = getPartOfDay newTime let newPartOfDay = getPartOfDay newTime
@ -129,8 +124,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
newState, newState,
if partOfDayChanged then if partOfDayChanged then
lights lights
|> Seq.collect (fun light -> |> Seq.collect (fun light -> generateZigbeeCommandsToFixLight newLightToState[light] light)
generateZigbeeCommandsToFixLight newLightToState[light].State newPartOfDay light)
else else
Seq.empty Seq.empty
| _, None -> return! Error TimeIsUnknown | _, None -> return! Error TimeIsUnknown