Give every light its own type

This commit is contained in:
Sven van Heugten 2026-02-27 17:49:20 +01:00
parent dbb0389e61
commit 50c8a413a6
6 changed files with 72 additions and 50 deletions

View file

@ -46,32 +46,52 @@ type LightControl =
| RemoteRight
type Light =
| VardagsrumFonsterlampa
| VardagsrumVagglampa
| VardagsrumGolvlampa
| BadrumTaklampa
| SovrumNattduksbordlampa
type LightProps =
{ FriendlyName: DeviceFriendlyName
Room: Room
Bulb: Bulb
ControlledWithRemote: LightControl }
let lightProps light =
match light with
| VardagsrumFonsterlampa ->
{ FriendlyName = DeviceFriendlyName "Vardagsrum - Fönsterlampa"
Room = Bedroom
Bulb = IkeaBulb
ControlledWithRemote = RemoteRight }
| VardagsrumVagglampa ->
{ FriendlyName = DeviceFriendlyName "Vardagsrum - Vägglampa"
Room = LivingRoom
Bulb = PaulmannBulb
ControlledWithRemote = NonRemote }
| VardagsrumGolvlampa ->
{ FriendlyName = DeviceFriendlyName "Vardagsrum - Golvlampa"
Room = LivingRoom
Bulb = PaulmannBulb
ControlledWithRemote = NonRemote }
| BadrumTaklampa ->
{ FriendlyName = DeviceFriendlyName "Badrum - Taklampa"
Room = Bathroom
Bulb = IkeaBulb
ControlledWithRemote = NonRemote }
| SovrumNattduksbordlampa ->
{ FriendlyName = DeviceFriendlyName "Sovrum - Nattduksbordlampa"
Room = Bedroom
Bulb = IkeaBulb
ControlledWithRemote = RemoteLeft }
let lights =
[ { FriendlyName = DeviceFriendlyName "Vardagsrum - Fönsterlampa"
Room = Bedroom
Bulb = IkeaBulb
ControlledWithRemote = RemoteRight }
{ FriendlyName = DeviceFriendlyName "Vardagsrum - Vägglampa"
Room = LivingRoom
Bulb = PaulmannBulb
ControlledWithRemote = NonRemote }
{ FriendlyName = DeviceFriendlyName "Vardagsrum - Golvlampa"
Room = LivingRoom
Bulb = PaulmannBulb
ControlledWithRemote = NonRemote }
{ FriendlyName = DeviceFriendlyName "Badrum - Taklampa"
Room = Bathroom
Bulb = IkeaBulb
ControlledWithRemote = NonRemote }
{ FriendlyName = DeviceFriendlyName "Sovrum - Nattduksbordlampa"
Room = Bedroom
Bulb = IkeaBulb
ControlledWithRemote = RemoteLeft } ]
[ VardagsrumFonsterlampa
VardagsrumVagglampa
VardagsrumGolvlampa
BadrumTaklampa
SovrumNattduksbordlampa ]
let remoteControlFriendlyName = DeviceFriendlyName "Fjärrkontroll"

View file

@ -9,14 +9,14 @@ open NightLight.Core.Moods
open FsToolkit.ErrorHandling
let internal tryFindLight friendlyName =
Seq.tryFind (fun light -> light.FriendlyName = friendlyName) lights
Seq.tryFind (fun light -> (lightProps light).FriendlyName = friendlyName) lights
let internal generateZigbeeCommandsToFixLight (light: Light) (desiredLightState: LightState) =
seq {
match light.ControlledWithRemote, desiredLightState.State with
match (lightProps light).ControlledWithRemote, desiredLightState.State with
| NonRemote, On -> ()
| NonRemote, Off -> failwith $"Unexpectly trying to turn off {light}. It's not remote-controlled."
| _, On when light.Bulb = IkeaBulb -> () // Rely on the brightness command for turning it on
| _, On when (lightProps light).Bulb = IkeaBulb -> () // Rely on the brightness command for turning it on
| _, _ -> yield generateStateCommand desiredLightState.State light
if desiredLightState.State = On then
@ -40,7 +40,8 @@ let internal createOrUpdateNightLightState
lights
|> Seq.map (fun light ->
let color, brightness =
getDesiredMood light.Room partOfDay |> getDesiredColorAndBrightness light.Bulb
getDesiredMood (lightProps light).Room partOfDay
|> getDesiredColorAndBrightness (lightProps light).Bulb
let previousState =
maybeOldLightToState
@ -50,7 +51,7 @@ let internal createOrUpdateNightLightState
light,
{ Color = color
Brightness =
if alarm && light.ControlledWithRemote <> NonRemote then
if alarm && (lightProps light).ControlledWithRemote <> NonRemote then
brightness.Scale(getAlarmWeight time)
else
brightness
@ -72,7 +73,7 @@ let internal withStateFor (light: Light) (state: State) (oldNightLightState: Nig
let internal withStateForRemoteControlledLights (state: State) (oldNightLightState: NightLightState) =
lights
|> Seq.filter (not << _.ControlledWithRemote.IsNonRemote)
|> Seq.filter (fun light -> (lightProps light).ControlledWithRemote.IsNonRemote |> not)
|> Seq.fold (fun acc light -> acc |> withStateFor light state) oldNightLightState
let internal withAlarmOff (oldNightLightState: NightLightState) =
@ -113,7 +114,8 @@ type NightLightStateMachine private (maybeState: NightLightState option) =
| PressedOff -> currentState |> withAlarmOff |> withStateForRemoteControlledLights Off
| PressedLeft ->
let lightThatShouldBeOn =
lights |> Seq.find (fun light -> light.ControlledWithRemote = RemoteLeft)
lights
|> Seq.find (fun light -> (lightProps light).ControlledWithRemote = RemoteLeft)
currentState
|> withAlarmOff

View file

@ -4,7 +4,7 @@ open System.Text.Json.Nodes
open NightLight.Core.Models
let toZigbeeCommand light payload =
let topic = $"zigbee2mqtt/{light.FriendlyName.Get}/set"
let topic = $"zigbee2mqtt/{(lightProps light).FriendlyName.Get}/set"
{ Topic = topic; Payload = payload }
let generateStateCommand state light =
@ -15,7 +15,7 @@ let generateStateCommand state light =
| On -> "ON"
| Off -> "OFF"
if light.Bulb = IkeaBulb then
if (lightProps light).Bulb = IkeaBulb then
commandObj["transition"] <- 0
commandObj.ToJsonString() |> toZigbeeCommand light