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:
1 parent
383143024a
commit
9c89504399
15 files changed
+115
-86
No files matched your search
@@ -28,6 +28,7 @@ createBedroomScene = Service
|
||||
, "snapshot_entities" .= [l | EntityId l <- bedroomLightTargets]
|
||||
]
|
||||
, serviceTarget= []
|
||||
, serviceExclusions= []
|
||||
}
|
||||
|
||||
-- Bedroom presence
|
||||
@@ -89,14 +90,14 @@ bedroomPresenceController =
|
||||
lightsOff =
|
||||
arr (\(o, _, _, _) -> not o)
|
||||
>>> waitFor 300
|
||||
>>|| callService "Turn off lights" (light bedroomLightTargets Off)
|
||||
>>|| callService "Turn off lights" (light bedroomLightTargets [] Off)
|
||||
|
||||
anyLightsAreOn :: Foldable f => f LightObservation -> Bool
|
||||
anyLightsAreOn = any observedOn
|
||||
toLight :: Bool -> T.Text -> LightObservation -> [Service]
|
||||
toLight _ entityId LightObservation{observedOn=False} = [light [EntityId entityId] Off]
|
||||
toLight _ entityId LightObservation{observedOn=False} = [light [EntityId entityId] [] Off]
|
||||
toLight capped entityId LightObservation{observedOn=True, observedTemperature=t, observedBrightness=b} =
|
||||
[ light [EntityId entityId] (On def
|
||||
[ light [EntityId entityId] [] (On def
|
||||
{lightBrightness = fmap (\b' -> BrightnessAbsolute $ if capped then 140 `min` b' else b') b
|
||||
, lightColorTemperature = t
|
||||
})
|
||||
@@ -130,10 +131,10 @@ bedroomButtonController = proc x -> do
|
||||
Event (Masse (OnButton LongClick)) -> do
|
||||
callService "scene.makuuhuone_kirkas" (activateScene "scene.makuuhuone_kirkas") -< ()
|
||||
Event (Masse (OffButton LongClick)) -> do
|
||||
callService "Lights off" (light [AreaId "makuuhuone"] Off) -< ()
|
||||
callService "Hallway lights off" (light [EntityId "light.hallway_lights"] Off) -< ()
|
||||
callService "Lights off" (light [AreaId "makuuhuone"] [] Off) -< ()
|
||||
callService "Hallway lights off" (light [EntityId "light.hallway_lights"] [] Off) -< ()
|
||||
Event (Masse (OffButton _)) -> do
|
||||
callService "Lights off" (light [AreaId "makuuhuone"] Off) -< ()
|
||||
callService "Lights off" (light [AreaId "makuuhuone"] [] Off) -< ()
|
||||
Event (Enishen (OnButton ShortRelease)) -> do
|
||||
callService "scene.makuuhuone_jemina" (activateScene "scene.makuuhuone_jemina") -< ()
|
||||
Event (Enishen (OnButton DoubleClick)) -> do
|
||||
@@ -141,10 +142,10 @@ bedroomButtonController = proc x -> do
|
||||
Event (Enishen (OnButton LongClick)) -> do
|
||||
callService "scene.makuuhuone_kirkas" (activateScene "scene.makuuhuone_kirkas") -< ()
|
||||
Event (Enishen (OffButton LongClick)) -> do
|
||||
callService "Lights off" (light [AreaId "makuuhuone"] Off) -< ()
|
||||
callService "Hallway lights off" (light [EntityId "light.hallway_lights"] Off) -< ()
|
||||
callService "Lights off" (light [AreaId "makuuhuone"] [] Off) -< ()
|
||||
callService "Hallway lights off" (light [EntityId "light.hallway_lights"] [] Off) -< ()
|
||||
Event (Enishen (OffButton _)) -> do
|
||||
callService "Lights off" (light [AreaId "makuuhuone"] Off) -< ()
|
||||
callService "Lights off" (light [AreaId "makuuhuone"] [] Off) -< ()
|
||||
_ -> returnA -< ()
|
||||
|
||||
|
||||
@@ -161,9 +162,9 @@ bedroomDrawerController =
|
||||
where
|
||||
entity = EntityId "switch.bedroom_drawer_light_masse"
|
||||
lightOff = filterEvent (== Closed)
|
||||
>>|| callService "drawer light off" (switchService [entity] False)
|
||||
>>|| callService "drawer light off" (switchService [entity] [] False)
|
||||
lightOn = filterEvent (== Open)
|
||||
>>|| callService "drawer light on" (switchService [entity] True)
|
||||
>>|| callService "drawer light on" (switchService [entity] [] True)
|
||||
|
||||
door :: HASS (Event Value) (Event DoorState)
|
||||
door = entityBool "binary_sensor.makuuhuone_ovi_contact"
|
||||
@@ -191,4 +192,5 @@ humidifierController = proc x -> do
|
||||
, serviceName= bool "turn_off" "turn_on" state
|
||||
, serviceData= Nothing
|
||||
, serviceTarget= [EntityId "humidifier.makuuhuone_ilmankostutin"]
|
||||
, serviceExclusions= []
|
||||
}
|
||||
@@ -87,11 +87,11 @@ lightsOn :: HASS a ()
|
||||
lightsOn = foldMap atTime timersOn
|
||||
>>> AFRP.onEvent (callService "Lights on" lights)
|
||||
where
|
||||
lights = light [AreaId "lasten_makuuhuone"] (On def{lightBrightness=Just (BrightnessPercent 100), lightTransition = Just 5} )
|
||||
lights = light [AreaId "lasten_makuuhuone"] [] (On def{lightBrightness=Just (BrightnessPercent 100), lightTransition = Just 5} )
|
||||
|
||||
lightsOff :: HASS a ()
|
||||
lightsOff = foldMap atTime timersOff
|
||||
>>> AFRP.onEvent (callService "Lights off" (light [AreaId "lasten_makuuhuone"] Off))
|
||||
>>> AFRP.onEvent (callService "Lights off" (light [AreaId "lasten_makuuhuone"] [LabelId "decoration"] Off))
|
||||
|
||||
|
||||
childrenBedroomButton :: HASS (Event Value) (Event (IkeaButton IkeaGesture))
|
||||
@@ -104,7 +104,7 @@ childrenBedroomButtonController = proc x -> do
|
||||
Event (OnButton ShortRelease) -> callService "scene.lasten_nukkuvalmistelu" (activateScene "scene.lasten_nukkuvalmistelu") -< ()
|
||||
Event (OnButton DoubleClick) -> callService "scene.lasten_luku" (activateScene "scene.lasten_luku") -< ()
|
||||
Event (OnButton LongClick) -> callService "scene.lasten_kirkas" (activateScene "scene.lasten_kirkas") -< ()
|
||||
Event (OffButton _) -> callService "Lights off" (light [AreaId "lasten_makuuhuone"] Off) -< ()
|
||||
Event (OffButton _) -> callService "Lights off" (light [AreaId "lasten_makuuhuone"] [LabelId "decoration"] Off) -< ()
|
||||
_ -> returnA -< ()
|
||||
|
||||
newtype PM25 = PM25 Float
|
||||
@@ -163,5 +163,6 @@ childrenAirFilterController = proc ev -> do
|
||||
{ serviceDomain = "fan"
|
||||
, serviceName = "set_percentage"
|
||||
, serviceTarget = [EntityId "fan.children_fan"]
|
||||
, serviceExclusions = []
|
||||
, serviceData = Just $ toJSON percentage
|
||||
}
|
||||
@@ -70,8 +70,8 @@ modeC = modeTrigger >>> mconcat
|
||||
] >>>
|
||||
arr (const ())
|
||||
where
|
||||
lightOff = callService "Lower light off" (light [EntityId "light.hallway_ceiling"] Off)
|
||||
lightOn = callService "Lower light back on" (light [EntityId "light.hallway_ceiling"] (On def))
|
||||
lightOff = callService "Lower light off" (light [EntityId "light.hallway_ceiling"] [] Off)
|
||||
lightOn = callService "Lower light back on" (light [EntityId "light.hallway_ceiling"] [] (On def))
|
||||
|
||||
onC :: HASS (Event Value) ()
|
||||
onC = proc ev -> do
|
||||
@@ -84,7 +84,7 @@ onC = proc ev -> do
|
||||
Event _ -> lightsOn -< entities
|
||||
Tick -> returnA -< ()
|
||||
where
|
||||
lightsOn = callServiceDyn "Lights on" (\es -> light es (On def))
|
||||
lightsOn = callServiceDyn "Lights on" (\es -> light es [] (On def))
|
||||
|
||||
lightsOffTrigger :: HASS (Event Value) (Event ())
|
||||
lightsOffTrigger = hallwayPresence >>> arr (== Unoccupied) >>>
|
||||
@@ -94,7 +94,7 @@ offC :: HASS (Event Value) ()
|
||||
offC = (lightsOffTrigger AFRP.>>|| lightsOff) >>>
|
||||
arr (const ())
|
||||
where
|
||||
lightsOff = callService "Lights off" (light [EntityId "light.hallway_lights"] Off)
|
||||
lightsOff = callService "Lights off" (light [EntityId "light.hallway_lights"] [] Off)
|
||||
|
||||
hallwayLightsController :: HASS (Event Value) ()
|
||||
hallwayLightsController = onC <> offC <> modeC
|
||||
@@ -23,8 +23,8 @@ kitchenCeilingController = proc x -> do
|
||||
ev <- lightingModeEvents -< p
|
||||
traceEvent -< ev
|
||||
case ev of
|
||||
Event LightingOn | lightsAllowed now -> callServiceDyn "light.kitchen_ceiling on" (light [EntityId "light.kitchen_ceiling"]) -< On def
|
||||
Event LightingOff -> callServiceDyn "light.kitchen_ceiling off" (light [EntityId "light.kitchen_ceiling"]) -< Off
|
||||
Event LightingOn | lightsAllowed now -> callServiceDyn "light.kitchen_ceiling on" (light [EntityId "light.kitchen_ceiling"] []) -< On def
|
||||
Event LightingOff -> callServiceDyn "light.kitchen_ceiling off" (light [EntityId "light.kitchen_ceiling"] []) -< Off
|
||||
_ -> returnA -< ()
|
||||
where
|
||||
lightsAllowed (LocalTime _ tod) = not (tod > TimeOfDay 1 45 0 && tod < TimeOfDay 5 0 0)
|
||||
@@ -53,7 +53,7 @@ dinnerTableController =
|
||||
lightsOn =
|
||||
AFRP.edge
|
||||
>>|| sleep "sleep off" False
|
||||
>>> callService "Dining light on" (light [EntityId "light.kitchen_dining_large"] $ On def)
|
||||
>>> callService "Dining light on" (light [EntityId "light.kitchen_dining_large"] [] $ On def)
|
||||
|
||||
lightsSleep =
|
||||
arr not
|
||||
@@ -64,10 +64,10 @@ dinnerTableController =
|
||||
arr not
|
||||
>>> AFRP.waitFor 300
|
||||
>>|| sleep "sleep off" False
|
||||
>>> callService "Dining light on" (light [EntityId "light.kitchen_dining_large"] Off)
|
||||
>>> 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)
|
||||
sleep lbl b = callService lbl (switchService [EntityId "switch.adaptive_lighting_ruokapoydan_valot_sleep_mode"] [] b)
|
||||
|
||||
|
||||
kitchenMotionController :: HASS (Event Value) ()
|
||||
|
||||
@@ -47,16 +47,19 @@ data LightCommand
|
||||
= Off
|
||||
| On LightAttributes
|
||||
|
||||
light :: [Target] -> LightCommand -> Service
|
||||
light targets (On settings) = Service
|
||||
{ serviceDomain="light"
|
||||
, serviceName= "turn_on"
|
||||
, serviceData= formatLightAttributes settings
|
||||
, serviceTarget=targets
|
||||
}
|
||||
light targets Off = Service
|
||||
{ serviceDomain="light"
|
||||
, serviceName= "turn_off"
|
||||
, serviceData=Nothing
|
||||
, serviceTarget=targets
|
||||
}
|
||||
light :: [Target] -> [Target] -> LightCommand -> Service
|
||||
light targets exclusions = \case
|
||||
(On settings) -> Service
|
||||
{ serviceDomain="light"
|
||||
, serviceName= "turn_on"
|
||||
, serviceData= formatLightAttributes settings
|
||||
, serviceTarget=targets
|
||||
, serviceExclusions = exclusions
|
||||
}
|
||||
Off -> Service
|
||||
{ serviceDomain="light"
|
||||
, serviceName= "turn_off"
|
||||
, serviceData=Nothing
|
||||
, serviceTarget=targets
|
||||
, serviceExclusions = exclusions
|
||||
}
|
||||
@@ -39,8 +39,8 @@ livingroomPresenceController = proc x -> do
|
||||
ev <- lightingModeEvents . livingroomPresence -< x
|
||||
traceEvent -< ev
|
||||
case ev of
|
||||
AFRP.Event LightingOn -> callServiceDyn "Lights on" (light livingroomLightTargets) -< On def
|
||||
AFRP.Event LightingOff -> callServiceDyn "Lights off" (light livingroomLightTargets) -< Off
|
||||
AFRP.Event LightingOn -> callServiceDyn "Lights on" (light livingroomLightTargets []) -< On def
|
||||
AFRP.Event LightingOff -> callServiceDyn "Lights off" (light livingroomLightTargets []) -< Off
|
||||
_ -> returnA -< ()
|
||||
returnA -< ()
|
||||
|
||||
|
||||
@@ -18,4 +18,5 @@ activateSceneWith sceneId d = Service
|
||||
, serviceName= "turn_on"
|
||||
, serviceTarget= [EntityId sceneId]
|
||||
, serviceData = d
|
||||
, serviceExclusions = []
|
||||
}
|
||||
@@ -6,7 +6,10 @@ module HomeAssistant.Controller.Service
|
||||
import Data.Aeson (Value)
|
||||
import qualified Data.Text as T
|
||||
|
||||
data Target = EntityId !T.Text | AreaId !T.Text
|
||||
data Target
|
||||
= EntityId !T.Text
|
||||
| AreaId !T.Text
|
||||
| LabelId !T.Text
|
||||
deriving (Show,Eq,Ord)
|
||||
|
||||
data Service = Service
|
||||
@@ -14,5 +17,6 @@ data Service = Service
|
||||
, serviceName :: T.Text
|
||||
, serviceData :: Maybe Value
|
||||
, serviceTarget :: [Target]
|
||||
, serviceExclusions :: [Target]
|
||||
}
|
||||
deriving (Show, Eq)
|
||||
@@ -5,10 +5,11 @@ module HomeAssistant.Controller.Switch (switchService) where
|
||||
import Data.Bool (bool)
|
||||
import HomeAssistant.Controller.Service (Service (..), Target)
|
||||
|
||||
switchService :: [Target] -> Bool -> Service
|
||||
switchService targets b = Service
|
||||
switchService :: [Target] -> [Target] -> Bool -> Service
|
||||
switchService targets exclusions b = Service
|
||||
{ serviceDomain="switch"
|
||||
, serviceName= bool "turn_off" "turn_on" b
|
||||
, serviceData=Nothing
|
||||
, serviceTarget=targets
|
||||
, serviceExclusions=exclusions
|
||||
}
|
||||
@@ -146,17 +146,21 @@ encodeService callId Service{..} = object $
|
||||
, "type" .= ("call_service" :: T.Text)
|
||||
, "domain" .= serviceDomain
|
||||
, "service" .= serviceName
|
||||
, "target" .= targetObject serviceTarget
|
||||
, "target" .= targetObject serviceTarget serviceExclusions
|
||||
] <> maybe [] (\d -> ["service_data" .= d]) serviceData
|
||||
|
||||
|
||||
-- | A single target encodes as a scalar; multiple encode as a list. Empty
|
||||
-- lists are omitted so Home Assistant receives only populated keys.
|
||||
targetObject :: [Target] -> Value
|
||||
targetObject targets = object $ entityPart <> areaPart
|
||||
-- lists are omitted so Home Assistant receives only populated keys. Exclusions
|
||||
-- nest under "exclude".
|
||||
targetObject :: [Target] -> [Target] -> Value
|
||||
targetObject targets exclusions = object $
|
||||
targetKeys targets
|
||||
<> [ "exclude" .= object (targetKeys exclusions) | not (null exclusions) ]
|
||||
where
|
||||
entityPart = emit "entity_id" [e | EntityId e <- targets]
|
||||
areaPart = emit "area_id" [a | AreaId a <- targets]
|
||||
targetKeys ts = emit "entity_id" [e | EntityId e <- ts]
|
||||
<> emit "area_id" [a | AreaId a <- ts]
|
||||
<> emit "label_id" [l | LabelId l <- ts]
|
||||
emit _ [] = []
|
||||
emit k [x] = [k .= x]
|
||||
emit k many = [k .= many]
|
||||
Reference in new issue
Block a user