Store a custom snapshot of lights

This commit is contained in:
MasseR committed 2026-09-29 22:51:37 +03:00
1 parent fcaccfdeb0
commit fefeae6301
15 files changed
+173 -24

No files matched your search

+75 -4
View File
@@ -2,13 +2,16 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MonadComprehensions #-}
module HomeAssistant.Controller
( Service(..)
, HASSEff(..)
, HASS
, callService
, callServices
, callServiceDyn
, callServicesDyn
, entityChangeEvent
, entityChangeEvent'
, entityRead
@@ -28,6 +31,8 @@ module HomeAssistant.Controller
, Target(..)
, brightness
, Light(..)
, lightSignal
, LightStatus(..)
, ikeaQuickButton
, IkeaButton(..)
, IkeaGesture(..)
@@ -35,11 +40,15 @@ module HomeAssistant.Controller
, activateSceneWith
, ikeaBoxRemote
, IkeaController(..)
, LightSettings(..)
, BrightnessSettings(..)
, formatLightSettings
) where
import AFRP (Mealy (..), eff, Event(..), events, filterA, (>>|), toEvent, Request, hold, lMerge, waitFor, tag, edge)
import Control.Arrow (Arrow(..), returnA)
import Control.Category ((>>>))
import Control.Category ((>>>), (.))
import Prelude hiding ((.))
import Data.Aeson (Value, object, (.=))
import qualified Data.Text as T
import qualified Data.Set as S
@@ -50,6 +59,9 @@ import Data.Bool (bool)
import Data.Serialize (Serialize)
import GHC.Generics (Generic)
import Data.Time (NominalDiffTime)
import Data.Default (Default)
import Data.Aeson.Types (Pair)
import Data.Maybe (catMaybes)
data Target = EntityId !T.Text | AreaId !T.Text
deriving (Show,Eq,Ord)
@@ -64,6 +76,7 @@ data Service = Service
data HASSEff a where
CallService :: Request -> Service -> HASSEff ()
CallServices :: Request -> [Service] -> HASSEff ()
Debug :: Show a => a -> HASSEff ()
Trace :: Show a => Request -> a -> HASSEff ()
@@ -72,9 +85,15 @@ type HASS a b = Mealy HASSEff a b
callService :: Service -> HASS a ()
callService service = eff (\req _ -> CallService req service)
callServices :: [Service] -> HASS a ()
callServices service = eff (\req _ -> CallServices req service)
callServiceDyn :: (a -> Service) -> HASS a ()
callServiceDyn mkService = eff (\req a -> CallService req (mkService a))
callServicesDyn :: (a -> [Service]) -> HASS a ()
callServicesDyn mkService = eff (\req a -> CallServices req (mkService a))
debug :: Show a => HASS a a
debug = proc x -> do
eff (const Debug) -< x
@@ -133,16 +152,44 @@ motionToPresence delay = (eventOccupied &&& eventUnoccupied)
instance Serialize Motion
data BrightnessSettings = BrightnessPercentage Double | Brightness Int
deriving (Show, Generic)
instance Default BrightnessSettings where
data LightSettings = LightSettings
{ lightSettingsBrightness :: Maybe BrightnessSettings
, lightSettingsTemperature :: Maybe Int
, lightSettingsTransition :: Maybe Double
}
deriving (Show, Generic)
instance Default LightSettings 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]
]
where
toObject :: [Pair] -> Maybe Value
toObject [] = Nothing
toObject xs = Just $ object xs
data Light
= Off
| On { brightnessPercentage :: Maybe Double }
| On LightSettings
-- Turn off lights when door is closed
light :: [Target] -> Light -> Service
light targets (On {brightnessPercentage}) = Service
light targets (On settings) = Service
{ serviceDomain="light"
, serviceName= "turn_on"
, serviceData=fmap (\pct -> object ["brightness_pct" .= pct]) brightnessPercentage
-- , serviceData= fmap (\pct -> object ["brightness_pct" .= pct]) brightnessPercentage
, serviceData= formatLightSettings settings
, serviceTarget=targets
}
light targets Off = Service
@@ -199,6 +246,30 @@ brightness entityId =
where
eventBrightness v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "brightness" . _Integral
temperature :: T.Text -> HASS (Event Value) (Event Int)
temperature entityId =
entityChangeEvent' entityId
>>| arr (maybe (Left ()) Right . eventTemperature)
>>> AFRP.toEvent
where
eventTemperature v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "color_temp_kelvin" . _Integral
-- XXX: Needs better name
data LightStatus = LightStatus
{ lightEnabled :: Bool
, lightBrightness :: Int
, lightTemperature :: Int
}
deriving (Show, Generic, Eq)
instance Serialize LightStatus
lightSignal :: T.Text -> HASS (Event Value) LightStatus
lightSignal entityId = proc ev -> do
e <- hold False . entityBool entityId -< ev
b <- hold 0 . brightness entityId -< ev
t <- hold 0 . temperature entityId -< ev
returnA -< LightStatus e b t
data IkeaGesture
= ShortClick
| ShortRelease