Add failing spec for hallway lights automation

This commit is contained in:
MasseR committed 2026-11-02 18:13:31 +02:00
1 parent 868a5d7e4f
commit 2039d03a5e
3 files changed
+98

No files matched your search

+1
View File
@@ -182,6 +182,7 @@ test-suite home-assistant-controller-test
, ConnectionSpec , ConnectionSpec
, FlagsSpec , FlagsSpec
, GraphingSpec , GraphingSpec
, HallwaySpec
, HttpServerSpec , HttpServerSpec
, KitchenSpec , KitchenSpec
, LivingroomSpec , LivingroomSpec
+95
View File
@@ -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)]
]
+2
View File
@@ -8,6 +8,7 @@ import qualified BusSpec
import qualified ConnectionSpec import qualified ConnectionSpec
import qualified FlagsSpec import qualified FlagsSpec
import qualified GraphingSpec import qualified GraphingSpec
import qualified HallwaySpec
import qualified HttpServerSpec import qualified HttpServerSpec
import qualified KitchenSpec import qualified KitchenSpec
import qualified LivingroomSpec import qualified LivingroomSpec
@@ -24,6 +25,7 @@ main = hspec $ do
ConnectionSpec.spec ConnectionSpec.spec
FlagsSpec.spec FlagsSpec.spec
GraphingSpec.spec GraphingSpec.spec
HallwaySpec.spec
HttpServerSpec.spec HttpServerSpec.spec
KitchenSpec.spec KitchenSpec.spec
LivingroomSpec.spec LivingroomSpec.spec