Close connection properly on error

This commit is contained in:
Pierre Ståhl
2020-10-04 22:07:38 +02:00
committed by rospogrigio
parent 2565074c9c
commit f971646333
2 changed files with 5 additions and 3 deletions

View File

@@ -108,7 +108,7 @@ class TuyaDevice(pytuya.TuyaListener):
for entity in config_entry[CONF_ENTITIES]:
self._dps_to_request[entity[CONF_ID]] = None
def connect(self, delay=None):
def connect(self):
"""Connet to device if not already connected."""
if self._connect_task is None:
self._connect_task = asyncio.ensure_future(self._make_connection())

View File

@@ -336,11 +336,13 @@ class TuyaProtocol(asyncio.Protocol):
def close(self):
"""Close connection and abort all outstanding listeners."""
if self.transport is not None:
self.dispatcher.abort()
self.heartbeater.cancel()
transport = self.transport
self.transport = None
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):
"""Send and receive a message, returning response from device."""