diff --git a/meshtastic/serial_interface.py b/meshtastic/serial_interface.py index 8d1397c6..e80bc2d6 100644 --- a/meshtastic/serial_interface.py +++ b/meshtastic/serial_interface.py @@ -35,8 +35,6 @@ def __init__( debugOut {stream} -- If a stream is provided, any debug serial output from the device will be emitted to that stream. (default: {None}) timeout -- How long to wait for replies (default: 300 seconds) """ - self.noProto = noProto - self.devPath: Optional[str] = devPath if self.devPath is None: @@ -52,6 +50,11 @@ def __init__( else: self.devPath = ports[0] + StreamInterface.__init__( + self, debugOut=debugOut, noProto=noProto, connectNow=connectNow, noNodes=noNodes, timeout=timeout + ) + + def connect(self) -> None: logger.debug(f"Connecting to {self.devPath}") if sys.platform != "win32": @@ -65,9 +68,7 @@ def __init__( self.stream.flush() # type: ignore[attr-defined] time.sleep(0.1) - StreamInterface.__init__( - self, debugOut=debugOut, noProto=noProto, connectNow=connectNow, noNodes=noNodes, timeout=timeout - ) + super().connect() def _set_hupcl_with_termios(self, f: TextIOWrapper): """first we need to set the HUPCL so the device will not reboot based on RTS and/or DTR diff --git a/meshtastic/stream_interface.py b/meshtastic/stream_interface.py index 06ee28a3..a13404d2 100644 --- a/meshtastic/stream_interface.py +++ b/meshtastic/stream_interface.py @@ -39,15 +39,14 @@ def __init__( # pylint: disable=R0917 timeout -- How long to wait for replies (default: 300 seconds) Raises: - Exception: [description] - Exception: [description] + RuntimeError: Raised if StreamInterface is instantiated when noProto is false. """ - if not hasattr(self, "stream") and not noProto: - raise Exception( # pylint: disable=W0719 + if not noProto and type(self) == StreamInterface: # pylint: disable=C0123 + raise RuntimeError( "StreamInterface is now abstract (to update existing code create SerialInterface instead)" ) - self.stream: Optional[serial.Serial] # only serial uses this, TCPInterface overrides the relevant methods instead + self.stream: Optional[serial.Serial] = None # only serial uses this, TCPInterface overrides the relevant methods instead self._rxBuf = bytes() # empty self._wantExit = False diff --git a/meshtastic/tcp_interface.py b/meshtastic/tcp_interface.py index 732f37ef..9fbd7fec 100644 --- a/meshtastic/tcp_interface.py +++ b/meshtastic/tcp_interface.py @@ -31,19 +31,11 @@ def __init__( hostname {string} -- Hostname/IP address of the device to connect to timeout -- How long to wait for replies (default: 300 seconds) """ - - self.stream = None - self.hostname: str = hostname self.portNumber: int = portNumber self.socket: Optional[socket.socket] = None - if connectNow: - self.myConnect() - else: - self.socket = None - super().__init__(debugOut=debugOut, noProto=noProto, connectNow=connectNow, noNodes=noNodes, timeout=timeout) def __repr__(self): @@ -68,8 +60,13 @@ def _socket_shutdown(self) -> None: if self.socket is not None: self.socket.shutdown(socket.SHUT_RDWR) + def connect(self) -> None: + """Connect the interface""" + self.myConnect() + super().connect() + def myConnect(self) -> None: - """Connect to socket""" + """Connect to socket (without attempting to start the interface's receive thread""" logger.debug(f"Connecting to {self.hostname}") # type: ignore[str-bytes-safe] server_address = (self.hostname, self.portNumber) self.socket = socket.create_connection(server_address) diff --git a/meshtastic/tests/test_stream_interface.py b/meshtastic/tests/test_stream_interface.py index 3411ffb8..1e1d5e3c 100644 --- a/meshtastic/tests/test_stream_interface.py +++ b/meshtastic/tests/test_stream_interface.py @@ -15,7 +15,7 @@ def test_StreamInterface(): """Test that we cannot instantiate a StreamInterface based on noProto""" with pytest.raises(Exception) as pytest_wrapped_e: StreamInterface() - assert pytest_wrapped_e.type == Exception + assert pytest_wrapped_e.type == RuntimeError # Note: This takes a bit, so moving from unit to slow