2020-05-07 07:59:21 +02:00
|
|
|
"""Exceptions for Broadlink devices."""
|
2020-09-06 10:51:45 +02:00
|
|
|
import struct
|
2020-05-07 07:59:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BroadlinkException(Exception):
|
|
|
|
"""Common base class for all Broadlink exceptions."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
|
|
|
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]
|
2020-09-16 03:55:37 +02:00
|
|
|
self.strerror = str(args[1])
|
2020-09-06 10:51:45 +02:00
|
|
|
elif len(args) == 1:
|
|
|
|
self.errno = None
|
2020-09-16 03:55:37 +02:00
|
|
|
self.strerror = str(args[0])
|
2020-09-06 10:51:45 +02:00
|
|
|
else:
|
|
|
|
self.errno = None
|
2020-09-16 03:55:37 +02:00
|
|
|
self.strerror = ""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""Return the error message."""
|
|
|
|
if self.errno is not None:
|
2020-09-16 03:55:37 +02:00
|
|
|
return "[Errno %s] %s" % (self.errno, self.strerror)
|
|
|
|
return self.strerror
|
2020-09-06 10:51:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FirmwareException(BroadlinkException):
|
|
|
|
"""Common base class for all firmware exceptions."""
|
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class AuthenticationError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Authentication error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class AuthorizationError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Authorization error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class CommandNotSupportedError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Command not supported error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class ConnectionClosedError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Connection closed error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class DataValidationError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Data validation error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class DeviceOfflineError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Device offline error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class ReadError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Read error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class SendError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Send error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class SSIDNotFoundError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""SSID not found error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class StorageError(FirmwareException):
|
2020-05-07 07:59:21 +02:00
|
|
|
"""Storage error."""
|
2020-09-06 10:51:45 +02:00
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class WriteError(FirmwareException):
|
|
|
|
"""Write error."""
|
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-09-16 03:55:37 +02:00
|
|
|
class DNSError(SDKException):
|
|
|
|
"""Domain name resolution error."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
class NetworkTimeoutError(SDKException):
|
|
|
|
"""Network timeout error."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownError(BroadlinkException):
|
|
|
|
"""Unknown error."""
|
|
|
|
|
2020-05-07 07:59:21 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-06 10:51:45 +02:00
|
|
|
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"),
|
2020-09-16 03:55:37 +02:00
|
|
|
-4013: (DNSError, "Domain name resolution error"),
|
2020-05-07 07:59:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def exception(error_code):
|
|
|
|
"""Return exception corresponding to an error code."""
|
|
|
|
try:
|
2020-09-06 10:51:45 +02:00
|
|
|
exc, msg = BROADLINK_EXCEPTIONS[error_code]
|
|
|
|
return exc(error_code, msg)
|
2020-05-07 07:59:21 +02:00
|
|
|
except KeyError:
|
2020-09-06 10:51:45 +02:00
|
|
|
return UnknownError(error_code, "Unknown error")
|
2020-05-07 07:59:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_error(error):
|
|
|
|
"""Raise exception if an error occurred."""
|
2020-09-06 10:51:45 +02:00
|
|
|
error_code = struct.unpack("h", error)[0]
|
2020-05-07 07:59:21 +02:00
|
|
|
if error_code:
|
|
|
|
raise exception(error_code)
|