From 5ebf6c8aa17d9fdd51bc2165c62b7f6f2f4fc6b5 Mon Sep 17 00:00:00 2001 From: Vlad Firoiu Date: Tue, 24 Oct 2023 18:44:52 +0100 Subject: [PATCH] CLI: allow websocket connection by specifying api token. --- README.md | 2 +- scripts/neohub_cli.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0870a76..02c5fe6 100644 --- a/README.md +++ b/README.md @@ -54,5 +54,5 @@ $ neohub_cli.py help # Shows all commands $ neohub_cli.py help set_time # Displays help for the set_time function $ neohub_cli.py --hub_ip=myneohub set_time "2021-01-31 15:43:00" # Specify times like this $ neohub_cli.py --hub_ip=myneohub set_lock 1234 "Living Room" # Name NeoStats like this. +$ neohub_cli.py --hub_ip=myneohub --hub_token=XXX get_system # Get system variables with websocket connection ``` - diff --git a/scripts/neohub_cli.py b/scripts/neohub_cli.py index e4604be..6c00cae 100755 --- a/scripts/neohub_cli.py +++ b/scripts/neohub_cli.py @@ -35,8 +35,9 @@ class NeoHubCLIArgumentError(Error): class NeoHubCLI(object): """A runner for neohub_cli operations.""" - def __init__(self, command, args, hub_ip=None, hub_port=4242): - self._hub = NeoHub(host=hub_ip, port=hub_port) + def __init__(self, command, args, hub_ip=None, hub_token=None): + hub_port = 4242 if hub_token is None else 4243 + self._hub = NeoHub(host=hub_ip, port=hub_port, token=hub_token) self._command = command self._args = args # live data cached from the neohub. We assume this data will remain current @@ -279,8 +280,7 @@ class NeoHubCLI(object): async def main(): argp = argparse.ArgumentParser(description='CLI to neohub devices') argp.add_argument('--hub_ip', help='IP address of NeoHub', default=None) - argp.add_argument( - '--hub_port', help='Port number of NeoHub to talk to', default=4242) + argp.add_argument('--hub_token', help='API token', default=None) argp.add_argument('--format', help='Output format', default='list') argp.add_argument('command', help='Command to issue') argp.add_argument('arg', help='Arguments to command', nargs='*') @@ -291,7 +291,8 @@ async def main(): args.command, args.arg, hub_ip=args.hub_ip, - hub_port=args.hub_port) + hub_token=args.hub_token, + ) m = await nhc.callable() if m: result = await m()