Much improved hallway handling
This commit is contained in:
1 parent
95fcc61c4f
commit
6e35445090
9 files changed
+354
-100
No files matched your search
+177
-73
@@ -2,16 +2,24 @@
|
||||
|
||||
module HallwaySpec (spec) where
|
||||
|
||||
import AFRP (Event (..), entities)
|
||||
import qualified Data.Set as S
|
||||
import AFRP (Event (..))
|
||||
import qualified Data.Text as T
|
||||
import Data.Aeson (Value)
|
||||
import Data.Bool (bool)
|
||||
import Data.Default (def)
|
||||
import Data.List (scanl')
|
||||
import Data.Time (UTCTime, addUTCTime)
|
||||
import HomeAssistant.Controller
|
||||
import HomeAssistant.Controller.Hallway
|
||||
import Support
|
||||
import Test.Hspec
|
||||
import Test.Hspec.Hedgehog
|
||||
import qualified Hedgehog.Gen as Gen
|
||||
import qualified Hedgehog.Range as Range
|
||||
|
||||
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)
|
||||
|
||||
motionSensor :: T.Text -> Event Value
|
||||
motionSensor = Event . stateEvent "binary_sensor.hallway_movement_occupancy"
|
||||
@@ -22,86 +30,182 @@ 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)
|
||||
-- Same shape as the generator in PresenceSpec: mostly short gaps (< 500s
|
||||
-- timeout), occasionally a 600s gap so presence can expire and the room can
|
||||
-- become unoccupied again.
|
||||
data TestEvent = TestEvent Int (Event Bool)
|
||||
deriving Show
|
||||
|
||||
genTestEvent :: Gen TestEvent
|
||||
genTestEvent = TestEvent
|
||||
<$> Gen.frequency [(8, Gen.integral (Range.linear 0 20)), (2, pure 600)]
|
||||
<*> Gen.frequency [(3, pure Tick), (1, Event <$> Gen.bool)]
|
||||
|
||||
-- | Presence and both of its presence-derived triggers, observed in lock-step
|
||||
-- over the same motion stream, so the triggers can be judged against the
|
||||
-- intermediate presence value at each step.
|
||||
presenceProbe :: HASS (Event Value) (Presence, Event (), Event ())
|
||||
presenceProbe = (,,) <$> hallwayPresence <*> lightsOnTrigger <*> lightsOffTrigger
|
||||
|
||||
-- | Expected firing steps: every step that *enters* `target`. The implicit
|
||||
-- state before the first step is Unoccupied — the room starts unoccupied —
|
||||
-- so an Unoccupied-trigger never fires at the first step.
|
||||
edgeSteps :: Presence -> [Presence] -> [Int]
|
||||
edgeSteps target ps =
|
||||
[ i
|
||||
| (i, (prev, p)) <- zip [0 ..] (zip (Unoccupied : ps) ps)
|
||||
, prev /= target
|
||||
, p == target
|
||||
]
|
||||
|
||||
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
|
||||
presenceTransitionSpec
|
||||
modeTriggerSpec
|
||||
|
||||
presenceTransitionSpec :: Spec
|
||||
presenceTransitionSpec = describe "presenceTriggers" $
|
||||
it "fires exactly on the corresponding presence transitions" $ hedgehog $ do
|
||||
events <- forAll $ Gen.list (Range.linear 0 100) genTestEvent
|
||||
let steps = drop 1 $ scanl' (flip addUTCTime) day [fromIntegral t | TestEvent t _ <- events]
|
||||
timed = values (runHASSTimed presenceProbe
|
||||
(zipWith (\t (TestEvent _ e) ->
|
||||
(t, stateEvent "binary_sensor.hallway_movement_occupancy" . bool "off" "on" <$> e))
|
||||
steps events))
|
||||
presences = [p | (p, _, _) <- timed]
|
||||
onSteps = [i | (i, (_, Event (), _)) <- zip [0 ..] timed]
|
||||
offSteps = [i | (i, (_, _, Event ())) <- zip [0 ..] timed]
|
||||
onSteps === edgeSteps Occupied presences
|
||||
offSteps === edgeSteps Unoccupied presences
|
||||
cover 30 "at least one unoccupied -> occupied transition" (not (null onSteps))
|
||||
cover 30 "at least one occupied -> unoccupied transition" (not (null offSteps))
|
||||
|
||||
-- | Presence, the chosen mode and the mode-adjustment trigger observed in
|
||||
-- lock-step over the same event stream.
|
||||
modeProbe :: HASS (Event Value) (Presence, Mode, Event Mode)
|
||||
modeProbe = (,,) <$> hallwayPresence <*> mode <*> modeTrigger
|
||||
|
||||
data ModeAction
|
||||
= Motion Bool -- ^ @True@ is motion detected
|
||||
| MasterDoor Bool -- ^ @True@ is closed
|
||||
| ChildrenDoor Bool -- ^ @True@ is closed
|
||||
| Idle
|
||||
deriving Show
|
||||
|
||||
data ModeEvent = ModeEvent Int ModeAction
|
||||
deriving Show
|
||||
|
||||
genModeEvent :: Gen ModeEvent
|
||||
genModeEvent = ModeEvent
|
||||
<$> Gen.frequency [(6, Gen.integral (Range.linear 0 15)), (1, pure 900)]
|
||||
<*> Gen.frequency [(4, Motion <$> Gen.bool)
|
||||
,(2, MasterDoor <$> Gen.bool)
|
||||
,(2, ChildrenDoor <$> Gen.bool)
|
||||
,(2, pure Idle)
|
||||
]
|
||||
|
||||
modeEventValue :: ModeAction -> Event Value
|
||||
modeEventValue (Motion b) = Event $ stateEvent "binary_sensor.hallway_movement_occupancy" (bool "off" "on" b)
|
||||
modeEventValue (MasterDoor b) = Event $ stateEvent "binary_sensor.makuuhuone_ovi_contact" (bool "off" "on" b)
|
||||
modeEventValue (ChildrenDoor b) = Event $ stateEvent "binary_sensor.lasten_makuuhuone_ovi_contact" (bool "off" "on" b)
|
||||
modeEventValue Idle = Tick
|
||||
|
||||
-- | The mode-adjustment trigger: the presence-based lights are already on
|
||||
-- (room occupied), the world changed which lights should be on, so adjust.
|
||||
-- The first observed mode is the baseline the lights-on trigger already
|
||||
-- acted on, so it is not an adjustment.
|
||||
modeTriggerSpec :: Spec
|
||||
modeTriggerSpec = describe "modeTrigger" $
|
||||
it "fires exactly on occupied steps that change the mode, tagged with the new mode" $ hedgehog $ do
|
||||
events <- forAll $ Gen.list (Range.linear 0 100) genModeEvent
|
||||
let steps = drop 1 $ scanl' (flip addUTCTime) (clock 20 0) [fromIntegral t | ModeEvent t _ <- events]
|
||||
timed = values (runHASSTimed modeProbe
|
||||
(zipWith (\t (ModeEvent _ a) -> (t, modeEventValue a)) steps events))
|
||||
modes = [m | (_, m, _) <- timed]
|
||||
-- The previous mode; at the first step it is the mode itself, since
|
||||
-- the first observed mode is the baseline the lights-on trigger
|
||||
-- already acted on, not an adjustment.
|
||||
prior = case modes of [] -> []; m0 : _ -> m0 : init modes
|
||||
expected =
|
||||
[ i
|
||||
| (i, (prevM, (p, m, _))) <- zip [0 ..] (zip prior timed)
|
||||
, prevM /= m
|
||||
, p == Occupied
|
||||
]
|
||||
suppressed =
|
||||
[ () | (prevM, (p, m, _)) <- zip prior timed
|
||||
, p == Unoccupied
|
||||
, prevM /= m
|
||||
]
|
||||
fired = [i | (i, (_, _, Event _)) <- zip [0 ..] timed]
|
||||
tags = [(i, tag) | (i, (_, _, Event tag)) <- zip [0 ..] timed]
|
||||
fired === expected
|
||||
tags === [(i, m) | i <- expected, let m = modes !! i]
|
||||
cover 25 "fires at least once" (not (null fired))
|
||||
cover 25 "suppressed a mode change while unoccupied" (not (null suppressed))
|
||||
cover 25 "reaches quiet mode" (Quiet `elem` modes)
|
||||
|
||||
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)]]
|
||||
lightsSpec = do
|
||||
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, 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 the full lights on when someone enters during daytime preceding tick" $
|
||||
services (runHASSTimed hallwayLightsController [(day, Tick), (day, motionSensor "on")])
|
||||
`shouldBe` [[], [light [EntityId "light.hallway_lights"] (On def)]]
|
||||
|
||||
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 "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 "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 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 "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)]
|
||||
]
|
||||
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)]
|
||||
]
|
||||
Reference in new issue
Block a user