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

152 lines
4.3 KiB
Python
Raw Normal View History

"""Exceptions for Broadlink devices."""
2021-03-11 08:33:14 +01:00
import collections
import struct
class BroadlinkException(Exception):
2021-03-11 08:33:14 +01:00
"""Base class common to all Broadlink exceptions."""
def __init__(self, *args, **kwargs):
"""Initialize the exception."""
super().__init__(*args, **kwargs)
2021-03-11 08:33:14 +01:00
if len(args) >= 2:
self.errno = args[0]
2021-03-11 08:33:14 +01:00
self.strerror = ": ".join(str(arg) for arg in args[1:])
elif len(args) == 1:
self.errno = None
2020-09-16 03:55:37 +02:00
self.strerror = str(args[0])
else:
self.errno = None
2020-09-16 03:55:37 +02:00
self.strerror = ""
def __str__(self):
2021-03-11 08:33:14 +01:00
"""Return str(self)."""
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
2021-03-11 08:33:14 +01:00
def __eq__(self, other):
"""Return self==value."""
return type(self) == type(other) and self.args == other.args
def __hash__(self):
"""Return hash(self)."""
return hash((type(self), self.args))
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
2021-03-11 08:33:14 +01:00
def __str__(self):
"""Return str(self)."""
return self.strerror
2021-03-11 08:33:14 +01:00
class AuthenticationError(BroadlinkException):
"""Authentication error."""
2021-03-11 08:33:14 +01:00
class AuthorizationError(BroadlinkException):
"""Authorization error."""
2021-03-11 08:33:14 +01:00
class CommandNotSupportedError(BroadlinkException):
"""Command not supported error."""
2021-03-11 08:33:14 +01:00
class ConnectionClosedError(BroadlinkException):
"""Connection closed error."""
2021-03-11 08:33:14 +01:00
class StructureAbnormalError(BroadlinkException):
"""Structure abnormal error."""
2021-03-11 08:33:14 +01:00
class DeviceOfflineError(BroadlinkException):
"""Device offline error."""
2021-03-11 08:33:14 +01:00
class ReadError(BroadlinkException):
"""Read error."""
2021-03-11 08:33:14 +01:00
class SendError(BroadlinkException):
"""Send error."""
2021-03-11 08:33:14 +01:00
class SSIDNotFoundError(BroadlinkException):
"""SSID not found error."""
2021-03-11 08:33:14 +01:00
class StorageError(BroadlinkException):
"""Storage error."""
2021-03-11 08:33:14 +01:00
class WriteError(BroadlinkException):
"""Write error."""
2021-03-11 08:33:14 +01:00
class NetworkTimeoutError(BroadlinkException):
"""Network timeout error."""
2021-03-11 08:33:14 +01:00
class DataValidationError(BroadlinkException):
"""Data validation error."""
class UnknownError(BroadlinkException):
"""Unknown error."""
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"),
2021-03-11 08:33:14 +01:00
-6: (StructureAbnormalError, "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"),
# SDK related errors are generated by this module.
2021-03-11 08:33:14 +01:00
-2040: (DataValidationError, "Device information is not intact"),
-4000: (NetworkTimeoutError, "Network timeout"),
2021-03-11 08:33:14 +01:00
-4007: (DataValidationError, "Received data packet length 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"),
}
2021-03-11 08:33:14 +01:00
def exception(err_code: int) -> BroadlinkException:
"""Return exception corresponding to an error code."""
try:
2021-03-11 08:33:14 +01:00
exc, msg = BROADLINK_EXCEPTIONS[err_code]
return exc(err_code, msg)
except KeyError:
2021-03-11 08:33:14 +01:00
return UnknownError(err_code, "Unknown error")
2021-03-11 08:33:14 +01:00
def check_error(error: bytes) -> None:
"""Raise exception if an error occurred."""
error_code = struct.unpack("h", error)[0]
if error_code:
raise exception(error_code)