Rename light command layer (Light, LightSettings, BrightnessSettings)

This commit is contained in:
MasseR committed 2026-09-29 23:14:07 +03:00
1 parent fefeae6301
commit 7ce994e814
6 files changed
+36 -36

No files matched your search

+23 -23
View File
@@ -3,6 +3,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MonadComprehensions #-}
{-# LANGUAGE DuplicateRecordFields #-}
module HomeAssistant.Controller
( Service(..)
@@ -30,7 +31,7 @@ module HomeAssistant.Controller
, switch
, Target(..)
, brightness
, Light(..)
, LightCommand(..)
, lightSignal
, LightStatus(..)
, ikeaQuickButton
@@ -40,9 +41,9 @@ module HomeAssistant.Controller
, activateSceneWith
, ikeaBoxRemote
, IkeaController(..)
, LightSettings(..)
, BrightnessSettings(..)
, formatLightSettings
, LightAttributes(..)
, Brightness(..)
, formatLightAttributes
) where
import AFRP (Mealy (..), eff, Event(..), events, filterA, (>>|), toEvent, Request, hold, lMerge, waitFor, tag, edge)
@@ -152,44 +153,43 @@ motionToPresence delay = (eventOccupied &&& eventUnoccupied)
instance Serialize Motion
data BrightnessSettings = BrightnessPercentage Double | Brightness Int
deriving (Show, Generic)
instance Default BrightnessSettings where
data Brightness = BrightnessPercent Double | BrightnessAbsolute Int
deriving (Show, Generic)
instance Default Brightness where
data LightSettings = LightSettings
{ lightSettingsBrightness :: Maybe BrightnessSettings
, lightSettingsTemperature :: Maybe Int
, lightSettingsTransition :: Maybe Double
data LightAttributes = LightAttributes
{ lightBrightness :: Maybe Brightness
, lightColorTemperature :: Maybe Int
, lightTransition :: Maybe Double
}
deriving (Show, Generic)
instance Default LightSettings where
instance Default LightAttributes where
formatLightSettings :: LightSettings -> Maybe Value
formatLightSettings s = toObject $ catMaybes
[ ["transition" .= x | x <- lightSettingsTransition s]
, ["color_temp_kelvin" .= k | k <- lightSettingsTemperature s]
, ["brightness" .= b | Brightness b <- lightSettingsBrightness s]
, ["brightness_pct" .= b | BrightnessPercentage b <- lightSettingsBrightness s]
formatLightAttributes :: LightAttributes -> Maybe Value
formatLightAttributes LightAttributes{lightBrightness, lightColorTemperature, lightTransition} = toObject $ catMaybes
[ ["transition" .= x | x <- lightTransition]
, ["color_temp_kelvin" .= k | k <- lightColorTemperature]
, ["brightness" .= b | BrightnessAbsolute b <- lightBrightness]
, ["brightness_pct" .= b | BrightnessPercent b <- lightBrightness]
]
where
toObject :: [Pair] -> Maybe Value
toObject [] = Nothing
toObject xs = Just $ object xs
data Light
data LightCommand
= Off
| On LightSettings
| On LightAttributes
-- Turn off lights when door is closed
light :: [Target] -> Light -> Service
light :: [Target] -> LightCommand -> Service
light targets (On settings) = Service
{ serviceDomain="light"
, serviceName= "turn_on"
-- , serviceData= fmap (\pct -> object ["brightness_pct" .= pct]) brightnessPercentage
, serviceData= formatLightSettings settings
, serviceData= formatLightAttributes settings
, serviceTarget=targets
}
light targets Off = Service