mirror of
https://github.com/mjg59/python-broadlink.git
synced 2024-11-21 22:51:41 +01:00
Improve exceptions (#551)
This commit is contained in:
parent
a11b7233c9
commit
7c0b4d529f
@ -1,19 +1,17 @@
|
|||||||
"""Exceptions for Broadlink devices."""
|
"""Exceptions for Broadlink devices."""
|
||||||
|
import collections
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
|
|
||||||
class BroadlinkException(Exception):
|
class BroadlinkException(Exception):
|
||||||
"""Common base class for all Broadlink exceptions."""
|
"""Base class common to all Broadlink exceptions."""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
"""Initialize the exception."""
|
"""Initialize the exception."""
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
if len(args) >= 3:
|
if len(args) >= 2:
|
||||||
self.errno = args[0]
|
self.errno = args[0]
|
||||||
self.strerror = "%s: %s" % (args[1], args[2])
|
self.strerror = ": ".join(str(arg) for arg in args[1:])
|
||||||
elif len(args) == 2:
|
|
||||||
self.errno = args[0]
|
|
||||||
self.strerror = str(args[1])
|
|
||||||
elif len(args) == 1:
|
elif len(args) == 1:
|
||||||
self.errno = None
|
self.errno = None
|
||||||
self.strerror = str(args[0])
|
self.strerror = str(args[0])
|
||||||
@ -22,80 +20,92 @@ class BroadlinkException(Exception):
|
|||||||
self.strerror = ""
|
self.strerror = ""
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Return the error message."""
|
"""Return str(self)."""
|
||||||
if self.errno is not None:
|
if self.errno is not None:
|
||||||
return "[Errno %s] %s" % (self.errno, self.strerror)
|
return "[Errno %s] %s" % (self.errno, self.strerror)
|
||||||
return self.strerror
|
return self.strerror
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
"""Return self==value."""
|
||||||
|
return type(self) == type(other) and self.args == other.args
|
||||||
|
|
||||||
class FirmwareException(BroadlinkException):
|
def __hash__(self):
|
||||||
"""Common base class for all firmware exceptions."""
|
"""Return hash(self)."""
|
||||||
|
return hash((type(self), self.args))
|
||||||
|
|
||||||
|
|
||||||
class AuthenticationError(FirmwareException):
|
class MultipleErrors(BroadlinkException):
|
||||||
|
"""Multiple errors."""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
"""Initialize the exception."""
|
||||||
|
errors = args[0][:] if args else []
|
||||||
|
counter = collections.Counter(errors)
|
||||||
|
strerror = "Multiple errors occurred: %s" % counter
|
||||||
|
super().__init__(strerror, **kwargs)
|
||||||
|
self.errors = errors
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
"""Return repr(self)."""
|
||||||
|
return "MultipleErrors(%r)" % self.errors
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
"""Return str(self)."""
|
||||||
|
return self.strerror
|
||||||
|
|
||||||
|
|
||||||
|
class AuthenticationError(BroadlinkException):
|
||||||
"""Authentication error."""
|
"""Authentication error."""
|
||||||
|
|
||||||
|
|
||||||
class AuthorizationError(FirmwareException):
|
class AuthorizationError(BroadlinkException):
|
||||||
"""Authorization error."""
|
"""Authorization error."""
|
||||||
|
|
||||||
|
|
||||||
class CommandNotSupportedError(FirmwareException):
|
class CommandNotSupportedError(BroadlinkException):
|
||||||
"""Command not supported error."""
|
"""Command not supported error."""
|
||||||
|
|
||||||
|
|
||||||
class ConnectionClosedError(FirmwareException):
|
class ConnectionClosedError(BroadlinkException):
|
||||||
"""Connection closed error."""
|
"""Connection closed error."""
|
||||||
|
|
||||||
|
|
||||||
class DataValidationError(FirmwareException):
|
class StructureAbnormalError(BroadlinkException):
|
||||||
"""Data validation error."""
|
"""Structure abnormal error."""
|
||||||
|
|
||||||
|
|
||||||
class DeviceOfflineError(FirmwareException):
|
class DeviceOfflineError(BroadlinkException):
|
||||||
"""Device offline error."""
|
"""Device offline error."""
|
||||||
|
|
||||||
|
|
||||||
class ReadError(FirmwareException):
|
class ReadError(BroadlinkException):
|
||||||
"""Read error."""
|
"""Read error."""
|
||||||
|
|
||||||
|
|
||||||
class SendError(FirmwareException):
|
class SendError(BroadlinkException):
|
||||||
"""Send error."""
|
"""Send error."""
|
||||||
|
|
||||||
|
|
||||||
class SSIDNotFoundError(FirmwareException):
|
class SSIDNotFoundError(BroadlinkException):
|
||||||
"""SSID not found error."""
|
"""SSID not found error."""
|
||||||
|
|
||||||
|
|
||||||
class StorageError(FirmwareException):
|
class StorageError(BroadlinkException):
|
||||||
"""Storage error."""
|
"""Storage error."""
|
||||||
|
|
||||||
|
|
||||||
class WriteError(FirmwareException):
|
class WriteError(BroadlinkException):
|
||||||
"""Write error."""
|
"""Write error."""
|
||||||
|
|
||||||
|
|
||||||
class SDKException(BroadlinkException):
|
class NetworkTimeoutError(BroadlinkException):
|
||||||
"""Common base class for all SDK exceptions."""
|
|
||||||
|
|
||||||
|
|
||||||
class DeviceInformationError(SDKException):
|
|
||||||
"""Device information is not intact."""
|
|
||||||
|
|
||||||
|
|
||||||
class ChecksumError(SDKException):
|
|
||||||
"""Received data packet check error."""
|
|
||||||
|
|
||||||
|
|
||||||
class LengthError(SDKException):
|
|
||||||
"""Received data packet length error."""
|
|
||||||
|
|
||||||
|
|
||||||
class NetworkTimeoutError(SDKException):
|
|
||||||
"""Network timeout error."""
|
"""Network timeout error."""
|
||||||
|
|
||||||
|
|
||||||
|
class DataValidationError(BroadlinkException):
|
||||||
|
"""Data validation error."""
|
||||||
|
|
||||||
|
|
||||||
class UnknownError(BroadlinkException):
|
class UnknownError(BroadlinkException):
|
||||||
"""Unknown error."""
|
"""Unknown error."""
|
||||||
|
|
||||||
@ -107,30 +117,34 @@ BROADLINK_EXCEPTIONS = {
|
|||||||
-3: (DeviceOfflineError, "The device is offline"),
|
-3: (DeviceOfflineError, "The device is offline"),
|
||||||
-4: (CommandNotSupportedError, "Command not supported"),
|
-4: (CommandNotSupportedError, "Command not supported"),
|
||||||
-5: (StorageError, "The device storage is full"),
|
-5: (StorageError, "The device storage is full"),
|
||||||
-6: (DataValidationError, "Structure is abnormal"),
|
-6: (StructureAbnormalError, "Structure is abnormal"),
|
||||||
-7: (AuthorizationError, "Control key is expired"),
|
-7: (AuthorizationError, "Control key is expired"),
|
||||||
-8: (SendError, "Send error"),
|
-8: (SendError, "Send error"),
|
||||||
-9: (WriteError, "Write error"),
|
-9: (WriteError, "Write error"),
|
||||||
-10: (ReadError, "Read error"),
|
-10: (ReadError, "Read error"),
|
||||||
-11: (SSIDNotFoundError, "SSID could not be found in AP configuration"),
|
-11: (SSIDNotFoundError, "SSID could not be found in AP configuration"),
|
||||||
# SDK related errors are generated by this module.
|
# SDK related errors are generated by this module.
|
||||||
-2040: (DeviceInformationError, "Device information is not intact"),
|
-2040: (DataValidationError, "Device information is not intact"),
|
||||||
-4000: (NetworkTimeoutError, "Network timeout"),
|
-4000: (NetworkTimeoutError, "Network timeout"),
|
||||||
-4007: (LengthError, "Received data packet length error"),
|
-4007: (DataValidationError, "Received data packet length error"),
|
||||||
-4008: (ChecksumError, "Received data packet check error"),
|
-4008: (DataValidationError, "Received data packet check error"),
|
||||||
|
-4009: (DataValidationError, "Received data packet information type error"),
|
||||||
|
-4010: (DataValidationError, "Received encrypted data packet length error"),
|
||||||
|
-4011: (DataValidationError, "Received encrypted data packet check error"),
|
||||||
|
-4012: (AuthorizationError, "Device control ID error"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def exception(error_code):
|
def exception(err_code: int) -> BroadlinkException:
|
||||||
"""Return exception corresponding to an error code."""
|
"""Return exception corresponding to an error code."""
|
||||||
try:
|
try:
|
||||||
exc, msg = BROADLINK_EXCEPTIONS[error_code]
|
exc, msg = BROADLINK_EXCEPTIONS[err_code]
|
||||||
return exc(error_code, msg)
|
return exc(err_code, msg)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return UnknownError(error_code, "Unknown error")
|
return UnknownError(err_code, "Unknown error")
|
||||||
|
|
||||||
|
|
||||||
def check_error(error):
|
def check_error(error: bytes) -> None:
|
||||||
"""Raise exception if an error occurred."""
|
"""Raise exception if an error occurred."""
|
||||||
error_code = struct.unpack("h", error)[0]
|
error_code = struct.unpack("h", error)[0]
|
||||||
if error_code:
|
if error_code:
|
||||||
|
Loading…
Reference in New Issue
Block a user