{-# 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) 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" spec :: Spec spec = describe "Bedroom" $ do entitySpec drawerSpec 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 = 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` [[switch [entity] True]] it "turns the drawer light off when the drawer closes" $ services (runHASS bedroomDrawerController [drawerState "off"]) `shouldBe` [[switch [entity] False]] it "toggles the light as the drawer opens and closes" $ services (runHASS bedroomDrawerController [ drawerState "on", drawerState "off", drawerState "on" ]) `shouldBe` [ [switch [entity] True] , [switch [entity] False] , [switch [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` [[]] buttonSpec :: Spec buttonSpec = describe "bedroomButtonController" $ do let area = AreaId "makuuhuone" it "Masse single click turns on his nightstand scene (lowest)" $ services (runHASS bedroomButtonController [masseButton "1_short_release"]) `shouldBe` [[activateScene "scene.makuuhuone_masse"]] it "Masse double click turns on the middle scene" $ services (runHASS bedroomButtonController [masseButton "1_double_press"]) `shouldBe` [[activateScene "scene.makuuhuone_keski"]] it "Masse long click turns on the bright scene" $ services (runHASS bedroomButtonController [masseButton "1_long_press"]) `shouldBe` [[activateScene "scene.makuuhuone_kirkas"]] it "Masse off click turns the bedroom lights off" $ services (runHASS bedroomButtonController [masseButton "2_short_release"]) `shouldBe` [[light [area] Off]] it "Enishen single click turns on her nightstand scene (lowest)" $ services (runHASS bedroomButtonController [enishenButton "1_short_release"]) `shouldBe` [[activateScene "scene.makuuhuone_jemina"]] it "Enishen double click turns on the middle scene" $ services (runHASS bedroomButtonController [enishenButton "1_double_press"]) `shouldBe` [[activateScene "scene.makuuhuone_keski"]] it "Enishen long click turns on the bright scene" $ services (runHASS bedroomButtonController [enishenButton "1_long_press"]) `shouldBe` [[activateScene "scene.makuuhuone_kirkas"]] it "Enishen off click turns the bedroom lights off" $ services (runHASS bedroomButtonController [enishenButton "2_short_release"]) `shouldBe` [[light [area] 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` [[]]