mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 00:18:11 +01:00
Handle multiple serial ports better
This commit is contained in:
parent
0ab63dc4d4
commit
58e1b8454d
1 changed files with 38 additions and 23 deletions
|
@ -12,7 +12,7 @@ from esphomeyaml.const import CONF_ESPHOMEYAML, CONF_HOSTNAME, CONF_MANUAL_IP, C
|
||||||
CONF_STATIC_IP, \
|
CONF_STATIC_IP, \
|
||||||
CONF_WIFI, CONF_LOGGER, CONF_BAUD_RATE
|
CONF_WIFI, CONF_LOGGER, CONF_BAUD_RATE
|
||||||
from esphomeyaml.helpers import AssignmentExpression, RawStatement, _EXPRESSIONS, add, \
|
from esphomeyaml.helpers import AssignmentExpression, RawStatement, _EXPRESSIONS, add, \
|
||||||
get_variable, indent, quote, statement
|
get_variable, indent, quote, statement, color
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -36,16 +36,39 @@ def discover_serial_ports():
|
||||||
except ImportError:
|
except ImportError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
result = None
|
result = []
|
||||||
for port, _, info in comports():
|
descs = []
|
||||||
|
for port, desc, info in comports():
|
||||||
if not port:
|
if not port:
|
||||||
continue
|
continue
|
||||||
if "VID:PID" in info:
|
if "VID:PID" in info:
|
||||||
if result is not None:
|
result.append(port)
|
||||||
return None
|
descs.append(desc)
|
||||||
result = port
|
|
||||||
|
|
||||||
return result
|
if not result:
|
||||||
|
return None
|
||||||
|
if len(result) == 1:
|
||||||
|
return result[0]
|
||||||
|
print(u"Found multiple serial port options, please choose one:")
|
||||||
|
for i, (res, desc) in enumerate(zip(result, descs)):
|
||||||
|
print(u" [{}] {} ({})".format(i, res, desc))
|
||||||
|
print(u" [{}] Over The Air".format(len(result)))
|
||||||
|
print()
|
||||||
|
while True:
|
||||||
|
opt = raw_input('(number): ')
|
||||||
|
if opt in result:
|
||||||
|
opt = result.index(opt)
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
opt = int(opt)
|
||||||
|
if opt < 0 or opt > len(result):
|
||||||
|
raise ValueError
|
||||||
|
break
|
||||||
|
except ValueError:
|
||||||
|
print(color('red', u"Invalid option: '{}'".format(opt)))
|
||||||
|
if opt == len(result):
|
||||||
|
return None
|
||||||
|
return result[opt]
|
||||||
|
|
||||||
|
|
||||||
def run_platformio(*cmd):
|
def run_platformio(*cmd):
|
||||||
|
@ -125,15 +148,7 @@ def compile_program(config):
|
||||||
|
|
||||||
def upload_program(config, args, port):
|
def upload_program(config, args, port):
|
||||||
_LOGGER.info("Uploading binary...")
|
_LOGGER.info("Uploading binary...")
|
||||||
if args.upload_port is not None:
|
|
||||||
if args.upload_port == 'HELLO':
|
|
||||||
return run_platformio('platformio', 'run', '-d', get_base_path(config),
|
|
||||||
'-t', 'upload')
|
|
||||||
return run_platformio('platformio', 'run', '-d', get_base_path(config),
|
|
||||||
'-t', 'upload', '--upload-port', args.upload_port)
|
|
||||||
|
|
||||||
if port is not None:
|
if port is not None:
|
||||||
_LOGGER.info("Serial device discovered, using it for upload")
|
|
||||||
return run_platformio('platformio', 'run', '-d', get_base_path(config),
|
return run_platformio('platformio', 'run', '-d', get_base_path(config),
|
||||||
'-t', 'upload', '--upload-port', port)
|
'-t', 'upload', '--upload-port', port)
|
||||||
|
|
||||||
|
@ -159,7 +174,7 @@ def upload_program(config, args, port):
|
||||||
|
|
||||||
|
|
||||||
def show_logs(config, args, port):
|
def show_logs(config, args, port):
|
||||||
if port is not None:
|
if port is not None and port != 'OTA':
|
||||||
run_miniterm(config, port)
|
run_miniterm(config, port)
|
||||||
return 0
|
return 0
|
||||||
return mqtt.show_logs(config, args.topic, args.username, args.password, args.client_id)
|
return mqtt.show_logs(config, args.topic, args.username, args.password, args.client_id)
|
||||||
|
@ -211,8 +226,7 @@ def main():
|
||||||
parser_upload = subparsers.add_parser('upload', help='Validate the configuration '
|
parser_upload = subparsers.add_parser('upload', help='Validate the configuration '
|
||||||
'and upload the latest binary.')
|
'and upload the latest binary.')
|
||||||
parser_upload.add_argument('--upload-port', help="Manually specify the upload port to use. "
|
parser_upload.add_argument('--upload-port', help="Manually specify the upload port to use. "
|
||||||
"For example /dev/cu.SLAB_USBtoUAR.",
|
"For example /dev/cu.SLAB_USBtoUART.")
|
||||||
nargs='?', const='HELLO')
|
|
||||||
parser_upload.add_argument('--host-port', help="Specify the host port.", type=int)
|
parser_upload.add_argument('--host-port', help="Specify the host port.", type=int)
|
||||||
|
|
||||||
parser_logs = subparsers.add_parser('logs', help='Validate the configuration '
|
parser_logs = subparsers.add_parser('logs', help='Validate the configuration '
|
||||||
|
@ -221,12 +235,13 @@ def main():
|
||||||
parser_logs.add_argument('--username', help='Manually set the username.')
|
parser_logs.add_argument('--username', help='Manually set the username.')
|
||||||
parser_logs.add_argument('--password', help='Manually set the password.')
|
parser_logs.add_argument('--password', help='Manually set the password.')
|
||||||
parser_logs.add_argument('--client-id', help='Manually set the client id.')
|
parser_logs.add_argument('--client-id', help='Manually set the client id.')
|
||||||
|
parser_logs.add_argument('--serial-port', help="Manually specify a serial port to use"
|
||||||
|
"For example /dev/cu.SLAB_USBtoUART.")
|
||||||
|
|
||||||
parser_run = subparsers.add_parser('run', help='Validate the configuration, create a binary, '
|
parser_run = subparsers.add_parser('run', help='Validate the configuration, create a binary, '
|
||||||
'upload it, and start MQTT logs.')
|
'upload it, and start MQTT logs.')
|
||||||
parser_run.add_argument('--upload-port', help="Manually specify the upload port to use. "
|
parser_run.add_argument('--upload-port', help="Manually specify the upload port to use. "
|
||||||
"For example /dev/cu.SLAB_USBtoUAR.",
|
"For example /dev/cu.SLAB_USBtoUART.")
|
||||||
nargs='?', const='HELLO')
|
|
||||||
parser_run.add_argument('--host-port', help="Specify the host port to use for OTA", type=int)
|
parser_run.add_argument('--host-port', help="Specify the host port to use for OTA", type=int)
|
||||||
parser_run.add_argument('--no-logs', help='Disable starting MQTT logs.',
|
parser_run.add_argument('--no-logs', help='Disable starting MQTT logs.',
|
||||||
action='store_true')
|
action='store_true')
|
||||||
|
@ -268,14 +283,14 @@ def main():
|
||||||
_LOGGER.info(u"Successfully compiled program.")
|
_LOGGER.info(u"Successfully compiled program.")
|
||||||
return 0
|
return 0
|
||||||
elif args.command == 'upload':
|
elif args.command == 'upload':
|
||||||
port = discover_serial_ports()
|
port = args.upload_port or discover_serial_ports()
|
||||||
exit_code = upload_program(config, args, port)
|
exit_code = upload_program(config, args, port)
|
||||||
if exit_code != 0:
|
if exit_code != 0:
|
||||||
return exit_code
|
return exit_code
|
||||||
_LOGGER.info(u"Successfully uploaded program.")
|
_LOGGER.info(u"Successfully uploaded program.")
|
||||||
return 0
|
return 0
|
||||||
elif args.command == 'logs':
|
elif args.command == 'logs':
|
||||||
port = discover_serial_ports()
|
port = args.serial_port or discover_serial_ports()
|
||||||
return show_logs(config, args, port)
|
return show_logs(config, args, port)
|
||||||
elif args.command == 'clean-mqtt':
|
elif args.command == 'clean-mqtt':
|
||||||
return clean_mqtt(config, args)
|
return clean_mqtt(config, args)
|
||||||
|
@ -289,7 +304,7 @@ def main():
|
||||||
_LOGGER.info(u"Successfully compiled program.")
|
_LOGGER.info(u"Successfully compiled program.")
|
||||||
if args.no_logs:
|
if args.no_logs:
|
||||||
return 0
|
return 0
|
||||||
port = discover_serial_ports()
|
port = args.upload_port or discover_serial_ports()
|
||||||
exit_code = upload_program(config, args, port)
|
exit_code = upload_program(config, args, port)
|
||||||
if exit_code != 0:
|
if exit_code != 0:
|
||||||
return exit_code
|
return exit_code
|
||||||
|
|
Loading…
Reference in a new issue