Beispiel #1
0
def action_add(form):
    """Add a function Action"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['add']['title'],
        controller='{} {}'.format(TRANSLATIONS['conditional']['title'],
                                  TRANSLATIONS['actions']['title']))

    dep_unmet, _ = return_dependencies(form.action_type.data)
    if dep_unmet:
        list_unmet_deps = []
        for each_dep in dep_unmet:
            list_unmet_deps.append(each_dep[0])
        error.append(
            "The {dev} device you're trying to add has unmet dependencies: "
            "{dep}".format(dev=form.function_type.data,
                           dep=', '.join(list_unmet_deps)))

    if form.function_type.data == 'conditional':
        func = Conditional.query.filter(
            Conditional.unique_id == form.function_id.data).first()
    elif form.function_type.data == 'trigger':
        func = Trigger.query.filter(
            Trigger.unique_id == form.function_id.data).first()
    elif form.function_type.data == 'function_actions':
        func = Function.query.filter(
            Function.unique_id == form.function_id.data).first()
    else:
        func = None
        error.append("Invalid Function type: {}".format(form.function_type.data))

    if form.function_type.data != 'function_actions' and func and func.is_activated:
        error.append("Deactivate before adding an Action")

    if form.action_type.data == '':
        error.append("Must select an action")

    try:
        new_action = Actions()
        new_action.function_id = form.function_id.data
        new_action.function_type = form.function_type.data
        new_action.action_type = form.action_type.data

        if form.action_type.data == 'command':
            new_action.do_output_state = 'mycodo'  # user to execute shell command as

        if not error:
            new_action.save()

    except sqlalchemy.exc.OperationalError as except_msg:
        error.append(except_msg)
    except sqlalchemy.exc.IntegrityError as except_msg:
        error.append(except_msg)
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_function'))

    if dep_unmet:
        return 1
Beispiel #2
0
def action_add(form):
    """Add a function Action"""
    error = []
    action = '{action} {controller}'.format(
        action=TRANSLATIONS['add']['title'],
        controller='{} {}'.format(TRANSLATIONS['conditional']['title'],
                                  TRANSLATIONS['actions']['title']))

    dep_unmet, _ = return_dependencies(form.action_type.data)
    if dep_unmet:
        list_unmet_deps = []
        for each_dep in dep_unmet:
            list_unmet_deps.append(each_dep[0])
        error.append(
            "The {dev} device you're trying to add has unmet dependencies: "
            "{dep}".format(dev=form.function_type.data,
                           dep=', '.join(list_unmet_deps)))

    if form.function_type.data == 'conditional':
        func = Conditional.query.filter(
            Conditional.unique_id == form.function_id.data).first()
    elif form.function_type.data == 'trigger':
        func = Trigger.query.filter(
            Trigger.unique_id == form.function_id.data).first()
    elif form.function_type.data == 'function_actions':
        func = Function.query.filter(
            Function.unique_id == form.function_id.data).first()
    else:
        func = None
        error.append("Invalid Function type: {}".format(form.function_type.data))

    if form.function_type.data != 'function_actions' and func and func.is_activated:
        error.append("Deactivate before adding an Action")

    if form.action_type.data == '':
        error.append("Must select an action")

    try:
        new_action = Actions()
        new_action.function_id = form.function_id.data
        new_action.function_type = form.function_type.data
        new_action.action_type = form.action_type.data

        if form.action_type.data == 'command':
            new_action.do_output_state = 'mycodo'  # user to execute shell command as

        elif form.action_type.data == 'mqtt_publish':
            # Fill in default values
            # TODO: Future improvements to actions will be single-file modules, making this obsolete
            custom_options = {
                "hostname": "localhost",
                "port": 1883,
                "topic": "paho/test/single",
                "keepalive": 60,
                "clientid": "mycodo_mqtt_client",
                "login": False,
                "username": "******",
                "password": ""
            }
            new_action.custom_options = json.dumps(custom_options)

        if not error:
            new_action.save()

    except sqlalchemy.exc.OperationalError as except_msg:
        error.append(except_msg)
    except sqlalchemy.exc.IntegrityError as except_msg:
        error.append(except_msg)
    except Exception as except_msg:
        error.append(except_msg)

    flash_success_errors(error, action, url_for('routes_page.page_function'))

    if dep_unmet:
        return 1
Beispiel #3
0
def action_add(form):
    """Add an Action"""
    messages = {"success": [], "info": [], "warning": [], "error": []}
    action_id = None
    dep_name = ""
    page_refresh = False

    dep_unmet, _ = return_dependencies(form.action_type.data)
    if dep_unmet:
        list_unmet_deps = []
        for each_dep in dep_unmet:
            list_unmet_deps.append(each_dep[0])
        messages["error"].append(
            "{dev} has unmet dependencies. They must be installed before the Action can be added."
            .format(dev=form.action_type.data))
        dep_name = form.action_type.data

        return messages, dep_name, list_unmet_deps, None

    if form.function_type.data == 'conditional':
        func = Conditional.query.filter(
            Conditional.unique_id == form.function_id.data).first()
    elif form.function_type.data == 'trigger':
        func = Trigger.query.filter(
            Trigger.unique_id == form.function_id.data).first()
    elif form.function_type.data == 'function':
        func = Function.query.filter(
            Function.unique_id == form.function_id.data).first()
    else:
        func = None
        messages["error"].append("Invalid Function type: {}".format(
            form.function_type.data))

    if form.function_type.data != 'function' and func and func.is_activated:
        messages["error"].append("Deactivate before adding an Action")

    if form.action_type.data == '':
        messages["error"].append("Must select an action")

    try:
        new_action = Actions()
        new_action.function_id = form.function_id.data
        new_action.function_type = form.function_type.data
        new_action.action_type = form.action_type.data

        if form.action_type.data == 'command':
            new_action.do_output_state = 'mycodo'  # user to execute shell command as

        elif form.action_type.data == 'mqtt_publish':
            # Fill in default values
            # TODO: Future improvements to actions will be single-file modules, making this obsolete
            custom_options = {
                "hostname": "localhost",
                "port": 1883,
                "topic": "paho/test/single",
                "keepalive": 60,
                "clientid": "mycodo_mqtt_client",
                "login": False,
                "username": "******",
                "password": ""
            }
            new_action.custom_options = json.dumps(custom_options)

        if not messages["error"]:
            new_action.save()
            action_id = new_action.unique_id
            page_refresh = True
            messages["success"].append('{action} {controller}'.format(
                action=TRANSLATIONS['add']['title'],
                controller=TRANSLATIONS['actions']['title']))

    except sqlalchemy.exc.OperationalError as except_msg:
        messages["error"].append(str(except_msg))
    except sqlalchemy.exc.IntegrityError as except_msg:
        messages["error"].append(str(except_msg))
    except Exception as except_msg:
        messages["error"].append(str(except_msg))

    return messages, dep_name, dep_unmet, action_id, page_refresh