Skip to content

Commit

Permalink
Remove unused UART autodetection code
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Dec 1, 2020
1 parent 67cf8a9 commit 61f8ea7
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 347 deletions.
17 changes: 0 additions & 17 deletions tests/application/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,6 @@ async def test_reconnect(device, event_loop, make_application):
await app.shutdown()


@pytest.mark.parametrize("device", FORMED_DEVICES)
async def test_auto_connect(device, mocker, make_application):
app, znp_server = make_application(server_cls=device)

uart_guess_port = mocker.patch(
"zigpy_znp.uart.guess_port", return_value=znp_server._port_path
)

app._config[conf.CONF_DEVICE][conf.CONF_DEVICE_PATH] = "auto"
await app.startup(auto_form=False)

assert uart_guess_port.call_count == 1
assert app._config[conf.CONF_DEVICE][conf.CONF_DEVICE_PATH] == znp_server._port_path

await app.shutdown()


@pytest.mark.parametrize("device", FORMED_DEVICES)
async def test_shutdown_from_app(device, mocker, make_application):
app, znp_server = make_application(server_cls=device)
Expand Down
12 changes: 0 additions & 12 deletions tests/test_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,6 @@ def test_uart_frame_received_error(connected_uart, mocker):
znp.frame_received.call_count == 3


@pytest.mark.asyncio
async def test_connect_auto(dummy_serial_conn, mocker):
device, _ = dummy_serial_conn

mocker.patch("zigpy_znp.uart.guess_port", return_value=device)

znp = mocker.Mock()
await znp_uart.connect(
conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: "auto"}), api=znp, toggle_rts=False
)


@pytest.mark.asyncio
async def test_connection_lost(dummy_serial_conn, mocker, event_loop):
device, _ = dummy_serial_conn
Expand Down
230 changes: 0 additions & 230 deletions tests/test_uart_autodetection.py

This file was deleted.

81 changes: 0 additions & 81 deletions zigpy_znp/uart.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import typing
import asyncio
import logging
import platform
import warnings
from collections import defaultdict

import serial
import serial.tools

try:
from serial.tools.list_ports import comports as list_com_ports
except ValueError:
# macOS Big Sur can't import this method with current PySerial
if platform.system() != "Darwin":
raise

def list_com_ports():
return []


import zigpy_znp.config as conf
import zigpy_znp.frames as frames
Expand Down Expand Up @@ -155,80 +141,13 @@ def __repr__(self) -> str:
return f"<{type(self).__name__} for {self._api}>"


def find_ti_ports() -> typing.Iterable[
typing.Tuple[str, serial.tools.list_ports_common.ListPortInfo]
]:
"""
Finds all TI serial ports and yields an iterable of tuples, where the first element
is the serial number of the device.
"""

# Each dev kit has two serial ports, one of which is a debugger
found_ports = defaultdict(list)

for port in list_com_ports():
if (port.vid, port.pid) == (0x0451, 0x16A8):
# CC2531
found_ports[port.serial_number].append(port)
elif (port.vid, port.pid) == (0x0451, 0xBEF3):
# LAUNCHXL-CC26X2R1
found_ports[port.serial_number].append(port)
elif (port.vid, port.pid) == (0x10C4, 0xEA60):
# slae.sh CC2652RB stick
if "slae.sh cc2652rb stick" in (port.product or ""):
found_ports[port.serial_number].append(port)
else:
found_ports["CP210x"].append(port)
elif (port.vid, port.pid) == (0x1A86, 0x7523):
# ZZH (CH340, no way to distinguish it from any other CH340 device)
found_ports["CH340"].append(port)

# Python guarantees insertion order for dictionaries
for serial_number, ports in found_ports.items():
first_port = sorted(ports, key=lambda p: p.device)[0]

yield serial_number, first_port


def guess_port() -> str:
"""
Autodetects the best TI radio.
The CH340 used by the ZZH adapter has no distinguishing information so it is not
possible to tell whether or not a port belongs to a cheap Arduino clone or the ZZH.
Known TI serial ports are picked over the CH340, which may belong to a cheap Arduino
clone instead of the ZZH.
"""

# Move generic serial adapters to the bottom of the list but keep the order of the
# rest because Python's sort is stable.
candidates = sorted(find_ti_ports(), key=lambda p: p[0] in ("CH340", "CP210x"))

if not candidates:
raise RuntimeError("Failed to detect any TI ports")

_, port = candidates[0]

if len(candidates) > 1:
LOGGER.warning(
"Found multiple possible Texas Instruments devices: %s",
candidates,
)
LOGGER.warning("Picking the first one: %s", port)

return port.device


async def connect(config: conf.ConfigType, api, *, toggle_rts=True) -> ZnpMtProtocol:
loop = asyncio.get_running_loop()

port = config[conf.CONF_DEVICE_PATH]
baudrate = config[conf.CONF_DEVICE_BAUDRATE]
flow_control = config[conf.CONF_DEVICE_FLOW_CONTROL]

if port == "auto":
port = guess_port()

LOGGER.debug("Connecting to %s at %s baud", port, baudrate)

transport, protocol = await serial_asyncio.create_serial_connection(
Expand Down
7 changes: 0 additions & 7 deletions zigpy_znp/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,6 @@ async def _startup(self, auto_form=False):

self._bind_callbacks()

# XXX: To make sure we don't switch to the wrong device upon reconnect,
# update our config to point to the last-detected port.
if self._config[conf.CONF_DEVICE][conf.CONF_DEVICE_PATH] == "auto":
self._config[conf.CONF_DEVICE][
conf.CONF_DEVICE_PATH
] = self._znp._uart._transport.serial.name

# Next, read out the NVRAM item that Zigbee2MQTT writes when it has configured
# a device to make sure that our network settings will not be reset.
if self.is_zstack_home_12:
Expand Down

0 comments on commit 61f8ea7

Please sign in to comment.