runtime: count only trigger event frames and harden metrics tests
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user