Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f8b3cbe00 | ||
|
|
078f24e4b9 |
No files matched your search
+2
-2
@@ -507,7 +507,7 @@ thenA f g = f >>> arr Left ||| g
|
|||||||
(>>|) :: (ArrowChoice cat, Arrow cat) => cat a (Either b1 c) -> cat c (Either b1 b2) -> cat a (Either b1 b2)
|
(>>|) :: (ArrowChoice cat, Arrow cat) => cat a (Either b1 c) -> cat c (Either b1 b2) -> cat a (Either b1 b2)
|
||||||
(>>|) = thenA
|
(>>|) = thenA
|
||||||
|
|
||||||
infixl 2 >>||
|
infixl 2 >>|
|
||||||
|
|
||||||
(>>||) :: (ArrowChoice cat, Arrow cat) => cat a (Event b) -> cat b c -> cat a (Event c)
|
(>>||) :: (ArrowChoice cat, Arrow cat) => cat a (Event b) -> cat b c -> cat a (Event c)
|
||||||
f >>|| g = proc x -> do
|
f >>|| g = proc x -> do
|
||||||
@@ -516,7 +516,7 @@ f >>|| g = proc x -> do
|
|||||||
Event e' -> g >>> arr Event -< e'
|
Event e' -> g >>> arr Event -< e'
|
||||||
Tick -> arr (const Tick) -< ()
|
Tick -> arr (const Tick) -< ()
|
||||||
|
|
||||||
infixr 3 >>|
|
infixr 1 >>||
|
||||||
|
|
||||||
toEvent :: Mealy eff (Either () a) (Event a)
|
toEvent :: Mealy eff (Either () a) (Event a)
|
||||||
toEvent = arr (either (const Tick) Event)
|
toEvent = arr (either (const Tick) Event)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
{-# LANGUAGE Arrows #-}
|
{-# LANGUAGE Arrows #-}
|
||||||
module HomeAssistant.Controller.Kitchen where
|
module HomeAssistant.Controller.Kitchen where
|
||||||
import HomeAssistant.Controller
|
import HomeAssistant.Controller
|
||||||
import AFRP (Event (..))
|
import AFRP (Event (..), (>>||))
|
||||||
import qualified AFRP
|
import qualified AFRP
|
||||||
import Data.Aeson (Value)
|
import Data.Aeson (Value)
|
||||||
import Control.Arrow ((>>>), Arrow (..), returnA)
|
import Control.Arrow ((>>>), Arrow (..), returnA)
|
||||||
@@ -36,44 +36,39 @@ dinnerLightLevel = entityRead "sensor.sijainti_keittio_light_level" >>> AFRP.hol
|
|||||||
dinnerPresence :: HASS (Event Value) Presence
|
dinnerPresence :: HASS (Event Value) Presence
|
||||||
dinnerPresence = presence "binary_sensor.sijainti_keittio_occupancy" >>> AFRP.hold Unoccupied
|
dinnerPresence = presence "binary_sensor.sijainti_keittio_occupancy" >>> AFRP.hold Unoccupied
|
||||||
|
|
||||||
dinnerLightEvents :: HASS (Event Value) (Event LightingMode)
|
|
||||||
dinnerLightEvents =
|
dinnerTableController :: HASS (Event Value) ()
|
||||||
|
dinnerTableController =
|
||||||
((,) <$> dinnerLightLevel <*> dinnerPresence)
|
((,) <$> dinnerLightLevel <*> dinnerPresence)
|
||||||
>>> arr lightWanted
|
>>> arr lightWanted
|
||||||
>>> (sleepEvent &&& offEvent &&& onEvent)
|
-- These are done "in parallel", there is a chance that two services are called in one go
|
||||||
>>> arr (\(s, (o, n)) -> s `AFRP.lMerge` o `AFRP.lMerge` n)
|
>>> mconcat
|
||||||
|
[ AFRP.named "Lights on" lightsOn
|
||||||
|
, AFRP.named "Lights sleep" lightsSleep
|
||||||
|
, AFRP.named "Lights off" lightsOff ]
|
||||||
|
>>> arr (const ()) -- I don't like this extra arr here
|
||||||
where
|
where
|
||||||
lightWanted (lightLevel, occ) = occ == Occupied && lightLevel < 13
|
lightWanted (lightLevel, occ) = occ == Occupied && lightLevel < 13
|
||||||
|
|
||||||
onEvent =
|
lightsOn =
|
||||||
AFRP.edge
|
AFRP.edge
|
||||||
>>> arr (fmap (const LightingOn))
|
>>|| sleep "sleep off" False
|
||||||
|
>>> callService "Dining light on" (light [EntityId "light.kitchen_dining_large"] $ On def)
|
||||||
|
|
||||||
sleepEvent =
|
lightsSleep =
|
||||||
arr not
|
arr not
|
||||||
>>> AFRP.edge
|
>>> AFRP.edge
|
||||||
>>> arr (fmap (const LightingSleep))
|
>>|| sleep "sleep on" True
|
||||||
|
|
||||||
offEvent =
|
lightsOff =
|
||||||
arr not
|
arr not
|
||||||
>>> AFRP.waitFor 300
|
>>> AFRP.waitFor 300
|
||||||
>>> arr (fmap (const LightingOff))
|
>>|| sleep "sleep off" False
|
||||||
|
>>> callService "Dining light on" (light [EntityId "light.kitchen_dining_large"] Off)
|
||||||
|
|
||||||
|
|
||||||
|
sleep lbl b = callService lbl (switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] b)
|
||||||
|
|
||||||
dinnerTableController :: HASS (Event Value) ()
|
|
||||||
dinnerTableController = proc x -> do
|
|
||||||
ev <- dinnerLightEvents -< x
|
|
||||||
case ev of
|
|
||||||
Event LightingOn -> do
|
|
||||||
sleep "sleep off"-< False
|
|
||||||
callServiceDyn "Dining light on" (light [EntityId "light.kitchen_dining_large"]) -< On def
|
|
||||||
Event LightingOff -> do
|
|
||||||
sleep "sleep off" -< False
|
|
||||||
callServiceDyn "Dining light off" (light [EntityId "light.kitchen_dining_large"]) -< Off
|
|
||||||
Event LightingSleep -> do
|
|
||||||
sleep "sleep on" -< True
|
|
||||||
_ -> returnA -< ()
|
|
||||||
where
|
|
||||||
sleep lbl = callServiceDyn lbl (switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"])
|
|
||||||
|
|
||||||
kitchenMotionController :: HASS (Event Value) ()
|
kitchenMotionController :: HASS (Event Value) ()
|
||||||
kitchenMotionController = kitchenCeilingController <> dinnerTableController
|
kitchenMotionController = kitchenCeilingController <> dinnerTableController
|
||||||
+3
-2
@@ -20,6 +20,7 @@ import qualified Hedgehog.Range as Range
|
|||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Hedgehog
|
import Test.Hspec.Hedgehog
|
||||||
import Data.Maybe (listToMaybe)
|
import Data.Maybe (listToMaybe)
|
||||||
|
import AFRP.Diagram (emptyDiagram)
|
||||||
|
|
||||||
fakeRequest :: Request
|
fakeRequest :: Request
|
||||||
fakeRequest = Request (sec 0) utc nil "test"
|
fakeRequest = Request (sec 0) utc nil "test"
|
||||||
@@ -536,11 +537,11 @@ sampleSpec = describe "sample" $ do
|
|||||||
|
|
||||||
-- | A stateless arrow carrying a fixed entity set, for testing propagation.
|
-- | A stateless arrow carrying a fixed entity set, for testing propagation.
|
||||||
subscribed :: S.Set T.Text -> Mealy Identity Int Int
|
subscribed :: S.Set T.Text -> Mealy Identity Int Int
|
||||||
subscribed ents = Mealy ents $ \_ -> Fun $ \_ a -> a
|
subscribed ents = Mealy ents emptyDiagram $ \_ -> Fun $ \_ a -> a
|
||||||
|
|
||||||
-- | Same as 'subscribed' but yields a function, for testing '<*>'.
|
-- | Same as 'subscribed' but yields a function, for testing '<*>'.
|
||||||
subscribedF :: S.Set T.Text -> Mealy Identity Int (Int -> Int)
|
subscribedF :: S.Set T.Text -> Mealy Identity Int (Int -> Int)
|
||||||
subscribedF ents = Mealy ents $ \_ -> Fun $ \_ a -> (a +)
|
subscribedF ents = Mealy ents emptyDiagram $ \_ -> Fun $ \_ a -> (a +)
|
||||||
|
|
||||||
entitiesSpec :: Spec
|
entitiesSpec :: Spec
|
||||||
entitiesSpec = describe "entities" $ do
|
entitiesSpec = describe "entities" $ do
|
||||||
|
|||||||
+9
-2
@@ -11,6 +11,7 @@ import HomeAssistant.Controller.Kitchen
|
|||||||
import Support
|
import Support
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Data.Default (def)
|
import Data.Default (def)
|
||||||
|
import Data.Time (getCurrentTime, addUTCTime)
|
||||||
|
|
||||||
-- Helper functions for creating test events
|
-- Helper functions for creating test events
|
||||||
testDinnerLightLevel :: Int -> Event Value
|
testDinnerLightLevel :: Int -> Event Value
|
||||||
@@ -37,9 +38,15 @@ dinnerTableSpec = describe "dinnerTableController" $ do
|
|||||||
services (runHASS dinnerTableController [testDinnerLightLevel 10, testDinnerPresence "on"])
|
services (runHASS dinnerTableController [testDinnerLightLevel 10, testDinnerPresence "on"])
|
||||||
`shouldBe` [[switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True], [switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] False, light [EntityId "light.kitchen_dining_large"] (On def)]]
|
`shouldBe` [[switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True], [switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] False, light [EntityId "light.kitchen_dining_large"] (On def)]]
|
||||||
|
|
||||||
it "turns lights off when presence is unoccupied" $ do
|
it "turns lights to sleep when presence is unoccupied" $ do
|
||||||
services (runHASS dinnerTableController [testDinnerLightLevel 10, testDinnerPresence "off"])
|
now <- getCurrentTime
|
||||||
|
services (runHASSTimed dinnerTableController [(now, testDinnerLightLevel 10), (now, testDinnerPresence "off")])
|
||||||
`shouldBe` [[switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True], []]
|
`shouldBe` [[switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True], []]
|
||||||
|
it "turns lights off when no presence for 300 seconds" $ do
|
||||||
|
now <- getCurrentTime
|
||||||
|
let later = 305 `addUTCTime` now
|
||||||
|
services (runHASSTimed dinnerTableController [(now, testDinnerLightLevel 10), (now, testDinnerPresence "off"), (later, Tick)])
|
||||||
|
`shouldBe` [[switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True], [], [switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] False, light [EntityId "light.kitchen_dining_large"] Off]]
|
||||||
|
|
||||||
it "does nothing on Tick" $ do
|
it "does nothing on Tick" $ do
|
||||||
services (runHASS dinnerTableController [Tick])
|
services (runHASS dinnerTableController [Tick])
|
||||||
|
|||||||
Reference in new issue
Block a user