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
| ColorByTemperature of int
type internal LightState =
type internal LightSettings =
{ State: State
Brightness: Brightness
Color: Color }

View file

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