Hallway: only dim for an open bedroom door while it is ready to sleep
The parents' bedroom door is open at night for the cats, so the door state says nothing about whoever sleeps in it. The doorway now leaks into no one's sleep unless the bedroom reports someone present with the lights off or dimmed far down (mmWave presence sensor); when it does, the open door is protected like before.
This commit is contained in:
1 parent
05263fed2b
commit
c5c11fe8d3
3 files changed
+109
-12
No files matched your search
+86
-6
@@ -4,7 +4,7 @@ module HallwaySpec (spec) where
|
||||
|
||||
import AFRP (Event (..))
|
||||
import qualified Data.Text as T
|
||||
import Data.Aeson (Value)
|
||||
import Data.Aeson (Value, object, (.=))
|
||||
import Data.Bool (bool)
|
||||
import Data.Default (def)
|
||||
import Data.List (scanl')
|
||||
@@ -30,6 +30,25 @@ masterDoor = Event . stateEvent "binary_sensor.makuuhuone_ovi_contact"
|
||||
childrenDoor :: T.Text -> Event Value
|
||||
childrenDoor = Event . stateEvent "binary_sensor.lasten_makuuhuone_ovi_contact"
|
||||
|
||||
bedroomPresenceSensor :: T.Text -> Event Value
|
||||
bedroomPresenceSensor = Event . stateEvent "binary_sensor.presence_sensor_bedroom_occupancy"
|
||||
|
||||
-- A light turning on with a raw brightness value (0-255)
|
||||
bedroomLightEvent :: T.Text -> Int -> Event Value
|
||||
bedroomLightEvent entityId brightness = Event . object $
|
||||
[ "event" .= object
|
||||
[ "variables" .= object
|
||||
[ "trigger" .= object
|
||||
[ "entity_id" .= (entityId :: T.Text)
|
||||
, "to_state" .= object
|
||||
[ "state" .= ("on" :: T.Text)
|
||||
, "attributes" .= object [ "brightness" .= brightness ]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
-- 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.
|
||||
@@ -91,6 +110,8 @@ data ModeAction
|
||||
= Motion Bool -- ^ @True@ is motion detected
|
||||
| MasterDoor Bool -- ^ @True@ is closed
|
||||
| ChildrenDoor Bool -- ^ @True@ is closed
|
||||
| BedroomPresence Bool -- ^ @True@ is someone in the parents' bedroom
|
||||
| BedroomLight (Maybe Int) -- ^ @Nothing@ is off, otherwise the brightness
|
||||
| Idle
|
||||
deriving Show
|
||||
|
||||
@@ -103,6 +124,8 @@ genModeEvent = ModeEvent
|
||||
<*> Gen.frequency [(4, Motion <$> Gen.bool)
|
||||
,(2, MasterDoor <$> Gen.bool)
|
||||
,(2, ChildrenDoor <$> Gen.bool)
|
||||
,(1, BedroomPresence <$> Gen.bool)
|
||||
,(1, BedroomLight <$> Gen.choice [pure Nothing, pure (Just 25), pure (Just 230)])
|
||||
,(2, pure Idle)
|
||||
]
|
||||
|
||||
@@ -110,7 +133,11 @@ 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
|
||||
modeEventValue (BedroomPresence b) = Event $ stateEvent "binary_sensor.presence_sensor_bedroom_occupancy" (bool "off" "on" b)
|
||||
modeEventValue (BedroomLight b) = case b of
|
||||
Nothing -> Event $ stateEvent "light.bedroom_ceiling" "off"
|
||||
Just n -> bedroomLightEvent "light.bedroom_ceiling" n
|
||||
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.
|
||||
@@ -184,15 +211,68 @@ lightsSpec = do
|
||||
, [light [EntityId "light.hallway_ceiling_2"] (On def)]
|
||||
]
|
||||
|
||||
it "sets the lower hallway light off when a door is opened during sleep hours" $
|
||||
it "leaves the lights unchanged when an empty bedroom door opens during sleep hours" $
|
||||
services (runHASSTimed hallwayLightsController
|
||||
[ (night, masterDoor "off")
|
||||
, (night, childrenDoor "off")
|
||||
[ (night, masterDoor "off") -- closed
|
||||
, (night, childrenDoor "off") -- closed
|
||||
, (night, motionSensor "on")
|
||||
, (night, masterDoor "on")
|
||||
, (night, masterDoor "on") -- opens, but nobody is ready to sleep there
|
||||
])
|
||||
`shouldBe` [ []
|
||||
, []
|
||||
, [light [EntityId "light.hallway_lights"] (On def)]
|
||||
, []
|
||||
]
|
||||
|
||||
it "keeps the full lights when an awake bedroom sits behind an open door" $
|
||||
services (runHASSTimed hallwayLightsController
|
||||
[ (night, masterDoor "on") -- open for the cats
|
||||
, (night, childrenDoor "off") -- closed
|
||||
, (night, bedroomPresenceSensor "on")
|
||||
, (night, bedroomLightEvent "light.bedroom_ceiling" 230)
|
||||
, (night, motionSensor "on")
|
||||
])
|
||||
`shouldBe` [ [], [], [], []
|
||||
, [light [EntityId "light.hallway_lights"] (On def)]
|
||||
]
|
||||
|
||||
it "protects an open bedroom door as soon as its light is dimmed down" $
|
||||
services (runHASSTimed hallwayLightsController
|
||||
[ (night, masterDoor "on") -- open for the cats
|
||||
, (night, childrenDoor "off") -- closed
|
||||
, (night, bedroomLightEvent "light.bedroom_ceiling" 230) -- still bright
|
||||
, (night, bedroomPresenceSensor "on")
|
||||
, (night, motionSensor "on")
|
||||
, (night, bedroomLightEvent "light.bedroom_ceiling" 25)
|
||||
])
|
||||
`shouldBe` [ [], [], [], []
|
||||
, [light [EntityId "light.hallway_lights"] (On def)]
|
||||
, [light [EntityId "light.hallway_ceiling"] Off]
|
||||
]
|
||||
|
||||
it "counts a barely lit bedroom as ready to sleep" $
|
||||
services (runHASSTimed hallwayLightsController
|
||||
[ (night, masterDoor "off") -- closed
|
||||
, (night, childrenDoor "off") -- closed
|
||||
, (night, bedroomPresenceSensor "on")
|
||||
, (night, bedroomLightEvent "light.bedroom_ceiling" 25)
|
||||
, (night, motionSensor "on")
|
||||
, (night, masterDoor "on") -- opens for the cats, someone ready to sleep
|
||||
])
|
||||
`shouldBe` [ [], [], [], []
|
||||
, [light [EntityId "light.hallway_lights"] (On def)]
|
||||
, [light [EntityId "light.hallway_ceiling"] Off]
|
||||
]
|
||||
|
||||
it "dims the lower light when an occupied bedroom door opens during sleep hours" $
|
||||
services (runHASSTimed hallwayLightsController
|
||||
[ (night, masterDoor "off") -- closed
|
||||
, (night, childrenDoor "off") -- closed
|
||||
, (night, bedroomPresenceSensor "on")
|
||||
, (night, motionSensor "on")
|
||||
, (night, masterDoor "on") -- opens for the cats, someone ready to sleep
|
||||
])
|
||||
`shouldBe` [ [], [], []
|
||||
, [light [EntityId "light.hallway_lights"] (On def)]
|
||||
, [light [EntityId "light.hallway_ceiling"] Off]
|
||||
]
|
||||
|
||||
Reference in new issue
Block a user