Much improved hallway handling

This commit is contained in:
MasseR committed 2026-11-03 14:47:01 +02:00
1 parent 95fcc61c4f
commit 6e35445090
9 files changed
+354 -100

No files matched your search

+8 -2
View File
@@ -31,6 +31,7 @@ module AFRP
, SerializeUTCTime(..)
, SerializeLocalTime(..)
, edge
, edgeFrom
, waitFor
, duration
, tag
@@ -528,10 +529,15 @@ lMerge (Event a) _ = Event a
lMerge Tick (Event a) = Event a
edge :: Mealy eff Bool (Event ())
edge =
edge = edgeFrom False
-- | Edge that behaves as if the input has been `start` forever, so the first
-- True input does not fire when `start` is already True (no transition).
edgeFrom :: Bool -> Mealy eff Bool (Event ())
edgeFrom start =
mapAccum
(\(_, current) new -> (current, new))
(False, False)
(start, start)
(\(old, current) ->
if not old && current
then Event ()
+10 -1
View File
@@ -1,9 +1,18 @@
module HomeAssistant.Controller.Door (DoorState(..)) where
module HomeAssistant.Controller.Door (DoorState(..), doorState) where
import GHC.Generics (Generic)
import Data.Serialize (Serialize)
import AFRP (Mealy, Event)
import Data.Aeson (Value)
import Data.Text (Text)
import HomeAssistant.Controller.Entity (entityBool)
import Control.Arrow ((>>>), arr)
import Data.Bool (bool)
data DoorState = Open | Closed
deriving (Show, Eq, Generic)
instance Serialize DoorState
doorState :: Text -> Mealy eff (Event Value) (Event DoorState)
doorState entity = entityBool entity >>> arr (fmap (bool Closed Open))
+74 -15
View File
@@ -1,31 +1,90 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Arrows #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE RecordWildCards #-}
module HomeAssistant.Controller.Hallway where
import HomeAssistant.Controller (Presence, HASS, motion, motionToPresence, traceEvent, callServiceDyn, light, Target (..), LightCommand (..))
import HomeAssistant.Controller.Light.Mode (LightingMode (..), lightingModeEvents)
import HomeAssistant.Controller (Presence (..), HASS, motion, motionToPresence, callServiceDyn, light, Target (..), LightCommand (..), doorState, DoorState (Closed), callService)
import AFRP (Event (..))
import qualified AFRP
import Data.Aeson (Value)
import Control.Arrow ((>>>), returnA)
import Control.Arrow ((>>>), arr, returnA)
import Data.Time (LocalTime(..), TimeOfDay (..))
import Data.Default (def)
import Data.Serialize (Serialize)
import GHC.Generics (Generic)
data Mode
= Quiet -- ^ Possibly sleep time, doors are open, minimize light leakage
| Regular -- ^ Doors are closed, light leakage is not a concern
deriving (Generic, Eq, Show)
instance Serialize Mode
data ModeInput = ModeInput
{ roomTimeOfDay :: TimeOfDay
, bedroomDoor :: Maybe DoorState
, childrenBedroomDoor :: Maybe DoorState
}
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
timeOfDay = fmap localTimeOfDay AFRP.currentTime
masterBedroom = doorState "binary_sensor.makuuhuone_ovi_contact" >>> AFRP.holdKnown
childrenBedroom = doorState "binary_sensor.lasten_makuuhuone_ovi_contact" >>> AFRP.holdKnown
hallwayPresence :: HASS (Event Value) Presence
hallwayPresence = motion "binary_sensor.hallway_movement_occupancy" >>> motionToPresence 500
hallwayLightsController :: HASS (Event Value) ()
hallwayLightsController = proc x -> do
now <- AFRP.currentTime -< ()
p <- hallwayPresence -< x
ev <- lightingModeEvents -< p
traceEvent -< ev
case ev of
-- The lights entity is a "group of lights" in hass
Event LightingOn | lightsAllowed now -> callServiceDyn "Lights on" (light [EntityId "light.hallway_lights"]) -< On def
Event LightingOff -> callServiceDyn "Lights off" (light [EntityId "light.hallway_lights"]) -< Off
_ -> returnA -< ()
lightsOnTrigger :: HASS (Event Value) (Event ())
lightsOnTrigger = hallwayPresence >>> arr (== Occupied) >>> AFRP.edge
modeTrigger :: HASS (Event Value) (Event Mode)
modeTrigger = ((,) <$> hallwayPresence <*> (mode >>> AFRP.changes)) >>> arr go
where
lightsAllowed (LocalTime _ tod) = not (tod > TimeOfDay 1 45 0 && tod < TimeOfDay 5 0 0)
go (Unoccupied, _) = Tick
go (Occupied, e) = e
modeC :: HASS (Event Value) ()
modeC = modeTrigger >>> mconcat
[ AFRP.filterEvent (== Quiet) AFRP.>>|| lightOff
, AFRP.filterEvent (== Regular) AFRP.>>|| lightOn
] >>>
arr (const ())
where
lightOff = callService "Lower light off" (light [EntityId "light.hallway_ceiling"] Off)
lightOn = callService "Lower light back on" (light [EntityId "light.hallway_ceiling"] (On def))
onC :: HASS (Event Value) ()
onC = proc ev -> do
trigger <- lightsOnTrigger -< ev
m <- mode -< ev
let entities = case m of
Quiet -> [EntityId "light.hallway_ceiling_2"]
Regular -> [EntityId "light.hallway_lights"]
case trigger of
Event _ -> lightsOn -< entities
Tick -> returnA -< ()
where
lightsOn = callServiceDyn "Lights on" (\es -> light es (On def))
lightsOffTrigger :: HASS (Event Value) (Event ())
lightsOffTrigger = hallwayPresence >>> arr (== Unoccupied) >>>
AFRP.edgeFrom True -- we start from 'edgeFrom True' because the initial state is to be unoccupied
offC :: HASS (Event Value) ()
offC = (lightsOffTrigger AFRP.>>|| lightsOff) >>>
arr (const ())
where
lightsOff = callService "Lights off" (light [EntityId "light.hallway_lights"] Off)
hallwayLightsController :: HASS (Event Value) ()
hallwayLightsController = onC <> offC <> modeC
+14 -9
View File
@@ -3,10 +3,11 @@ module HomeAssistant.Controller.Presence
, presence
, Motion(..)
, motion
, motionEvent
, motionToPresence
) where
import AFRP (Event, hold, lMerge, waitFor, tag, edge)
import AFRP (Event, hold, lMerge, waitFor, tag, edge, holdKnown)
import Control.Arrow (arr, (&&&))
import Control.Category ((>>>))
import Data.Aeson (Value)
@@ -28,28 +29,32 @@ presence entityId = entityBool entityId
>>> arr (fmap (bool Unoccupied Occupied))
data Motion = MotionDetected | MotionNotDetected | MotionUnknown
data Motion = MotionDetected | MotionNotDetected
deriving (Show, Eq, Generic)
motion :: T.Text -> HASS (Event Value) Motion
motionEvent :: T.Text -> HASS (Event Value) (Event Motion)
motionEvent entityId = entityBool entityId
>>> arr (fmap (bool MotionNotDetected MotionDetected))
motion :: T.Text -> HASS (Event Value) (Maybe Motion)
motion entityId = entityBool entityId
>>> arr (fmap (bool MotionNotDetected MotionDetected))
>>> hold MotionUnknown
>>> holdKnown
-- Convert a motion status into a simulated presence
--
-- A motion tracker detects _motion_, but if you are still, it can't detect you.
-- A simple and cheap heuristic is to assume the person is a bit longer there
motionToPresence :: NominalDiffTime -> HASS Motion Presence
motionToPresence :: NominalDiffTime -> HASS (Maybe Motion) Presence
motionToPresence delay = (eventOccupied &&& eventUnoccupied)
>>> arr (uncurry lMerge)
>>> AFRP.hold Unoccupied
where
eventOccupied :: HASS Motion (Event Presence)
eventOccupied = arr (== MotionDetected) >>> edge >>> arr (fmap (const Occupied))
eventUnoccupied :: HASS Motion (Event Presence)
eventUnoccupied = arr (== MotionNotDetected) >>> waitFor delay >>> arr (tag Unoccupied)
eventOccupied :: HASS (Maybe Motion) (Event Presence)
eventOccupied = arr (== Just MotionDetected) >>> edge >>> arr (fmap (const Occupied))
eventUnoccupied :: HASS (Maybe Motion) (Event Presence)
eventUnoccupied = arr (== Just MotionNotDetected) >>> waitFor delay >>> arr (tag Unoccupied)
instance Serialize Motion