{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE Arrows #-} module HomeAssistant.Controller.Livingroom where import HomeAssistant.Controller (HASS, Presence (..), Target (..), presence, callServiceDyn, light, Light (..), traceEvent) import qualified AFRP import Control.Arrow ((>>>), Arrow (..), returnA) import Data.Aeson (Value) import Prelude hiding ((.)) import Control.Category ((.)) data Lights = LightsOn | LightsOff deriving (Show) livingroomPresence :: HASS (AFRP.Event Value) Presence livingroomPresence = presence "binary_sensor.occupancy_olohuone_derived" >>> AFRP.hold Unoccupied eventLights :: HASS Presence (AFRP.Event Lights) eventLights = AFRP.changes >>> arr (fmap presenceLights) where presenceLights Occupied = LightsOn presenceLights Unoccupied = LightsOff -- There's a small race condition / corner case bug in the og implementation -- I have a couple of lights in the Livingroom -- -- - Ceiling ilght -- - "Sofa light", a small reading nook light -- - A few strong lights at a plant corner -- -- The plant corner lights are not started by the automation. They are started by a separate schedule -- But, they are turned off by the automation -- So their status gets messed if their schedule turns on while there is nobody in the room -- -- -- With that in mind, I'll be explicitly controlling the two lights, without any scene shenanigans lights :: [Target] lights = [EntityId "light.livingroom_ceiling", EntityId "light.livingroom_sofa"] livingroomPresenceController :: HASS (AFRP.Event Value) () livingroomPresenceController = proc x -> do ev <- eventLights . livingroomPresence -< x traceEvent -< ev case ev of AFRP.Event LightsOn -> callServiceDyn (light lights) -< On Nothing AFRP.Event LightsOff -> callServiceDyn (light lights) -< Off _ -> returnA -< () returnA -< ()