Add target exclusions to service calls

Services can now carry excluded targets, encoded as target.exclude in
the WebSocket payload. The children's bedroom lights-off calls skip
lights labelled 'decoration'.
This commit is contained in:
MasseR committed 2026-11-14 19:36:46 +02:00
1 parent 383143024a
commit 9c89504399
15 files changed
+115 -86

No files matched your search

+15 -15
View File
@@ -85,18 +85,18 @@ drawerSpec = describe "bedroomDrawerController" $ do
it "turns the drawer light on when the drawer opens" $
services (runHASS bedroomDrawerController [drawerState "on"])
`shouldBe` [[switchService [entity] True]]
`shouldBe` [[switchService [entity] [] True]]
it "turns the drawer light off when the drawer closes" $
services (runHASS bedroomDrawerController [drawerState "off"])
`shouldBe` [[switchService [entity] False]]
`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]
`shouldBe` [ [switchService [entity] [] True]
, [switchService [entity] [] False]
, [switchService [entity] [] True]
]
it "ignores state events for other entities" $
@@ -146,12 +146,12 @@ presenceSpec = describe "bedroomPresenceController" $ do
let later = 305 `addUTCTime` now
services (runHASSTimed bedroomPresenceController
[ (now, presenceSensor "on"), (now, presenceSensor "off"), (later, Tick) ])
`shouldBe` [ [], [], [light bedroomLightTargets Off] ]
`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
let masseRestored = light [EntityId "light.bedroom_masse"] [] (On def
{ lightBrightness = Just (BrightnessAbsolute 140)
, lightColorTemperature = Just 2700
})
@@ -167,9 +167,9 @@ presenceSpec = describe "bedroomPresenceController" $ do
, []
, [sleepScene]
, []
, [ light [EntityId "light.bedroom_ceiling"] Off
, light [EntityId "light.bedroom_jemina"] Off
, light [EntityId "light.bedroom_led_switch"] Off
, [ light [EntityId "light.bedroom_ceiling"] [] Off
, light [EntityId "light.bedroom_jemina"] [] Off
, light [EntityId "light.bedroom_led_switch"] [] Off
, masseRestored
]
, []
@@ -193,15 +193,15 @@ buttonSpec = describe "bedroomButtonController" $ do
it "Masse off click turns the bedroom lights off" $
concat (services (runHASS bedroomButtonController [masseButton "2_short_release"]))
`shouldContain` [light [area] Off]
`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]
`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]
`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"]))
@@ -217,11 +217,11 @@ buttonSpec = describe "bedroomButtonController" $ do
it "Enishen off click turns the bedroom lights off" $
concat (services (runHASS bedroomButtonController [enishenButton "2_short_release"]))
`shouldContain` [light [area] Off]
`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]
`shouldContain` [light [EntityId "light.hallway_lights"] [] Off]
it "ignores the initial press (scene only fires on release)" $
services (runHASS bedroomButtonController [masseButton "1_initial_press"])
+2 -2
View File
@@ -40,7 +40,7 @@ spec = describe "Bus" $ do
it "channelHassEval writes CallService to the outbound channel" $ withTestBus $ \bus -> do
let req = Request (UTCTime (toEnum 0) 0) utc nil "test"
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"]) $
channelHassEval bus (CallService req svc)
(_, svc') <- atomically $ readTChan (busOutbound bus)
@@ -50,7 +50,7 @@ spec = describe "Bus" $ do
withTestFlagsBus $ \bus -> do
setEnabled (busFlags bus) "test-handler" False
let req = Request (UTCTime (toEnum 0) 0) utc nil "test-handler"
svc = Service "light" "turn_on" Nothing [EntityId "light.test"]
svc = Service "light" "turn_on" Nothing [EntityId "light.test"] []
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
channelHassEval bus (CallService req svc)
result <- timeout 1000000 $ atomically $ readTChan (busOutbound bus)
+16 -3
View File
@@ -27,7 +27,7 @@ spec :: Spec
spec = do
describe "encodeService" $ do
it "encodes a call_service message" $
encodeService 7 (Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"])
encodeService 7 (Service "light" "turn_on" Nothing [EntityId "light.bedroom_masse"] [])
`shouldBe` object
[ "id" .= (7 :: Int)
, "type" .= ("call_service" :: Text)
@@ -37,7 +37,7 @@ spec = do
]
it "includes service_data when present" $
encodeService 8 (Service "light" "turn_on" (Just (object ["brightness" .= (200 :: Int)])) [EntityId "light.bedroom_masse"])
encodeService 8 (Service "light" "turn_on" (Just (object ["brightness" .= (200 :: Int)])) [EntityId "light.bedroom_masse"] [])
`shouldBe` object
[ "id" .= (8 :: Int)
, "type" .= ("call_service" :: Text)
@@ -47,6 +47,19 @@ spec = do
, "service_data" .= object ["brightness" .= (200 :: Int)]
]
it "encodes target exclusions" $
encodeService 9 (Service "light" "turn_off" Nothing [AreaId "lasten_makuuhuone"] [LabelId "decoration"])
`shouldBe` object
[ "id" .= (9 :: Int)
, "type" .= ("call_service" :: Text)
, "domain" .= ("light" :: Text)
, "service" .= ("turn_off" :: Text)
, "target" .= object
[ "area_id" .= ("lasten_makuuhuone" :: Text)
, "exclude" .= object ["label_id" .= ("decoration" :: Text)]
]
]
describe "isTriggerEvent" $ do
it "is true for event frames" $
isTriggerEvent (object ["type" .= ("event" :: Text)]) `shouldBe` True
@@ -80,5 +93,5 @@ req n = Request (UTCTime (toEnum 0) (fromIntegral (0 :: Int))) utc
uuid = "00000000-0000-0000-0000-" <> pad n
lightOn :: [Target] -> Service
lightOn = Service "light" "turn_on" Nothing
lightOn targets = Service "light" "turn_on" Nothing targets []
+15 -15
View File
@@ -180,11 +180,11 @@ lightsSpec = do
describe "hallwayLightsController" $ do
it "turns the full lights on when someone enters during daytime" $
services (runHASSTimed hallwayLightsController [(day, motionSensor "on")])
`shouldBe` [[light [EntityId "light.hallway_lights"] (On def)]]
`shouldBe` [[light [EntityId "light.hallway_lights"] [] (On def)]]
it "turns the full lights on when someone enters during daytime preceding tick" $
services (runHASSTimed hallwayLightsController [(day, Tick), (day, motionSensor "on")])
`shouldBe` [[], [light [EntityId "light.hallway_lights"] (On def)]]
`shouldBe` [[], [light [EntityId "light.hallway_lights"] [] (On def)]]
it "turns the lights off when everyone leaves, without sleep mode" $
let later = 505 `addUTCTime` day
@@ -195,9 +195,9 @@ lightsSpec = do
, (later, Tick)
])
`shouldBe` [ []
, [light [EntityId "light.hallway_lights"] (On def)]
, [light [EntityId "light.hallway_lights"] [] (On def)]
, []
, [light [EntityId "light.hallway_lights"] Off]
, [light [EntityId "light.hallway_lights"] [] Off]
]
it "turns on only the ceiling light when entering during sleep hours with a door open" $
@@ -208,7 +208,7 @@ lightsSpec = do
])
`shouldBe` [ []
, []
, [light [EntityId "light.hallway_ceiling_2"] (On def)]
, [light [EntityId "light.hallway_ceiling_2"] [] (On def)]
]
it "leaves the lights unchanged when an empty bedroom door opens during sleep hours" $
@@ -220,7 +220,7 @@ lightsSpec = do
])
`shouldBe` [ []
, []
, [light [EntityId "light.hallway_lights"] (On def)]
, [light [EntityId "light.hallway_lights"] [] (On def)]
, []
]
@@ -233,7 +233,7 @@ lightsSpec = do
, (night, motionSensor "on")
])
`shouldBe` [ [], [], [], []
, [light [EntityId "light.hallway_lights"] (On def)]
, [light [EntityId "light.hallway_lights"] [] (On def)]
]
it "protects an open bedroom door as soon as its light is dimmed down" $
@@ -246,8 +246,8 @@ lightsSpec = do
, (night, bedroomLightEvent "light.bedroom_ceiling" 25)
])
`shouldBe` [ [], [], [], []
, [light [EntityId "light.hallway_lights"] (On def)]
, [light [EntityId "light.hallway_ceiling"] Off]
, [light [EntityId "light.hallway_lights"] [] (On def)]
, [light [EntityId "light.hallway_ceiling"] [] Off]
]
it "counts a barely lit bedroom as ready to sleep" $
@@ -260,8 +260,8 @@ lightsSpec = do
, (night, masterDoor "on") -- opens for the cats, someone ready to sleep
])
`shouldBe` [ [], [], [], []
, [light [EntityId "light.hallway_lights"] (On def)]
, [light [EntityId "light.hallway_ceiling"] Off]
, [light [EntityId "light.hallway_lights"] [] (On def)]
, [light [EntityId "light.hallway_ceiling"] [] Off]
]
it "dims the lower light when an occupied bedroom door opens during sleep hours" $
@@ -273,8 +273,8 @@ lightsSpec = do
, (night, masterDoor "on") -- opens for the cats, someone ready to sleep
])
`shouldBe` [ [], [], []
, [light [EntityId "light.hallway_lights"] (On def)]
, [light [EntityId "light.hallway_ceiling"] Off]
, [light [EntityId "light.hallway_lights"] [] (On def)]
, [light [EntityId "light.hallway_ceiling"] [] Off]
]
it "turns the lower hallway light on when the doors close while presence is active" $
@@ -285,7 +285,7 @@ lightsSpec = do
, (night, childrenDoor "off")
])
`shouldBe` [ []
, [light [EntityId "light.hallway_ceiling_2"] (On def)]
, [light [EntityId "light.hallway_ceiling_2"] [] (On def)]
, []
, [light [EntityId "light.hallway_ceiling"] (On def)]
, [light [EntityId "light.hallway_ceiling"] [] (On def)]
]
+4 -4
View File
@@ -36,18 +36,18 @@ dinnerTableSpec = describe "dinnerTableController" $ do
it "turns lights on when presence is occupied and light level is low" $ do
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 to sleep when presence is unoccupied" $ do
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]]
`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
services (runHASS dinnerTableController [Tick])
`shouldBe` [[switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] True]]
`shouldBe` [[switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] [] True]]