broadlink-magic 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- broadlink_magic/__init__.py +231 -0
- broadlink_magic/climate.py +104 -0
- broadlink_magic/const.py +26 -0
- broadlink_magic/curtain.py +64 -0
- broadlink_magic/discovery.py +121 -0
- broadlink_magic/exceptions.py +21 -0
- broadlink_magic/fastcon.py +81 -0
- broadlink_magic/gateway.py +270 -0
- broadlink_magic/import_data.py +353 -0
- broadlink_magic/inbound.py +214 -0
- broadlink_magic/lan.py +298 -0
- broadlink_magic/light.py +52 -0
- broadlink_magic/models.py +422 -0
- broadlink_magic/py.typed +0 -0
- broadlink_magic/remote.py +276 -0
- broadlink_magic/status.py +133 -0
- broadlink_magic/switch.py +11 -0
- broadlink_magic-0.1.0.dist-info/METADATA +77 -0
- broadlink_magic-0.1.0.dist-info/RECORD +22 -0
- broadlink_magic-0.1.0.dist-info/WHEEL +5 -0
- broadlink_magic-0.1.0.dist-info/licenses/LICENSE +21 -0
- broadlink_magic-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"""BroadLink Magic Home local protocol support."""
|
|
2
|
+
|
|
3
|
+
from .climate import (
|
|
4
|
+
AC_FAN_AUTO,
|
|
5
|
+
AC_FAN_HIGH,
|
|
6
|
+
AC_FAN_LOW,
|
|
7
|
+
AC_FAN_MEDIUM,
|
|
8
|
+
AC_MODE_AUTO,
|
|
9
|
+
AC_MODE_COOL,
|
|
10
|
+
AC_MODE_DRY,
|
|
11
|
+
AC_MODE_FAN,
|
|
12
|
+
AC_MODE_HEAT,
|
|
13
|
+
FLOOR_HEATING_MAX_TEMP,
|
|
14
|
+
FLOOR_HEATING_MIN_TEMP,
|
|
15
|
+
FRESH_AIR_FAN_HIGH,
|
|
16
|
+
FRESH_AIR_FAN_LOW,
|
|
17
|
+
FRESH_AIR_FAN_MEDIUM,
|
|
18
|
+
FRESH_AIR_FAN_MEDIUM_HIGH,
|
|
19
|
+
FRESH_AIR_FAN_MEDIUM_LOW,
|
|
20
|
+
FRESH_AIR_MODE_COOL,
|
|
21
|
+
FRESH_AIR_MODE_FRESH,
|
|
22
|
+
FRESH_AIR_MODE_PURIFY,
|
|
23
|
+
FRESH_AIR_MODE_SLEEP,
|
|
24
|
+
FRESH_AIR_MODE_SMART,
|
|
25
|
+
FRESH_AIR_MODE_TURBO,
|
|
26
|
+
encode_ac_attribute,
|
|
27
|
+
encode_floor_heating_attribute,
|
|
28
|
+
encode_fresh_air_attribute,
|
|
29
|
+
)
|
|
30
|
+
from .const import (
|
|
31
|
+
AC_PID,
|
|
32
|
+
GATEWAY_LISTEN_PORT,
|
|
33
|
+
RM_MAX_DEVTYPE,
|
|
34
|
+
RM_MAX_PID,
|
|
35
|
+
STB_PID,
|
|
36
|
+
THREE_IN_ONE_PID,
|
|
37
|
+
TV_PID,
|
|
38
|
+
)
|
|
39
|
+
from .curtain import (
|
|
40
|
+
CURTAIN_CLOSE,
|
|
41
|
+
CURTAIN_OPEN,
|
|
42
|
+
CURTAIN_STOP,
|
|
43
|
+
encode_curtain_backlight,
|
|
44
|
+
encode_curtain_limit,
|
|
45
|
+
encode_curtain_motion,
|
|
46
|
+
encode_curtain_position,
|
|
47
|
+
encode_curtain_switch,
|
|
48
|
+
)
|
|
49
|
+
from .discovery import build_probe_request, discover_rmmax, parse_probe_response
|
|
50
|
+
from .exceptions import (
|
|
51
|
+
BroadLinkMagicError,
|
|
52
|
+
CommandError,
|
|
53
|
+
DiscoveryError,
|
|
54
|
+
ImportDataError,
|
|
55
|
+
ProtocolError,
|
|
56
|
+
)
|
|
57
|
+
from .fastcon import (
|
|
58
|
+
apply_family_token,
|
|
59
|
+
device_control,
|
|
60
|
+
scene_control,
|
|
61
|
+
)
|
|
62
|
+
from .gateway import GatewaySet, GatewayState
|
|
63
|
+
from .import_data import mark_rm_children, parse_import_payload
|
|
64
|
+
from .inbound import (
|
|
65
|
+
GATEWAY_SESSION_TIMEOUT,
|
|
66
|
+
MSG_CONTROL,
|
|
67
|
+
MSG_DISCOVERY,
|
|
68
|
+
MSG_DISCOVERY_RESPONSE,
|
|
69
|
+
MSG_HEARTBEAT,
|
|
70
|
+
MSG_HEARTBEAT_RESPONSE,
|
|
71
|
+
MSG_STATUS_ACK,
|
|
72
|
+
MSG_STATUS_QUERY,
|
|
73
|
+
MSG_STATUS_QUERY_ACK,
|
|
74
|
+
MSG_STATUS_UPLOAD,
|
|
75
|
+
InboundGatewayRegistry,
|
|
76
|
+
gateway_mac_from_handshake,
|
|
77
|
+
pack_message,
|
|
78
|
+
pack_status_query,
|
|
79
|
+
parse_status_upload,
|
|
80
|
+
unpack_header,
|
|
81
|
+
)
|
|
82
|
+
from .lan import (
|
|
83
|
+
LAN_CONTROL_RESPONSE,
|
|
84
|
+
LAN_HEARTBEAT_RESPONSE,
|
|
85
|
+
LanGatewayConnection,
|
|
86
|
+
pack_lan_control,
|
|
87
|
+
pack_lan_heartbeat,
|
|
88
|
+
parse_lan_response,
|
|
89
|
+
unpack_lan_response,
|
|
90
|
+
)
|
|
91
|
+
from .light import (
|
|
92
|
+
encode_light_brightness,
|
|
93
|
+
encode_light_power,
|
|
94
|
+
encode_rgbcw_light,
|
|
95
|
+
)
|
|
96
|
+
from .models import (
|
|
97
|
+
RM_PROFILE_BY_PID,
|
|
98
|
+
DeviceKind,
|
|
99
|
+
DiscoveredRMMax,
|
|
100
|
+
Family,
|
|
101
|
+
ImportedDevice,
|
|
102
|
+
ImportedGroup,
|
|
103
|
+
ImportedScene,
|
|
104
|
+
RMProfile,
|
|
105
|
+
Room,
|
|
106
|
+
VirtualSubToken,
|
|
107
|
+
rm_profile_for_pid,
|
|
108
|
+
)
|
|
109
|
+
from .remote import (
|
|
110
|
+
PROFILE_COMMANDS,
|
|
111
|
+
encode_channel,
|
|
112
|
+
encode_commands,
|
|
113
|
+
encode_key,
|
|
114
|
+
encode_profile_commands,
|
|
115
|
+
encode_step,
|
|
116
|
+
supports_profile_command,
|
|
117
|
+
)
|
|
118
|
+
from .status import (
|
|
119
|
+
FastConReport,
|
|
120
|
+
parse_device_status,
|
|
121
|
+
parse_device_upload,
|
|
122
|
+
parse_fastcon_report,
|
|
123
|
+
)
|
|
124
|
+
from .switch import encode_switch_channel
|
|
125
|
+
|
|
126
|
+
__all__ = [
|
|
127
|
+
"AC_FAN_AUTO",
|
|
128
|
+
"AC_FAN_HIGH",
|
|
129
|
+
"AC_FAN_LOW",
|
|
130
|
+
"AC_FAN_MEDIUM",
|
|
131
|
+
"AC_MODE_AUTO",
|
|
132
|
+
"AC_MODE_COOL",
|
|
133
|
+
"AC_MODE_DRY",
|
|
134
|
+
"AC_MODE_FAN",
|
|
135
|
+
"AC_MODE_HEAT",
|
|
136
|
+
"AC_PID",
|
|
137
|
+
"CURTAIN_CLOSE",
|
|
138
|
+
"CURTAIN_OPEN",
|
|
139
|
+
"CURTAIN_STOP",
|
|
140
|
+
"FLOOR_HEATING_MAX_TEMP",
|
|
141
|
+
"FLOOR_HEATING_MIN_TEMP",
|
|
142
|
+
"FRESH_AIR_FAN_HIGH",
|
|
143
|
+
"FRESH_AIR_FAN_LOW",
|
|
144
|
+
"FRESH_AIR_FAN_MEDIUM",
|
|
145
|
+
"FRESH_AIR_FAN_MEDIUM_HIGH",
|
|
146
|
+
"FRESH_AIR_FAN_MEDIUM_LOW",
|
|
147
|
+
"FRESH_AIR_MODE_COOL",
|
|
148
|
+
"FRESH_AIR_MODE_FRESH",
|
|
149
|
+
"FRESH_AIR_MODE_PURIFY",
|
|
150
|
+
"FRESH_AIR_MODE_SLEEP",
|
|
151
|
+
"FRESH_AIR_MODE_SMART",
|
|
152
|
+
"FRESH_AIR_MODE_TURBO",
|
|
153
|
+
"GATEWAY_LISTEN_PORT",
|
|
154
|
+
"GATEWAY_SESSION_TIMEOUT",
|
|
155
|
+
"LAN_CONTROL_RESPONSE",
|
|
156
|
+
"LAN_HEARTBEAT_RESPONSE",
|
|
157
|
+
"MSG_CONTROL",
|
|
158
|
+
"MSG_DISCOVERY",
|
|
159
|
+
"MSG_DISCOVERY_RESPONSE",
|
|
160
|
+
"MSG_HEARTBEAT",
|
|
161
|
+
"MSG_HEARTBEAT_RESPONSE",
|
|
162
|
+
"MSG_STATUS_ACK",
|
|
163
|
+
"MSG_STATUS_QUERY",
|
|
164
|
+
"MSG_STATUS_QUERY_ACK",
|
|
165
|
+
"MSG_STATUS_UPLOAD",
|
|
166
|
+
"PROFILE_COMMANDS",
|
|
167
|
+
"RM_MAX_DEVTYPE",
|
|
168
|
+
"RM_MAX_PID",
|
|
169
|
+
"RM_PROFILE_BY_PID",
|
|
170
|
+
"STB_PID",
|
|
171
|
+
"THREE_IN_ONE_PID",
|
|
172
|
+
"TV_PID",
|
|
173
|
+
"BroadLinkMagicError",
|
|
174
|
+
"CommandError",
|
|
175
|
+
"DeviceKind",
|
|
176
|
+
"DiscoveredRMMax",
|
|
177
|
+
"DiscoveryError",
|
|
178
|
+
"Family",
|
|
179
|
+
"FastConReport",
|
|
180
|
+
"GatewaySet",
|
|
181
|
+
"GatewayState",
|
|
182
|
+
"ImportDataError",
|
|
183
|
+
"ImportedDevice",
|
|
184
|
+
"ImportedGroup",
|
|
185
|
+
"ImportedScene",
|
|
186
|
+
"InboundGatewayRegistry",
|
|
187
|
+
"LanGatewayConnection",
|
|
188
|
+
"ProtocolError",
|
|
189
|
+
"RMProfile",
|
|
190
|
+
"Room",
|
|
191
|
+
"VirtualSubToken",
|
|
192
|
+
"apply_family_token",
|
|
193
|
+
"build_probe_request",
|
|
194
|
+
"device_control",
|
|
195
|
+
"discover_rmmax",
|
|
196
|
+
"encode_ac_attribute",
|
|
197
|
+
"encode_channel",
|
|
198
|
+
"encode_commands",
|
|
199
|
+
"encode_curtain_backlight",
|
|
200
|
+
"encode_curtain_limit",
|
|
201
|
+
"encode_curtain_motion",
|
|
202
|
+
"encode_curtain_position",
|
|
203
|
+
"encode_curtain_switch",
|
|
204
|
+
"encode_floor_heating_attribute",
|
|
205
|
+
"encode_fresh_air_attribute",
|
|
206
|
+
"encode_key",
|
|
207
|
+
"encode_light_brightness",
|
|
208
|
+
"encode_light_power",
|
|
209
|
+
"encode_profile_commands",
|
|
210
|
+
"encode_rgbcw_light",
|
|
211
|
+
"encode_step",
|
|
212
|
+
"encode_switch_channel",
|
|
213
|
+
"gateway_mac_from_handshake",
|
|
214
|
+
"mark_rm_children",
|
|
215
|
+
"pack_lan_control",
|
|
216
|
+
"pack_lan_heartbeat",
|
|
217
|
+
"pack_message",
|
|
218
|
+
"pack_status_query",
|
|
219
|
+
"parse_device_status",
|
|
220
|
+
"parse_device_upload",
|
|
221
|
+
"parse_fastcon_report",
|
|
222
|
+
"parse_import_payload",
|
|
223
|
+
"parse_lan_response",
|
|
224
|
+
"parse_probe_response",
|
|
225
|
+
"parse_status_upload",
|
|
226
|
+
"rm_profile_for_pid",
|
|
227
|
+
"scene_control",
|
|
228
|
+
"supports_profile_command",
|
|
229
|
+
"unpack_header",
|
|
230
|
+
"unpack_lan_response",
|
|
231
|
+
]
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"""RM Max air-conditioner command encoding."""
|
|
2
|
+
|
|
3
|
+
from .exceptions import CommandError
|
|
4
|
+
|
|
5
|
+
AC_MODE_AUTO = 0
|
|
6
|
+
AC_MODE_HEAT = 1
|
|
7
|
+
AC_MODE_COOL = 2
|
|
8
|
+
AC_MODE_FAN = 4
|
|
9
|
+
AC_MODE_DRY = 8
|
|
10
|
+
|
|
11
|
+
AC_FAN_AUTO = 0
|
|
12
|
+
AC_FAN_LOW = 1
|
|
13
|
+
AC_FAN_MEDIUM = 2
|
|
14
|
+
AC_FAN_HIGH = 3
|
|
15
|
+
|
|
16
|
+
FLOOR_HEATING_MIN_TEMP = 16
|
|
17
|
+
FLOOR_HEATING_MAX_TEMP = 45
|
|
18
|
+
|
|
19
|
+
FRESH_AIR_MODE_FRESH = 1
|
|
20
|
+
FRESH_AIR_MODE_COOL = 2
|
|
21
|
+
FRESH_AIR_MODE_SMART = 3
|
|
22
|
+
FRESH_AIR_MODE_TURBO = 4
|
|
23
|
+
FRESH_AIR_MODE_SLEEP = 5
|
|
24
|
+
FRESH_AIR_MODE_PURIFY = 6
|
|
25
|
+
|
|
26
|
+
FRESH_AIR_FAN_LOW = 1
|
|
27
|
+
FRESH_AIR_FAN_MEDIUM = 2
|
|
28
|
+
FRESH_AIR_FAN_HIGH = 3
|
|
29
|
+
FRESH_AIR_FAN_MEDIUM_LOW = 4
|
|
30
|
+
FRESH_AIR_FAN_MEDIUM_HIGH = 5
|
|
31
|
+
|
|
32
|
+
# Three-in-one panels share function code 0x00 for all power switches; bits 1-2
|
|
33
|
+
# select the floor-heating or fresh-air load and bit 0 carries the state.
|
|
34
|
+
_FLOOR_HEATING_POWER = (0x02, 0x03)
|
|
35
|
+
_FRESH_AIR_POWER = (0x04, 0x05)
|
|
36
|
+
|
|
37
|
+
_ATTRIBUTE_INDEX = {
|
|
38
|
+
"power": 0,
|
|
39
|
+
"mode": 1,
|
|
40
|
+
"temperature": 2,
|
|
41
|
+
"fan": 3,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def encode_ac_attribute(attribute: str, value: int) -> bytes:
|
|
46
|
+
"""Encode one AC attribute as the three-byte RM Max TLV."""
|
|
47
|
+
try:
|
|
48
|
+
index = _ATTRIBUTE_INDEX[attribute]
|
|
49
|
+
except KeyError as err:
|
|
50
|
+
raise CommandError(f"unsupported AC attribute: {attribute}") from err
|
|
51
|
+
|
|
52
|
+
if attribute == "power" and value not in {0, 1}:
|
|
53
|
+
raise CommandError("AC power must be 0 or 1")
|
|
54
|
+
if attribute == "mode" and value not in {
|
|
55
|
+
AC_MODE_AUTO,
|
|
56
|
+
AC_MODE_HEAT,
|
|
57
|
+
AC_MODE_COOL,
|
|
58
|
+
AC_MODE_FAN,
|
|
59
|
+
AC_MODE_DRY,
|
|
60
|
+
}:
|
|
61
|
+
raise CommandError("unsupported AC mode")
|
|
62
|
+
if attribute == "temperature" and not 16 <= value <= 30:
|
|
63
|
+
raise CommandError("AC temperature must be between 16 and 30")
|
|
64
|
+
if attribute == "fan" and value not in {
|
|
65
|
+
AC_FAN_AUTO,
|
|
66
|
+
AC_FAN_LOW,
|
|
67
|
+
AC_FAN_MEDIUM,
|
|
68
|
+
AC_FAN_HIGH,
|
|
69
|
+
}:
|
|
70
|
+
raise CommandError("unsupported AC fan mode")
|
|
71
|
+
return bytes((index, 1, value))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def encode_floor_heating_attribute(attribute: str, value: int) -> bytes:
|
|
75
|
+
"""Encode one three-in-one floor-heating attribute."""
|
|
76
|
+
if attribute == "power":
|
|
77
|
+
if value not in {0, 1}:
|
|
78
|
+
raise CommandError("floor heating power must be 0 or 1")
|
|
79
|
+
return bytes((0x00, 1, _FLOOR_HEATING_POWER[value]))
|
|
80
|
+
if attribute == "temperature":
|
|
81
|
+
if not FLOOR_HEATING_MIN_TEMP <= value <= FLOOR_HEATING_MAX_TEMP:
|
|
82
|
+
raise CommandError(
|
|
83
|
+
f"floor heating temperature must be between "
|
|
84
|
+
f"{FLOOR_HEATING_MIN_TEMP} and {FLOOR_HEATING_MAX_TEMP}"
|
|
85
|
+
)
|
|
86
|
+
return bytes((0x04, 1, value))
|
|
87
|
+
raise CommandError(f"unsupported floor heating attribute: {attribute}")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def encode_fresh_air_attribute(attribute: str, value: int) -> bytes:
|
|
91
|
+
"""Encode one three-in-one fresh-air attribute."""
|
|
92
|
+
if attribute == "power":
|
|
93
|
+
if value not in {0, 1}:
|
|
94
|
+
raise CommandError("fresh air power must be 0 or 1")
|
|
95
|
+
return bytes((0x00, 1, _FRESH_AIR_POWER[value]))
|
|
96
|
+
if attribute == "mode":
|
|
97
|
+
if not FRESH_AIR_MODE_FRESH <= value <= FRESH_AIR_MODE_PURIFY:
|
|
98
|
+
raise CommandError("unsupported fresh air mode")
|
|
99
|
+
return bytes((0x01, 1, value << 4))
|
|
100
|
+
if attribute == "fan":
|
|
101
|
+
if not FRESH_AIR_FAN_LOW <= value <= FRESH_AIR_FAN_MEDIUM_HIGH:
|
|
102
|
+
raise CommandError("unsupported fresh air fan speed")
|
|
103
|
+
return bytes((0x03, 1, value << 4))
|
|
104
|
+
raise CommandError(f"unsupported fresh air attribute: {attribute}")
|
broadlink_magic/const.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Constants for the BroadLink Magic SDK."""
|
|
2
|
+
|
|
3
|
+
from typing import Final
|
|
4
|
+
|
|
5
|
+
RM_MAX_DEVTYPE: Final = 0xAF8B
|
|
6
|
+
RM_MAX_PID: Final = "0000000000000000000000008baf0000"
|
|
7
|
+
AC_DEVTYPE: Final = 43919
|
|
8
|
+
AC_PID: Final = "0000000000000000000000008fab0000"
|
|
9
|
+
THREE_IN_ONE_PID: Final = "000000000000000000000000bbaf0000"
|
|
10
|
+
TV_DEVTYPE: Final = 44843
|
|
11
|
+
TV_PID: Final = "0000000000000000000000002baf0000"
|
|
12
|
+
STB_DEVTYPE: Final = 44844
|
|
13
|
+
STB_PID: Final = "0000000000000000000000002caf0000"
|
|
14
|
+
SWITCH_PROTOCOL_DEVTYPES: Final = frozenset({44164, 44165, 44166, 44167, 44180, 44181})
|
|
15
|
+
|
|
16
|
+
DEFAULT_PORT: Final = 80
|
|
17
|
+
GATEWAY_LISTEN_PORT: Final = 12346
|
|
18
|
+
DISCOVERY_ADDRESS: Final = ("255.255.255.255", DEFAULT_PORT)
|
|
19
|
+
NETWORK_MAGIC: Final = 0x55AAA55A55AAA55A
|
|
20
|
+
NETWORK_HEADER_LENGTH: Final = 48
|
|
21
|
+
PROBE_BODY_LENGTH: Final = 16
|
|
22
|
+
LAN_PROBE_REQUEST: Final = 6
|
|
23
|
+
LAN_PROBE_RESPONSE: Final = 7
|
|
24
|
+
|
|
25
|
+
IMPORT_PATH: Final = "/industry/v1/familyinfo/import"
|
|
26
|
+
IMPORT_SESSION_TTL: Final = 180
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""EasyIndustry and kvgo-compatible curtain control payloads."""
|
|
2
|
+
|
|
3
|
+
from .exceptions import ProtocolError
|
|
4
|
+
|
|
5
|
+
CURTAIN_OPEN = 1
|
|
6
|
+
CURTAIN_CLOSE = 2
|
|
7
|
+
CURTAIN_STOP = 3
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _profile() -> bytearray:
|
|
11
|
+
payload = bytearray(6)
|
|
12
|
+
payload[1] = 0xFF
|
|
13
|
+
payload[3] = 0xFF
|
|
14
|
+
return payload
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def encode_curtain_motion(action: int, *, motor: int = 1) -> bytes:
|
|
18
|
+
"""Encode open, close, or stop for one curtain motor."""
|
|
19
|
+
if action not in {CURTAIN_OPEN, CURTAIN_CLOSE, CURTAIN_STOP}:
|
|
20
|
+
raise ProtocolError("curtain action must be open, close, or stop")
|
|
21
|
+
if motor not in {1, 2}:
|
|
22
|
+
raise ProtocolError("curtain motor must be 1 or 2")
|
|
23
|
+
payload = _profile()
|
|
24
|
+
if motor == 1:
|
|
25
|
+
payload[0] = action
|
|
26
|
+
else:
|
|
27
|
+
payload[5] = action << 1
|
|
28
|
+
return bytes(payload)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def encode_curtain_position(position: int, *, motor: int = 1) -> bytes:
|
|
32
|
+
"""Encode a 0..100 target position for one curtain motor."""
|
|
33
|
+
if not 0 <= position <= 100:
|
|
34
|
+
raise ProtocolError("curtain position must be between 0 and 100")
|
|
35
|
+
if motor not in {1, 2}:
|
|
36
|
+
raise ProtocolError("curtain motor must be 1 or 2")
|
|
37
|
+
payload = _profile()
|
|
38
|
+
if motor == 1:
|
|
39
|
+
payload[1] = position
|
|
40
|
+
else:
|
|
41
|
+
payload[3] = position
|
|
42
|
+
return bytes(payload)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def encode_curtain_limit(value: int) -> bytes:
|
|
46
|
+
"""Encode the kvgo curtain_limit profile."""
|
|
47
|
+
if not 0 <= value <= 0x0F:
|
|
48
|
+
raise ProtocolError("curtain limit must fit in four bits")
|
|
49
|
+
payload = _profile()
|
|
50
|
+
payload[4] = value
|
|
51
|
+
return bytes(payload)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def encode_curtain_backlight(on: bool) -> bytes:
|
|
55
|
+
"""Encode the kvgo backlight command passed to device_control."""
|
|
56
|
+
return bytes((0x80 if on else 0,))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def encode_curtain_switch(channel: int, on: bool) -> bytes:
|
|
60
|
+
"""Encode the pwrN compatibility path used by curtain panels."""
|
|
61
|
+
if not 1 <= channel <= 8:
|
|
62
|
+
raise ProtocolError("curtain switch channel must be between 1 and 8")
|
|
63
|
+
mask = 1 << (channel - 1)
|
|
64
|
+
return bytes((mask, mask if on else 0))
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""RM Max local network discovery."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import socket
|
|
7
|
+
import struct
|
|
8
|
+
import time
|
|
9
|
+
|
|
10
|
+
from .const import (
|
|
11
|
+
DISCOVERY_ADDRESS,
|
|
12
|
+
LAN_PROBE_REQUEST,
|
|
13
|
+
LAN_PROBE_RESPONSE,
|
|
14
|
+
NETWORK_HEADER_LENGTH,
|
|
15
|
+
NETWORK_MAGIC,
|
|
16
|
+
PROBE_BODY_LENGTH,
|
|
17
|
+
RM_MAX_DEVTYPE,
|
|
18
|
+
)
|
|
19
|
+
from .models import DiscoveredRMMax
|
|
20
|
+
|
|
21
|
+
_CHECKSUM_OFFSET = 32
|
|
22
|
+
_DEVTYPE_OFFSET = 36
|
|
23
|
+
_MSGTYPE_OFFSET = 38
|
|
24
|
+
_SUBINDEX_OFFSET = 40
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def checksum(data: bytes | bytearray) -> int:
|
|
28
|
+
"""Return the BroadLink LAN checksum."""
|
|
29
|
+
return (0xBEAF + sum(data)) & 0xFFFF
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def build_probe_request(subindex: int | None = None) -> bytes:
|
|
33
|
+
"""Build a BroadLink LAN probe request."""
|
|
34
|
+
packet = bytearray(NETWORK_HEADER_LENGTH)
|
|
35
|
+
struct.pack_into("<Q", packet, 0, NETWORK_MAGIC)
|
|
36
|
+
struct.pack_into("<H", packet, _MSGTYPE_OFFSET, LAN_PROBE_REQUEST)
|
|
37
|
+
if subindex is None:
|
|
38
|
+
subindex = int(time.time() * 1000) | 0x8000
|
|
39
|
+
struct.pack_into("<H", packet, _SUBINDEX_OFFSET, subindex & 0xFFFF)
|
|
40
|
+
struct.pack_into("<H", packet, _CHECKSUM_OFFSET, checksum(packet))
|
|
41
|
+
return bytes(packet)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def parse_probe_response(data: bytes, host: str) -> DiscoveredRMMax | None:
|
|
45
|
+
"""Parse an RM Max probe response."""
|
|
46
|
+
if len(data) < NETWORK_HEADER_LENGTH + PROBE_BODY_LENGTH:
|
|
47
|
+
return None
|
|
48
|
+
if struct.unpack_from("<Q", data, 0)[0] != NETWORK_MAGIC:
|
|
49
|
+
return None
|
|
50
|
+
if struct.unpack_from("<H", data, _MSGTYPE_OFFSET)[0] != LAN_PROBE_RESPONSE:
|
|
51
|
+
return None
|
|
52
|
+
|
|
53
|
+
body_offset = NETWORK_HEADER_LENGTH
|
|
54
|
+
devtype = struct.unpack_from("<H", data, body_offset + 4)[0]
|
|
55
|
+
if devtype != RM_MAX_DEVTYPE:
|
|
56
|
+
return None
|
|
57
|
+
did = data[body_offset + 10 : body_offset + 16]
|
|
58
|
+
mac = bytes(reversed(did)).hex()
|
|
59
|
+
|
|
60
|
+
name = ""
|
|
61
|
+
locked = False
|
|
62
|
+
extension_offset = body_offset + PROBE_BODY_LENGTH
|
|
63
|
+
if len(data) >= extension_offset + 64:
|
|
64
|
+
name = (
|
|
65
|
+
data[extension_offset : extension_offset + 62]
|
|
66
|
+
.split(b"\0", 1)[0]
|
|
67
|
+
.decode(errors="replace")
|
|
68
|
+
)
|
|
69
|
+
locked = bool(data[extension_offset + 63])
|
|
70
|
+
return DiscoveredRMMax(
|
|
71
|
+
host=host,
|
|
72
|
+
mac=mac,
|
|
73
|
+
devtype=devtype,
|
|
74
|
+
name=name,
|
|
75
|
+
locked=locked,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _discover_blocking(
|
|
80
|
+
timeout: float,
|
|
81
|
+
local_address: str,
|
|
82
|
+
broadcast_address: tuple[str, int],
|
|
83
|
+
stop_on_first: bool,
|
|
84
|
+
) -> list[DiscoveredRMMax]:
|
|
85
|
+
devices: dict[str, DiscoveredRMMax] = {}
|
|
86
|
+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
|
87
|
+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
88
|
+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
89
|
+
sock.bind((local_address, 0))
|
|
90
|
+
sock.settimeout(min(timeout, 0.25))
|
|
91
|
+
sock.sendto(build_probe_request(), broadcast_address)
|
|
92
|
+
deadline = time.monotonic() + timeout
|
|
93
|
+
while time.monotonic() < deadline:
|
|
94
|
+
try:
|
|
95
|
+
data, addr = sock.recvfrom(1460)
|
|
96
|
+
except TimeoutError:
|
|
97
|
+
continue
|
|
98
|
+
if device := parse_probe_response(data, addr[0]):
|
|
99
|
+
devices[device.mac] = device
|
|
100
|
+
if stop_on_first:
|
|
101
|
+
break
|
|
102
|
+
return list(devices.values())
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
async def discover_rmmax(
|
|
106
|
+
timeout: float = 3.0,
|
|
107
|
+
*,
|
|
108
|
+
local_address: str = "0.0.0.0",
|
|
109
|
+
broadcast_address: tuple[str, int] = DISCOVERY_ADDRESS,
|
|
110
|
+
stop_on_first: bool = False,
|
|
111
|
+
) -> list[DiscoveredRMMax]:
|
|
112
|
+
"""Discover all RM Max gateways visible on a local interface."""
|
|
113
|
+
if timeout <= 0:
|
|
114
|
+
raise ValueError("timeout must be positive")
|
|
115
|
+
return await asyncio.to_thread(
|
|
116
|
+
_discover_blocking,
|
|
117
|
+
timeout,
|
|
118
|
+
local_address,
|
|
119
|
+
broadcast_address,
|
|
120
|
+
stop_on_first,
|
|
121
|
+
)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Exceptions raised by the BroadLink Magic SDK."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class BroadLinkMagicError(Exception):
|
|
5
|
+
"""Base BroadLink Magic error."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ImportDataError(BroadLinkMagicError):
|
|
9
|
+
"""Invalid Magic Home family import data."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class DiscoveryError(BroadLinkMagicError):
|
|
13
|
+
"""RM Max discovery failed."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ProtocolError(BroadLinkMagicError):
|
|
17
|
+
"""Invalid or unsupported protocol data."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class CommandError(BroadLinkMagicError):
|
|
21
|
+
"""Invalid remote command."""
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""FastCon command framing used by RM Max gateways."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .exceptions import ProtocolError
|
|
6
|
+
|
|
7
|
+
FASTCON_FRAME_LENGTH = 22
|
|
8
|
+
FASTCON_TYPE_CONTROL_REQUEST = 5
|
|
9
|
+
FASTCON_TLV_TYPE_CONTROL = 2
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def fastcon_checksum(frame: bytes | bytearray) -> int:
|
|
13
|
+
"""Return the FastCon checksum, excluding the checksum byte itself."""
|
|
14
|
+
return sum(value for index, value in enumerate(frame) if index != 3) & 0xFF
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def device_control(
|
|
18
|
+
short_addr: int,
|
|
19
|
+
command: bytes,
|
|
20
|
+
*,
|
|
21
|
+
seq: int,
|
|
22
|
+
source_addr: int,
|
|
23
|
+
) -> bytes:
|
|
24
|
+
"""Build a 22-byte FastCon device-control frame."""
|
|
25
|
+
if not 0 <= short_addr <= 0xFFFF:
|
|
26
|
+
raise ProtocolError("short address must fit in uint16")
|
|
27
|
+
if not command or len(command) > 14:
|
|
28
|
+
raise ProtocolError("command length must be between 1 and 14 bytes")
|
|
29
|
+
|
|
30
|
+
frame = bytearray(FASTCON_FRAME_LENGTH)
|
|
31
|
+
high_addr = (short_addr >> 8) & 0x0F
|
|
32
|
+
frame[0] = 0x80 | ((FASTCON_TYPE_CONTROL_REQUEST & 0x07) << 4) | high_addr
|
|
33
|
+
frame[1] = seq & 0xFF
|
|
34
|
+
frame[2] = source_addr & 0xFF
|
|
35
|
+
frame[4] = FASTCON_TLV_TYPE_CONTROL | (((len(command) + 1) & 0x0F) << 4)
|
|
36
|
+
frame[5] = short_addr & 0xFF
|
|
37
|
+
frame[6 : 6 + len(command)] = command
|
|
38
|
+
frame[3] = fastcon_checksum(frame)
|
|
39
|
+
return bytes(frame)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def scene_control(
|
|
43
|
+
scene_id: int,
|
|
44
|
+
*,
|
|
45
|
+
seq: int,
|
|
46
|
+
source_addr: int,
|
|
47
|
+
) -> bytes:
|
|
48
|
+
"""Build a 22-byte FastCon scene-control frame."""
|
|
49
|
+
if not 0 <= scene_id <= 0xFFFFFF:
|
|
50
|
+
raise ProtocolError("scene ID must fit in 24 bits")
|
|
51
|
+
frame = bytearray(FASTCON_FRAME_LENGTH)
|
|
52
|
+
frame[0] = 0x80 | ((FASTCON_TYPE_CONTROL_REQUEST & 0x07) << 4)
|
|
53
|
+
frame[1] = seq & 0xFF
|
|
54
|
+
frame[2] = source_addr & 0xFF
|
|
55
|
+
frame[4] = 0x3B
|
|
56
|
+
frame[5] = (scene_id >> 16) & 0xFF
|
|
57
|
+
frame[6] = 0
|
|
58
|
+
frame[7] = scene_id & 0xFF
|
|
59
|
+
frame[8] = (scene_id >> 8) & 0xFF
|
|
60
|
+
frame[3] = fastcon_checksum(frame)
|
|
61
|
+
return bytes(frame)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def apply_family_token(frame: bytes, bletoken: str) -> bytes:
|
|
65
|
+
"""Apply the family token source address and recalculate the checksum.
|
|
66
|
+
|
|
67
|
+
This matches ``fastcon_passthough_pkt``: the final token byte replaces
|
|
68
|
+
packet byte 2 and packet byte 3 is recalculated over all other bytes.
|
|
69
|
+
"""
|
|
70
|
+
try:
|
|
71
|
+
token = bytes.fromhex(bletoken.strip())
|
|
72
|
+
except ValueError as err:
|
|
73
|
+
raise ProtocolError("bletoken must be hexadecimal") from err
|
|
74
|
+
if not token:
|
|
75
|
+
raise ProtocolError("bletoken must not be empty")
|
|
76
|
+
if len(frame) <= 3:
|
|
77
|
+
return frame
|
|
78
|
+
output = bytearray(frame)
|
|
79
|
+
output[2] = token[-1]
|
|
80
|
+
output[3] = fastcon_checksum(output)
|
|
81
|
+
return bytes(output)
|