Beispiel #1
0
def save_device(id):
    """
    Save an edited device definition
    :param roomid:
    :return:
    """
    # NOTE
    # The jQuery $.ajax call sends arguments as the data property[TPLink Python Library](https://github.com/GadgetReactor/pyHS100)
    # 5.  [Meross Python Library](https://github.com/albertogeniola/MerossIot)
    # in the initiating call. The arguments show up in the
    # request.form property provided by Flask. So,
    # data: { 'state': new_state } --> request.form['state']
    name = request.form['name']
    location = request.form['location']
    device_mfg = request.form['mfg']
    address = request.form['address']
    channel = int(request.form["channel"])
    color = request.form["color"]
    brightness = request.form["brightness"]

    api_req = AHPSRequest()

    r = api_req.update_device(id, name, location, device_mfg, address, channel,
                              color, brightness)

    # We are obligated to send a json response
    if r:
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #2
0
def save_device_program(id):
    """
    Save an edited device program
    :param roomid:
    :return:
    """
    program = {"id": id}
    program["name"] = request.form['name']
    program["day-mask"] = request.form['daymask']
    program["trigger-method"] = request.form['triggermethod']
    program["time"] = request.form['time']
    program["offset"] = request.form['offset']
    program["randomize"] = normalize_boolean(request.form["randomize"])
    program["randomize-amount"] = request.form['randomizeamount']
    program["command"] = request.form['command']
    program["color"] = request.form['color']
    program["brightness"] = request.form['brightness']

    api_req = AHPSRequest()

    r = api_req.update_device_program(program)
    if r:
        logger.debug("Update device program: %s", json.dumps(program,
                                                             indent=4))

        # We are obligated to send a json response
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #3
0
def set_groups_state(group_id):
    """
    Change state of all devices in group to on or off
    :param groupid:
    :return:
    """
    # NOTE
    # The jQuery $.ajax call sends arguments as the data property
    # in the initiating call. The arguments show up in the
    # request.form property provided by Flask. So,
    # data: { 'state': new_state } --> request.form['state']
    arg = request.form['state']
    api_req = AHPSRequest()
    if arg == "on":
        res = api_req.group_on(group_id)
    elif arg == "off":
        res = api_req.group_off(group_id)
    else:
        # Return an error
        res = None

    # We are obligated to send a json response
    if res:
        resp = jsonify(res)
        if res["result-code"]:
            resp.status_code = HTTPStatus.BAD_REQUEST
        else:
            resp.status_code = HTTPStatus.OK
        return resp

    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #4
0
def define_device():
    """
    Save an new device definition
    :param roomid:
    :return:
    """
    # NOTE
    # The jQuery $.ajax call sends arguments as the data property
    # in the initiating call. The arguments show up in the
    # request.form property provided by Flask. So,
    # data: { 'state': new_state } --> request.form['state']
    name = request.form['name']
    location = request.form['location']
    device_mfg = request.form['mfg']
    address = request.form['address']
    channel = int(request.form["channel"])
    color = request.form["color"]
    brightness = request.form["brightness"]

    api_req = AHPSRequest()

    r = api_req.define_device(name, location, device_mfg, address, channel,
                              color, brightness)

    # We are obligated to send a json response
    if r:
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #5
0
def discover_devices():
    api_req = AHPSRequest()
    res = api_req.discover_devices()
    if res and "result-code" in res.keys() and res["result-code"] == 0:
        return jsonify({})
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #6
0
def delete_action_group(group_id):
    api_req = AHPSRequest()
    res = api_req.delete_action_group(group_id)
    if res:
        return jsonify(res)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #7
