def node_action(node_id, action): if node_id not in zwave: return ret_jerr(404, "node-id: {} does not exist".format(node_id)) if action not in (NODE_ACTIONS + NODE_ATTRS): return ret_jerr(404, "Requested action: {} not available".format(action)) ret = action_handler(NODE_ATTRS, NODE_SUB_ACTIONS, NODE_WRAPPED, action, request.args, zwave.get_node(node_id), zwave) return ret_jmsg(msg="Action: {} sent for node-id: {} returned: {}". \ format(action, node_id, str(ret)))
def available_ctrl_actions(ctrl_idx=0): ctrl = zwave.ctrl[ctrl_idx] if len(zwave.ctrl) > ctrl_idx else None if ctrl is None or not zwave.net: return ret_jerr(404, "Network and/or Controller inactive") # @TODO: order these similar to the ones in node (also seperate POST/GET) return ret_jajax((CTRL_ACTIONS + CTRL_ATTRS))
def netaction(action): """exec an 'action', restricted to the listed ones (for now)""" if action not in (NET_ACTIONS + NET_ATTRS): return ret_jerr(404, "Requested action: {} not available".format(action)) if (zwave.net is None and action != "start") and not action in NET_ACTIONS: return ret_jerr(404, "Network and/or Controller inactive") ret = action_handler(NET_ATTRS, NET_SUB_ACTIONS, NET_WRAPPED, action, request.args, zwave.net, zwave) if zwave.net is None: return ret_jerr(404, "Network and/or Controller inactive") return Ajax(data={"returned": ret, "executed": action}, jsonify=True) \ .set_state("net", NetState(zwave.net.state)).render()
def ctrlaction(action, ctrl_idx=0): ctrl = zwave.ctrl[ctrl_idx] if len(zwave.ctrl) > ctrl_idx else None if ctrl is None or not zwave.net: return ret_jerr(404, "Network and/or Controller inactive") if action not in (CTRL_ACTIONS + CTRL_ATTRS): return ret_jerr(404, "Requested action: {} not available".format(action)) ret = action_handler(CTRL_ATTRS, CTRL_SUB_ACTIONS, CTRL_WRAPPED, action, request.args, zwave.get_main_ctrl(), zwave) if isinstance(ret, set): ret = list(sorted(ret)) return ret_jajax({ "returned": ret, "executed": action, "controller": ctrl.to_dict() })
def node_action(node_id, action): if node_id not in zwave: return ret_jerr(404, msg="node-id: {} does not exist".format(node_id)) # @TOOD: add list of possible node-actions node = zwave[node_id] ret = getattr(node, action) if callable(ret): ret = ret() return ret_jmsg(msg="Action: {} sent for node-id: {} returned: {}". \ format(action, node_id, str(ret)))
def run_mqtt(): if not cfg.mqtt.active: return ret_jerr("MQTT not active") if cfg.mqtt.topics.raw: for node_id in zwave.net.nodes: node = zwave.get_node(node_id) topic = f"{cfg.mqtt.topics.raw}/node/{node.node_id}" mqtt.publish(topic, node.to_dict()) if cfg.mqtt.topics.ha: for node_id in zwave.net.nodes: node = zwave.get_node(node_id) dev_dct = { "identifiers": [10000 + int(node_id)], "manufacturer": node.manufacturer_name, "model": node.product_name, "name": node.name or ("node_id " + str(node_id)) } for value_id, value in node.values.items(): comp = None cmd_cls = value.command_class if cmd_cls == 37: comp = "switch" elif cmd_cls in [49, 50]: comp = "sensor" else: print(f"unhandled COMMAND CLS: {cmd_cls}") continue _t = f"{cfg.mqtt.topics.ha}/{comp}/{node.node_id}/{value_id}/" # HA auto-discover stuff cfg_topic = _t + "config" myname = node.name or ("node_" + str(node.node_id)) payload = { "unique_id": md5( bytes( str(node.node_id) + "___" + str(value_id), "ascii")).hexdigest(), "name": value.label, "device": dev_dct } state_topic = _t + "state" command_topic = _t + "set" if comp == "switch": payload["command_topic"] = command_topic elif comp in ["sensor", "switch"]: payload["state_topic"] = state_topic mqtt.publish(cfg_topic, json.dumps(payload)) if comp == "switch": mqtt.subscribe(command_topic) # state publish ( state_topic = _t + "state" mqtt.publish(state_topic, value.data) return ret_jmsg("MQTT topics populated")
def nodelist(): if not zwave.net: return ret_jerr(404, "Network and/or Controller inactive") return ret_jajax([to_json(zwave.get_node_details(node_id, NODE_ATTRS)) \ for node_id in zwave.net.nodes])
def available_net_actions(): if zwave.net is None: return ret_jerr(404, "Network and/or Controller inactive") return ret_jajax((NET_ACTIONS + NET_ATTRS))
def netinfo(): if zwave.net is None: return ret_jerr(404, "Network and/or Controller inactive") return ret_jajax(zwave.net.to_dict())
def no_controller(error): return ret_jerr(415, "no controller found")
def not_ready(error): return ret_jerr(414, "not ready")
def not_allowed(error): return ret_jerr(403, "not allowed")
def not_found(error): return ret_jerr(404, "not found: {}".format(request.url))