A lot of internals as I tried to do a switch based logic
This commit is contained in:
@@ -23,16 +23,18 @@ module HomeAssistant.Controller
|
||||
, traceValue
|
||||
, switch
|
||||
, Target(..)
|
||||
, brightness
|
||||
, Light(..)
|
||||
) where
|
||||
|
||||
import AFRP (Mealy (..), eff, Event(..), events, filterA, (>>|), toEvent, Request)
|
||||
import Control.Arrow (Arrow(..), returnA)
|
||||
import Control.Category ((>>>))
|
||||
import Data.Aeson (Value)
|
||||
import Data.Aeson (Value, object, (.=))
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Set as S
|
||||
import Control.Lens (has, only, (^?), to)
|
||||
import Data.Aeson.Lens (key, _String)
|
||||
import Data.Aeson.Lens (key, _String, _Integral)
|
||||
import qualified Data.Text.Lens as TL
|
||||
import Data.Bool (bool)
|
||||
|
||||
@@ -84,12 +86,21 @@ presence entityId =entityBool entityId
|
||||
|
||||
|
||||
|
||||
data Light
|
||||
= Off
|
||||
| On { brightnessPercentage :: Maybe Double }
|
||||
|
||||
-- Turn off lights when door is closed
|
||||
light :: [Target] -> Bool -> Service
|
||||
light targets b = Service
|
||||
light :: [Target] -> Light -> Service
|
||||
light targets (On {brightnessPercentage}) = Service
|
||||
{ serviceDomain="light"
|
||||
, serviceName= bool "turn_off" "turn_on" b
|
||||
, serviceName= "turn_on"
|
||||
, serviceData=fmap (\pct -> object ["brightness_pct" .= pct]) brightnessPercentage
|
||||
, serviceTarget=targets
|
||||
}
|
||||
light targets Off = Service
|
||||
{ serviceDomain="light"
|
||||
, serviceName= "turn_off"
|
||||
, serviceData=Nothing
|
||||
, serviceTarget=targets
|
||||
}
|
||||
@@ -132,3 +143,11 @@ entityRead entityId = entityRead' entityId >>> toEvent
|
||||
|
||||
entityBool :: T.Text -> Mealy eff (Event Value) (Event Bool)
|
||||
entityBool entityId = entityBool' entityId >>> toEvent
|
||||
|
||||
brightness :: T.Text -> HASS (Event Value) (Event Int)
|
||||
brightness entityId =
|
||||
entityChangeEvent' entityId
|
||||
>>| arr (maybe (Left ()) Right . eventBrightness)
|
||||
>>> AFRP.toEvent
|
||||
where
|
||||
eventBrightness v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "brightness" . _Integral
|
||||
|
||||
Reference in New Issue
Block a user