5 Commits
Author SHA1 Message Date
MasseR 7fc07c1883 Plant lights 2026-09-25 10:31:06 +03:00
MasseR 9c47bbcb42 More 2026-09-24 20:27:11 +03:00
MasseR 6063aafe8b Toggle adaptive mode 2026-09-24 20:21:33 +03:00
MasseR fafc7111bb The center button also disables sleep mode 2026-09-24 10:07:02 +03:00
MasseR 69cac934ec Fix tests 2026-09-24 10:06:20 +03:00
4 changed files with 108 additions and 29 deletions

No files matched your search

+39 -11
View File
@@ -57,14 +57,17 @@ bedroomPresenceController = proc x -> do
callService (light bedroomLights Off) -< () callService (light bedroomLights Off) -< ()
Event LightsOn -> do Event LightsOn -> do
sleepMode -< False sleepMode -< False
adaptiveMode -< False
callService (activateScene "scene.makuuhuone_lights_snapshot") -< () callService (activateScene "scene.makuuhuone_lights_snapshot") -< ()
Event LightsSleep -> do Event LightsSleep -> do
callService createBedroomScene -< () callService createBedroomScene -< ()
adaptiveMode -< True
sleepMode -< True sleepMode -< True
_ -> returnA -< () _ -> returnA -< ()
where where
sleepMode = callServiceDyn (switch [EntityId "switch.adaptive_lighting_makuuhuoneen_valot_sleep_mode"] ) sleepMode = callServiceDyn (switch [EntityId "switch.adaptive_lighting_makuuhuoneen_valot_sleep_mode"] )
adaptiveMode = callServiceDyn (switch [EntityId "switch.adaptive_lighting_makuuhuoneen_valot"] )
data BedroomControls data BedroomControls
@@ -87,20 +90,45 @@ bedroomButtonController = proc x -> do
ev <- bedroomButton >>> traceEvent -< x ev <- bedroomButton >>> traceEvent -< x
-- Any event will cause the sleep mode to be turned off -- Any event will cause the sleep mode to be turned off
case ev of case ev of
Event _ -> sleepMode -< False Event (Masse (OnButton ShortRelease)) -> do
_ -> returnA -< () sleepMode -< False
case ev of adaptiveMode -< False
Event (Masse (OnButton ShortRelease)) -> callService (activateScene "scene.makuuhuone_masse") -< () callService (activateScene "scene.makuuhuone_masse") -< ()
Event (Masse (OnButton DoubleClick)) -> callService (activateScene "scene.makuuhuone_keski") -< () Event (Masse (OnButton DoubleClick)) -> do
Event (Masse (OnButton LongClick)) -> callService (activateScene "scene.makuuhuone_kirkas") -< () sleepMode -< False
Event (Masse (OffButton _)) -> callService (light [AreaId "makuuhuone"] Off) -< () adaptiveMode -< False
Event (Enishen (OnButton ShortRelease)) -> callService (activateScene "scene.makuuhuone_jemina") -< () callService (activateScene "scene.makuuhuone_keski") -< ()
Event (Enishen (OnButton DoubleClick)) -> callService (activateScene "scene.makuuhuone_keski") -< () Event (Masse (OnButton LongClick)) -> do
Event (Enishen (OnButton LongClick)) -> callService (activateScene "scene.makuuhuone_kirkas") -< () sleepMode -< False
Event (Enishen (OffButton _)) -> callService (light [AreaId "makuuhuone"] Off) -< () adaptiveMode -< False
callService (activateScene "scene.makuuhuone_kirkas") -< ()
Event (Masse (OffButton _)) -> do
sleepMode -< False
adaptiveMode -< False
callService (light [AreaId "makuuhuone"] Off) -< ()
Event (Enishen (OnButton ShortRelease)) -> do
sleepMode -< False
adaptiveMode -< False
callService (activateScene "scene.makuuhuone_jemina") -< ()
Event (Enishen (OnButton DoubleClick)) -> do
sleepMode -< False
adaptiveMode -< False
callService (activateScene "scene.makuuhuone_keski") -< ()
Event (Enishen (OnButton LongClick)) -> do
sleepMode -< False
adaptiveMode -< False
callService (activateScene "scene.makuuhuone_kirkas") -< ()
Event (Enishen (OffButton _)) -> do
sleepMode -< False
adaptiveMode -< False
callService (light [AreaId "makuuhuone"] Off) -< ()
Event (Central _) -> do
sleepMode -< False
adaptiveMode -< False
_ -> returnA -< () _ -> returnA -< ()
where where
sleepMode = callServiceDyn (switch [EntityId "switch.adaptive_lighting_makuuhuoneen_valot_sleep_mode"] ) sleepMode = callServiceDyn (switch [EntityId "switch.adaptive_lighting_makuuhuoneen_valot_sleep_mode"] )
adaptiveMode = callServiceDyn (switch [EntityId "switch.adaptive_lighting_makuuhuoneen_valot"] )
+50 -1
View File
@@ -2,12 +2,13 @@
{-# LANGUAGE Arrows #-} {-# LANGUAGE Arrows #-}
module HomeAssistant.Controller.Livingroom where module HomeAssistant.Controller.Livingroom where
import HomeAssistant.Controller (HASS, Presence (..), Target (..), presence, callServiceDyn, light, Light (..), traceEvent) import HomeAssistant.Controller (HASS, Presence (..), Target (..), presence, callServiceDyn, light, Light (..), traceEvent, callService, activateScene)
import qualified AFRP import qualified AFRP
import Control.Arrow ((>>>), Arrow (..), returnA) import Control.Arrow ((>>>), Arrow (..), returnA)
import Data.Aeson (Value) import Data.Aeson (Value)
import Prelude hiding ((.)) import Prelude hiding ((.))
import Control.Category ((.)) import Control.Category ((.))
import Data.Time (LocalTime(..), toGregorian, TimeOfDay (..))
data Lights = LightsOn | LightsOff data Lights = LightsOn | LightsOff
deriving (Show) deriving (Show)
@@ -49,3 +50,51 @@ livingroomPresenceController = proc x -> do
AFRP.Event LightsOff -> callServiceDyn (light lights) -< Off AFRP.Event LightsOff -> callServiceDyn (light lights) -< Off
_ -> returnA -< () _ -> returnA -< ()
returnA -< () returnA -< ()
data Season = Summer | Fall | Winter | Spring
deriving Show
season :: HASS a Season
season = AFRP.currentTime >>> arr toSeason
where
toSeason t =
case toGregorian (localDay t) of
(_, m, _)
| m >= 12 || m <= 2 -> Winter
| m <= 5 -> Spring
| m <= 8 -> Summer
| otherwise -> Fall
tod :: HASS a TimeOfDay
tod = AFRP.currentTime >>> arr localTimeOfDay
data PlantLightMode = Growth | Ambience
deriving Show
plantLightMode :: HASS (Season, TimeOfDay) (AFRP.Event PlantLightMode)
plantLightMode =
(lightMode &&& ambienceMode) >>> arr (uncurry AFRP.lMerge)
where
lightMode = arr (uncurry lightWanted) >>> AFRP.edge >>> arr (AFRP.tag Growth)
ambienceMode = arr (uncurry ambienceWanted) >>> AFRP.edge >>> arr (AFRP.tag Ambience)
-- Full brightness during daytime only non-summer
lightWanted :: Season -> TimeOfDay -> Bool
lightWanted Summer _ = False
lightWanted _ TimeOfDay{todHour=10,todMin=00} = True
lightWanted _ _ = False
-- Ambience mode is always
ambienceWanted :: Season -> TimeOfDay -> Bool
ambienceWanted _ TimeOfDay{todHour=20,todMin=0} = True
ambienceWanted _ _ = False
livingroomPlantLights :: HASS (AFRP.Event a) ()
livingroomPlantLights = (season &&& tod)
>>> plantLightMode
>>> setLights
where
setLights = proc mode -> do
case mode of
AFRP.Event Growth -> callService (activateScene "scene.olohuoneen_kasvivalo") -< ()
AFRP.Event Ambience -> callService (activateScene "scene.makuuhuone_lepotila") -< ()
_ -> returnA -< ()
+2 -1
View File
@@ -36,7 +36,7 @@ import qualified HomeAssistant.Runtime.Metrics
import System.FilePath ((</>)) import System.FilePath ((</>))
import Text.Read (readMaybe) import Text.Read (readMaybe)
import HomeAssistant.Controller.Kitchen (kitchenMotionController) import HomeAssistant.Controller.Kitchen (kitchenMotionController)
import HomeAssistant.Controller.Livingroom (livingroomPresenceController) import HomeAssistant.Controller.Livingroom (livingroomPresenceController, livingroomPlantLights)
import HomeAssistant.Runtime.RateLimit (slidingWindowLimiter, registerRateLimitMetrics) import HomeAssistant.Runtime.RateLimit (slidingWindowLimiter, registerRateLimitMetrics)
import UnliftIO.Async import UnliftIO.Async
import HomeAssistant.Runtime.Supervisor (supervised) import HomeAssistant.Runtime.Supervisor (supervised)
@@ -63,6 +63,7 @@ controllers =
, Controller True "livingroom-presence" livingroomPresenceController , Controller True "livingroom-presence" livingroomPresenceController
, Controller True "hallway-motion-controller" hallwayLightsController , Controller True "hallway-motion-controller" hallwayLightsController
, Controller True "children-button" childrenBedroomButtonController , Controller True "children-button" childrenBedroomButtonController
, Controller True "livingroom-plants" livingroomPlantLights
] ]
-- | Steps the machine for every inbound message; service calls go to the -- | Steps the machine for every inbound message; service calls go to the
+17 -16
View File
@@ -45,6 +45,7 @@ entitySpec = describe "entities" $ do
`shouldBe` S.fromList `shouldBe` S.fromList
[ "event.bedroom_quick_remote_masse_action" [ "event.bedroom_quick_remote_masse_action"
, "event.bedroom_quick_jemina_action" , "event.bedroom_quick_jemina_action"
, "event.bedroom_remote_action"
] ]
it "bedroomPresenceController subscribes to the presence sensor" $ it "bedroomPresenceController subscribes to the presence sensor" $
@@ -89,36 +90,36 @@ buttonSpec = describe "bedroomButtonController" $ do
let area = AreaId "makuuhuone" let area = AreaId "makuuhuone"
it "Masse single click turns on his nightstand scene (lowest)" $ it "Masse single click turns on his nightstand scene (lowest)" $
services (runHASS bedroomButtonController [masseButton "1_short_release"]) concat (services (runHASS bedroomButtonController [masseButton "1_short_release"]))
`shouldBe` [[activateScene "scene.makuuhuone_masse"]] `shouldContain` [activateScene "scene.makuuhuone_masse"]
it "Masse double click turns on the middle scene" $ it "Masse double click turns on the middle scene" $
services (runHASS bedroomButtonController [masseButton "1_double_press"]) concat (services (runHASS bedroomButtonController [masseButton "1_double_press"]))
`shouldBe` [[activateScene "scene.makuuhuone_keski"]] `shouldContain` [activateScene "scene.makuuhuone_keski"]
it "Masse long click turns on the bright scene" $ it "Masse long click turns on the bright scene" $
services (runHASS bedroomButtonController [masseButton "1_long_press"]) concat (services (runHASS bedroomButtonController [masseButton "1_long_press"]))
`shouldBe` [[activateScene "scene.makuuhuone_kirkas"]] `shouldContain` [activateScene "scene.makuuhuone_kirkas"]
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"]) concat (services (runHASS bedroomButtonController [masseButton "2_short_release"]))
`shouldBe` [[light [area] Off]] `shouldContain` [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"]) concat (services (runHASS bedroomButtonController [enishenButton "1_short_release"]))
`shouldBe` [[activateScene "scene.makuuhuone_jemina"]] `shouldContain` [activateScene "scene.makuuhuone_jemina"]
it "Enishen double click turns on the middle scene" $ it "Enishen double click turns on the middle scene" $
services (runHASS bedroomButtonController [enishenButton "1_double_press"]) concat (services (runHASS bedroomButtonController [enishenButton "1_double_press"]))
`shouldBe` [[activateScene "scene.makuuhuone_keski"]] `shouldContain` [activateScene "scene.makuuhuone_keski"]
it "Enishen long click turns on the bright scene" $ it "Enishen long click turns on the bright scene" $
services (runHASS bedroomButtonController [enishenButton "1_long_press"]) concat (services (runHASS bedroomButtonController [enishenButton "1_long_press"]))
`shouldBe` [[activateScene "scene.makuuhuone_kirkas"]] `shouldContain` [activateScene "scene.makuuhuone_kirkas"]
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"]) concat (services (runHASS bedroomButtonController [enishenButton "2_short_release"]))
`shouldBe` [[light [area] Off]] `shouldContain` [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"])