mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-12 02:18:02 +00:00
Defer module updates for less volatile modules (#1052)
Addresses stability issues on older hw device versions - Handles module timeout errors better by querying modules individually on errors and disabling problematic modules like Firmware that go out to the internet to get updates. - Addresses an issue with the Led module on P100 hardware version 1.0 which appears to have a memory leak and will cause the device to crash after approximately 500 calls. - Delays updates of modules that do not have regular changes like LightPreset and LightEffect and enables them to be updated on the next update cycle only if required values have changed.
This commit is contained in:
@@ -75,13 +75,21 @@ class HttpClient:
|
||||
now = time.time()
|
||||
gap = now - self._last_request_time
|
||||
if gap < self._wait_between_requests:
|
||||
await asyncio.sleep(self._wait_between_requests - gap)
|
||||
sleep = self._wait_between_requests - gap
|
||||
_LOGGER.debug(
|
||||
"Device %s waiting %s seconds to send request",
|
||||
self._config.host,
|
||||
sleep,
|
||||
)
|
||||
await asyncio.sleep(sleep)
|
||||
|
||||
_LOGGER.debug("Posting to %s", url)
|
||||
response_data = None
|
||||
self._last_url = url
|
||||
self.client.cookie_jar.clear()
|
||||
return_json = bool(json)
|
||||
client_timeout = aiohttp.ClientTimeout(total=self._config.timeout)
|
||||
|
||||
# If json is not a dict send as data.
|
||||
# This allows the json parameter to be used to pass other
|
||||
# types of data such as async_generator and still have json
|
||||
@@ -95,9 +103,10 @@ class HttpClient:
|
||||
params=params,
|
||||
data=data,
|
||||
json=json,
|
||||
timeout=self._config.timeout,
|
||||
timeout=client_timeout,
|
||||
cookies=cookies_dict,
|
||||
headers=headers,
|
||||
ssl=False,
|
||||
)
|
||||
async with resp:
|
||||
if resp.status == 200:
|
||||
@@ -106,9 +115,15 @@ class HttpClient:
|
||||
response_data = json_loads(response_data.decode())
|
||||
|
||||
except (aiohttp.ServerDisconnectedError, aiohttp.ClientOSError) as ex:
|
||||
if isinstance(ex, aiohttp.ClientOSError):
|
||||
if not self._wait_between_requests:
|
||||
_LOGGER.debug(
|
||||
"Device %s received an os error, "
|
||||
"enabling sequential request delay: %s",
|
||||
self._config.host,
|
||||
ex,
|
||||
)
|
||||
self._wait_between_requests = self.WAIT_BETWEEN_REQUESTS_ON_OSERROR
|
||||
self._last_request_time = time.time()
|
||||
self._last_request_time = time.time()
|
||||
raise _ConnectionError(
|
||||
f"Device connection error: {self._config.host}: {ex}", ex
|
||||
) from ex
|
||||
|
Reference in New Issue
Block a user