0
def get_device(id):
    api_req = AHPSRequest()
    res = api_req.get_device(id)
    if res:
        return jsonify({"data": res["device"]})
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #8
0
def get_action_group_devices(group_id):
    api_req = AHPSRequest()
    res = api_req.get_action_group_devices(group_id)
    if res and "devices" in res.keys():
        return jsonify({"data": res["devices"]})
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #9
0
def get_available_meross_devices():
    api_req = AHPSRequest()
    res = api_req.get_all_available_devices("meross")
    if res and "devices" in res.keys():
        kv = res["devices"]
        return jsonify({"data": kv})
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #10
0
def add_device_to_group(group_id, device_id):
    api_req = AHPSRequest()

    r = api_req.assign_device_to_group(group_id, device_id)

    if r and r["result-code"] == 0:
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #11
0
def get_device_program(id):
    """
    Get the device program for progran ID
    :param id:
    :return:
    """
    api_req = AHPSRequest()
    res = api_req.get_program_by_id(id)

    if res:
        return jsonify({"data": res["program"]})
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #12
0
def get_available_group_devices(id):
    """
    Get all devices available for assignment to a given group ID
    :param id: The group ID for avialable devices
    :return:
    """
    api_req = AHPSRequest()
    res = api_req.get_available_devices_for_group_id(id)

    # Build response with program summary
    if res and "devices" in res.keys():
        return jsonify({"data": res["devices"]})
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #13
0
def delete_device_program(device_id, program_id):
    """
    Delete a program from a device
    :param id:
    :return:
    """
    api_req = AHPSRequest()

    r = api_req.delete_device_program(device_id, program_id)

    # We are obligated to send a json response
    if r:
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #14
0
def delete_action_group_device(group_id, device_id):
    """
    Delete a from from an action group
    :param group_id:
    :param device_id:
    :return:
    """
    api_req = AHPSRequest()

    r = api_req.delete_action_group_device(group_id, device_id)

    # We are obligated to send a json response
    if r and r["result-code"] == 0:
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #15
0
def delete_device(id):
    """
    Delete a device definition
    :param roomid:
    :return:
    """
    api_req = AHPSRequest()

    r = api_req.delete_device(id)
    # r = {"message": "Success"}

    # We are obligated to send a json response
    if r:
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #16
0
def get_all_programs():
    """
    Get all programs
    :param id:
    :return:
    """
    api_req = AHPSRequest()
    res = api_req.get_all_programs()

    # Build response with program summary
    if res:
        for p in res["programs"]:
            p["summary"] = build_program_summary(p)

        return jsonify({"data": res["programs"]})
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #17
0
def get_available_device_programs(id):
    """
    Get all programs available for assignment to a given device ID
    :param id: The device ID for avialable programs
    :return:
    """
    api_req = AHPSRequest()
    res = api_req.get_available_programs_for_device_id(id)

    # Build response with program summary
    if res:
        for p in res["programs"]:
            p["summary"] = build_program_summary(p)

        return jsonify({"data": res["programs"]})
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #18
0
def save_action_group(id):
    """
    Save an edited action group
    :param roomid:
    :return:
    """
    group = {"group-id": id, "group-name": request.form['name']}

    api_req = AHPSRequest()

    r = api_req.update_action_group(group)
    if r:
        logger.debug("Update action group: %s", json.dumps(group, indent=4))

        # We are obligated to send a json response
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #19
0
def set_new_device_state():
    """
    Change state of a new device to on or off
    :return:
    """
    # NOTE
    # The jQuery $.ajax call sends arguments as the data property
    # in the initiating call. The arguments show up in the
    # request.form property provided by Flask. So,
    # data: { 'state': new_state } --> request.form['state']
    state = request.form['state']
    mfg = request.form['mfg']
    address = request.form['address']
    channel = request.form['channel']
    name = request.form['name'] if "name" in request.form.keys(
    ) else "new device"

    api_req = AHPSRequest()
    if state == "on":
        color = request.form['color'] if "color" in request.form.keys() else ""
        brightness = request.form[
            'brightness'] if "brightness" in request.form.keys() else 100
        res = api_req.new_device_on(mfg, address, channel, name, color,
                                    brightness)
    elif state == "off":
        res = api_req.new_device_off(mfg, address, channel, name)
    else:
        # Return an error
        res = None

    # We are obligated to send a json response
    if res:
        resp = jsonify(res)
        if res["result-code"]:
            resp.status_code = HTTPStatus.BAD_REQUEST
        else:
            resp.status_code = HTTPStatus.OK
        return resp

    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #20
0
def create_new_program():
    program = {}
    program["name"] = request.form['name']
    program["day-mask"] = request.form['daymask']
    program["trigger-method"] = request.form['triggermethod']
    program["time"] = request.form['time']
    program["offset"] = request.form['offset']
    program["randomize"] = normalize_boolean(request.form["randomize"])
    program["randomize-amount"] = request.form['randomizeamount']
    program["command"] = request.form['command']
    program["color"] = request.form['color']
    program["brightness"] = request.form['brightness']

    api_req = AHPSRequest()
    res = api_req.define_device_program(program)
    if res:
        return jsonify(res)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #21
0
def set_devices_state(id):
    """
    Change state of device to on or off
    :param roomid:
    :return:
    """
    # NOTE
    # The jQuery $.ajax call sends arguments as the data property
    # in the initiating call. The arguments show up in the
    # request.form property provided by Flask. So,
    # data: { 'state': new_state } --> request.form['state']
    arg = request.form['state']
    api_req = AHPSRequest()
    if arg == "on":
        if "color" in request.form.keys():
            color = request.form['color']
        else:
            color = None
        if "brightness" in request.form.keys():
            brightness = request.form['brightness']
        else:
            brightness = None
        res = api_req.device_on(id, color=color, brightness=brightness)
    elif arg == "off":
        res = api_req.device_off(id)
    else:
        # Return an error
        res = None

    # We are obligated to send a json response
    if res:
        resp = jsonify(res)
        if res["result-code"]:
            resp.status_code = HTTPStatus.BAD_REQUEST
        else:
            resp.status_code = HTTPStatus.OK
        return resp

    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #22
0
def define_action_group():
    """
    Save an new action group definition
    :param roomid:
    :return:
    """
    # NOTE
    # The jQuery $.ajax call sends arguments as the data property
    # in the initiating call. The arguments show up in the
    # request.form property provided by Flask. So,
    # data: { 'state': new_state } --> request.form['state']
    name = request.form['name']
    api_req = AHPSRequest()

    r = api_req.define_action_group(name)

    # We are obligated to send a json response
    if r and r["result-code"] == 0:
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response
Beispiel #23
0
def save_all_devices():
    """
    Save all device definitions
    :return:
    """
    devices = request.get_json()
    api_req = AHPSRequest()

    for device in devices:
        r = api_req.update_device(device["id"], device["name"],
                                  device["location"], device["type"],
                                  device["address"])
        if not r:
            response = jsonify(api_req.last_error)
            response.status_code = HTTPStatus.BAD_REQUEST
            return response

    # We are obligated to send a json response
    if r:
        return jsonify(r)
    response = jsonify(api_req.last_error)
    response.status_code = HTTPStatus.BAD_REQUEST
    return response