1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-14 03:20:14 +01:00
python-broadlink/broadlink/exceptions.py

171 lines
3.7 KiB
Python
Raw Normal View History

"""Exceptions for Broadlink devices."""
import struct
class BroadlinkException(Exception):
"""Common base class for all Broadlink exceptions."""
def __init__(self, *args, **kwargs):
"""Initialize the exception."""
super().__init__(*args, **kwargs)
if len(args) >= 3:
self.errno = args[0]
self.strerror = "%s: %s" % (args[1], args[2])
elif len(args) == 2:
self.errno = args[0]
self.strerror = args[1]
elif len(args) == 1:
self.errno = None
self.strerror = args[0]
else:
self.errno = None
self.strerror = None
def __str__(self):
"""Return the error message."""
if self.errno is not None:
err_msg = "[Errno %s] %s" % (self.errno, self.strerror)
elif self.strerror is not None:
err_msg = "%s" % (self.strerror)
else:
err_msg = ""
return err_msg
class FirmwareException(BroadlinkException):
"""Common base class for all firmware exceptions."""
pass
class AuthenticationError(FirmwareException):
"""Authentication error."""
pass
class AuthorizationError(FirmwareException):
"""Authorization error."""
pass
class CommandNotSupportedError(FirmwareException):
"""Command not supported error."""
pass
class ConnectionClosedError(FirmwareException):
"""Connection closed error."""
pass
class DataValidationError(FirmwareException):
"""Data validation error."""
pass
class DeviceOfflineError(FirmwareException):
"""Device offline error."""
pass
class ReadError(FirmwareException):
"""Read error."""
pass
class SendError(FirmwareException):
"""Send error."""
pass
class SSIDNotFoundError(FirmwareException):
"""SSID not found error."""
pass
class StorageError(FirmwareException):
"""Storage error."""
pass
class WriteError(FirmwareException):
"""Write error."""
pass
class SDKException(BroadlinkException):
"""Common base class for all SDK exceptions."""
pass
class ChecksumError(SDKException):
"""Received data packet check error."""
pass
class LengthError(SDKException):
"""Received data packet length error."""
pass
class NetworkTimeoutError(SDKException):
"""Network timeout error."""
pass
class UnknownError(BroadlinkException):
"""Unknown error."""
pass
BROADLINK_EXCEPTIONS = {
# Firmware-related errors are generated by the device.
-1: (AuthenticationError, "Authentication failed"),
-2: (ConnectionClosedError, "You have been logged out"),
-3: (DeviceOfflineError, "The device is offline"),
-4: (CommandNotSupportedError, "Command not supported"),
-5: (StorageError, "The device storage is full"),
-6: (DataValidationError, "Structure is abnormal"),
-7: (AuthorizationError, "Control key is expired"),
-8: (SendError, "Send error"),
-9: (WriteError, "Write error"),
-10: (ReadError, "Read error"),
-11: (SSIDNotFoundError, "SSID could not be found in AP configuration"),
# DNASDK related errors are generated by this module.
-4000: (NetworkTimeoutError, "Network timeout"),
-4007: (LengthError, "Received data packet length error"),
-4008: (ChecksumError, "Received data packet check error"),
}
def exception(error_code):
"""Return exception corresponding to an error code."""
try:
exc, msg = BROADLINK_EXCEPTIONS[error_code]
return exc(error_code, msg)
except KeyError:
return UnknownError(error_code, "Unknown error")
def check_error(error):
"""Raise exception if an error occurred."""
error_code = struct.unpack("h", error)[0]
if error_code:
raise exception(error_code)