From 5241bcc43b4ea5856540667f6e2b1002dc64d6d9 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Tue, 5 Dec 2023 16:35:39 +0100 Subject: [PATCH] Raise InvalidTokenException on invalid token (#1874) Invalid checksum raises now a more specialized `InvalidTokenException`. This will allow downstreams to request the user to check the token. --- miio/__init__.py | 1 + miio/exceptions.py | 4 ++++ miio/miioprotocol.py | 9 +++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/miio/__init__.py b/miio/__init__.py index 895ae3e06..8e50b2aba 100644 --- a/miio/__init__.py +++ b/miio/__init__.py @@ -11,6 +11,7 @@ from miio.devicestatus import DeviceStatus from miio.exceptions import ( DeviceError, + InvalidTokenException, DeviceException, UnsupportedFeatureException, DeviceInfoUnavailableException, diff --git a/miio/exceptions.py b/miio/exceptions.py index 4c1e9bf1b..90f6832d3 100644 --- a/miio/exceptions.py +++ b/miio/exceptions.py @@ -2,6 +2,10 @@ class DeviceException(Exception): """Exception wrapping any communication errors with the device.""" +class InvalidTokenException(DeviceException): + """Exception raised when invalid token is detected.""" + + class PayloadDecodeException(DeviceException): """Exception for failures in payload decoding. diff --git a/miio/miioprotocol.py b/miio/miioprotocol.py index 6fe8eea09..e56624712 100644 --- a/miio/miioprotocol.py +++ b/miio/miioprotocol.py @@ -13,7 +13,12 @@ import construct -from .exceptions import DeviceError, DeviceException, RecoverableError +from .exceptions import ( + DeviceError, + DeviceException, + InvalidTokenException, + RecoverableError, +) from .protocol import Message _LOGGER = logging.getLogger(__name__) @@ -219,7 +224,7 @@ def send( except KeyError: return payload except construct.core.ChecksumError as ex: - raise DeviceException( + raise InvalidTokenException( "Got checksum error which indicates use " "of an invalid token. " "Please check your token!"