1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-10 15:22:24 +01:00

Try to fix serial ports listing (#1155)

This commit is contained in:
Otto Winter
2020-07-24 10:09:43 +02:00
committed by GitHub
parent ebbfab608c
commit e8272759be
3 changed files with 42 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
from typing import Union
from typing import Union, List
import collections
import io
@@ -7,6 +7,7 @@ import os
import re
import subprocess
import sys
from pathlib import Path
from esphome import const
@@ -256,3 +257,34 @@ def filter_yaml_files(files):
files = [f for f in files if os.path.basename(f) != 'secrets.yaml']
files = [f for f in files if not os.path.basename(f).startswith('.')]
return files
class SerialPort:
def __init__(self, path: str, description: str):
self.path = path
self.description = description
# from https://github.com/pyserial/pyserial/blob/master/serial/tools/list_ports.py
def get_serial_ports() -> List[SerialPort]:
from serial.tools.list_ports import comports
result = []
for port, desc, info in comports(include_links=True):
if not port:
continue
if "VID:PID" in info:
result.append(SerialPort(path=port, description=desc))
# Also add objects in /dev/serial/by-id/
# ref: https://github.com/esphome/issues/issues/1346
by_id_path = Path('/dev/serial/by-id')
if sys.platform.lower().startswith('linux') and by_id_path.exists():
from serial.tools.list_ports_linux import SysFS
for path in by_id_path.glob('*'):
device = SysFS(path)
if device.subsystem == 'platform':
result.append(SerialPort(path=str(path), description=info[1]))
result.sort(key=lambda x: x.path)
return result