mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-04-26 16:46:23 +00:00
Improve and document close behavior (#654)
* Close connection on smartprotocol timeout * tweaks
This commit is contained in:
parent
38159140fb
commit
d62b5a55cc
@ -19,7 +19,7 @@ class HttpClient:
|
||||
|
||||
def __init__(self, config: DeviceConfig) -> None:
|
||||
self._config = config
|
||||
self._client: aiohttp.ClientSession = None
|
||||
self._client_session: aiohttp.ClientSession = None
|
||||
self._jar = aiohttp.CookieJar(unsafe=True, quote_cookie=False)
|
||||
self._last_url = f"http://{self._config.host}/"
|
||||
|
||||
@ -31,9 +31,9 @@ class HttpClient:
|
||||
):
|
||||
return self._config.http_client
|
||||
|
||||
if not self._client:
|
||||
self._client = aiohttp.ClientSession(cookie_jar=get_cookie_jar())
|
||||
return self._client
|
||||
if not self._client_session:
|
||||
self._client_session = aiohttp.ClientSession(cookie_jar=get_cookie_jar())
|
||||
return self._client_session
|
||||
|
||||
async def post(
|
||||
self,
|
||||
@ -91,8 +91,8 @@ class HttpClient:
|
||||
return None
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Close the client."""
|
||||
client = self._client
|
||||
self._client = None
|
||||
"""Close the ClientSession."""
|
||||
client = self._client_session
|
||||
self._client_session = None
|
||||
if client:
|
||||
await client.close()
|
||||
|
@ -320,7 +320,7 @@ class KlapTransport(BaseTransport):
|
||||
+ f"Response status is {response_status}, Request was {request}"
|
||||
)
|
||||
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 response_status == 403:
|
||||
self._handshake_done = False
|
||||
@ -351,9 +351,8 @@ class KlapTransport(BaseTransport):
|
||||
return json_payload
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Close the transport."""
|
||||
"""Mark the handshake as not done since we likely lost the connection."""
|
||||
self._handshake_done = False
|
||||
await self._http_client.close()
|
||||
|
||||
@staticmethod
|
||||
def generate_auth_hash(creds: Credentials):
|
||||
|
@ -84,8 +84,8 @@ class SmartProtocol(TPLinkProtocol):
|
||||
raise ex
|
||||
continue
|
||||
except TimeoutException as ex:
|
||||
if retry >= retry_count:
|
||||
await self.close()
|
||||
if retry >= retry_count:
|
||||
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
||||
raise ex
|
||||
await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_TIMEOUT)
|
||||
@ -167,7 +167,12 @@ class SmartProtocol(TPLinkProtocol):
|
||||
raise SmartDeviceException(msg, error_code=error_code)
|
||||
|
||||
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()
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user