From 55855db55bed17a8897311231ede6f37fcd6f8b1 Mon Sep 17 00:00:00 2001 From: Mats Rauhala Date: Mon, 12 Oct 2026 11:11:37 +0300 Subject: [PATCH] Try a separate `holdKnown` primitive --- src/AFRP.hs | 6 ++++++ src/HomeAssistant/Controller/Children.hs | 7 ++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/AFRP.hs b/src/AFRP.hs index 4fcb319..11da618 100644 --- a/src/AFRP.hs +++ b/src/AFRP.hs @@ -8,6 +8,7 @@ module AFRP , withEntities , Event(..) , hold + , holdKnown , events -- , switch , preMapAccum @@ -326,6 +327,11 @@ hold def = mapAccum step def id Tick -> prev Event new -> new + +-- A variant of 'hold' which makes it explicit whether we have a previous value +holdKnown :: (Serialize a, Eq a) => Mealy m (Event a) (Maybe a) +holdKnown = arr (fmap Just) >>> hold Nothing + snapshot :: (Serialize a, Eq a) => a -> Mealy m (Event (), a) a snapshot def = mapAccum step def id where diff --git a/src/HomeAssistant/Controller/Children.hs b/src/HomeAssistant/Controller/Children.hs index 65767df..24e3b1e 100644 --- a/src/HomeAssistant/Controller/Children.hs +++ b/src/HomeAssistant/Controller/Children.hs @@ -19,6 +19,7 @@ import Data.Aeson.Types (ToJSON (..)) import Data.Aeson.Lens (key, _String) import Control.Lens ((^?), to) import Data.Text (Text) +import Control.Monad (join) -- Let's see building some reasonable interface for utctime dow :: Day -> (WeekOfYear, Int) @@ -131,8 +132,8 @@ data HumidifierAction instance Serialize HumidifierAction -- what is the humidifer doing right now? -humidifierEntityAction :: HASS (Event Value) (Event (Maybe HumidifierAction)) -humidifierEntityAction = entityChangeEvent "humidifier.lapset_ilmankostutin" +humidifierOperatingState :: HASS (Event Value) (Event (Maybe HumidifierAction)) +humidifierOperatingState = entityChangeEvent "humidifier.lapset_ilmankostutin" >>> arr (fmap entityAction) where entityAction v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "action" . _String . to humidifierAction @@ -149,7 +150,7 @@ instance ToJSON Percentage where childrenAirFilterController :: HASS (Event Value) () childrenAirFilterController = proc ev -> do level <- pm25 >>> pm25Level >>> AFRP.changes >>> traceEvent -< ev - humidifier <- humidifierEntityAction >>> AFRP.hold Nothing -< ev + humidifier <- humidifierOperatingState >>> AFRP.holdKnown >>> arr join -< ev case level of Event PM25High -> AFRP.whenA (== Just Idle) (callService (fanOnTo (Percentage 80))) -< humidifier