From e015fd2440edbbe142f24785bc53f442d7401bc7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 20 Mar 2024 16:06:59 -1000 Subject: [PATCH] Use intersection for determine_script_action (#113915) --- homeassistant/helpers/config_validation.py | 74 ++++++++-------------- 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index bf666cf2e03..cf65da5917c 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1825,54 +1825,36 @@ SCRIPT_ACTION_WAIT_FOR_TRIGGER = "wait_for_trigger" SCRIPT_ACTION_WAIT_TEMPLATE = "wait_template" +ACTIONS_MAP = { + CONF_DELAY: SCRIPT_ACTION_DELAY, + CONF_WAIT_TEMPLATE: SCRIPT_ACTION_WAIT_TEMPLATE, + CONF_CONDITION: SCRIPT_ACTION_CHECK_CONDITION, + "and": SCRIPT_ACTION_CHECK_CONDITION, + "or": SCRIPT_ACTION_CHECK_CONDITION, + "not": SCRIPT_ACTION_CHECK_CONDITION, + CONF_EVENT: SCRIPT_ACTION_FIRE_EVENT, + CONF_DEVICE_ID: SCRIPT_ACTION_DEVICE_AUTOMATION, + CONF_SCENE: SCRIPT_ACTION_ACTIVATE_SCENE, + CONF_REPEAT: SCRIPT_ACTION_REPEAT, + CONF_CHOOSE: SCRIPT_ACTION_CHOOSE, + CONF_WAIT_FOR_TRIGGER: SCRIPT_ACTION_WAIT_FOR_TRIGGER, + CONF_VARIABLES: SCRIPT_ACTION_VARIABLES, + CONF_IF: SCRIPT_ACTION_IF, + CONF_SERVICE: SCRIPT_ACTION_CALL_SERVICE, + CONF_SERVICE_TEMPLATE: SCRIPT_ACTION_CALL_SERVICE, + CONF_STOP: SCRIPT_ACTION_STOP, + CONF_PARALLEL: SCRIPT_ACTION_PARALLEL, + CONF_SET_CONVERSATION_RESPONSE: SCRIPT_ACTION_SET_CONVERSATION_RESPONSE, +} + +ACTIONS_SET = set(ACTIONS_MAP) + + def determine_script_action(action: dict[str, Any]) -> str: """Determine action type.""" - if CONF_DELAY in action: - return SCRIPT_ACTION_DELAY - - if CONF_WAIT_TEMPLATE in action: - return SCRIPT_ACTION_WAIT_TEMPLATE - - if any(key in action for key in (CONF_CONDITION, "and", "or", "not")): - return SCRIPT_ACTION_CHECK_CONDITION - - if CONF_EVENT in action: - return SCRIPT_ACTION_FIRE_EVENT - - if CONF_DEVICE_ID in action: - return SCRIPT_ACTION_DEVICE_AUTOMATION - - if CONF_SCENE in action: - return SCRIPT_ACTION_ACTIVATE_SCENE - - if CONF_REPEAT in action: - return SCRIPT_ACTION_REPEAT - - if CONF_CHOOSE in action: - return SCRIPT_ACTION_CHOOSE - - if CONF_WAIT_FOR_TRIGGER in action: - return SCRIPT_ACTION_WAIT_FOR_TRIGGER - - if CONF_VARIABLES in action: - return SCRIPT_ACTION_VARIABLES - - if CONF_IF in action: - return SCRIPT_ACTION_IF - - if CONF_SERVICE in action or CONF_SERVICE_TEMPLATE in action: - return SCRIPT_ACTION_CALL_SERVICE - - if CONF_STOP in action: - return SCRIPT_ACTION_STOP - - if CONF_PARALLEL in action: - return SCRIPT_ACTION_PARALLEL - - if CONF_SET_CONVERSATION_RESPONSE in action: - return SCRIPT_ACTION_SET_CONVERSATION_RESPONSE - - raise ValueError("Unable to determine action") + if not (actions := ACTIONS_SET.intersection(action)): + raise ValueError("Unable to determine action") + return ACTIONS_MAP[actions.pop()] ACTION_TYPE_SCHEMAS: dict[str, Callable[[Any], dict]] = {