From 2039d03a5e1cf7b666841dbf4b9fae90063ef64f Mon Sep 17 00:00:00 2001 From: Mats Rauhala Date: Mon, 2 Nov 2026 18:13:31 +0200 Subject: [PATCH] Add failing spec for hallway lights automation --- home-assistant-controller.cabal | 1 + test/HallwaySpec.hs | 95 +++++++++++++++++++++++++++++++++ test/Main.hs | 2 + 3 files changed, 98 insertions(+) create mode 100644 test/HallwaySpec.hs diff --git a/home-assistant-controller.cabal b/home-assistant-controller.cabal index c986e26..b139ea7 100644 --- a/home-assistant-controller.cabal +++ b/home-assistant-controller.cabal @@ -182,6 +182,7 @@ test-suite home-assistant-controller-test , ConnectionSpec , FlagsSpec , GraphingSpec + , HallwaySpec , HttpServerSpec , KitchenSpec , LivingroomSpec diff --git a/test/HallwaySpec.hs b/test/HallwaySpec.hs new file mode 100644 index 0000000..2813c9b --- /dev/null +++ b/test/HallwaySpec.hs @@ -0,0 +1,95 @@ +{-# 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)] + ] diff --git a/test/Main.hs b/test/Main.hs index 88ee50c..0abedd5 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -8,6 +8,7 @@ import qualified BusSpec import qualified ConnectionSpec import qualified FlagsSpec import qualified GraphingSpec +import qualified HallwaySpec import qualified HttpServerSpec import qualified KitchenSpec import qualified LivingroomSpec @@ -24,6 +25,7 @@ main = hspec $ do ConnectionSpec.spec FlagsSpec.spec GraphingSpec.spec + HallwaySpec.spec HttpServerSpec.spec KitchenSpec.spec LivingroomSpec.spec