-
-
Notifications
You must be signed in to change notification settings - Fork 565
/
miioprotocol.py
298 lines (252 loc) · 9.7 KB
/
miioprotocol.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""miIO protocol implementation.
This module contains the implementation of routines to send handshakes, send commands
and discover devices (MiIOProtocol).
"""
import binascii
import codecs
import logging
import socket
from datetime import datetime, timedelta, timezone
from pprint import pformat as pf
from typing import Any, Optional
import construct
from .exceptions import (
DeviceError,
DeviceException,
InvalidTokenException,
RecoverableError,
)
from .protocol import Message
_LOGGER = logging.getLogger(__name__)
class MiIOProtocol:
def __init__(
self,
ip: Optional[str] = None,
token: Optional[str] = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = 5,
) -> None:
"""Create a :class:`Device` instance.
:param ip: IP address or a hostname for the device
:param token: Token used for encryption
:param start_id: Running message id sent to the device
:param debug: Wanted debug level
"""
self.ip = ip
self.port = 54321
if token is None:
token = 32 * "0"
self.token = bytes.fromhex(token)
self.debug = debug
self.lazy_discover = lazy_discover
self._timeout = timeout
self.__id = start_id
self._discovered = False
# these come from the device, but we initialize them here to make mypy happy
self._device_ts: datetime = datetime.now(tz=timezone.utc)
self._device_id = b""
def send_handshake(self, *, retry_count=3) -> Message:
"""Send a handshake to the device.
This returns some information, such as device type and serial,
as well as device's timestamp in response.
The handshake must also be done regularly to enable communication
with the device.
:raises DeviceException: if the device could not be discovered after retries.
"""
try:
m = MiIOProtocol.discover(self.ip)
except DeviceException as ex:
if retry_count > 0:
return self.send_handshake(retry_count=retry_count - 1)
raise ex
if m is None:
_LOGGER.debug("Unable to discover a device at address %s", self.ip)
raise DeviceException("Unable to discover the device %s" % self.ip)
header = m.header.value
self._device_id = header.device_id
self._device_ts = header.ts
self._discovered = True
if self.debug > 1:
_LOGGER.debug(m)
_LOGGER.debug(
"Discovered %s with ts: %s, token: %s",
binascii.hexlify(self._device_id).decode(),
self._device_ts,
codecs.encode(m.checksum, "hex"),
)
return m
@staticmethod
def discover(addr: Optional[str] = None, timeout: int = 5) -> Any:
"""Scan for devices in the network. This method is used to discover supported
devices by sending a handshake message to the broadcast address on port 54321.
If the target IP address is given, the handshake will be send as an unicast
packet.
:param str addr: Target IP address
"""
is_broadcast = addr is None
seen_addrs: list[str] = []
if is_broadcast:
addr = "<broadcast>"
is_broadcast = True
_LOGGER.info("Sending discovery to %s with timeout of %ss..", addr, timeout)
# magic, length 32
helobytes = bytes.fromhex(
"21310020ffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.settimeout(timeout)
for _ in range(3):
s.sendto(helobytes, (addr, 54321))
while True:
try:
data, recv_addr = s.recvfrom(1024)
m: Message = Message.parse(data)
_LOGGER.debug("Got a response: %s", m)
if not is_broadcast:
return m
if recv_addr[0] not in seen_addrs:
_LOGGER.info(
" IP %s (ID: %s) - token: %s",
recv_addr[0],
binascii.hexlify(m.header.value.device_id).decode(),
codecs.encode(m.checksum, "hex"),
)
seen_addrs.append(recv_addr[0])
except socket.timeout:
if is_broadcast:
_LOGGER.info("Discovery done")
return # ignore timeouts on discover
except Exception as ex:
_LOGGER.warning("error while reading discover results: %s", ex)
break
def send(
self,
command: str,
parameters: Optional[Any] = None,
retry_count: int = 3,
*,
extra_parameters: Optional[dict] = None,
) -> Any:
"""Build and send the given command. Note that this will implicitly call
:func:`send_handshake` to do a handshake, and will re-try in case of errors
while incrementing the `_id` by 100.
:param str command: Command to send
:param dict parameters: Parameters to send, or an empty list
:param retry_count: How many times to retry in case of failure, how many handshakes to send
:param dict extra_parameters: Extra top-level parameters
:raises DeviceException: if an error has occurred during communication.
"""
if not self.lazy_discover or not self._discovered:
self.send_handshake()
request = self._create_request(command, parameters, extra_parameters)
send_ts = self._device_ts + timedelta(seconds=1)
header = {
"length": 0,
"unknown": 0x00000000,
"device_id": self._device_id,
"ts": send_ts,
}
msg = {"data": {"value": request}, "header": {"value": header}, "checksum": 0}
m = Message.build(msg, token=self.token)
_LOGGER.debug("%s:%s >>: %s", self.ip, self.port, pf(request))
if self.debug > 1:
_LOGGER.debug(
"send (timeout %s): %s",
self._timeout,
Message.parse(m, token=self.token),
)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(self._timeout)
try:
s.sendto(m, (self.ip, self.port))
except OSError as ex:
_LOGGER.error("failed to send msg: %s", ex)
raise DeviceException from ex
try:
data, addr = s.recvfrom(4096)
m = Message.parse(data, token=self.token)
if self.debug > 1:
_LOGGER.debug("recv from %s: %s", addr[0], m)
header = m.header.value
payload = m.data.value
self.__id = payload["id"]
self._device_ts = header["ts"] # type: ignore # ts uses timeadapter
_LOGGER.debug(
"%s:%s (ts: %s, id: %s) << %s",
self.ip,
self.port,
header["ts"],
payload["id"],
pf(payload),
)
if "error" in payload:
self._handle_error(payload["error"])
try:
return payload["result"]
except KeyError:
return payload
except construct.core.ChecksumError as ex:
raise InvalidTokenException(
"Got checksum error which indicates use "
"of an invalid token. "
"Please check your token!"
) from ex
except OSError as ex:
if retry_count > 0:
_LOGGER.debug(
"Retrying with incremented id, retries left: %s", retry_count
)
self.__id += 100
self._discovered = False
return self.send(
command,
parameters,
retry_count - 1,
extra_parameters=extra_parameters,
)
_LOGGER.error("Got error when receiving: %s", ex)
raise DeviceException("No response from the device") from ex
except RecoverableError as ex:
if retry_count > 0:
_LOGGER.debug(
"Retrying to send failed command, retries left: %s", retry_count
)
return self.send(
command,
parameters,
retry_count - 1,
extra_parameters=extra_parameters,
)
_LOGGER.error("Got error when receiving: %s", ex)
raise DeviceException("Unable to recover failed command") from ex
@property
def _id(self) -> int:
"""Increment and return the sequence id."""
self.__id += 1
if self.__id >= 9999:
self.__id = 1
return self.__id
@property
def raw_id(self):
return self.__id
def _handle_error(self, error):
"""Raise exception based on the given error code."""
RECOVERABLE_ERRORS = [-30001, -9999]
if "code" in error and error["code"] in RECOVERABLE_ERRORS:
raise RecoverableError(error)
raise DeviceError(error)
def _create_request(
self, command: str, parameters: Any, extra_parameters: Optional[dict] = None
):
"""Create request payload."""
request = {"id": self._id, "method": command}
if parameters is not None:
request["params"] = parameters
else:
request["params"] = []
if extra_parameters is not None:
request = {**request, **extra_parameters}
return request