runtime: count only trigger event frames and harden metrics tests

This commit is contained in:
2026-09-15 17:25:31 +03:00
parent 2a477b4977
commit 7d08acec9d
4 changed files with 36 additions and 23 deletions
+6 -2
View File
@@ -6,6 +6,7 @@ module HomeAssistant.Runtime.Connection
, writerAction
, encodeService
, dedupeBatch
, isTriggerEvent
) where
import Control.Concurrent.STM
@@ -23,7 +24,7 @@ import Control.Concurrent (threadDelay)
import Control.Exception (onException)
import Control.Exception.Annotated (throw)
import Control.Lens ((^?))
import Control.Monad (forever, forM_)
import Control.Monad (forever, forM_, when)
import Data.Aeson (Value, eitherDecode, encode, object, (.=))
import Data.Aeson.Lens (key, _String)
import Data.List (sort)
@@ -69,6 +70,9 @@ expectType expected msg =
Just t | t == expected -> pure ()
_ -> throw (Fatal $ "expected " <> expected <> ", got: " <> T.pack (show msg))
isTriggerEvent :: Value -> Bool
isTriggerEvent v = v ^? key "type" . _String == Just "event"
subscribe :: Bus -> WS.Connection -> S.Set T.Text -> IO ()
subscribe bus conn ents =
forM_ (S.toList ents) $ \entityId -> do
@@ -97,7 +101,7 @@ receiveLoop bus conn = forever $ do
Right msg -> case eitherDecode msg of
Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err
Right v -> do
recordInbound bus
when (isTriggerEvent v) (recordInbound bus)
atomically $ writeTChan (busInbound bus) (Event v)
receiveJSON :: WS.Connection -> IO Value