diff --git a/test/HallwaySpec.hs b/test/HallwaySpec.hs index 2813c9b..5ac27ff 100644 --- a/test/HallwaySpec.hs +++ b/test/HallwaySpec.hs @@ -22,9 +22,14 @@ 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 = sec 36000 -- 10:00, outside sleep hours -night = sec 75600 -- 21:00, inside sleep hours (20:30 - 07:30) +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 @@ -34,37 +39,44 @@ spec = describe "Hallway" $ do 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" + 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" $ - services (runHASSTimed hallwayLightsController [(day, motionSensor "on")]) - `shouldBe` [[light [EntityId "light.hallway_lights"] (On def)]] + -- 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, motionSensor "on") + [ (day, Tick) + , (day, motionSensor "on") , (day, motionSensor "off") , (later, Tick) ]) - `shouldBe` [ [light [EntityId "light.hallway_lights"] (On def)] + `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, Tick) + , (night, masterDoor "on") , (night, motionSensor "on") ]) `shouldBe` [ [] + , [] , [light [EntityId "light.hallway_ceiling_2"] (On def)] ]