Raise KasaException on decryption errors (#1078)

Currently if the library encounters an invalid decryption error it
raises a value error. This PR wraps it in a KasaException so consumers
such as HA can catch an expected library exception.
This commit is contained in:
Steven B.
2024-07-22 19:33:31 +01:00
committed by GitHub
parent c4f015a2fb
commit 06ff598d9c
2 changed files with 73 additions and 3 deletions

View File

@@ -50,7 +50,7 @@ import logging
import secrets
import struct
import time
from typing import Any, cast
from typing import TYPE_CHECKING, Any, cast
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
@@ -354,9 +354,14 @@ class KlapTransport(BaseTransport):
else:
_LOGGER.debug("Device %s query posted %s", self._host, msg)
# Check for mypy
if self._encryption_session is not None:
if TYPE_CHECKING:
assert self._encryption_session
try:
decrypted_response = self._encryption_session.decrypt(response_data)
except Exception as ex:
raise KasaException(
f"Error trying to decrypt device {self._host} response: {ex}"
) from ex
json_payload = json_loads(decrypted_response)