75 lines
2.6 KiB
Haskell
75 lines
2.6 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE Arrows #-}
|
|
module HomeAssistant.Controller.Kitchen where
|
|
import HomeAssistant.Controller
|
|
import AFRP (Event (..), (>>||))
|
|
import qualified AFRP
|
|
import Data.Aeson (Value)
|
|
import Control.Arrow ((>>>), Arrow (..), returnA)
|
|
import Prelude hiding (id)
|
|
import Data.Time (LocalTime(..), TimeOfDay (..))
|
|
import Data.Default (def)
|
|
|
|
|
|
-- Kitchen has two "presence" sensors. One IKEA motion sensor and one SwitchBot presence sensor
|
|
|
|
kitchenPresence :: HASS (Event Value) Presence
|
|
kitchenPresence = motion "binary_sensor.kitchen_movement_occupancy" >>> motionToPresence 500
|
|
|
|
kitchenCeilingController :: HASS (Event Value) ()
|
|
kitchenCeilingController = proc x -> do
|
|
now <- AFRP.currentTime -< ()
|
|
p <- kitchenPresence -< x
|
|
ev <- lightingModeEvents -< p
|
|
traceEvent -< ev
|
|
case ev of
|
|
Event LightingOn | lightsAllowed now -> callServiceDyn "light.kitchen_ceiling on" (light [EntityId "light.kitchen_ceiling"]) -< On def
|
|
Event LightingOff -> callServiceDyn "light.kitchen_ceiling off" (light [EntityId "light.kitchen_ceiling"]) -< Off
|
|
_ -> returnA -< ()
|
|
where
|
|
lightsAllowed (LocalTime _ tod) = not (tod > TimeOfDay 1 45 0 && tod < TimeOfDay 5 0 0)
|
|
|
|
|
|
dinnerLightLevel :: HASS (Event Value) Int
|
|
dinnerLightLevel = entityRead "sensor.sijainti_keittio_light_level" >>> AFRP.hold 0
|
|
|
|
dinnerPresence :: HASS (Event Value) Presence
|
|
dinnerPresence = presence "binary_sensor.sijainti_keittio_occupancy" >>> AFRP.hold Unoccupied
|
|
|
|
|
|
dinnerTableController :: HASS (Event Value) ()
|
|
dinnerTableController =
|
|
((,) <$> dinnerLightLevel <*> dinnerPresence)
|
|
>>> arr lightWanted
|
|
-- These are done "in parallel", there is a chance that two services are called in one go
|
|
>>> mconcat
|
|
[ AFRP.named "Lights on" lightsOn
|
|
, AFRP.named "Lights sleep" lightsSleep
|
|
, AFRP.named "Lights off" lightsOff ]
|
|
>>> arr (const ()) -- I don't like this extra arr here
|
|
where
|
|
lightWanted (lightLevel, occ) = occ == Occupied && lightLevel < 13
|
|
|
|
lightsOn =
|
|
AFRP.edge
|
|
>>|| sleep "sleep off" False
|
|
>>> callService "Dining light on" (light [EntityId "light.kitchen_dining_large"] $ On def)
|
|
|
|
lightsSleep =
|
|
arr not
|
|
>>> AFRP.edge
|
|
>>|| sleep "sleep on" True
|
|
|
|
lightsOff =
|
|
arr not
|
|
>>> AFRP.waitFor 300
|
|
>>|| sleep "sleep off" False
|
|
>>> callService "Dining light on" (light [EntityId "light.kitchen_dining_large"] Off)
|
|
|
|
|
|
sleep lbl b = callService lbl (switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] b)
|
|
|
|
|
|
kitchenMotionController :: HASS (Event Value) ()
|
|
kitchenMotionController = kitchenCeilingController <> dinnerTableController
|