Logging and really not working light restoration

This commit is contained in:
MasseR committed 2026-09-30 13:46:16 +03:00
1 parent 1098be227d
commit c75fc1a7b9
10 files changed
+180 -151

No files matched your search

+16 -13
View File
@@ -12,7 +12,6 @@ module HomeAssistant.Runtime.Flags
import Control.Concurrent.MVar (MVar, newMVar, withMVar)
import Control.Concurrent.STM (TVar, atomically, newTVarIO, readTVarIO, stateTVar)
import Control.Exception (IOException, handle)
import Control.Monad.IO.Class (MonadIO, liftIO)
import qualified Conduit as C
import Conduit ((.|))
@@ -24,8 +23,12 @@ import Data.Set (Set)
import qualified Data.Set as S
import Data.Text (Text)
import System.FilePath ((</>))
import System.IO (hPutStrLn, stderr)
import System.IO.Error (isDoesNotExistError)
import GHC.Stack (HasCallStack)
import Katip (KatipContext, logLocM, Severity (..), ls)
import Control.Monad.Catch (MonadCatch, handle)
import Control.Exception (IOException)
import Data.Maybe (fromMaybe)
type Name = Text
@@ -36,14 +39,14 @@ data Flags = Flags
, flagsValidNames :: Set Text
}
loadFlags :: MonadIO m => FilePath -> [Name] -> m Flags
loadFlags rootPath allNames = liftIO $ do
loadFlags :: (KatipContext m, MonadCatch m, MonadIO m) => FilePath -> [Name] -> m Flags
loadFlags rootPath allNames = do
let path = rootPath </> "handlers.json"
fileMap <- readFlagsFile path
let defaults = M.fromList [(n, True) | n <- allNames]
initial = M.unionWith (\fileVal _default -> fileVal) fileMap defaults
tv <- newTVarIO initial
lock <- newMVar ()
initial = M.unionWith (\fileVal -> const fileVal) fileMap defaults
tv <- liftIO $ newTVarIO initial
lock <- liftIO $ newMVar ()
pure Flags
{ flagsState = tv
, flagsLock = lock
@@ -51,21 +54,21 @@ loadFlags rootPath allNames = liftIO $ do
, flagsValidNames = S.fromList allNames
}
readFlagsFile :: FilePath -> IO (Map Name Bool)
readFlagsFile :: forall m. (HasCallStack, KatipContext m, MonadCatch m) => FilePath -> m (Map Name Bool)
readFlagsFile path =
handle onMissingOrUnreadable $ do
decoded <- decodeFileStrict' path
decoded <- liftIO $ decodeFileStrict' path
case decoded of
Nothing -> do
hPutStrLn stderr ("handlers.json is unparseable; treating all handlers as enabled: " <> path)
logLocM WarningS $ ls ("handlers.json is unparseable; treating all handlers as enabled: " <> path)
pure mempty
Just m -> pure (maybe mempty id m)
Just m -> pure (fromMaybe mempty m)
where
onMissingOrUnreadable :: IOException -> IO (Map Name Bool)
onMissingOrUnreadable :: HasCallStack => IOException -> m (Map Name Bool)
onMissingOrUnreadable e
| isDoesNotExistError e = pure mempty
| otherwise = do
hPutStrLn stderr ("could not read handlers.json (" <> show e <> "); treating all handlers as enabled")
logLocM WarningS $ ls ("could not read handlers.json (" <> show e <> "); treating all handlers as enabled")
pure mempty
isEnabled :: MonadIO m => Flags -> Name -> m Bool