From 03440a282cc4f130e36995da7bf2e3b34e8b4193 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Tue, 20 Feb 2024 02:33:09 +0300 Subject: mqtt_node_util: support arbitrary node ids --- bin/mqtt_node_util.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'bin/mqtt_node_util.py') diff --git a/bin/mqtt_node_util.py b/bin/mqtt_node_util.py index 639d4b9..a685f26 100755 --- a/bin/mqtt_node_util.py +++ b/bin/mqtt_node_util.py @@ -41,9 +41,13 @@ def on_mqtt_connect(): if __name__ == '__main__': nodes_config = MqttNodesConfig() + node_names = nodes_config.get_nodes(only_names=True) parser = ArgumentParser() - parser.add_argument('--node-id', type=str, required=True, choices=nodes_config.get_nodes(only_names=True)) + parser.add_argument('--node-id', type=str, required=True, + help='one of: '+', '.join(node_names)) + parser.add_argument('--node-id-no-check', action='store_true', + help='when enabled, the script will not check for definition of the node in the mqtt_nodes.yaml config and will use the default password') parser.add_argument('--modules', type=str, choices=get_mqtt_modules(), nargs='*', help='mqtt modules to include') parser.add_argument('--switch-relay', choices=[0, 1], type=int, @@ -56,6 +60,9 @@ if __name__ == '__main__': config.load_app(parser=parser, no_config=True) arg = parser.parse_args() + if not arg.node_id_no_check and arg.node_id not in node_names: + raise ArgumentError(None, f'invalid node_id {arg.node_id}') + if arg.no_wait: no_wait = True @@ -65,8 +72,16 @@ if __name__ == '__main__': mqtt = MqttWrapper(randomize_client_id=True, client_id='mqtt_node_util') mqtt.add_connect_callback(on_mqtt_connect) + + try: + node_password = nodes_config.get_node(arg.node_id)['password'] + except KeyError as e: + if arg.node_id_no_check: + node_password = nodes_config['common']['password'] + else: + raise e mqtt_node = MqttNode(node_id=arg.node_id, - node_secret=nodes_config.get_node(arg.node_id)['password']) + node_secret=node_password) mqtt.add_node(mqtt_node) -- cgit v1.2.3