Use intersection for determine_script_action (#113915)

pull/113922/head
J. Nick Koston 2024-03-20 16:06:59 -10:00 committed by GitHub
parent b574220247
commit e015fd2440
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 46 deletions

View File

@ -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]] = {