]> git.sven.stormbind.net Git - sven/scripts.git/commitdiff
Move enverbridge setid script from optparse to argparse
authorSven Hoexter <sven@stormbind.net>
Thu, 31 Mar 2022 20:18:51 +0000 (22:18 +0200)
committerSven Hoexter <sven@stormbind.net>
Thu, 31 Mar 2022 20:18:51 +0000 (22:18 +0200)
home/enverbridge.py

index 9f8690740cd64ab00e5e95998d75d13c57b0e473..04cbe0e8d365c6d22f772762bda467ac7847fc72 100755 (executable)
 # port 8765. Wait for a response paket to the broadcast address
 # on port 8764.
 
-from optparse import OptionParser
+import argparse
 import socket
 import sys
 import re
 
-parser = OptionParser(usage="usage: %prog [options] MIIDs")
-parser.add_option("-b", "--bid",
-                  action="store",
-                  type="int",
-                  dest="bid",
-                  help="Serial Number of your EnverBridge")
-(options, args) = parser.parse_args()
+parser = argparse.ArgumentParser()
+parser.add_argument("-b",
+                    "--bid",
+                    action="store",
+                    type=int,
+                    dest="bid",
+                    required=True,
+                    help="Serial Number of your EnverBridge")
+parser.add_argument("miids", nargs="+", help="MIIDs of your MicroInverter")
+args = parser.parse_args()
 
 
 def validateMIID(miids):
@@ -49,17 +52,12 @@ def buildMIIDList(miids):
     return msg
 
 
-# minimal input validation
-if len(args) < 1:
-    print("Error: missing MIIDs, provide at least one inverter serial")
-    sys.exit(22)
-
-validateMIID(args)
+validateMIID(args.miids)
 
 # assemble message string and convert to bytes
-message = str(options.bid) + buildMIIDList(args)
+message = str(args.bid) + buildMIIDList(args.miids)
 message = message.encode()
-print('Sending to EnverBridge with ID', options.bid)
+print('Sending to EnverBridge with ID', args.bid)
 
 # Create a UDP client socket w/ broadcast
 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)