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
@@ -40,6 +40,13 @@ bedroomPresenceOccupied = bedroomPresence >>> hold Unoccupied >>> arr (== Occupi
|
||||
bedroomLightStatus :: HASS (Event Value) (Map T.Text LightObservation)
|
||||
bedroomLightStatus = M.fromList <$> traverse (\e -> (e,) <$> observedLight e) [e | EntityId e <- bedroomLightTargets]
|
||||
|
||||
-- Ready to sleep: someone detected and every light off or dimmed far down
|
||||
readyForSleep :: HASS (Event Value) Bool
|
||||
readyForSleep = (&&) <$> bedroomPresenceOccupied <*> (all sleepLight . M.elems <$> bedroomLightStatus)
|
||||
where
|
||||
-- ~10% of the 0-255 brightness range
|
||||
sleepLight o = not (observedOn o) || maybe False (<= 25) (observedBrightness o)
|
||||
|
||||
lightLevel :: HASS (Event Value) Int
|
||||
lightLevel = entityRead "sensor.presence_sensor_bedroom_light_level" >>> AFRP.hold 1
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
{-# LANGUAGE RecordWildCards #-}
|
||||
module HomeAssistant.Controller.Hallway where
|
||||
|
||||
import HomeAssistant.Controller (Presence (..), HASS, motion, motionToPresence, callServiceDyn, light, Target (..), LightCommand (..), doorState, DoorState (Closed), callService)
|
||||
import HomeAssistant.Controller (Presence (..), HASS, motion, motionToPresence, callServiceDyn, light, Target (..), LightCommand (..), doorState, DoorState (..), callService)
|
||||
import HomeAssistant.Controller.Bedroom (readyForSleep)
|
||||
import AFRP (Event (..))
|
||||
import qualified AFRP
|
||||
import Data.Aeson (Value)
|
||||
@@ -25,17 +26,26 @@ data ModeInput = ModeInput
|
||||
{ roomTimeOfDay :: TimeOfDay
|
||||
, bedroomDoor :: Maybe DoorState
|
||||
, childrenBedroomDoor :: Maybe DoorState
|
||||
, bedroomReadyForSleep :: Bool
|
||||
}
|
||||
|
||||
mode :: HASS (Event Value) Mode
|
||||
mode = roomState >>> arr sleepMode
|
||||
where
|
||||
sleepMode :: ModeInput -> Mode
|
||||
sleepMode ModeInput{bedroomDoor = Just Closed, childrenBedroomDoor = Just Closed} = Regular
|
||||
sleepMode ModeInput{..}
|
||||
| roomTimeOfDay > TimeOfDay 20 30 0 || roomTimeOfDay < TimeOfDay 7 30 0 = Quiet
|
||||
| otherwise = Regular
|
||||
roomState = ModeInput <$> timeOfDay <*> masterBedroom <*> childrenBedroom
|
||||
sleepMode ModeInput{..} = sleep
|
||||
where
|
||||
-- An open parents' bedroom door only leaks into someone's sleep while
|
||||
-- that someone is actually ready to sleep; an open door into an
|
||||
-- empty or awake room does not need the hallway dimmed.
|
||||
bedroomForLeakage = case bedroomDoor of
|
||||
Just Open | not bedroomReadyForSleep -> Just Closed
|
||||
door -> door
|
||||
sleep
|
||||
| Just Closed <- bedroomForLeakage, Just Closed <- childrenBedroomDoor = Regular
|
||||
| roomTimeOfDay > TimeOfDay 20 30 0 || roomTimeOfDay < TimeOfDay 7 30 0 = Quiet
|
||||
| otherwise = Regular
|
||||
roomState = ModeInput <$> timeOfDay <*> masterBedroom <*> childrenBedroom <*> readyForSleep
|
||||
timeOfDay = fmap localTimeOfDay AFRP.currentTime
|
||||
masterBedroom = doorState "binary_sensor.makuuhuone_ovi_contact" >>> AFRP.holdKnown
|
||||
childrenBedroom = doorState "binary_sensor.lasten_makuuhuone_ovi_contact" >>> AFRP.holdKnown
|
||||
|
||||
+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