Improve and document close behavior (#654)

* Close connection on smartprotocol timeout

* tweaks
This commit is contained in:
J. Nick Koston 2024-01-19 10:30:01 -10:00 committed by GitHub
parent 38159140fb
commit d62b5a55cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 12 deletions

View File

@ -19,7 +19,7 @@ class HttpClient:
def __init__(self, config: DeviceConfig) -> None: def __init__(self, config: DeviceConfig) -> None:
self._config = config self._config = config
self._client: aiohttp.ClientSession = None self._client_session: aiohttp.ClientSession = None
self._jar = aiohttp.CookieJar(unsafe=True, quote_cookie=False) self._jar = aiohttp.CookieJar(unsafe=True, quote_cookie=False)
self._last_url = f"http://{self._config.host}/" self._last_url = f"http://{self._config.host}/"
@ -31,9 +31,9 @@ class HttpClient:
): ):
return self._config.http_client return self._config.http_client
if not self._client: if not self._client_session:
self._client = aiohttp.ClientSession(cookie_jar=get_cookie_jar()) self._client_session = aiohttp.ClientSession(cookie_jar=get_cookie_jar())
return self._client return self._client_session
async def post( async def post(
self, self,
@ -91,8 +91,8 @@ class HttpClient:
return None return None
async def close(self) -> None: async def close(self) -> None:
"""Close the client.""" """Close the ClientSession."""
client = self._client client = self._client_session
self._client = None self._client_session = None
if client: if client:
await client.close() await client.close()

View File

@ -320,7 +320,7 @@ class KlapTransport(BaseTransport):
+ f"Response status is {response_status}, Request was {request}" + f"Response status is {response_status}, Request was {request}"
) )
if response_status != 200: if response_status != 200:
_LOGGER.error("Query failed after succesful authentication " + msg) _LOGGER.error("Query failed after successful authentication " + msg)
# If we failed with a security error, force a new handshake next time. # If we failed with a security error, force a new handshake next time.
if response_status == 403: if response_status == 403:
self._handshake_done = False self._handshake_done = False
@ -351,9 +351,8 @@ class KlapTransport(BaseTransport):
return json_payload return json_payload
async def close(self) -> None: async def close(self) -> None:
"""Close the transport.""" """Mark the handshake as not done since we likely lost the connection."""
self._handshake_done = False self._handshake_done = False
await self._http_client.close()
@staticmethod @staticmethod
def generate_auth_hash(creds: Credentials): def generate_auth_hash(creds: Credentials):

View File

@ -84,8 +84,8 @@ class SmartProtocol(TPLinkProtocol):
raise ex raise ex
continue continue
except TimeoutException as ex: except TimeoutException as ex:
await self.close()
if retry >= retry_count: if retry >= retry_count:
await self.close()
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry) _LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
raise ex raise ex
await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_TIMEOUT) await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_TIMEOUT)
@ -167,7 +167,12 @@ class SmartProtocol(TPLinkProtocol):
raise SmartDeviceException(msg, error_code=error_code) raise SmartDeviceException(msg, error_code=error_code)
async def close(self) -> None: async def close(self) -> None:
"""Close the protocol.""" """Close the underlying transport.
Some transports may close the connection, and some may
use this as a hint that they need to reconnect, or
reauthenticate.
"""
await self._transport.close() await self._transport.close()