Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bb96833e1 | ||
|
|
2730657952 |
+6
-6
@@ -1,6 +1,6 @@
|
|||||||
{ mkDerivation, aeson, annotated-exception, async, base, bytestring
|
{ mkDerivation, aeson, annotated-exception, async, base, bytestring
|
||||||
, hedgehog, hspec, hspec-hedgehog, katip, lens, lens-aeson, lib
|
, containers, hedgehog, hspec, hspec-hedgehog, katip, lens
|
||||||
, network, stm, text, time, uuid, websockets
|
, lens-aeson, lib, network, stm, text, time, uuid, websockets
|
||||||
}:
|
}:
|
||||||
mkDerivation {
|
mkDerivation {
|
||||||
pname = "home-assistant-controller";
|
pname = "home-assistant-controller";
|
||||||
@@ -9,13 +9,13 @@ mkDerivation {
|
|||||||
isLibrary = true;
|
isLibrary = true;
|
||||||
isExecutable = true;
|
isExecutable = true;
|
||||||
libraryHaskellDepends = [
|
libraryHaskellDepends = [
|
||||||
aeson annotated-exception async base bytestring katip lens
|
aeson annotated-exception async base bytestring containers katip
|
||||||
lens-aeson network stm text time uuid websockets
|
lens lens-aeson network stm text time uuid websockets
|
||||||
];
|
];
|
||||||
executableHaskellDepends = [ base ];
|
executableHaskellDepends = [ base ];
|
||||||
testHaskellDepends = [
|
testHaskellDepends = [
|
||||||
aeson annotated-exception async base hedgehog hspec hspec-hedgehog
|
aeson annotated-exception async base containers hedgehog hspec
|
||||||
katip stm text time uuid
|
hspec-hedgehog katip stm text time uuid
|
||||||
];
|
];
|
||||||
license = lib.meta.getLicenseFromSpdxId "BSD-3-Clause";
|
license = lib.meta.getLicenseFromSpdxId "BSD-3-Clause";
|
||||||
mainProgram = "home-assistant-controller";
|
mainProgram = "home-assistant-controller";
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ library
|
|||||||
, annotated-exception
|
, annotated-exception
|
||||||
, uuid
|
, uuid
|
||||||
, katip
|
, katip
|
||||||
|
, containers
|
||||||
|
|
||||||
-- Directories containing source files.
|
-- Directories containing source files.
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
@@ -165,4 +166,5 @@ test-suite home-assistant-controller-test
|
|||||||
annotated-exception,
|
annotated-exception,
|
||||||
time,
|
time,
|
||||||
uuid,
|
uuid,
|
||||||
katip
|
katip,
|
||||||
|
containers
|
||||||
|
|||||||
+80
-24
@@ -1,8 +1,10 @@
|
|||||||
{-# LANGUAGE LambdaCase #-}
|
{-# LANGUAGE LambdaCase #-}
|
||||||
|
{-# LANGUAGE Arrows #-}
|
||||||
|
|
||||||
module AFRP
|
module AFRP
|
||||||
( Mealy(..)
|
( Mealy(..)
|
||||||
, eff
|
, eff
|
||||||
|
, withEntities
|
||||||
, Event(..)
|
, Event(..)
|
||||||
, hold
|
, hold
|
||||||
, events
|
, events
|
||||||
@@ -20,6 +22,7 @@ module AFRP
|
|||||||
, lMerge
|
, lMerge
|
||||||
, Request(..)
|
, Request(..)
|
||||||
, edge
|
, edge
|
||||||
|
, dropFirst
|
||||||
, duration
|
, duration
|
||||||
, tag
|
, tag
|
||||||
, isEvent
|
, isEvent
|
||||||
@@ -29,64 +32,90 @@ module AFRP
|
|||||||
, sliding
|
, sliding
|
||||||
, fixed
|
, fixed
|
||||||
, debounce
|
, debounce
|
||||||
|
, currentTime
|
||||||
|
, onEvent
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Category (Category(..), (>>>))
|
import Control.Category (Category(..), (>>>))
|
||||||
import Prelude hiding ((.), id)
|
import Prelude hiding ((.), id)
|
||||||
import Control.Arrow (Arrow(..), ArrowChoice(..), ArrowLoop(..))
|
import Control.Arrow (Arrow(..), ArrowChoice(..), ArrowLoop(..))
|
||||||
import Data.Time (UTCTime, NominalDiffTime, diffUTCTime, addUTCTime)
|
import Data.Time (UTCTime, NominalDiffTime, diffUTCTime, addUTCTime, TimeZone, LocalTime, utcToLocalTime)
|
||||||
import Control.Monad.Fix (MonadFix (mfix))
|
import Control.Monad.Fix (MonadFix (mfix))
|
||||||
import Data.Either (fromLeft)
|
import Data.Either (fromLeft)
|
||||||
import Data.Bool (bool)
|
import Data.Bool (bool)
|
||||||
import Data.Monoid (Endo(..))
|
import Data.Monoid (Endo(..))
|
||||||
import Data.UUID (UUID)
|
import Data.UUID (UUID)
|
||||||
|
import qualified Data.Set as S
|
||||||
|
import qualified Data.Text as T
|
||||||
|
|
||||||
data Request = Request
|
data Request = Request
|
||||||
{ requestTime :: !UTCTime
|
{ requestTime :: !UTCTime
|
||||||
|
, requestTimeZone :: !TimeZone
|
||||||
, requestTraceId :: !UUID
|
, requestTraceId :: !UUID
|
||||||
}
|
} deriving Show
|
||||||
deriving Show
|
|
||||||
|
|
||||||
newtype Mealy eff a b = Mealy
|
-- | The set of entity ids an arrow subscribes to. Static: it does not
|
||||||
{ runMealy :: forall m. MonadFix m => (forall x. eff x -> m x) -> Request -> a -> m (b, Mealy eff a b) }
|
-- change as the machine steps, so the runtime can read it once to build
|
||||||
|
-- trigger subscriptions.
|
||||||
|
data Mealy eff a b = Mealy
|
||||||
|
{ entities :: S.Set T.Text
|
||||||
|
, runMealy :: forall m. MonadFix m => (forall x. eff x -> m x) -> Request -> a -> m (b, Mealy eff a b)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
instance Semigroup b => Semigroup (Mealy eff a b) where
|
||||||
|
Mealy ast af <> Mealy bst bf = Mealy (ast <> bst) $ \nt r a -> do
|
||||||
|
(x, af') <- af nt r a
|
||||||
|
(x', bf') <- bf nt r a
|
||||||
|
pure (x <> x', af' <> bf')
|
||||||
|
|
||||||
|
|
||||||
|
instance Monoid b => Monoid (Mealy eff a b) where
|
||||||
|
mempty = Mealy mempty $ \_ _ _ -> pure (mempty, mempty)
|
||||||
|
|
||||||
eff :: (Request -> a -> eff b) -> Mealy eff a b
|
eff :: (Request -> a -> eff b) -> Mealy eff a b
|
||||||
eff f = Mealy $ \nt req x ->
|
eff f = Mealy mempty $ \nt req x ->
|
||||||
nt (f req x) >>= \b -> pure (b, eff f)
|
nt (f req x) >>= \b -> pure (b, eff f)
|
||||||
|
|
||||||
|
-- | Override the static entity set of an arrow. Use when a combinator
|
||||||
|
-- (e.g. 'switch') hides continuation entities from the runtime's
|
||||||
|
-- startup subscription scan.
|
||||||
|
withEntities :: S.Set T.Text -> Mealy eff a b -> Mealy eff a b
|
||||||
|
withEntities es (Mealy _ f) = Mealy es f
|
||||||
|
|
||||||
instance Category (Mealy eff) where
|
instance Category (Mealy eff) where
|
||||||
id = Mealy (\_ _ x -> pure (x, id))
|
id = Mealy mempty (\_ _ x -> pure (x, id))
|
||||||
(Mealy f) . (Mealy g) = Mealy $ \nt t a -> do
|
(Mealy ast f) . (Mealy bst g) = Mealy (ast <> bst) $ \nt t a -> do
|
||||||
(b, g') <- g nt t a
|
(b, g') <- g nt t a
|
||||||
(c, f') <- f nt t b
|
(c, f') <- f nt t b
|
||||||
pure (c, f' . g')
|
pure (c, f' . g')
|
||||||
|
|
||||||
instance Arrow (Mealy eff) where
|
instance Arrow (Mealy eff) where
|
||||||
arr f = Mealy $ \_ _ b -> pure (f b, arr f)
|
arr f = Mealy mempty $ \_ _ b -> pure (f b, arr f)
|
||||||
first (Mealy f) = Mealy $ \nt t (b,d) -> do
|
first (Mealy st f) = Mealy st $ \nt t (b,d) -> do
|
||||||
(c, f') <- f nt t b
|
(c, f') <- f nt t b
|
||||||
pure ((c, d), first f')
|
pure ((c, d), first f')
|
||||||
|
|
||||||
instance ArrowChoice (Mealy eff) where
|
instance ArrowChoice (Mealy eff) where
|
||||||
left (Mealy f) = Mealy $ \nt t -> \case
|
left m@(Mealy st f) = Mealy st $ \nt t -> \case
|
||||||
Left b -> do
|
Left b -> do
|
||||||
(c, f') <- f nt t b
|
(c, f') <- f nt t b
|
||||||
pure (Left c, left f')
|
pure (Left c, left f')
|
||||||
Right d -> pure (Right d, left (Mealy f))
|
Right d -> pure (Right d, left m)
|
||||||
|
|
||||||
instance ArrowLoop (Mealy eff) where
|
instance ArrowLoop (Mealy eff) where
|
||||||
loop (Mealy f) = Mealy $ \nt t b -> do
|
loop (Mealy st f) = Mealy st $ \nt t b -> do
|
||||||
((c,_), f') <- mfix $ \((_,d), _) -> f nt t (b,d)
|
((c,_), f') <- mfix $ \((_,d), _) -> f nt t (b,d)
|
||||||
pure (c, loop f')
|
pure (c, loop f')
|
||||||
|
|
||||||
instance Functor (Mealy eff a) where
|
instance Functor (Mealy eff a) where
|
||||||
fmap f (Mealy g) = Mealy $ \nt t a -> do
|
fmap f (Mealy st g) = Mealy st $ \nt t a -> do
|
||||||
(b, g') <- g nt t a
|
(b, g') <- g nt t a
|
||||||
pure (f b, fmap f g')
|
pure (f b, fmap f g')
|
||||||
|
|
||||||
instance Applicative (Mealy eff a) where
|
instance Applicative (Mealy eff a) where
|
||||||
pure b = Mealy $ \_ _ _ -> pure (b, pure b)
|
pure b = Mealy mempty $ \_ _ _ -> pure (b, pure b)
|
||||||
Mealy f <*> Mealy x = Mealy $ \nt t a -> do
|
Mealy ast f <*> Mealy bst x = Mealy (ast <> bst) $ \nt t a -> do
|
||||||
(f', fNext) <- f nt t a
|
(f', fNext) <- f nt t a
|
||||||
(x', xNext) <- x nt t a
|
(x', xNext) <- x nt t a
|
||||||
pure (f' x', fNext <*> xNext)
|
pure (f' x', fNext <*> xNext)
|
||||||
@@ -96,8 +125,14 @@ data Event a
|
|||||||
| Event a
|
| Event a
|
||||||
deriving (Show, Eq, Functor, Foldable, Traversable)
|
deriving (Show, Eq, Functor, Foldable, Traversable)
|
||||||
|
|
||||||
|
instance Semigroup (Event a) where
|
||||||
|
(<>) = lMerge
|
||||||
|
|
||||||
|
instance Monoid (Event a) where
|
||||||
|
mempty = Tick
|
||||||
|
|
||||||
hold :: a -> Mealy eff (Event a) a
|
hold :: a -> Mealy eff (Event a) a
|
||||||
hold a = Mealy $ \_ _ -> \case
|
hold a = Mealy mempty $ \_ _ -> \case
|
||||||
Tick -> pure (a, hold a)
|
Tick -> pure (a, hold a)
|
||||||
Event a' -> pure (a', hold a')
|
Event a' -> pure (a', hold a')
|
||||||
|
|
||||||
@@ -114,7 +149,7 @@ tag :: b -> Event a -> Event b
|
|||||||
tag b ev = b <$ ev
|
tag b ev = b <$ ev
|
||||||
|
|
||||||
switch :: Mealy eff a (b, Event c) -> (c -> Mealy eff a b) -> Mealy eff a b
|
switch :: Mealy eff a (b, Event c) -> (c -> Mealy eff a b) -> Mealy eff a b
|
||||||
switch (Mealy f) s = Mealy $ \nt t a -> do
|
switch (Mealy st f) s = Mealy st $ \nt t a -> do
|
||||||
((b, ev), f') <- f nt t a
|
((b, ev), f') <- f nt t a
|
||||||
case ev of
|
case ev of
|
||||||
Tick -> pure (b, switch f' s)
|
Tick -> pure (b, switch f' s)
|
||||||
@@ -126,28 +161,28 @@ sample = arr (uncurry tag)
|
|||||||
preMapAccum :: (x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
preMapAccum :: (x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
||||||
preMapAccum f x extract = go x
|
preMapAccum f x extract = go x
|
||||||
where
|
where
|
||||||
go b = Mealy $ \_ _ a ->
|
go b = Mealy mempty $ \_ _ a ->
|
||||||
let next = f b a
|
let next = f b a
|
||||||
in pure (extract b, go next)
|
in pure (extract b, go next)
|
||||||
|
|
||||||
preMapAccumRequest :: (Request -> x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
preMapAccumRequest :: (Request -> x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
||||||
preMapAccumRequest f x extract = go x
|
preMapAccumRequest f x extract = go x
|
||||||
where
|
where
|
||||||
go b = Mealy $ \_ t a ->
|
go b = Mealy mempty $ \_ t a ->
|
||||||
let next = f t b a
|
let next = f t b a
|
||||||
in pure (extract b, go next)
|
in pure (extract b, go next)
|
||||||
|
|
||||||
mapAccum :: (x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
mapAccum :: (x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
||||||
mapAccum f x extract = go x
|
mapAccum f x extract = go x
|
||||||
where
|
where
|
||||||
go b = Mealy $ \_ _ a ->
|
go b = Mealy mempty $ \_ _ a ->
|
||||||
let next = f b a
|
let next = f b a
|
||||||
in pure (extract next, go next)
|
in pure (extract next, go next)
|
||||||
|
|
||||||
mapAccumRequest :: (Request -> x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
mapAccumRequest :: (Request -> x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
||||||
mapAccumRequest f x extract = go x
|
mapAccumRequest f x extract = go x
|
||||||
where
|
where
|
||||||
go b = Mealy $ \_ t a ->
|
go b = Mealy mempty $ \_ t a ->
|
||||||
let next = f t b a
|
let next = f t b a
|
||||||
in pure (extract next, go next)
|
in pure (extract next, go next)
|
||||||
|
|
||||||
@@ -229,14 +264,26 @@ lMerge Tick (Event a) = Event a
|
|||||||
edge :: Mealy eff Bool (Event ())
|
edge :: Mealy eff Bool (Event ())
|
||||||
edge = go False
|
edge = go False
|
||||||
where
|
where
|
||||||
go True = Mealy $ \_ _ -> \case
|
go True = Mealy mempty $ \_ _ -> \case
|
||||||
True -> pure (Tick, go True)
|
True -> pure (Tick, go True)
|
||||||
False -> pure (Tick, go False)
|
False -> pure (Tick, go False)
|
||||||
go False = Mealy $ \_ _ -> \case
|
go False = Mealy mempty $ \_ _ -> \case
|
||||||
True -> pure (Event (), go True)
|
True -> pure (Event (), go True)
|
||||||
False -> pure (Tick, go False)
|
False -> pure (Tick, go False)
|
||||||
|
|
||||||
|
|
||||||
|
-- | Drop the first 'Event' and pass through everything after. Useful for
|
||||||
|
-- ignoring a self-triggered event (e.g. a service call that changes the
|
||||||
|
-- very entity the arrow listens to).
|
||||||
|
dropFirst :: Mealy eff (Event a) (Event a)
|
||||||
|
dropFirst = go False
|
||||||
|
where
|
||||||
|
go seen = Mealy mempty $ \_ _ input ->
|
||||||
|
case input of
|
||||||
|
Event _ | not seen -> pure (Tick, go True)
|
||||||
|
_ -> pure (input, go seen)
|
||||||
|
|
||||||
|
|
||||||
duration :: forall eff a. Mealy eff a NominalDiffTime
|
duration :: forall eff a. Mealy eff a NominalDiffTime
|
||||||
duration = mapAccumRequest go (Nothing @(UTCTime, NominalDiffTime)) (maybe 0 snd)
|
duration = mapAccumRequest go (Nothing @(UTCTime, NominalDiffTime)) (maybe 0 snd)
|
||||||
where
|
where
|
||||||
@@ -289,3 +336,12 @@ fixed seconds = mapAccumRequest go Nothing (maybe [] ((`appEndo` []) . snd))
|
|||||||
| otherwise -> Just (end, acc)
|
| otherwise -> Just (end, acc)
|
||||||
Event a | requestTime req >= end -> Just (addUTCTime (fromIntegral seconds) end, e a)
|
Event a | requestTime req >= end -> Just (addUTCTime (fromIntegral seconds) end, e a)
|
||||||
| otherwise -> Just (end, acc <> e a)
|
| otherwise -> Just (end, acc <> e a)
|
||||||
|
|
||||||
|
|
||||||
|
currentTime :: Mealy eff a LocalTime
|
||||||
|
currentTime = Mealy mempty $ \_ Request{requestTime, requestTimeZone} _ ->
|
||||||
|
pure (utcToLocalTime requestTimeZone requestTime, currentTime)
|
||||||
|
|
||||||
|
|
||||||
|
onEvent :: Mealy eff a () -> Mealy eff (Event a) ()
|
||||||
|
onEvent f = events >>> (arr (const ()) ||| f)
|
||||||
|
|||||||
@@ -23,15 +23,18 @@ module HomeAssistant.Controller
|
|||||||
, traceValue
|
, traceValue
|
||||||
, switch
|
, switch
|
||||||
, Target(..)
|
, Target(..)
|
||||||
|
, brightness
|
||||||
|
, Light(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import AFRP (Mealy (..), eff, Event(..), events, filterA, (>>|), toEvent, Request)
|
import AFRP (Mealy (..), eff, Event(..), events, filterA, (>>|), toEvent, Request)
|
||||||
import Control.Arrow (Arrow(..), returnA)
|
import Control.Arrow (Arrow(..), returnA)
|
||||||
import Control.Category ((>>>))
|
import Control.Category ((>>>))
|
||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value, object, (.=))
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import qualified Data.Set as S
|
||||||
import Control.Lens (has, only, (^?), to)
|
import Control.Lens (has, only, (^?), to)
|
||||||
import Data.Aeson.Lens (key, _String)
|
import Data.Aeson.Lens (key, _String, _Integral)
|
||||||
import qualified Data.Text.Lens as TL
|
import qualified Data.Text.Lens as TL
|
||||||
import Data.Bool (bool)
|
import Data.Bool (bool)
|
||||||
|
|
||||||
@@ -62,7 +65,7 @@ debug = proc x -> do
|
|||||||
returnA -< x
|
returnA -< x
|
||||||
|
|
||||||
traceEvent :: Show a => HASS (Event a) (Event a)
|
traceEvent :: Show a => HASS (Event a) (Event a)
|
||||||
traceEvent = Mealy $ \nt req -> \case
|
traceEvent = Mealy mempty $ \nt req -> \case
|
||||||
Event a -> nt (Trace req a) >>= \() -> pure (Event a, traceEvent)
|
Event a -> nt (Trace req a) >>= \() -> pure (Event a, traceEvent)
|
||||||
Tick -> pure (Tick, traceEvent)
|
Tick -> pure (Tick, traceEvent)
|
||||||
|
|
||||||
@@ -83,12 +86,21 @@ presence entityId =entityBool entityId
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
data Light
|
||||||
|
= Off
|
||||||
|
| On { brightnessPercentage :: Maybe Double }
|
||||||
|
|
||||||
-- Turn off lights when door is closed
|
-- Turn off lights when door is closed
|
||||||
light :: [Target] -> Bool -> Service
|
light :: [Target] -> Light -> Service
|
||||||
light targets b = Service
|
light targets (On {brightnessPercentage}) = Service
|
||||||
{ serviceDomain="light"
|
{ serviceDomain="light"
|
||||||
, serviceName= bool "turn_off" "turn_on" b
|
, serviceName= "turn_on"
|
||||||
|
, serviceData=fmap (\pct -> object ["brightness_pct" .= pct]) brightnessPercentage
|
||||||
|
, serviceTarget=targets
|
||||||
|
}
|
||||||
|
light targets Off = Service
|
||||||
|
{ serviceDomain="light"
|
||||||
|
, serviceName= "turn_off"
|
||||||
, serviceData=Nothing
|
, serviceData=Nothing
|
||||||
, serviceTarget=targets
|
, serviceTarget=targets
|
||||||
}
|
}
|
||||||
@@ -107,20 +119,20 @@ entityChangeEvent :: T.Text -> Mealy eff (Event Value) (Event Value)
|
|||||||
entityChangeEvent entityId = entityChangeEvent' entityId >>> toEvent
|
entityChangeEvent entityId = entityChangeEvent' entityId >>> toEvent
|
||||||
|
|
||||||
entityChangeEvent' :: T.Text -> Mealy eff (Event Value) (Either () Value)
|
entityChangeEvent' :: T.Text -> Mealy eff (Event Value) (Either () Value)
|
||||||
entityChangeEvent' entityId = events >>| filterA isEntity
|
entityChangeEvent' entityId = Mealy (S.singleton entityId) $ runMealy (events >>| filterA isEntity)
|
||||||
where
|
where
|
||||||
isEntity :: Value -> Bool
|
isEntity :: Value -> Bool
|
||||||
isEntity = has (key "event" . key "data" . key "entity_id" . _String . only entityId)
|
isEntity = has (key "event" . key "variables" . key "trigger" . key "entity_id" . _String . only entityId)
|
||||||
|
|
||||||
entityRead' :: (Read a) => T.Text -> Mealy eff (Event Value) (Either () a)
|
entityRead' :: (Read a) => T.Text -> Mealy eff (Event Value) (Either () a)
|
||||||
entityRead' entityId = entityChangeEvent' entityId >>| (arr state >>> arr (maybe (Left ()) Right))
|
entityRead' entityId = entityChangeEvent' entityId >>| (arr state >>> arr (maybe (Left ()) Right))
|
||||||
where
|
where
|
||||||
state v = v ^? key "event" . key "data" . key "new_state" . key "state" . _String . TL.unpacked . to read
|
state v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "state" . _String . TL.unpacked . to read
|
||||||
|
|
||||||
entityBool' :: T.Text -> Mealy eff (Event Value) (Either () Bool)
|
entityBool' :: T.Text -> Mealy eff (Event Value) (Either () Bool)
|
||||||
entityBool' entityId = entityChangeEvent' entityId >>| (arr state >>> arr (maybe (Left ()) Right))
|
entityBool' entityId = entityChangeEvent' entityId >>| (arr state >>> arr (maybe (Left ()) Right))
|
||||||
where
|
where
|
||||||
state v = v ^? key "event" . key "data" . key "new_state" . key "state" . _String . TL.unpacked . to toBool
|
state v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "state" . _String . TL.unpacked . to toBool
|
||||||
toBool = \case
|
toBool = \case
|
||||||
"on" -> True
|
"on" -> True
|
||||||
"off" -> False
|
"off" -> False
|
||||||
@@ -131,3 +143,11 @@ entityRead entityId = entityRead' entityId >>> toEvent
|
|||||||
|
|
||||||
entityBool :: T.Text -> Mealy eff (Event Value) (Event Bool)
|
entityBool :: T.Text -> Mealy eff (Event Value) (Event Bool)
|
||||||
entityBool entityId = entityBool' entityId >>> toEvent
|
entityBool entityId = entityBool' entityId >>> toEvent
|
||||||
|
|
||||||
|
brightness :: T.Text -> HASS (Event Value) (Event Int)
|
||||||
|
brightness entityId =
|
||||||
|
entityChangeEvent' entityId
|
||||||
|
>>| arr (maybe (Left ()) Right . eventBrightness)
|
||||||
|
>>> AFRP.toEvent
|
||||||
|
where
|
||||||
|
eventBrightness v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "brightness" . _Integral
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ bedroomPresenceController :: HASS (Event Value) ()
|
|||||||
bedroomPresenceController = proc x -> do
|
bedroomPresenceController = proc x -> do
|
||||||
p <- bedroomPresence -< x
|
p <- bedroomPresence -< x
|
||||||
case p of
|
case p of
|
||||||
Event Unoccupied -> callService createBedroomScene >>> callService (light bedroomLights False) -< ()
|
Event Unoccupied -> callService createBedroomScene >>> callService (light bedroomLights Off) -< ()
|
||||||
Event Occupied -> callService (activateScene "makuuhuone_lights_snapshot") -< ()
|
Event Occupied -> callService (activateScene "makuuhuone_lights_snapshot") -< ()
|
||||||
_ -> returnA -< ()
|
_ -> returnA -< ()
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ ikeaQuickButton entityId =
|
|||||||
>>| arr (maybe (Left ()) Right . eventType)
|
>>| arr (maybe (Left ()) Right . eventType)
|
||||||
>>> toEvent
|
>>> toEvent
|
||||||
where
|
where
|
||||||
eventType v = v ^? key "event" . key "data" . key "new_state" . key "attributes" . key "event_type" . _String . to toIkeaQuickButton . traversed
|
eventType v = v ^? key "event" . key "variables" . key "trigger" . key "to_state" . key "attributes" . key "event_type" . _String . to toIkeaQuickButton . traversed
|
||||||
|
|
||||||
|
|
||||||
data BedroomControls
|
data BedroomControls
|
||||||
@@ -101,11 +101,11 @@ bedroomButtonController = proc x -> do
|
|||||||
Event (Masse (OnButton ShortRelease)) -> callService (activateScene "scene.makuuhuone_masse") -< ()
|
Event (Masse (OnButton ShortRelease)) -> callService (activateScene "scene.makuuhuone_masse") -< ()
|
||||||
Event (Masse (OnButton DoubleClick)) -> callService (activateScene "scene.makuuhuone_keski") -< ()
|
Event (Masse (OnButton DoubleClick)) -> callService (activateScene "scene.makuuhuone_keski") -< ()
|
||||||
Event (Masse (OnButton LongClick)) -> callService (activateScene "scene.makuuhuone_kirkas") -< ()
|
Event (Masse (OnButton LongClick)) -> callService (activateScene "scene.makuuhuone_kirkas") -< ()
|
||||||
Event (Masse (OffButton _)) -> callService (light [AreaId "makuuhuone"] False) -< ()
|
Event (Masse (OffButton _)) -> callService (light [AreaId "makuuhuone"] Off) -< ()
|
||||||
Event (Enishen (OnButton ShortRelease)) -> callService (activateScene "scene.makuuhuone_jemina") -< ()
|
Event (Enishen (OnButton ShortRelease)) -> callService (activateScene "scene.makuuhuone_jemina") -< ()
|
||||||
Event (Enishen (OnButton DoubleClick)) -> callService (activateScene "scene.makuuhuone_keski") -< ()
|
Event (Enishen (OnButton DoubleClick)) -> callService (activateScene "scene.makuuhuone_keski") -< ()
|
||||||
Event (Enishen (OnButton LongClick)) -> callService (activateScene "scene.makuuhuone_kirkas") -< ()
|
Event (Enishen (OnButton LongClick)) -> callService (activateScene "scene.makuuhuone_kirkas") -< ()
|
||||||
Event (Enishen (OffButton _)) -> callService (light [AreaId "makuuhuone"] False) -< ()
|
Event (Enishen (OffButton _)) -> callService (light [AreaId "makuuhuone"] Off) -< ()
|
||||||
_ -> returnA -< ()
|
_ -> returnA -< ()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,82 @@
|
|||||||
|
{-# LANGUAGE Arrows #-}
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
module HomeAssistant.Controller.Children where
|
module HomeAssistant.Controller.Children where
|
||||||
import HomeAssistant.Controller (HASS)
|
import HomeAssistant.Controller (HASS, callService, Target (AreaId), light, Light(..))
|
||||||
import AFRP (Event)
|
import AFRP (Event)
|
||||||
import Data.Aeson (Value)
|
import qualified AFRP
|
||||||
import Control.Arrow (Arrow(..))
|
import Control.Arrow (Arrow(..), (>>>))
|
||||||
|
import Data.Time (Day, TimeOfDay (..), localDay, LocalTime (..))
|
||||||
|
import Data.Time.Calendar.OrdinalDate (WeekOfYear, mondayStartWeek)
|
||||||
|
import Data.Functor.Contravariant (Predicate (..), (>$<))
|
||||||
|
|
||||||
|
-- Let's see building some reasonable interface for utctime
|
||||||
|
dow :: Day -> (WeekOfYear, Int)
|
||||||
|
dow = mondayStartWeek
|
||||||
|
|
||||||
|
|
||||||
|
weekday :: Predicate Day
|
||||||
|
weekday = Predicate (betweenInclusive 1 5 . snd . dow)
|
||||||
|
where
|
||||||
|
betweenInclusive a b c = c >= a && c <= b
|
||||||
|
|
||||||
|
time :: (Int, Int) -> Predicate TimeOfDay
|
||||||
|
time (h,m) = mconcat
|
||||||
|
[ Predicate (equals h . todHour)
|
||||||
|
, Predicate (equals m . todMin)
|
||||||
|
]
|
||||||
|
where
|
||||||
|
equals a b = a == b
|
||||||
|
|
||||||
|
|
||||||
|
atTime :: Predicate LocalTime -> HASS a (Event ())
|
||||||
|
atTime p = AFRP.currentTime
|
||||||
|
>>> arr (getPredicate p)
|
||||||
|
>>> AFRP.edge
|
||||||
|
|
||||||
-- I don't have any proper presence sensors in their bedroom
|
-- I don't have any proper presence sensors in their bedroom
|
||||||
-- and they are notoriously bad at changing clothes in complete darkness
|
-- and they are notoriously bad at changing clothes in complete darkness
|
||||||
-- So I have set up an automation that attempts to turn on the lights sometime
|
-- So I have set up an automation that attempts to turn on the lights sometime
|
||||||
-- before they leave for school and turns them off a bit later
|
-- before they leave for school and turns them off a bit later
|
||||||
--
|
|
||||||
-- I don't have enough primitives for this yet, leaving as a placeholder
|
-- Don't mconcat these predicates they have && behavior
|
||||||
schoolLightController :: HASS (Event Value) ()
|
-- if you mconcat the actual arrows, they combine the behaviors of the separate branches
|
||||||
schoolLightController = arr (const ())
|
-- essentially becoming || behavior
|
||||||
|
timersOff :: [Predicate LocalTime]
|
||||||
|
timersOff =
|
||||||
|
[ day 1 <> at (08,15)
|
||||||
|
, day 2 <> at (09,15)
|
||||||
|
, day 3 <> at (08,15)
|
||||||
|
, day 4 <> at (08,15)
|
||||||
|
, day 5 <> at (08,15)
|
||||||
|
, at (18,57) -- debug
|
||||||
|
]
|
||||||
|
where
|
||||||
|
dayOfWeek = snd . mondayStartWeek . localDay
|
||||||
|
at (h,m) = localTimeOfDay >$< Predicate (\TimeOfDay{todHour, todMin} -> todHour == h && todMin == m)
|
||||||
|
day n = dayOfWeek >$< Predicate (== n)
|
||||||
|
|
||||||
|
timersOn :: [Predicate LocalTime]
|
||||||
|
timersOn =
|
||||||
|
[ day 1 <> at (07,30)
|
||||||
|
, day 2 <> at (08,30)
|
||||||
|
, day 3 <> at (07,30)
|
||||||
|
, day 4 <> at (07,30)
|
||||||
|
, day 5 <> at (07,30)
|
||||||
|
, at (18,55) -- debug
|
||||||
|
]
|
||||||
|
where
|
||||||
|
dayOfWeek = snd . mondayStartWeek . localDay
|
||||||
|
at (h,m) = localTimeOfDay >$< Predicate (\TimeOfDay{todHour, todMin} -> todHour == h && todMin == m)
|
||||||
|
day n = dayOfWeek >$< Predicate (== n)
|
||||||
|
|
||||||
|
schoolLightController :: HASS a ()
|
||||||
|
schoolLightController = lightsOn <> lightsOff
|
||||||
|
|
||||||
|
|
||||||
|
lightsOn :: HASS a ()
|
||||||
|
lightsOn = foldMap atTime timersOn
|
||||||
|
>>> AFRP.onEvent (callService (light [AreaId "lasten_makuuhuone"] On{brightnessPercentage = Just 100}))
|
||||||
|
|
||||||
|
lightsOff :: HASS a ()
|
||||||
|
lightsOff = foldMap atTime timersOff
|
||||||
|
>>> AFRP.onEvent (callService (light [AreaId "lasten_makuuhuone"] Off))
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import Control.Concurrent.Async (async, waitAny)
|
|||||||
import Control.Concurrent.STM (atomically, dupTChan, readTChan)
|
import Control.Concurrent.STM (atomically, dupTChan, readTChan)
|
||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Time (getCurrentTime)
|
import Data.Time (getCurrentTime, getCurrentTimeZone)
|
||||||
import Data.Void (Void, absurd)
|
import Data.Void (Void, absurd)
|
||||||
import HomeAssistant.Controller (HASS, HASSEff (..))
|
import HomeAssistant.Controller (HASS, HASSEff (..))
|
||||||
import HomeAssistant.Runtime.Bus
|
import HomeAssistant.Runtime.Bus
|
||||||
@@ -33,11 +33,13 @@ import Katip (runKatipT, logF, sl, Severity (..), ls, Namespace (Namespace), run
|
|||||||
import Control.Monad.IO.Class (liftIO, MonadIO)
|
import Control.Monad.IO.Class (liftIO, MonadIO)
|
||||||
import Control.Monad.Fix (MonadFix)
|
import Control.Monad.Fix (MonadFix)
|
||||||
import HomeAssistant.Controller.Ruuvi (ruuviController)
|
import HomeAssistant.Controller.Ruuvi (ruuviController)
|
||||||
|
import HomeAssistant.Controller.Children (schoolLightController)
|
||||||
|
|
||||||
step :: (MonadFix m, MonadIO m) => (forall x. eff x -> m x) -> UUID -> Mealy eff a b -> a -> m (b, Mealy eff a b)
|
step :: (MonadFix m, MonadIO m) => (forall x. eff x -> m x) -> UUID -> Mealy eff a b -> a -> m (b, Mealy eff a b)
|
||||||
step nt trace (Mealy f) a = do
|
step nt trace (Mealy _ f) a = do
|
||||||
now <- liftIO getCurrentTime
|
now <- liftIO getCurrentTime
|
||||||
f nt (Request now trace) a
|
tz <- liftIO getCurrentTimeZone
|
||||||
|
f nt (Request now tz trace) a
|
||||||
|
|
||||||
data Controller = forall b. Controller T.Text (HASS (Event Value) b) Bool
|
data Controller = forall b. Controller T.Text (HASS (Event Value) b) Bool
|
||||||
|
|
||||||
@@ -48,6 +50,7 @@ controllers =
|
|||||||
, Controller "bedroom-drawer" bedroomDrawerController True
|
, Controller "bedroom-drawer" bedroomDrawerController True
|
||||||
, Controller "bedroom-humidifier" humidifierController False
|
, Controller "bedroom-humidifier" humidifierController False
|
||||||
, Controller "ruuvi-controller" ruuviController False
|
, Controller "ruuvi-controller" ruuviController False
|
||||||
|
, Controller "school-light-controller" schoolLightController True
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | Steps the machine for every inbound message; service calls go to the
|
-- | Steps the machine for every inbound message; service calls go to the
|
||||||
@@ -62,7 +65,7 @@ runController bus (Controller name machine _enabled) = do
|
|||||||
msg <- atomically (readTChan inbound)
|
msg <- atomically (readTChan inbound)
|
||||||
uuid <- UUID.V4.nextRandom
|
uuid <- UUID.V4.nextRandom
|
||||||
let ns = Namespace [name]
|
let ns = Namespace [name]
|
||||||
(_, f') <- step (runKatipContextT (busLogEnv bus) () ns . channelHassEval bus) uuid f (Event msg)
|
(_, f') <- step (runKatipContextT (busLogEnv bus) () ns . channelHassEval bus) uuid f msg
|
||||||
go inbound f'
|
go inbound f'
|
||||||
|
|
||||||
defaultMain :: IO ()
|
defaultMain :: IO ()
|
||||||
@@ -71,8 +74,10 @@ defaultMain = withSocketsDo $ do
|
|||||||
withBus severity $ \bus -> do
|
withBus severity $ \bus -> do
|
||||||
token <- getEnv "HA_TOKEN"
|
token <- getEnv "HA_TOKEN"
|
||||||
host <- getEnv "HA_HOST"
|
host <- getEnv "HA_HOST"
|
||||||
let workers =
|
let active = [c | c@(Controller _ _ True) <- controllers]
|
||||||
[ ("reader", readerAction host 8123 token bus)
|
ents = foldMap (\(Controller _ m _) -> entities m) active
|
||||||
|
workers =
|
||||||
|
[ ("reader", readerAction host 8123 token ents bus)
|
||||||
, ("writer", writerAction bus)
|
, ("writer", writerAction bus)
|
||||||
] ++ [ (name, runController bus c) | c@(Controller name _ True) <- controllers ]
|
] ++ [ (name, runController bus c) | c@(Controller name _ True) <- controllers ]
|
||||||
as <- mapM (\(name, act) -> async (supervised name defaultBackoff act)) workers
|
as <- mapM (\(name, act) -> async (supervised name defaultBackoff act)) workers
|
||||||
|
|||||||
@@ -26,14 +26,14 @@ import Katip (LogEnv, closeScribes, mkHandleScribe, ColorStrategy (..), permitIt
|
|||||||
import Control.Exception (bracket)
|
import Control.Exception (bracket)
|
||||||
import System.IO (stdout)
|
import System.IO (stdout)
|
||||||
import Data.UUID (toText)
|
import Data.UUID (toText)
|
||||||
import AFRP (Request(..))
|
import AFRP (Request(..), Event(..))
|
||||||
import Control.Monad.IO.Class (MonadIO, liftIO)
|
import Control.Monad.IO.Class (MonadIO, liftIO)
|
||||||
|
|
||||||
-- | Shared runtime state: inbound is a broadcast channel (controllers
|
-- | Shared runtime state: inbound is a broadcast channel (controllers
|
||||||
-- read from 'dupTChan' copies), outbound queues service calls for the
|
-- read from 'dupTChan' copies), outbound queues service calls for the
|
||||||
-- writer, conn holds the current websocket (Nothing before first connect).
|
-- writer, conn holds the current websocket (Nothing before first connect).
|
||||||
data Bus = Bus
|
data Bus = Bus
|
||||||
{ busInbound :: TChan Value
|
{ busInbound :: TChan (Event Value)
|
||||||
, busOutbound :: TChan (Request, Service)
|
, busOutbound :: TChan (Request, Service)
|
||||||
, busConn :: TVar (Maybe Connection)
|
, busConn :: TVar (Maybe Connection)
|
||||||
, busGen :: CallIdGen
|
, busGen :: CallIdGen
|
||||||
|
|||||||
@@ -15,13 +15,15 @@ import Control.Concurrent.STM
|
|||||||
, writeTChan
|
, writeTChan
|
||||||
, writeTVar
|
, writeTVar
|
||||||
)
|
)
|
||||||
|
import Control.Concurrent.Async (race)
|
||||||
|
import Control.Concurrent (threadDelay)
|
||||||
import Control.Exception (onException)
|
import Control.Exception (onException)
|
||||||
import Control.Exception.Annotated (throw)
|
import Control.Exception.Annotated (throw)
|
||||||
import Control.Lens ((^?))
|
import Control.Lens ((^?))
|
||||||
import Control.Monad (forever)
|
import Control.Monad (forever, forM_)
|
||||||
import Data.Aeson (Value, eitherDecode, encode, object, (.=))
|
import Data.Aeson (Value, eitherDecode, encode, object, (.=))
|
||||||
import Data.Aeson.Lens (key, _String)
|
import Data.Aeson.Lens (key, _String)
|
||||||
import qualified Data.ByteString.Lazy as BL
|
import qualified Data.Set as S
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Void (Void)
|
import Data.Void (Void)
|
||||||
import HomeAssistant.Controller (Service (..), Target (..))
|
import HomeAssistant.Controller (Service (..), Target (..))
|
||||||
@@ -30,16 +32,16 @@ import HomeAssistant.Runtime.Supervisor (Fatal (..))
|
|||||||
import qualified Network.WebSockets as WS
|
import qualified Network.WebSockets as WS
|
||||||
import Katip (runKatipContextT, sl, logFM, Severity (..), ls)
|
import Katip (runKatipContextT, sl, logFM, Severity (..), ls)
|
||||||
import Data.UUID (toText)
|
import Data.UUID (toText)
|
||||||
import AFRP (Request(..))
|
import AFRP (Request(..), Event(..))
|
||||||
|
|
||||||
-- | Connect, authenticate, subscribe, then receive and broadcast forever.
|
-- | Connect, authenticate, subscribe, then receive and broadcast forever.
|
||||||
-- Restarting this action reconnects. All setup sends happen before the
|
-- Restarting this action reconnects. All setup sends happen before the
|
||||||
-- connection is published in the bus, so only the writer sends afterwards.
|
-- connection is published in the bus, so only the writer sends afterwards.
|
||||||
readerAction :: String -> Int -> String -> Bus -> IO Void
|
readerAction :: String -> Int -> String -> S.Set T.Text -> Bus -> IO Void
|
||||||
readerAction host port token bus =
|
readerAction host port token ents bus =
|
||||||
WS.runClient host port "/api/websocket" $ \conn -> do
|
WS.runClient host port "/api/websocket" $ \conn -> do
|
||||||
handshake conn token
|
handshake conn token
|
||||||
subscribe bus conn
|
subscribe bus conn ents
|
||||||
atomically $ writeTVar (busConn bus) (Just conn)
|
atomically $ writeTVar (busConn bus) (Just conn)
|
||||||
putStrLn "[reader] connected"
|
putStrLn "[reader] connected"
|
||||||
-- Unpublish on exit so the writer blocks and the backlog survives the outage.
|
-- Unpublish on exit so the writer blocks and the backlog survives the outage.
|
||||||
@@ -62,23 +64,34 @@ expectType expected msg =
|
|||||||
Just t | t == expected -> pure ()
|
Just t | t == expected -> pure ()
|
||||||
_ -> throw (Fatal $ "expected " <> expected <> ", got: " <> T.pack (show msg))
|
_ -> throw (Fatal $ "expected " <> expected <> ", got: " <> T.pack (show msg))
|
||||||
|
|
||||||
subscribe :: Bus -> WS.Connection -> IO ()
|
subscribe :: Bus -> WS.Connection -> S.Set T.Text -> IO ()
|
||||||
subscribe bus conn = do
|
subscribe bus conn ents =
|
||||||
|
forM_ (S.toList ents) $ \entityId -> do
|
||||||
|
print entityId
|
||||||
sid <- generateCallId (busGen bus)
|
sid <- generateCallId (busGen bus)
|
||||||
WS.sendTextData conn $ encode $ object
|
WS.sendTextData conn $ encode $ object
|
||||||
[ "id" .= sid
|
[ "id" .= sid
|
||||||
, "type" .= ("subscribe_events" :: T.Text)
|
, "type" .= ("subscribe_trigger" :: T.Text)
|
||||||
, "event_type" .= ("state_changed" :: T.Text)
|
, "trigger" .= object
|
||||||
|
[ "platform" .= ("state" :: T.Text)
|
||||||
|
, "entity_id" .= entityId
|
||||||
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | Undecodable messages are skipped: reconnecting cannot fix a decode
|
-- | Undecodable messages are skipped: reconnecting cannot fix a decode
|
||||||
-- problem, so crashing here would only produce a hot restart loop.
|
-- problem, so crashing here would only produce a hot restart loop.
|
||||||
|
--
|
||||||
|
-- Each read races a one-second timeout: a timeout broadcasts 'Tick' so
|
||||||
|
-- time-based primitives (debounce, rollup, fixed, ...) keep advancing
|
||||||
|
-- even when no state changes arrive.
|
||||||
receiveLoop :: Bus -> WS.Connection -> IO Void
|
receiveLoop :: Bus -> WS.Connection -> IO Void
|
||||||
receiveLoop bus conn = forever $ do
|
receiveLoop bus conn = forever $ do
|
||||||
msg <- WS.receiveData conn :: IO BL.ByteString
|
winner <- race (threadDelay 1000000) (WS.receiveData conn)
|
||||||
case eitherDecode msg of
|
case winner of
|
||||||
|
Left () -> atomically $ writeTChan (busInbound bus) Tick
|
||||||
|
Right msg -> case eitherDecode msg of
|
||||||
Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err
|
Left err -> putStrLn $ "[reader] skipping undecodable message: " <> err
|
||||||
Right v -> atomically $ writeTChan (busInbound bus) v
|
Right v -> atomically $ writeTChan (busInbound bus) (Event v)
|
||||||
|
|
||||||
receiveJSON :: WS.Connection -> IO Value
|
receiveJSON :: WS.Connection -> IO Value
|
||||||
receiveJSON conn = do
|
receiveJSON conn = do
|
||||||
|
|||||||
+103
-13
@@ -1,13 +1,17 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
module AFRPSpec (spec) where
|
module AFRPSpec (spec) where
|
||||||
|
|
||||||
import Control.Arrow (arr)
|
import Control.Arrow (arr, (&&&), first, left)
|
||||||
import Control.Category ((>>>))
|
import Control.Category ((>>>))
|
||||||
import Control.Monad.Fix (MonadFix (..))
|
import Control.Monad.Fix (MonadFix (..))
|
||||||
import Data.Foldable (for_)
|
import Data.Foldable (for_)
|
||||||
import AFRP
|
import AFRP
|
||||||
import Data.Functor.Identity (Identity (..))
|
import Data.Functor.Identity (Identity (..))
|
||||||
import Data.List (sort)
|
import Data.List (sort)
|
||||||
import Data.Time (NominalDiffTime, UTCTime (..), addUTCTime, diffUTCTime)
|
import qualified Data.Set as S
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import Data.Time (NominalDiffTime, UTCTime (..), utc)
|
||||||
import Data.UUID (nil)
|
import Data.UUID (nil)
|
||||||
import Hedgehog
|
import Hedgehog
|
||||||
import qualified Hedgehog.Gen as Gen
|
import qualified Hedgehog.Gen as Gen
|
||||||
@@ -16,7 +20,7 @@ import Test.Hspec
|
|||||||
import Test.Hspec.Hedgehog
|
import Test.Hspec.Hedgehog
|
||||||
|
|
||||||
fakeRequest :: Request
|
fakeRequest :: Request
|
||||||
fakeRequest = Request (sec 0) nil
|
fakeRequest = Request (sec 0) utc nil
|
||||||
|
|
||||||
sec :: Integer -> UTCTime
|
sec :: Integer -> UTCTime
|
||||||
sec n = UTCTime (toEnum 0) (fromIntegral n)
|
sec n = UTCTime (toEnum 0) (fromIntegral n)
|
||||||
@@ -30,7 +34,7 @@ runPure m (a : as) = case runIdentity (AFRP.runMealy m id fakeRequest a) of
|
|||||||
runTimed :: Mealy Identity a b -> [(Integer, a)] -> [b]
|
runTimed :: Mealy Identity a b -> [(Integer, a)] -> [b]
|
||||||
runTimed _ [] = []
|
runTimed _ [] = []
|
||||||
runTimed m ((s, a) : as) =
|
runTimed m ((s, a) : as) =
|
||||||
case runIdentity (AFRP.runMealy m id (Request (sec s) nil) a) of
|
case runIdentity (AFRP.runMealy m id (Request (sec s) utc nil) a) of
|
||||||
(b, m') -> b : runTimed m' as
|
(b, m') -> b : runTimed m' as
|
||||||
|
|
||||||
-- | A minimal State monad for observing effectful arrows (e.g. whenA gating).
|
-- | A minimal State monad for observing effectful arrows (e.g. whenA gating).
|
||||||
@@ -59,6 +63,7 @@ runStEff m s0 as = go m s0 as
|
|||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = describe "AFRP" $ do
|
spec = describe "AFRP" $ do
|
||||||
|
entitiesSpec
|
||||||
holdSpec
|
holdSpec
|
||||||
eventsSpec
|
eventsSpec
|
||||||
isEventSpec
|
isEventSpec
|
||||||
@@ -67,6 +72,7 @@ spec = describe "AFRP" $ do
|
|||||||
lMergeSpec
|
lMergeSpec
|
||||||
changesSpec
|
changesSpec
|
||||||
edgeSpec
|
edgeSpec
|
||||||
|
dropFirstSpec
|
||||||
filterASpec
|
filterASpec
|
||||||
slidingSpec
|
slidingSpec
|
||||||
mapAccumSpec
|
mapAccumSpec
|
||||||
@@ -134,6 +140,23 @@ lMergeSpec = describe "lMerge" $ do
|
|||||||
it "prefers right Event if left is Tick" $
|
it "prefers right Event if left is Tick" $
|
||||||
lMerge Tick (Event (2 :: Int)) `shouldBe` Event (2 :: Int)
|
lMerge Tick (Event (2 :: Int)) `shouldBe` Event (2 :: Int)
|
||||||
|
|
||||||
|
it "Tick is a left identity" $
|
||||||
|
hedgehog $ do
|
||||||
|
e <- forAll eventGen
|
||||||
|
lMerge Tick e === e
|
||||||
|
|
||||||
|
it "Tick is a right identity" $
|
||||||
|
hedgehog $ do
|
||||||
|
e <- forAll eventGen
|
||||||
|
lMerge e Tick === e
|
||||||
|
|
||||||
|
it "is associative" $
|
||||||
|
hedgehog $ do
|
||||||
|
a <- forAll eventGen
|
||||||
|
b <- forAll eventGen
|
||||||
|
c <- forAll eventGen
|
||||||
|
lMerge a (lMerge b c) === lMerge (lMerge a b) c
|
||||||
|
|
||||||
changesSpec :: Spec
|
changesSpec :: Spec
|
||||||
changesSpec = describe "changes" $ do
|
changesSpec = describe "changes" $ do
|
||||||
it "first output is always Tick" $
|
it "first output is always Tick" $
|
||||||
@@ -188,6 +211,20 @@ edgeSpec = describe "edge" $ do
|
|||||||
else o' === Tick
|
else o' === Tick
|
||||||
_ -> failure
|
_ -> failure
|
||||||
|
|
||||||
|
dropFirstSpec :: Spec
|
||||||
|
dropFirstSpec = describe "dropFirst" $ do
|
||||||
|
it "drops the first Event, passes the rest" $
|
||||||
|
runPure dropFirst [Tick, Event 1, Event 2, Event 3]
|
||||||
|
`shouldBe` [Tick, Tick, Event 2, Event 3 :: Event Int]
|
||||||
|
|
||||||
|
it "passes Tick through untouched before first Event" $
|
||||||
|
runPure dropFirst [Tick, Tick, Tick :: Event Int]
|
||||||
|
`shouldBe` [Tick, Tick, Tick :: Event Int]
|
||||||
|
|
||||||
|
it "drops only the first Event, Ticks before it are inert" $
|
||||||
|
runPure dropFirst [Tick, Tick, Event 'a', Tick, Event 'b']
|
||||||
|
`shouldBe` [Tick, Tick, Tick, Tick, Event 'b' :: Event Char]
|
||||||
|
|
||||||
filterASpec :: Spec
|
filterASpec :: Spec
|
||||||
filterASpec = describe "filterA" $ do
|
filterASpec = describe "filterA" $ do
|
||||||
it "lets through values matching predicate" $
|
it "lets through values matching predicate" $
|
||||||
@@ -245,7 +282,7 @@ mapAccumSpec = describe "mapAccum" $ do
|
|||||||
for_ (zip3 [0 ..] xs out) $ \(i, _x, cur) ->
|
for_ (zip3 [0 ..] xs out) $ \(i, _x, cur) ->
|
||||||
cur === sum (take (i + 1) xs)
|
cur === sum (take (i + 1) xs)
|
||||||
|
|
||||||
preMapAccumSpec :: Spec
|
preMapAccumSpec :: Spec
|
||||||
preMapAccumSpec = describe "preMapAccum" $ do
|
preMapAccumSpec = describe "preMapAccum" $ do
|
||||||
it "running sum with pre-state extraction" $
|
it "running sum with pre-state extraction" $
|
||||||
runPure (preMapAccum (+) (0 :: Int) id) [1, 2, 3]
|
runPure (preMapAccum (+) (0 :: Int) id) [1, 2, 3]
|
||||||
@@ -411,7 +448,7 @@ effSpec :: Spec
|
|||||||
effSpec = describe "eff" $ do
|
effSpec = describe "eff" $ do
|
||||||
it "lifts a pure effect function into a stateless Mealy" $
|
it "lifts a pure effect function into a stateless Mealy" $
|
||||||
runPure (eff (\_ x -> Identity (x + 1))) [1, 2, 3]
|
runPure (eff (\_ x -> Identity (x + 1))) [1, 2, 3]
|
||||||
`shouldBe` [2, 3, 4]
|
`shouldBe` [2 :: Int, 3, 4]
|
||||||
|
|
||||||
it "output equals f(input) for every step" $
|
it "output equals f(input) for every step" $
|
||||||
hedgehog $ do
|
hedgehog $ do
|
||||||
@@ -422,7 +459,7 @@ effSpec = describe "eff" $ do
|
|||||||
switchSpec :: Spec
|
switchSpec :: Spec
|
||||||
switchSpec = describe "switch" $ do
|
switchSpec = describe "switch" $ do
|
||||||
it "switches to the continuation at the first Event" $
|
it "switches to the continuation at the first Event" $
|
||||||
runPure (switch (arr (\x -> (x, if x >= 3 then Event () else Tick)))
|
runPure (switch (arr (\x -> (x, if x >= (3 :: Int) then Event () else Tick)))
|
||||||
(const (arr (const 99))))
|
(const (arr (const 99))))
|
||||||
[1, 2, 3, 4, 5]
|
[1, 2, 3, 4, 5]
|
||||||
`shouldBe` [1, 2, 99, 99, 99]
|
`shouldBe` [1, 2, 99, 99, 99]
|
||||||
@@ -431,14 +468,14 @@ switchSpec = describe "switch" $ do
|
|||||||
runPure (switch (arr (\x -> (x, Tick :: Event ())))
|
runPure (switch (arr (\x -> (x, Tick :: Event ())))
|
||||||
(const (arr (const 99))))
|
(const (arr (const 99))))
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
`shouldBe` [1, 2, 3]
|
`shouldBe` [1, 2, 3 :: Int]
|
||||||
|
|
||||||
it "prefix outputs come from the first arrow, suffix from the continuation" $
|
it "prefix outputs come from the first arrow, suffix from the continuation" $
|
||||||
hedgehog $ do
|
hedgehog $ do
|
||||||
threshold <- forAll $ Gen.int (Range.linear (-20) 20)
|
threshold <- forAll $ Gen.int (Range.linear (-20) 20)
|
||||||
xs <- forAll $ Gen.list (Range.linear 0 30) (Gen.int (Range.linear (-20) 20))
|
xs <- forAll $ Gen.list (Range.linear 0 30) (Gen.int (Range.linear (-20) 20))
|
||||||
let first = arr (\x -> (x, if x >= threshold then Event () else Tick))
|
let firstArr = arr (\x -> (x, if x >= threshold then Event () else Tick))
|
||||||
out = runPure (switch first (const (arr (const 99)))) xs
|
out = runPure (switch firstArr (const (arr (const 99)))) xs
|
||||||
(pre, _post) = break (>= threshold) xs
|
(pre, _post) = break (>= threshold) xs
|
||||||
take (length pre) out === pre
|
take (length pre) out === pre
|
||||||
drop (length pre) out === replicate (length xs - length pre) 99
|
drop (length pre) out === replicate (length xs - length pre) 99
|
||||||
@@ -508,7 +545,7 @@ thenASpec = describe "thenA" $ do
|
|||||||
xs <- forAll $ Gen.list (Range.linear 0 30) (Gen.int (Range.linear (-20) 20))
|
xs <- forAll $ Gen.list (Range.linear 0 30) (Gen.int (Range.linear (-20) 20))
|
||||||
let out = runPure (filterA (even @Int) >>| filterA (> 0)) xs
|
let out = runPure (filterA (even @Int) >>| filterA (> 0)) xs
|
||||||
expected =
|
expected =
|
||||||
[ if not (even x) then Left ()
|
[ if odd x then Left ()
|
||||||
else if x > 0 then Right x
|
else if x > 0 then Right x
|
||||||
else Left ()
|
else Left ()
|
||||||
| x <- xs ]
|
| x <- xs ]
|
||||||
@@ -518,14 +555,67 @@ sampleSpec :: Spec
|
|||||||
sampleSpec = describe "sample" $ do
|
sampleSpec = describe "sample" $ do
|
||||||
it "tags the current value onto the Event structure" $
|
it "tags the current value onto the Event structure" $
|
||||||
runPure sample [(1, Tick), (2, Event 'a'), (3, Tick)]
|
runPure sample [(1, Tick), (2, Event 'a'), (3, Tick)]
|
||||||
`shouldBe` [Tick, Event 2, Tick]
|
`shouldBe` [Tick, Event @Int 2, Tick]
|
||||||
|
|
||||||
it "output is Event a iff the input event is present" $
|
it "output is Event a iff the input event is present" $
|
||||||
hedgehog $ do
|
hedgehog $ do
|
||||||
vals <- forAll $ Gen.list (Range.linear 0 30) (Gen.int (Range.linear 0 100))
|
vals <- forAll $ Gen.list (Range.linear 0 30) (Gen.int (Range.linear 0 100))
|
||||||
evs <- forAll $ Gen.list (Range.linear 0 30) eventGen
|
evs <- forAll $ Gen.list (Range.linear 0 30) eventGen
|
||||||
let n = min (length vals) (length evs)
|
let n = min (length vals) (length evs)
|
||||||
ps = zip (take n vals) (take n evs)
|
ps = take n $ zip vals evs
|
||||||
out = runPure sample ps
|
out = runPure sample ps
|
||||||
for_ (zip ps out) $ \((v, ev), o) ->
|
for_ (zip ps out) $ \((v, ev), o) ->
|
||||||
o === tag v ev
|
o === tag v ev
|
||||||
|
|
||||||
|
-- | A stateless arrow carrying a fixed entity set, for testing propagation.
|
||||||
|
subscribed :: S.Set T.Text -> Mealy Identity Int Int
|
||||||
|
subscribed ents = Mealy ents $ \_ _ a -> pure (a, subscribed ents)
|
||||||
|
|
||||||
|
-- | Same as 'subscribed' but yields a function, for testing '<*>'.
|
||||||
|
subscribedF :: S.Set T.Text -> Mealy Identity Int (Int -> Int)
|
||||||
|
subscribedF ents = Mealy ents $ \_ _ a -> pure ((a +), subscribedF ents)
|
||||||
|
|
||||||
|
entitiesSpec :: Spec
|
||||||
|
entitiesSpec = describe "entities" $ do
|
||||||
|
it "id carries no entities" $
|
||||||
|
entities (arr id :: Mealy Identity Int Int) `shouldBe` S.empty
|
||||||
|
|
||||||
|
it "arr carries no entities" $
|
||||||
|
entities (arr (+ 1) :: Mealy Identity Int Int) `shouldBe` S.empty
|
||||||
|
|
||||||
|
it "eff carries no entities" $
|
||||||
|
entities (eff (\_ x -> Identity (x + 1 :: Int))) `shouldBe` S.empty
|
||||||
|
|
||||||
|
it "primitive combinators carry no entities" $ do
|
||||||
|
entities (hold 'a') `shouldBe` S.empty
|
||||||
|
entities (changes @Int) `shouldBe` S.empty
|
||||||
|
entities edge `shouldBe` S.empty
|
||||||
|
entities (sliding (3 :: Int)) `shouldBe` S.empty
|
||||||
|
|
||||||
|
it "Category (.) unions entity sets" $
|
||||||
|
entities (subscribed (S.singleton "a") >>> subscribed (S.singleton "b"))
|
||||||
|
`shouldBe` S.fromList ["a", "b"]
|
||||||
|
|
||||||
|
it "Applicative (<*>) unions entity sets" $
|
||||||
|
entities (subscribedF (S.singleton "a") <*> subscribed (S.singleton "b"))
|
||||||
|
`shouldBe` S.fromList ["a", "b"]
|
||||||
|
|
||||||
|
it "Arrow (&&&) unions entity sets" $
|
||||||
|
entities (subscribed (S.singleton "a") &&& subscribed (S.singleton "b"))
|
||||||
|
`shouldBe` S.fromList ["a", "b"]
|
||||||
|
|
||||||
|
it "(>>>) unions entity sets" $
|
||||||
|
entities (subscribed (S.singleton "a") >>> arr id >>> subscribed (S.singleton "b"))
|
||||||
|
`shouldBe` S.fromList ["a", "b"]
|
||||||
|
|
||||||
|
it "left preserves the entity set" $
|
||||||
|
entities (left (subscribed (S.singleton "a")) :: Mealy Identity (Either Int Int) (Either Int Int))
|
||||||
|
`shouldBe` S.singleton "a"
|
||||||
|
|
||||||
|
it "first preserves the entity set" $
|
||||||
|
entities (first (subscribed (S.singleton "a")) :: Mealy Identity (Int, Int) (Int, Int))
|
||||||
|
`shouldBe` S.singleton "a"
|
||||||
|
|
||||||
|
it "fmap preserves the entity set" $
|
||||||
|
entities (fmap (+ 1) (subscribed (S.singleton "a")))
|
||||||
|
`shouldBe` S.singleton "a"
|
||||||
|
|||||||
+34
-3
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
module BedroomSpec (spec) where
|
module BedroomSpec (spec) where
|
||||||
|
|
||||||
import AFRP (Event (..))
|
import AFRP (Event (..), entities)
|
||||||
|
import qualified Data.Set as S
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value)
|
||||||
import HomeAssistant.Controller
|
import HomeAssistant.Controller
|
||||||
@@ -21,9 +22,39 @@ drawerState = Event . stateEvent "binary_sensor.bedroom_nightstand_drawer_sensor
|
|||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = describe "Bedroom" $ do
|
spec = describe "Bedroom" $ do
|
||||||
|
entitySpec
|
||||||
drawerSpec
|
drawerSpec
|
||||||
buttonSpec
|
buttonSpec
|
||||||
|
|
||||||
|
entitySpec :: Spec
|
||||||
|
entitySpec = describe "entities" $ do
|
||||||
|
it "entityChangeEvent' registers its entity id" $
|
||||||
|
entities (entityChangeEvent' "sensor.foo")
|
||||||
|
`shouldBe` S.singleton "sensor.foo"
|
||||||
|
|
||||||
|
it "entityRead / entityBool inherit the entity id" $ do
|
||||||
|
entities (entityRead @Double "sensor.bar") `shouldBe` S.singleton "sensor.bar"
|
||||||
|
entities (entityBool "binary_sensor.baz") `shouldBe` S.singleton "binary_sensor.baz"
|
||||||
|
|
||||||
|
it "bedroomDrawerController subscribes to the drawer sensor" $
|
||||||
|
entities bedroomDrawerController
|
||||||
|
`shouldBe` S.singleton "binary_sensor.bedroom_nightstand_drawer_sensor_masse_contact"
|
||||||
|
|
||||||
|
it "bedroomButtonController subscribes to both remote event entities" $
|
||||||
|
entities bedroomButtonController
|
||||||
|
`shouldBe` S.fromList
|
||||||
|
[ "event.bedroom_quick_remote_masse_action"
|
||||||
|
, "event.bedroom_quick_jemina_action"
|
||||||
|
]
|
||||||
|
|
||||||
|
it "bedroomPresenceController subscribes to the presence sensor" $
|
||||||
|
entities bedroomPresenceController
|
||||||
|
`shouldBe` S.singleton "binary_sensor.presence_sensor_bedroom_occupancy"
|
||||||
|
|
||||||
|
it "humidifierController subscribes to the door sensor" $
|
||||||
|
entities humidifierController
|
||||||
|
`shouldBe` S.singleton "binary_sensor.makuuhuone_ovi_contact"
|
||||||
|
|
||||||
drawerSpec :: Spec
|
drawerSpec :: Spec
|
||||||
drawerSpec = describe "bedroomDrawerController" $ do
|
drawerSpec = describe "bedroomDrawerController" $ do
|
||||||
let entity = EntityId "switch.bedroom_drawer_light_masse"
|
let entity = EntityId "switch.bedroom_drawer_light_masse"
|
||||||
@@ -71,7 +102,7 @@ buttonSpec = describe "bedroomButtonController" $ do
|
|||||||
|
|
||||||
it "Masse off click turns the bedroom lights off" $
|
it "Masse off click turns the bedroom lights off" $
|
||||||
services (runHASS bedroomButtonController [masseButton "2_short_release"])
|
services (runHASS bedroomButtonController [masseButton "2_short_release"])
|
||||||
`shouldBe` [[light [area] False]]
|
`shouldBe` [[light [area] Off]]
|
||||||
|
|
||||||
it "Enishen single click turns on her nightstand scene (lowest)" $
|
it "Enishen single click turns on her nightstand scene (lowest)" $
|
||||||
services (runHASS bedroomButtonController [enishenButton "1_short_release"])
|
services (runHASS bedroomButtonController [enishenButton "1_short_release"])
|
||||||
@@ -87,7 +118,7 @@ buttonSpec = describe "bedroomButtonController" $ do
|
|||||||
|
|
||||||
it "Enishen off click turns the bedroom lights off" $
|
it "Enishen off click turns the bedroom lights off" $
|
||||||
services (runHASS bedroomButtonController [enishenButton "2_short_release"])
|
services (runHASS bedroomButtonController [enishenButton "2_short_release"])
|
||||||
`shouldBe` [[light [area] False]]
|
`shouldBe` [[light [area] Off]]
|
||||||
|
|
||||||
it "ignores the initial press (scene only fires on release)" $
|
it "ignores the initial press (scene only fires on release)" $
|
||||||
services (runHASS bedroomButtonController [masseButton "1_initial_press"])
|
services (runHASS bedroomButtonController [masseButton "1_initial_press"])
|
||||||
|
|||||||
+7
-7
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
module BusSpec (spec) where
|
module BusSpec (spec) where
|
||||||
|
|
||||||
import AFRP (Request (..))
|
import AFRP (Request (..), Event (..))
|
||||||
import Control.Concurrent.STM
|
import Control.Concurrent.STM
|
||||||
( atomically
|
( atomically
|
||||||
, dupTChan
|
, dupTChan
|
||||||
@@ -10,7 +10,7 @@ import Control.Concurrent.STM
|
|||||||
, writeTChan
|
, writeTChan
|
||||||
)
|
)
|
||||||
import Data.Aeson (Value (..))
|
import Data.Aeson (Value (..))
|
||||||
import Data.Time (UTCTime (..))
|
import Data.Time (UTCTime (..), utc)
|
||||||
import Data.UUID (nil)
|
import Data.UUID (nil)
|
||||||
import HomeAssistant.Controller (HASSEff (..), Service (..), Target(..))
|
import HomeAssistant.Controller (HASSEff (..), Service (..), Target(..))
|
||||||
import HomeAssistant.Runtime.Bus
|
import HomeAssistant.Runtime.Bus
|
||||||
@@ -22,15 +22,15 @@ spec = describe "Bus" $ do
|
|||||||
it "broadcasts inbound messages to every dup'd channel in order" $ withBus InfoS $ \bus -> do
|
it "broadcasts inbound messages to every dup'd channel in order" $ withBus InfoS $ \bus -> do
|
||||||
p1 <- atomically $ dupTChan (busInbound bus)
|
p1 <- atomically $ dupTChan (busInbound bus)
|
||||||
p2 <- atomically $ dupTChan (busInbound bus)
|
p2 <- atomically $ dupTChan (busInbound bus)
|
||||||
atomically $ writeTChan (busInbound bus) (Number 1)
|
atomically $ writeTChan (busInbound bus) (Event (Number 1))
|
||||||
atomically $ writeTChan (busInbound bus) (Number 2)
|
atomically $ writeTChan (busInbound bus) (Event (Number 2))
|
||||||
r1 <- atomically $ (,) <$> readTChan p1 <*> readTChan p1
|
r1 <- atomically $ (,) <$> readTChan p1 <*> readTChan p1
|
||||||
r2 <- atomically $ (,) <$> readTChan p2 <*> readTChan p2
|
r2 <- atomically $ (,) <$> readTChan p2 <*> readTChan p2
|
||||||
r1 `shouldBe` (Number 1, Number 2)
|
r1 `shouldBe` (Event (Number 1), Event (Number 2))
|
||||||
r2 `shouldBe` (Number 1, Number 2)
|
r2 `shouldBe` (Event (Number 1), Event (Number 2))
|
||||||
|
|
||||||
it "channelHassEval writes CallService to the outbound channel" $ withBus InfoS $ \bus -> do
|
it "channelHassEval writes CallService to the outbound channel" $ withBus InfoS $ \bus -> do
|
||||||
let req = Request (UTCTime (toEnum 0) 0) nil
|
let req = Request (UTCTime (toEnum 0) 0) utc nil
|
||||||
svc = Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"]
|
svc = Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"]
|
||||||
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
|
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
|
||||||
channelHassEval bus (CallService req svc)
|
channelHassEval bus (CallService req svc)
|
||||||
|
|||||||
+7
-5
@@ -16,9 +16,9 @@ spec = pure ()
|
|||||||
-- putStrLn "Before the delay"
|
-- putStrLn "Before the delay"
|
||||||
-- threadDelay 100000 -- let the controller dup its inbound channel
|
-- threadDelay 100000 -- let the controller dup its inbound channel
|
||||||
-- putStrLn "After the delay"
|
-- putStrLn "After the delay"
|
||||||
-- atomically $ writeTChan (busInbound bus) (doorEvent "on") -- initial value: no change event
|
-- atomically $ writeTChan (busInbound bus) (Event (doorEvent "on")) -- initial value: no change event
|
||||||
-- atomically $ writeTChan (busInbound bus) (doorEvent "off") -- door closes: lights on
|
-- atomically $ writeTChan (busInbound bus) (Event (doorEvent "off")) -- door closes: lights on
|
||||||
-- atomically $ writeTChan (busInbound bus) (doorEvent "on") -- door opens: lights off
|
-- atomically $ writeTChan (busInbound bus) (Event (doorEvent "on")) -- door opens: lights off
|
||||||
-- putStrLn "After the writes"
|
-- putStrLn "After the writes"
|
||||||
-- Right (_, svc1) <- boundedRead (busOutbound bus)
|
-- Right (_, svc1) <- boundedRead (busOutbound bus)
|
||||||
-- Right (_, svc2) <- boundedRead (busOutbound bus)
|
-- Right (_, svc2) <- boundedRead (busOutbound bus)
|
||||||
@@ -32,9 +32,11 @@ spec = pure ()
|
|||||||
-- doorEvent :: Text -> Value
|
-- doorEvent :: Text -> Value
|
||||||
-- doorEvent state = object
|
-- doorEvent state = object
|
||||||
-- [ "event" .= object
|
-- [ "event" .= object
|
||||||
-- [ "data" .= object
|
-- [ "variables" .= object
|
||||||
|
-- [ "trigger" .= object
|
||||||
-- [ "entity_id" .= ("binary_sensor.makuuhuone_ovi_contact" :: Text)
|
-- [ "entity_id" .= ("binary_sensor.makuuhuone_ovi_contact" :: Text)
|
||||||
-- , "new_state" .= object ["state" .= state]
|
-- , "to_state" .= object ["state" .= state]
|
||||||
|
-- ]
|
||||||
-- ]
|
-- ]
|
||||||
-- ]
|
-- ]
|
||||||
-- ]
|
-- ]
|
||||||
|
|||||||
+14
-8
@@ -14,13 +14,13 @@ module Support
|
|||||||
import Control.Monad.Fix (MonadFix (..))
|
import Control.Monad.Fix (MonadFix (..))
|
||||||
import Data.Aeson (Value, object, (.=))
|
import Data.Aeson (Value, object, (.=))
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Data.Time (UTCTime (..))
|
import Data.Time (UTCTime (..), utc)
|
||||||
import Data.UUID (nil)
|
import Data.UUID (nil)
|
||||||
import AFRP (Mealy (..), Request (..))
|
import AFRP (Mealy (..), Request (..))
|
||||||
import HomeAssistant.Controller (HASSEff (..), Service)
|
import HomeAssistant.Controller (HASSEff (..), Service)
|
||||||
|
|
||||||
fakeRequest :: Request
|
fakeRequest :: Request
|
||||||
fakeRequest = Request (sec 0) nil
|
fakeRequest = Request (sec 0) utc nil
|
||||||
|
|
||||||
sec :: Integer -> UTCTime
|
sec :: Integer -> UTCTime
|
||||||
sec n = UTCTime (toEnum 0) (fromIntegral n)
|
sec n = UTCTime (toEnum 0) (fromIntegral n)
|
||||||
@@ -57,25 +57,31 @@ runHASS m (a : as) =
|
|||||||
services :: [(b, [Service])] -> [[Service]]
|
services :: [(b, [Service])] -> [[Service]]
|
||||||
services = map snd
|
services = map snd
|
||||||
|
|
||||||
-- | Build a state-change event payload matching `entityChangeEvent'` / `entityBool'` lenses.
|
-- | Build a state-trigger payload matching `entityChangeEvent'` / `entityBool'`
|
||||||
|
-- lenses. The subscribe_trigger websocket event wraps the trigger datum under
|
||||||
|
-- `event.variables.trigger`, with `entity_id` and `to_state.state` fields.
|
||||||
stateEvent :: T.Text -> T.Text -> Value
|
stateEvent :: T.Text -> T.Text -> Value
|
||||||
stateEvent entityId state = object
|
stateEvent entityId state = object
|
||||||
[ "event" .= object
|
[ "event" .= object
|
||||||
[ "data" .= object
|
[ "variables" .= object
|
||||||
|
[ "trigger" .= object
|
||||||
[ "entity_id" .= entityId
|
[ "entity_id" .= entityId
|
||||||
, "new_state" .= object [ "state" .= state ]
|
, "to_state" .= object [ "state" .= state ]
|
||||||
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | Build an Ikea button event payload matching `ikeaQuickButton` lenses.
|
-- | Build an Ikea button trigger payload matching `ikeaQuickButton` lenses.
|
||||||
buttonEvent :: T.Text -> T.Text -> Value
|
buttonEvent :: T.Text -> T.Text -> Value
|
||||||
buttonEvent entityId eventType = object
|
buttonEvent entityId eventType = object
|
||||||
[ "event" .= object
|
[ "event" .= object
|
||||||
[ "data" .= object
|
[ "variables" .= object
|
||||||
|
[ "trigger" .= object
|
||||||
[ "entity_id" .= entityId
|
[ "entity_id" .= entityId
|
||||||
, "new_state" .= object
|
, "to_state" .= object
|
||||||
[ "attributes" .= object [ "event_type" .= eventType ] ]
|
[ "attributes" .= object [ "event_type" .= eventType ] ]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user