Add logging and stability fixes
This commit is contained in:
committed by
rospogrigio
parent
7384f1fc54
commit
ff3ca99b29
@@ -181,8 +181,7 @@ class TuyaDevice(pytuya.TuyaListener):
|
|||||||
@callback
|
@callback
|
||||||
def disconnected(self, exc):
|
def disconnected(self, exc):
|
||||||
"""Device disconnected."""
|
"""Device disconnected."""
|
||||||
if exc is not None:
|
_LOGGER.debug("Disconnected from %s: %s", self.id, exc)
|
||||||
_LOGGER.error("Disconnected from %: %s", exc)
|
|
||||||
|
|
||||||
signal = f"localtuya_{self._config_entry[CONF_DEVICE_ID]}"
|
signal = f"localtuya_{self._config_entry[CONF_DEVICE_ID]}"
|
||||||
async_dispatcher_send(self._hass, signal, None)
|
async_dispatcher_send(self._hass, signal, None)
|
||||||
|
@@ -331,7 +331,7 @@ class TuyaProtocol(asyncio.Protocol):
|
|||||||
try:
|
try:
|
||||||
await self.heartbeat()
|
await self.heartbeat()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
self.log.error("Heartbeat failed (%s), disconnecting", ex)
|
self.log.exception("Heartbeat failed (%s), disconnecting", ex)
|
||||||
break
|
break
|
||||||
await asyncio.sleep(HEARTBEAT_INTERVAL)
|
await asyncio.sleep(HEARTBEAT_INTERVAL)
|
||||||
self.close()
|
self.close()
|
||||||
@@ -346,21 +346,25 @@ class TuyaProtocol(asyncio.Protocol):
|
|||||||
|
|
||||||
def connection_lost(self, exc):
|
def connection_lost(self, exc):
|
||||||
"""Disconnected from device."""
|
"""Disconnected from device."""
|
||||||
self.close()
|
try:
|
||||||
listener = self.listener()
|
self.close()
|
||||||
if listener is not None:
|
except Exception:
|
||||||
listener.disconnected(exc)
|
self.log.exception("Failed to close connection")
|
||||||
|
finally:
|
||||||
|
listener = self.listener()
|
||||||
|
if listener is not None:
|
||||||
|
listener.disconnected(exc)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Close connection and abort all outstanding listeners."""
|
"""Close connection and abort all outstanding listeners."""
|
||||||
|
if self.heartbeater is not None:
|
||||||
|
self.heartbeater.cancel()
|
||||||
|
if self.dispatcher is not None:
|
||||||
|
self.dispatcher.abort()
|
||||||
if self.transport is not None:
|
if self.transport is not None:
|
||||||
transport = self.transport
|
transport = self.transport
|
||||||
self.transport = None
|
self.transport = None
|
||||||
transport.close()
|
transport.close()
|
||||||
if self.dispatcher is not None:
|
|
||||||
self.dispatcher.abort()
|
|
||||||
if self.heartbeater is not None:
|
|
||||||
self.heartbeater.cancel()
|
|
||||||
|
|
||||||
async def exchange(self, command, dps=None):
|
async def exchange(self, command, dps=None):
|
||||||
"""Send and receive a message, returning response from device."""
|
"""Send and receive a message, returning response from device."""
|
||||||
@@ -436,11 +440,12 @@ class TuyaProtocol(asyncio.Protocol):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.warning("Failed to get status: %s", e)
|
self.log.warning("Failed to get status: %s", e)
|
||||||
raise
|
raise
|
||||||
detected_dps.update(data["dps"])
|
if "dps" in data:
|
||||||
|
detected_dps.update(data["dps"])
|
||||||
|
|
||||||
if self.dev_type == "type_0a":
|
if self.dev_type == "type_0a":
|
||||||
return detected_dps
|
return detected_dps
|
||||||
|
self.log.debug("detected dps: %s", detected_dps)
|
||||||
return detected_dps
|
return detected_dps
|
||||||
|
|
||||||
def add_dps_to_request(self, dps_index):
|
def add_dps_to_request(self, dps_index):
|
||||||
|
Reference in New Issue
Block a user