108 lines
3.9 KiB
Haskell
108 lines
3.9 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"
|
|
|
|
-- runHASSTimed builds its request with the UTC zone, so a UTCTime given as
|
|
-- seconds-of-day reads back as exactly that local time of day
|
|
clock :: Int -> Int -> UTCTime
|
|
clock h m = sec (fromIntegral (h * 60 + m) * 60)
|
|
|
|
day, night :: UTCTime
|
|
day = clock 10 0 -- 10:00, outside sleep hours
|
|
night = clock 21 0 -- 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" $
|
|
entities hallwayLightsController
|
|
`shouldBe`
|
|
S.fromList [ "binary_sensor.hallway_movement_occupancy"
|
|
, "binary_sensor.makuuhuone_ovi_contact"
|
|
, "binary_sensor.lasten_makuuhuone_ovi_contact"
|
|
, "light.hallway_ceiling"
|
|
, "light.hallway_ceiling_2"
|
|
]
|
|
|
|
lightsSpec :: Spec
|
|
lightsSpec = describe "hallwayLightsController" $ do
|
|
it "turns the full lights on when someone enters during daytime" $
|
|
-- it uses the 'changes' combinator which means that the state must equivalize first hence the tick
|
|
services (runHASSTimed hallwayLightsController [(day, Tick), (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, Tick)
|
|
, (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, Tick)
|
|
, (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)]
|
|
]
|