diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index a3a10a3..5c8886f 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -112,7 +112,21 @@ class TuyaDevice(pytuya.TuyaListener): def connect(self): """Connet to device if not already connected.""" if not self._is_closing and self._connect_task is None and not self._interface: + _LOGGER.debug( + "Connecting to %s (%s)", + self._config_entry[CONF_HOST], + self._config_entry[CONF_DEVICE_ID], + ) self._connect_task = asyncio.ensure_future(self._make_connection()) + else: + _LOGGER.debug( + "Already connecting to %s (%s) - %s, %s, %s", + self._config_entry[CONF_HOST], + self._config_entry[CONF_DEVICE_ID], + self._is_closing, + self._connect_task, + self._interface, + ) async def _make_connection(self): backoff = min( diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index ca6b7ea..3b866e4 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -327,6 +327,7 @@ class TuyaProtocol(asyncio.Protocol): async def heartbeat_loop(): """Continuously send heart beat updates.""" + self.log.debug("Started heartbeat loop") while True: try: await self.heartbeat() @@ -334,6 +335,7 @@ class TuyaProtocol(asyncio.Protocol): self.log.exception("Heartbeat failed (%s), disconnecting", ex) break await asyncio.sleep(HEARTBEAT_INTERVAL) + self.log.debug("Stopped heartbeat loop") self.close() self.transport = transport @@ -346,17 +348,22 @@ class TuyaProtocol(asyncio.Protocol): def connection_lost(self, exc): """Disconnected from device.""" + self.log.debug("connection_lost: %s", exc) try: self.close() except Exception: self.log.exception("Failed to close connection") finally: - listener = self.listener() - if listener is not None: - listener.disconnected(exc) + try: + listener = self.listener() + if listener is not None: + listener.disconnected(exc) + except Exception: + self.log.exception("Failed to call disconnected callback") def close(self): """Close connection and abort all outstanding listeners.""" + self.log.debug("Closing connection") if self.heartbeater is not None: self.heartbeater.cancel() if self.dispatcher is not None: