Files
home-assistant-controller/test/HallwaySpec.hs
T

96 lines
3.4 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module HallwaySpec (spec) where
import AFRP (Event (..), entities)
import qualified Data.Set as S
import qualified Data.Text as T
import Data.Aeson (Value)
import Data.Default (def)
import Data.Time (UTCTime, addUTCTime)
import HomeAssistant.Controller
import HomeAssistant.Controller.Hallway
import Support
import Test.Hspec
motionSensor :: T.Text -> Event Value
motionSensor = Event . stateEvent "binary_sensor.hallway_movement_occupancy"
masterDoor :: T.Text -> Event Value
masterDoor = Event . stateEvent "binary_sensor.makuuhuone_ovi_contact"
childrenDoor :: T.Text -> Event Value
childrenDoor = Event . stateEvent "binary_sensor.lasten_makuuhuone_ovi_contact"
day, night :: UTCTime
day = sec 36000 -- 10:00, outside sleep hours
night = sec 75600 -- 21:00, inside sleep hours (20:30 - 07:30)
spec :: Spec
spec = describe "Hallway" $ do
entitySpec
lightsSpec
entitySpec :: Spec
entitySpec = describe "hallwayLightsController" $
it "subscribes to motion, both doors and the hallway lights" $
S.toList (entities hallwayLightsController)
`shouldContain`
[ "binary_sensor.hallway_movement_occupancy"
, "binary_sensor.makuuhuone_ovi_contact"
, "binary_sensor.lasten_makuuhuone_ovi_contact"
]
lightsSpec :: Spec
lightsSpec = describe "hallwayLightsController" $ do
it "turns the full lights on when someone enters during daytime" $
services (runHASSTimed hallwayLightsController [(day, motionSensor "on")])
`shouldBe` [[light [EntityId "light.hallway_lights"] (On def)]]
it "turns the lights off when everyone leaves, without sleep mode" $
let later = 505 `addUTCTime` day
in services (runHASSTimed hallwayLightsController
[ (day, motionSensor "on")
, (day, motionSensor "off")
, (later, Tick)
])
`shouldBe` [ [light [EntityId "light.hallway_lights"] (On def)]
, []
, [light [EntityId "light.hallway_lights"] Off]
]
it "turns on only the ceiling light when entering during sleep hours with a door open" $
services (runHASSTimed hallwayLightsController
[ (night, masterDoor "on")
, (night, motionSensor "on")
])
`shouldBe` [ []
, [light [EntityId "light.hallway_ceiling_2"] (On def)]
]
it "sets the lower hallway light off when a door is opened during sleep hours" $
services (runHASSTimed hallwayLightsController
[ (night, masterDoor "off")
, (night, childrenDoor "off")
, (night, motionSensor "on")
, (night, masterDoor "on")
])
`shouldBe` [ []
, []
, [light [EntityId "light.hallway_lights"] (On def)]
, [light [EntityId "light.hallway_ceiling"] Off]
]
it "turns the lower hallway light on when the doors close while presence is active" $
services (runHASSTimed hallwayLightsController
[ (night, masterDoor "on")
, (night, motionSensor "on")
, (night, masterDoor "off")
, (night, childrenDoor "off")
])
`shouldBe` [ []
, [light [EntityId "light.hallway_ceiling_2"] (On def)]
, []
, [light [EntityId "light.hallway_ceiling"] (On def)]
]