Skip to content

Commit

Permalink
Merge pull request #18 from SphtKr/argparse
Browse files Browse the repository at this point in the history
Initial implementation of argparse--CLI changed!
  • Loading branch information
SphtKr authored Jan 11, 2025
2 parents ef156a1 + 8ef843a commit fee6533
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,26 @@ Currently the module can be started like so:

```
python -m aqualogic_mqtt.client \
[serial port path] \
[MQTT hostname]:[MQTT port] \
[MQTT Discovery Prefix]
-s [serial port path] \
-m [MQTT hostname]:[MQTT port] \
-p [MQTT Discovery Prefix]
```

E.g.

```console
(venv-pool)$ python -m aqualogic_mqtt.client /dev/ttyUSB0 localhost:1883 homeassistant
(venv-pool)$ python -m aqualogic_mqtt.client -s /dev/ttyUSB0 -m localhost:1883 -p homeassistant
```

The MQTT Discovery Prefix determines the "path" on the MQTT broker where
the interface is exposed. For Home assistant, the default is
the interface is exposed. For Home Assistant, the default is
`homeassistant` unless you have changed it in your configuration.

> **NOTE:** While the topic cannot be covered in depth here, be aware that using multiple USB serial devices (including for example a mix of a USB RS485 interface and Z-Wave or Zigbee stick) may result in unpredictable paths for the serial devices--you may need to set up udev rules to make the correct devices show up at the configured path(s).
It is also possible to connect to a Serial/TCP converter (e.g. a USR-N510) with a host:port, like so
It is also possible to use the `-t` option (in lieu of `-s`) to connect to a Serial/TCP converter (e.g. a USR-N510) with a host:port, like so
```console
(venv-pool)$ python -m aqualogic_mqtt.client 192.168.1.88:8899 localhost:1883 homeassistant
(venv-pool)$ python -m aqualogic_mqtt.client -t 192.168.1.88:8899 -m localhost:1883 -p homeassistant
```
Note, however, that using a network converter such as this has
been found to be unreliable for device control (reading values
Expand Down
29 changes: 18 additions & 11 deletions aqualogic_mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import sys
import ssl
from time import sleep
import os
import argparse

import paho.mqtt.client as mqtt

Expand Down Expand Up @@ -105,17 +107,22 @@ def loop_forever(self):
autodisc_prefix = None
source = None
dest = None
if len(sys.argv) >= 3 and len(sys.argv) < 5:
print('Connecting to {}...'.format(sys.argv[1]))
source = sys.argv[1]
dest = sys.argv[2]
if len(sys.argv) == 4 and not sys.argv[3] == '':
autodisc_prefix = sys.argv[3]
else:
print('Usage: python -m aqualogic_mqtt.client [/serial/path|tcphost:port] [mqttdest] [autodiscover_prefix]')
quit()

mqtt_client = Client()

parser = argparse.ArgumentParser(
prog='aqualogic_mqtt',
description='MQTT adapter for pool controllers',
)
parser.add_argument('-m', '--mqtt-dest', required=True, type=str, help="MQTT broker destination in the format host:port")
source_group = parser.add_mutually_exclusive_group(required=True)
source_group.add_argument('-s', '--serial', type=str, help="serial device source (path)")
source_group.add_argument('-t', '--tcp', type=str, help="network serial adapter source in the format host:port")
parser.add_argument('-p', '--discover-prefix', default="homeassistant", type=str, help="MQTT prefix path (default is \"homeassistant\")")
args = parser.parse_args()

source = args.serial if args.serial is not None else args.tcp
dest = args.mqtt_dest

mqtt_client = Client(discover_prefix=args.discover_prefix)
mqtt_client.connect_mqtt(dest=dest)
mqtt_client.connect_panel(source)
mqtt_client.loop_forever()
Expand Down

0 comments on commit fee6533

Please sign in to comment.