From 1f90a2e9fe1fac39bc6a78c13130aa110f4e9c7d Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 21 Aug 2024 18:10:19 +0800 Subject: [PATCH] Fix? --- pytec/tec_qt.py | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index db715ad..cbc32da 100755 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -65,6 +65,7 @@ class MainWindow(QtWidgets.QMainWindow): self.thermostat = Thermostat( self, self.report_refresh_spin.value() ) + self._connecting_task = None def handle_connection_error(): self.info_box.display_info_box( @@ -245,33 +246,30 @@ class MainWindow(QtWidgets.QMainWindow): self.conn_menu.port_set_spin.value(), ) - self._connecting_task = None - try: - if (self._connecting_task is None) or (not self.thermostat.connected()): - self.status_lbl.setText("Connecting...") - self.connect_btn.setText("Stop") - self.conn_menu.host_set_line.setEnabled(False) - self.conn_menu.port_set_spin.setEnabled(False) + if (self._connecting_task is None) and (not self.thermostat.connected()): + self.status_lbl.setText("Connecting...") + self.connect_btn.setText("Stop") + self.conn_menu.host_set_line.setEnabled(False) + self.conn_menu.port_set_spin.setEnabled(False) - try: - self._connecting_task = asyncio.wait_for( - self.thermostat.start_session(host=host, port=port), timeout=5 - ) - await self._connecting_task - except asyncio.TimeoutError: - return - await self._on_connection_changed(True) - else: - if self._connecting_task is not None: - self._connecting_task.cancel() - await self.bail() - - # TODO: Remove asyncio.TimeoutError in Python 3.11 - except (OSError, asyncio.TimeoutError): try: + self._connecting_task = asyncio.create_task( + asyncio.wait_for(self.thermostat.start_session(host=host, port=port), timeout=5) + ) + await self._connecting_task + except (OSError, asyncio.TimeoutError): await self.bail() - except ConnectionResetError: - pass + return + except asyncio.CancelledError: + return + finally: + self._connecting_task = None + + await self._on_connection_changed(True) + else: + if self._connecting_task is not None: + self._connecting_task.cancel() + await self.bail() @asyncSlot() async def bail(self):