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

+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