238 lines
9.2 KiB
Haskell
238 lines
9.2 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module BedroomSpec (spec) where
|
|
|
|
import AFRP (Event (..), entities)
|
|
import qualified Data.Set as S
|
|
import qualified Data.Text as T
|
|
import Data.Aeson (Value, object, (.=))
|
|
import qualified Data.Aeson.Key as Key
|
|
import Data.Default (def)
|
|
import Data.Time (getCurrentTime, addUTCTime)
|
|
import HomeAssistant.Controller
|
|
import HomeAssistant.Controller.Bedroom
|
|
import Support
|
|
import Test.Hspec
|
|
|
|
masseButton :: T.Text -> Event Value
|
|
masseButton = Event . buttonEvent "event.bedroom_quick_remote_masse_action"
|
|
|
|
enishenButton :: T.Text -> Event Value
|
|
enishenButton = Event . buttonEvent "event.bedroom_quick_jemina_action"
|
|
|
|
drawerState :: T.Text -> Event Value
|
|
drawerState = Event . stateEvent "binary_sensor.bedroom_nightstand_drawer_sensor_masse_contact"
|
|
|
|
presenceSensor :: T.Text -> Event Value
|
|
presenceSensor = Event . stateEvent "binary_sensor.presence_sensor_bedroom_occupancy"
|
|
|
|
-- Light state event with observed attributes
|
|
masseLight :: T.Text -> [(T.Text, Int)] -> Event Value
|
|
masseLight state attrs = Event $ object
|
|
[ "event" .= object
|
|
[ "variables" .= object
|
|
[ "trigger" .= object
|
|
[ "entity_id" .= ("light.bedroom_masse" :: T.Text)
|
|
, "to_state" .= object
|
|
[ "state" .= state
|
|
, "attributes" .= object [ Key.fromString (T.unpack k) .= v | (k, v) <- attrs ]
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]
|
|
|
|
spec :: Spec
|
|
spec = describe "Bedroom" $ do
|
|
entitySpec
|
|
drawerSpec
|
|
buttonSpec
|
|
presenceSpec
|
|
|
|
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"
|
|
, "event.bedroom_remote_action"
|
|
]
|
|
|
|
it "bedroomPresenceController subscribes to the presence sensor" $
|
|
S.toList (entities bedroomPresenceController)
|
|
`shouldContain` [ "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 = describe "bedroomDrawerController" $ do
|
|
let entity = EntityId "switch.bedroom_drawer_light_masse"
|
|
|
|
it "turns the drawer light on when the drawer opens" $
|
|
services (runHASS bedroomDrawerController [drawerState "on"])
|
|
`shouldBe` [[switchService [entity] True]]
|
|
|
|
it "turns the drawer light off when the drawer closes" $
|
|
services (runHASS bedroomDrawerController [drawerState "off"])
|
|
`shouldBe` [[switchService [entity] False]]
|
|
|
|
it "toggles the light as the drawer opens and closes" $
|
|
services (runHASS bedroomDrawerController
|
|
[ drawerState "on", drawerState "off", drawerState "on" ])
|
|
`shouldBe` [ [switchService [entity] True]
|
|
, [switchService [entity] False]
|
|
, [switchService [entity] True]
|
|
]
|
|
|
|
it "ignores state events for other entities" $
|
|
services (runHASS bedroomDrawerController
|
|
[Event (stateEvent "binary_sensor.some_other_contact" "on")])
|
|
`shouldBe` [[]]
|
|
|
|
it "does nothing on Tick" $
|
|
services (runHASS bedroomDrawerController [Tick])
|
|
`shouldBe` [[]]
|
|
|
|
presenceSpec :: Spec
|
|
presenceSpec = describe "bedroomPresenceController" $ do
|
|
let sleepScene = activateScene "scene.makuuhuone_lepotila"
|
|
|
|
it "subscribes to presence, light level and the bedroom lights" $
|
|
entities bedroomPresenceController
|
|
`shouldBe` S.fromList
|
|
[ "binary_sensor.presence_sensor_bedroom_occupancy"
|
|
, "sensor.presence_sensor_bedroom_light_level"
|
|
, "light.bedroom_ceiling"
|
|
, "light.bedroom_jemina"
|
|
, "light.bedroom_led_switch"
|
|
, "light.bedroom_masse"
|
|
]
|
|
|
|
it "does nothing on Tick" $
|
|
services (runHASS bedroomPresenceController [Tick])
|
|
`shouldBe` [[]]
|
|
|
|
it "does nothing when entering for the first time (no snapshot yet)" $
|
|
services (runHASS bedroomPresenceController [presenceSensor "on"])
|
|
`shouldBe` [[]]
|
|
|
|
it "activates the sleep scene when leaving with the lights on" $
|
|
services (runHASS bedroomPresenceController
|
|
[ presenceSensor "on", masseLight "on" [], presenceSensor "off" ])
|
|
`shouldBe` [ [], [], [sleepScene] ]
|
|
|
|
it "does not activate the sleep scene when the lights are off" $
|
|
services (runHASS bedroomPresenceController
|
|
[ presenceSensor "on", masseLight "on" [], masseLight "off" [], presenceSensor "off" ])
|
|
`shouldBe` [ [], [], [], [] ]
|
|
|
|
it "turns the lights off 300s after leaving" $ do
|
|
now <- getCurrentTime
|
|
let later = 305 `addUTCTime` now
|
|
services (runHASSTimed bedroomPresenceController
|
|
[ (now, presenceSensor "on"), (now, presenceSensor "off"), (later, Tick) ])
|
|
`shouldBe` [ [], [], [light bedroomLightTargets Off] ]
|
|
|
|
it "restores the last snapshot when re-entering after sleep" $ do
|
|
now <- getCurrentTime
|
|
let later = 350 `addUTCTime` now
|
|
let masseRestored = light [EntityId "light.bedroom_masse"] (On def
|
|
{ lightBrightness = Just (BrightnessAbsolute 140)
|
|
, lightColorTemperature = Just 2700
|
|
})
|
|
services (runHASSTimed bedroomPresenceController
|
|
[ (now, presenceSensor "on")
|
|
, (now, masseLight "on" [("brightness", 255), ("color_temp_kelvin", 2700)])
|
|
, (now, presenceSensor "off")
|
|
, (now, Tick)
|
|
, (later, presenceSensor "on")
|
|
, (later, Tick)
|
|
])
|
|
`shouldBe` [ []
|
|
, []
|
|
, [sleepScene]
|
|
, []
|
|
, [ light [EntityId "light.bedroom_ceiling"] Off
|
|
, light [EntityId "light.bedroom_jemina"] Off
|
|
, light [EntityId "light.bedroom_led_switch"] Off
|
|
, masseRestored
|
|
]
|
|
, []
|
|
]
|
|
|
|
buttonSpec :: Spec
|
|
buttonSpec = describe "bedroomButtonController" $ do
|
|
let area = AreaId "makuuhuone"
|
|
|
|
it "Masse single click turns on his nightstand scene (lowest)" $
|
|
concat (services (runHASS bedroomButtonController [masseButton "1_short_release"]))
|
|
`shouldContain` [activateScene "scene.makuuhuone_masse"]
|
|
|
|
it "Masse double click turns on the middle scene" $
|
|
concat (services (runHASS bedroomButtonController [masseButton "1_double_press"]))
|
|
`shouldContain` [activateScene "scene.makuuhuone_keski"]
|
|
|
|
it "Masse long click turns on the bright scene" $
|
|
concat (services (runHASS bedroomButtonController [masseButton "1_long_press"]))
|
|
`shouldContain` [activateScene "scene.makuuhuone_kirkas"]
|
|
|
|
it "Masse off click turns the bedroom lights off" $
|
|
concat (services (runHASS bedroomButtonController [masseButton "2_short_release"]))
|
|
`shouldContain` [light [area] Off]
|
|
|
|
it "Masse long off click also turns the hallway lights off" $
|
|
concat (services (runHASS bedroomButtonController [masseButton "2_long_press"]))
|
|
`shouldContain` [light [EntityId "light.hallway_lights"] Off]
|
|
|
|
it "Masse short off click leaves the hallway lights alone" $
|
|
concat (services (runHASS bedroomButtonController [masseButton "2_short_release"]))
|
|
`shouldNotContain` [light [EntityId "light.hallway_lights"] Off]
|
|
|
|
it "Enishen single click turns on her nightstand scene (lowest)" $
|
|
concat (services (runHASS bedroomButtonController [enishenButton "1_short_release"]))
|
|
`shouldContain` [activateScene "scene.makuuhuone_jemina"]
|
|
|
|
it "Enishen double click turns on the middle scene" $
|
|
concat (services (runHASS bedroomButtonController [enishenButton "1_double_press"]))
|
|
`shouldContain` [activateScene "scene.makuuhuone_keski"]
|
|
|
|
it "Enishen long click turns on the bright scene" $
|
|
concat (services (runHASS bedroomButtonController [enishenButton "1_long_press"]))
|
|
`shouldContain` [activateScene "scene.makuuhuone_kirkas"]
|
|
|
|
it "Enishen off click turns the bedroom lights off" $
|
|
concat (services (runHASS bedroomButtonController [enishenButton "2_short_release"]))
|
|
`shouldContain` [light [area] Off]
|
|
|
|
it "Enishen long off click also turns the hallway lights off" $
|
|
concat (services (runHASS bedroomButtonController [enishenButton "2_long_press"]))
|
|
`shouldContain` [light [EntityId "light.hallway_lights"] Off]
|
|
|
|
it "ignores the initial press (scene only fires on release)" $
|
|
services (runHASS bedroomButtonController [masseButton "1_initial_press"])
|
|
`shouldBe` [[]]
|
|
|
|
it "ignores button events for other entities" $
|
|
services (runHASS bedroomButtonController
|
|
[Event (buttonEvent "event.some_other_action" "1_short_release")])
|
|
`shouldBe` [[]]
|
|
|
|
it "does nothing on Tick" $
|
|
services (runHASS bedroomButtonController [Tick])
|
|
`shouldBe` [[]]
|