LightState -> LightSettings

This commit is contained in:
Sven van Heugten 2026-03-15 10:50:22 +01:00
parent 53fd129bb8
commit 3507385d6c
2 changed files with 21 additions and 21 deletions

View file

@ -98,7 +98,7 @@ type internal Color =
| ColorByCoordinates of float * float | ColorByCoordinates of float * float
| ColorByTemperature of int | ColorByTemperature of int
type internal LightState = type internal LightSettings =
{ State: State { State: State
Brightness: Brightness Brightness: Brightness
Color: Color } Color: Color }

View file

@ -11,29 +11,29 @@ open FsToolkit.ErrorHandling
let internal tryFindLight friendlyName = let internal tryFindLight friendlyName =
Seq.tryFind (fun light -> (lightProps light).FriendlyName = friendlyName) lights Seq.tryFind (fun light -> (lightProps light).FriendlyName = friendlyName) lights
let internal generateZigbeeCommandsToFixLight (light: Light) (desiredLightState: LightState) = let internal generateZigbeeCommandsToFixLight (light: Light) (desiredLightSettings: LightSettings) =
seq { seq {
if desiredLightState.State = Off then if desiredLightSettings.State = Off then
yield generateStateCommand desiredLightState.State light yield generateStateCommand desiredLightSettings.State light
if desiredLightState.State = On then if desiredLightSettings.State = On then
yield generateBrightnessCommand light desiredLightState.Brightness yield generateBrightnessCommand light desiredLightSettings.Brightness
yield generateColorCommand light desiredLightState.Color yield generateColorCommand light desiredLightSettings.Color
} }
type internal NightLightState = type internal NightLightState =
{ Time: DateTime { Time: DateTime
Alarm: bool Alarm: bool
LightToState: Map<Light, LightState> } LightToLightSettings: Map<Light, LightSettings> }
let internal createOrUpdateNightLightState let internal createOrUpdateNightLightState
(time: DateTime) (time: DateTime)
(alarm: bool) (alarm: bool)
(maybeOldLightToState: Map<Light, LightState> option) (maybeOldLightToLightSettings: Map<Light, LightSettings> option)
= =
let partOfDay = getPartOfDay time let partOfDay = getPartOfDay time
let lightToState = let lightToLightSettings =
lights lights
|> Seq.map (fun light -> |> Seq.map (fun light ->
let color, brightness = let color, brightness =
@ -41,8 +41,8 @@ let internal createOrUpdateNightLightState
|> getDesiredColorAndBrightness (lightProps light).Bulb |> getDesiredColorAndBrightness (lightProps light).Bulb
let previousState = let previousState =
maybeOldLightToState maybeOldLightToLightSettings
|> Option.map (fun lightToState -> lightToState[light].State) |> Option.map (fun lightToLightSettings -> lightToLightSettings[light].State)
|> Option.defaultValue On |> Option.defaultValue On
light, light,
@ -61,27 +61,27 @@ let internal createOrUpdateNightLightState
{ Time = time { Time = time
Alarm = alarm Alarm = alarm
LightToState = lightToState } LightToLightSettings = lightToLightSettings }
let internal withStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) = let internal withStateFor (light: Light) (state: State) (oldNightLightState: NightLightState) =
let oldState = oldNightLightState.LightToState[light] let oldState = oldNightLightState.LightToLightSettings[light]
createOrUpdateNightLightState createOrUpdateNightLightState
oldNightLightState.Time oldNightLightState.Time
oldNightLightState.Alarm oldNightLightState.Alarm
(Map.add light { oldState with State = state } oldNightLightState.LightToState (Map.add light { oldState with State = state } oldNightLightState.LightToLightSettings
|> Some) |> Some)
let internal withAlarmOff (oldNightLightState: NightLightState) = let internal withAlarmOff (oldNightLightState: NightLightState) =
createOrUpdateNightLightState oldNightLightState.Time false (Some oldNightLightState.LightToState) createOrUpdateNightLightState oldNightLightState.Time false (Some oldNightLightState.LightToLightSettings)
let internal generateZigbeeCommandsForDifference (maybeBefore: NightLightState option) (after: NightLightState) = let internal generateZigbeeCommandsForDifference (maybeBefore: NightLightState option) (after: NightLightState) =
after.LightToState after.LightToLightSettings
|> Seq.collect (fun (KeyValue(light, newState)) -> |> Seq.collect (fun (KeyValue(light, newState)) ->
let oldState = maybeBefore |> Option.map _.LightToState[light] let oldState = maybeBefore |> Option.map _.LightToLightSettings[light]
if oldState <> Some newState then if oldState <> Some newState then
generateZigbeeCommandsToFixLight light after.LightToState[light] generateZigbeeCommandsToFixLight light after.LightToLightSettings[light]
else else
Seq.empty) Seq.empty)
@ -101,7 +101,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
this, this,
match maybeLight with match maybeLight with
| Some light -> generateZigbeeCommandsToFixLight light currentState.LightToState[light] | Some light -> generateZigbeeCommandsToFixLight light currentState.LightToLightSettings[light]
| None -> Seq.empty | None -> Seq.empty
| ButtonPress action -> | ButtonPress action ->
let newNightLightState = let newNightLightState =
@ -143,7 +143,7 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
|| maybeCurrentState |> Option.map _.Alarm |> Option.defaultValue false || maybeCurrentState |> Option.map _.Alarm |> Option.defaultValue false
let newNightLightState = let newNightLightState =
createOrUpdateNightLightState newTime alarm (maybeCurrentState |> Option.map _.LightToState) createOrUpdateNightLightState newTime alarm (maybeCurrentState |> Option.map _.LightToLightSettings)
return return
NightLightStateMachine(Some newNightLightState), NightLightStateMachine(Some newNightLightState),