forked from felipediel/python-broadlink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
137 lines (91 loc) · 3.61 KB
/
exceptions.py
File metadata and controls
137 lines (91 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""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 = str(args[1])
elif len(args) == 1:
self.errno = None
self.strerror = str(args[0])
else:
self.errno = None
self.strerror = ""
def __str__(self):
"""Return the error message."""
if self.errno is not None:
return "[Errno %s] %s" % (self.errno, self.strerror)
return self.strerror
class FirmwareException(BroadlinkException):
"""Common base class for all firmware exceptions."""
class AuthenticationError(FirmwareException):
"""Authentication error."""
class AuthorizationError(FirmwareException):
"""Authorization error."""
class CommandNotSupportedError(FirmwareException):
"""Command not supported error."""
class ConnectionClosedError(FirmwareException):
"""Connection closed error."""
class DataValidationError(FirmwareException):
"""Data validation error."""
class DeviceOfflineError(FirmwareException):
"""Device offline error."""
class ReadError(FirmwareException):
"""Read error."""
class SendError(FirmwareException):
"""Send error."""
class SSIDNotFoundError(FirmwareException):
"""SSID not found error."""
class StorageError(FirmwareException):
"""Storage error."""
class WriteError(FirmwareException):
"""Write error."""
class SDKException(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."""
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"),
-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"),
# SDK related errors are generated by this module.
-2040: (DeviceInformationError, "Device information is not intact"),
-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)