node-miio 0.7.0 → 0.8.0

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.
Files changed (77) hide show
  1. package/README.md +4 -0
  2. package/dist/cli/commands/roborock/call.d.ts +10 -0
  3. package/dist/cli/commands/roborock/call.js +34 -0
  4. package/dist/cli/commands/roborock/call.js.map +1 -0
  5. package/dist/cli/commands/roborock/devices.d.ts +8 -0
  6. package/dist/cli/commands/roborock/devices.js +58 -0
  7. package/dist/cli/commands/roborock/devices.js.map +1 -0
  8. package/dist/cli/commands/roborock/login.d.ts +7 -0
  9. package/dist/cli/commands/roborock/login.js +34 -0
  10. package/dist/cli/commands/roborock/login.js.map +1 -0
  11. package/dist/cli/commands/roborock/logout.d.ts +4 -0
  12. package/dist/cli/commands/roborock/logout.js +14 -0
  13. package/dist/cli/commands/roborock/logout.js.map +1 -0
  14. package/dist/cli/commands/roborock/map.d.ts +10 -0
  15. package/dist/cli/commands/roborock/map.js +74 -0
  16. package/dist/cli/commands/roborock/map.js.map +1 -0
  17. package/dist/cli/commands/roborock/robot.d.ts +30 -0
  18. package/dist/cli/commands/roborock/robot.js +61 -0
  19. package/dist/cli/commands/roborock/robot.js.map +1 -0
  20. package/dist/cli/commands/roborock/rooms.d.ts +9 -0
  21. package/dist/cli/commands/roborock/rooms.js +40 -0
  22. package/dist/cli/commands/roborock/rooms.js.map +1 -0
  23. package/dist/cli/commands/roborock.d.ts +5 -0
  24. package/dist/cli/commands/roborock.js +55 -0
  25. package/dist/cli/commands/roborock.js.map +1 -0
  26. package/dist/cli/index.js +2 -0
  27. package/dist/cli/index.js.map +1 -1
  28. package/dist/lib/device_info.d.ts +6 -0
  29. package/dist/lib/devices/vacuums/vacuum.d.ts +3 -3
  30. package/dist/lib/devices/vacuums/vacuum.js +13 -3
  31. package/dist/lib/devices/vacuums/vacuum.js.map +1 -1
  32. package/dist/lib/devices/vacuums/vacuum_modes.d.ts +2 -0
  33. package/dist/lib/devices/vacuums/vacuum_modes.js +15 -0
  34. package/dist/lib/devices/vacuums/vacuum_modes.js.map +1 -1
  35. package/dist/lib/index.d.ts +1 -0
  36. package/dist/lib/index.js +1 -0
  37. package/dist/lib/index.js.map +1 -1
  38. package/dist/lib/logger.js +1 -1
  39. package/dist/lib/logger.js.map +1 -1
  40. package/dist/lib/management.d.ts +2 -2
  41. package/dist/lib/management.js.map +1 -1
  42. package/dist/lib/roborock/device.d.ts +118 -0
  43. package/dist/lib/roborock/device.js +273 -0
  44. package/dist/lib/roborock/device.js.map +1 -0
  45. package/dist/lib/roborock/errors.d.ts +18 -0
  46. package/dist/lib/roborock/errors.js +37 -0
  47. package/dist/lib/roborock/errors.js.map +1 -0
  48. package/dist/lib/roborock/hawk.d.ts +39 -0
  49. package/dist/lib/roborock/hawk.js +48 -0
  50. package/dist/lib/roborock/hawk.js.map +1 -0
  51. package/dist/lib/roborock/index.d.ts +9 -0
  52. package/dist/lib/roborock/index.js +36 -0
  53. package/dist/lib/roborock/index.js.map +1 -0
  54. package/dist/lib/roborock/local_connection.d.ts +72 -0
  55. package/dist/lib/roborock/local_connection.js +232 -0
  56. package/dist/lib/roborock/local_connection.js.map +1 -0
  57. package/dist/lib/roborock/map_response.d.ts +26 -0
  58. package/dist/lib/roborock/map_response.js +104 -0
  59. package/dist/lib/roborock/map_response.js.map +1 -0
  60. package/dist/lib/roborock/message.d.ts +110 -0
  61. package/dist/lib/roborock/message.js +313 -0
  62. package/dist/lib/roborock/message.js.map +1 -0
  63. package/dist/lib/roborock/mqtt.d.ts +119 -0
  64. package/dist/lib/roborock/mqtt.js +305 -0
  65. package/dist/lib/roborock/mqtt.js.map +1 -0
  66. package/dist/lib/roborock/rooms.d.ts +17 -0
  67. package/dist/lib/roborock/rooms.js +23 -0
  68. package/dist/lib/roborock/rooms.js.map +1 -0
  69. package/dist/lib/roborock/session_store.d.ts +21 -0
  70. package/dist/lib/roborock/session_store.js +52 -0
  71. package/dist/lib/roborock/session_store.js.map +1 -0
  72. package/dist/lib/roborock/web_api.d.ts +142 -0
  73. package/dist/lib/roborock/web_api.js +334 -0
  74. package/dist/lib/roborock/web_api.js.map +1 -0
  75. package/docs/roborock.md +91 -0
  76. package/package.json +2 -1
  77. package/snapshots/vacuum-specs.json.gz +0 -0
@@ -0,0 +1,334 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RoborockWebApi = exports.RoborockLogin = exports.ROBOROCK_BASE_URLS = void 0;
7
+ exports.toDevices = toDevices;
8
+ /**
9
+ * The Roborock account API: what the Roborock app uses to log in and list the robots of the account.
10
+ *
11
+ * Robots paired with the Roborock app don't use the Xiaomi Cloud or miIO tokens. Instead, the account's home data
12
+ * gives each robot's `duid` and `localKey`, which encrypt the local (TCP 58867) and MQTT connections.
13
+ *
14
+ * The endpoints and their parameters follow python-roborock (Apache-2.0, https://github.com/Python-roborock/python-roborock),
15
+ * `roborock/web_api/`. No code is copied from it.
16
+ */
17
+ const node_crypto_1 = __importDefault(require("node:crypto"));
18
+ const logger_1 = require("../logger");
19
+ const errors_1 = require("./errors");
20
+ const hawk_1 = require("./hawk");
21
+ const debug = (0, logger_1.createDebug)("miio:roborock");
22
+ /** The regional servers that know where an account lives (`getUrlByEmail`) */
23
+ exports.ROBOROCK_BASE_URLS = [
24
+ "https://usiot.roborock.com",
25
+ "https://euiot.roborock.com",
26
+ "https://cniot.roborock.com",
27
+ "https://ruiot.roborock.com",
28
+ ];
29
+ class Requester {
30
+ options;
31
+ constructor(options) {
32
+ this.options = options;
33
+ }
34
+ async json(url, { method = "POST", query, form, headers = {} } = {}) {
35
+ const target = new URL(url);
36
+ for (const [key, value] of Object.entries(query ?? {})) {
37
+ target.searchParams.set(key, value);
38
+ }
39
+ const fetchImpl = this.options.fetch ?? fetch;
40
+ const response = await fetchImpl(target.toString(), {
41
+ method,
42
+ headers: form
43
+ ? { ...headers, "Content-Type": "application/x-www-form-urlencoded" }
44
+ : headers,
45
+ body: form ? new URLSearchParams(form).toString() : undefined,
46
+ signal: AbortSignal.timeout(this.options.timeoutMs ?? 15_000),
47
+ });
48
+ const text = await response.text();
49
+ try {
50
+ return JSON.parse(text);
51
+ }
52
+ catch {
53
+ throw new errors_1.RoborockApiError(`Unexpected response from ${target.host}${target.pathname} (HTTP ${response.status})`, response.status);
54
+ }
55
+ }
56
+ }
57
+ /**
58
+ * @param session The session (or the login in progress)
59
+ * @param session.email The email of the account
60
+ * @param session.clientId The random ID of this client
61
+ * @returns The `header_clientid` header: base64(md5(email + clientId))
62
+ */
63
+ function clientIdHeader({ email, clientId, }) {
64
+ return node_crypto_1.default
65
+ .createHash("md5")
66
+ .update(email)
67
+ .update(clientId)
68
+ .digest("base64");
69
+ }
70
+ /**
71
+ * @param response The API response
72
+ * @param what What was requested (for the error message)
73
+ * @returns The response's data
74
+ */
75
+ function expectOk(response, what) {
76
+ if (response.code === 200 && response.data !== undefined) {
77
+ return response.data;
78
+ }
79
+ throw new errors_1.RoborockApiError(`${what} failed: ${response.msg ?? "unknown error"} (code ${response.code})`, response.code);
80
+ }
81
+ /**
82
+ * Logs in to a Roborock account with a code sent by email (the Roborock app's default login).
83
+ *
84
+ * ```ts
85
+ * const login = await RoborockLogin.start("me@example.com");
86
+ * await login.requestCode();
87
+ * const session = await login.loginWithCode(code);
88
+ * ```
89
+ */
90
+ class RoborockLogin {
91
+ http;
92
+ email;
93
+ baseUrl;
94
+ country;
95
+ countryCode;
96
+ clientId;
97
+ constructor(http, email,
98
+ /** The regional server of the account */
99
+ baseUrl, country, countryCode, clientId) {
100
+ this.http = http;
101
+ this.email = email;
102
+ this.baseUrl = baseUrl;
103
+ this.country = country;
104
+ this.countryCode = countryCode;
105
+ this.clientId = clientId;
106
+ }
107
+ /**
108
+ * Finds the regional server of the account.
109
+ *
110
+ * @param email The email of the Roborock account
111
+ * @param options The options
112
+ * @returns The login
113
+ */
114
+ static async start(email, options = {}) {
115
+ const http = new Requester(options);
116
+ for (const baseUrl of exports.ROBOROCK_BASE_URLS) {
117
+ const response = await http.json(`${baseUrl}/api/v1/getUrlByEmail`, {
118
+ query: { email, needtwostepauth: "false" },
119
+ });
120
+ if (response.code === 2003) {
121
+ throw new errors_1.RoborockAuthError("The email is not valid", response.code);
122
+ }
123
+ const data = expectOk(response, "Finding the account's server");
124
+ if (data.country || data.countrycode) {
125
+ debug(`Account found on ${data.url} (country ${data.country})`);
126
+ return new RoborockLogin(http, email, data.url, data.country, data.countrycode === undefined ? undefined : String(data.countrycode), node_crypto_1.default.randomBytes(16).toString("base64url"));
127
+ }
128
+ }
129
+ throw new errors_1.RoborockAuthError("No Roborock account was found with this email on any region");
130
+ }
131
+ headers(extra = {}) {
132
+ return {
133
+ header_clientid: clientIdHeader({
134
+ email: this.email,
135
+ clientId: this.clientId,
136
+ }),
137
+ header_clientlang: "en",
138
+ ...extra,
139
+ };
140
+ }
141
+ /**
142
+ * Asks Roborock to email a verification code.
143
+ */
144
+ async requestCode() {
145
+ const response = this.country && this.countryCode
146
+ ? await this.http.json(`${this.baseUrl}/api/v4/email/code/send`, {
147
+ form: { email: this.email, type: "login", platform: "" },
148
+ headers: this.headers(),
149
+ })
150
+ : await this.http.json(`${this.baseUrl}/api/v1/sendEmailCode`, {
151
+ query: { username: this.email, type: "auth" },
152
+ headers: this.headers(),
153
+ });
154
+ if (response.code === 200)
155
+ return;
156
+ if (response.code === 2008) {
157
+ throw new errors_1.RoborockAuthError("This Roborock account doesn't exist", response.code);
158
+ }
159
+ if (response.code === 9002) {
160
+ throw new errors_1.RoborockAuthError("Too many codes requested: try again later", response.code);
161
+ }
162
+ throw new errors_1.RoborockApiError(`Requesting the code failed: ${response.msg} (code ${response.code})`, response.code);
163
+ }
164
+ /**
165
+ * @param code The code from the email
166
+ * @returns The session
167
+ */
168
+ async loginWithCode(code) {
169
+ const response = this.country && this.countryCode
170
+ ? await this.loginV4(code, this.country, this.countryCode)
171
+ : await this.http.json(`${this.baseUrl}/api/v1/loginWithCode`, {
172
+ query: {
173
+ username: this.email,
174
+ verifycode: code,
175
+ verifycodetype: "AUTH_EMAIL_CODE",
176
+ },
177
+ headers: this.headers(),
178
+ });
179
+ switch (response.code) {
180
+ case 2018:
181
+ throw new errors_1.RoborockAuthError("The code is not valid", response.code);
182
+ case 3009:
183
+ case 3006:
184
+ throw new errors_1.RoborockAuthError("Accept the user agreement in the Roborock app, then try again", response.code);
185
+ case 3039:
186
+ throw new errors_1.RoborockAuthError("This Roborock account doesn't exist in this region", response.code);
187
+ }
188
+ const userData = expectOk(response, "Logging in");
189
+ if (!userData?.token || !userData.rriot) {
190
+ throw new errors_1.RoborockApiError("The login response has no credentials");
191
+ }
192
+ return {
193
+ email: this.email,
194
+ baseUrl: this.baseUrl,
195
+ clientId: this.clientId,
196
+ userData,
197
+ };
198
+ }
199
+ async loginV4(code, country, countryCode) {
200
+ // Sign a random string: the login needs it (x-mercy-ks / x-mercy-k)
201
+ const ks = node_crypto_1.default
202
+ .randomBytes(12)
203
+ .toString("base64url")
204
+ .replace(/[-_]/g, "a")
205
+ .slice(0, 16);
206
+ const signed = expectOk(await this.http.json(`${this.baseUrl}/api/v3/key/sign`, {
207
+ query: { s: ks },
208
+ headers: this.headers(),
209
+ }), "Signing the login");
210
+ const agreement = await this.agreementVersion(country);
211
+ return this.http.json(`${this.baseUrl}/api/v4/auth/email/login/code`, {
212
+ form: {
213
+ country,
214
+ countryCode,
215
+ email: this.email,
216
+ code,
217
+ majorVersion: String(agreement.majorVersion),
218
+ minorVersion: String(agreement.minorVersion),
219
+ },
220
+ headers: this.headers({
221
+ "x-mercy-ks": ks,
222
+ "x-mercy-k": signed.k,
223
+ header_appversion: "4.54.02",
224
+ header_phonesystem: "iOS",
225
+ header_phonemodel: "iPhone16,1",
226
+ }),
227
+ });
228
+ }
229
+ /**
230
+ * @param country The country of the account
231
+ * @returns The current version of the user agreement: the login rejects an outdated one
232
+ */
233
+ async agreementVersion(country) {
234
+ try {
235
+ const response = await this.http.json(`${this.baseUrl}/api/v3/app/agreement/latest`, {
236
+ method: "GET",
237
+ query: { country },
238
+ headers: { header_clientlang: "en" },
239
+ });
240
+ const { majorVersion, minorVersion } = response.data ?? {};
241
+ if (response.code === 200 &&
242
+ typeof majorVersion === "number" &&
243
+ typeof minorVersion === "number") {
244
+ return { majorVersion, minorVersion };
245
+ }
246
+ }
247
+ catch (err) {
248
+ debug(`Couldn't get the user agreement version: ${err}`);
249
+ }
250
+ return { majorVersion: 14, minorVersion: 0 };
251
+ }
252
+ }
253
+ exports.RoborockLogin = RoborockLogin;
254
+ /**
255
+ * The account API, once logged in.
256
+ */
257
+ class RoborockWebApi {
258
+ session;
259
+ http;
260
+ constructor(session, options = {}) {
261
+ this.session = session;
262
+ this.http = new Requester(options);
263
+ }
264
+ /**
265
+ * @returns The ID of the account's home
266
+ */
267
+ async getHomeId() {
268
+ const response = await this.http.json(`${this.session.baseUrl}/api/v1/getHomeDetail`, {
269
+ method: "GET",
270
+ headers: {
271
+ header_clientid: clientIdHeader(this.session),
272
+ Authorization: this.session.userData.token,
273
+ },
274
+ });
275
+ if (response.code === 2010)
276
+ throw new errors_1.RoborockSessionExpiredError();
277
+ return expectOk(response, "Getting the home").rrHomeId;
278
+ }
279
+ /**
280
+ * @returns The home data: robots (with their local keys), products and rooms
281
+ */
282
+ async getHomeData() {
283
+ const homeId = await this.getHomeId();
284
+ const { rriot } = this.session.userData;
285
+ const path = `/v3/user/homes/${homeId}`;
286
+ const response = await this.http.json(`${rriot.r.a.replace(/\/$/, "")}${path}`, {
287
+ method: "GET",
288
+ headers: { Authorization: (0, hawk_1.hawkAuthorization)(rriot, path) },
289
+ });
290
+ if (!response.success || !response.result) {
291
+ throw new errors_1.RoborockApiError(`Getting the home data failed: ${response.msg ?? JSON.stringify(response)}`, response.code);
292
+ }
293
+ return {
294
+ ...response.result,
295
+ products: response.result.products ?? [],
296
+ devices: response.result.devices ?? [],
297
+ receivedDevices: response.result.receivedDevices ?? [],
298
+ rooms: response.result.rooms ?? [],
299
+ };
300
+ }
301
+ /**
302
+ * @returns The robots of the account (own and shared), with their models
303
+ */
304
+ async getDevices() {
305
+ return toDevices(await this.getHomeData());
306
+ }
307
+ }
308
+ exports.RoborockWebApi = RoborockWebApi;
309
+ /**
310
+ * @param home The home data
311
+ * @returns The robots of the home (own and shared), with their product details
312
+ */
313
+ function toDevices(home) {
314
+ const products = new Map(home.products.map((p) => [p.id, p]));
315
+ const map = (device, shared) => {
316
+ const product = products.get(device.productId);
317
+ return {
318
+ duid: device.duid,
319
+ name: device.name,
320
+ model: product?.model,
321
+ productName: product?.name,
322
+ protocolVersion: device.pv,
323
+ firmwareVersion: device.fv,
324
+ online: device.online,
325
+ localKey: device.localKey,
326
+ shared,
327
+ };
328
+ };
329
+ return [
330
+ ...home.devices.map((d) => map(d, false)),
331
+ ...home.receivedDevices.map((d) => map(d, true)),
332
+ ];
333
+ }
334
+ //# sourceMappingURL=web_api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web_api.js","sourceRoot":"","sources":["../../../src/lib/roborock/web_api.ts"],"names":[],"mappings":";;;;;;AAydA,8BAoBC;AA7eD;;;;;;;;GAQG;AACH,8DAAiC;AAEjC,sCAAwC;AAGxC,qCAIkB;AAClB,iCAA+D;AAE/D,MAAM,KAAK,GAAG,IAAA,oBAAW,EAAC,eAAe,CAAC,CAAC;AAE3C,8EAA8E;AACjE,QAAA,kBAAkB,GAAG;IAChC,4BAA4B;IAC5B,4BAA4B;IAC5B,4BAA4B;IAC5B,4BAA4B;CAC7B,CAAC;AA2FF,MAAM,SAAS;IACgB;IAA7B,YAA6B,OAA8B;QAA9B,YAAO,GAAP,OAAO,CAAuB;IAAG,CAAC;IAE/D,KAAK,CAAC,IAAI,CACR,GAAW,EACX,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,GAAG,EAAE,KAAqB,EAAE;QA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AAC,SAAS,CAAC,CAAC;QAC/C,OAAO;YACL,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,KAAK,EAAE,OAAO,EAAE,KAAK;YACrB,WAAW,EAAE,OAAO,EAAE,IAAI;YAC1B,eAAe,EAAE,MAAM,CAAC,EAAE;YAC1B,eAAe,EAAE,MAAM,CAAC,EAAE;YAC1B,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,MAAM;SACP,CAAC;IACJ,CAAC,CAAC;IACF,OAAO;QACL,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;QACzC,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;KACjD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,91 @@
1
+ # Roborock app accounts (experimental)
2
+
3
+ Robots paired with the **Roborock app** (instead of Mi Home) are registered on Roborock's own cloud, not Xiaomi's.
4
+ Their miIO token doesn't work: instead, each robot has a `localKey` that encrypts its local (TCP port 58867) and
5
+ MQTT messages. The Roborock account gives the `localKey` of every robot.
6
+
7
+ Status: node-miio can log in, list the robots and control them with the existing `Vacuum` class: over the local
8
+ network (TCP 58867) when possible, else through Roborock's cloud (MQTT), which also finds the robot's IP and sends its
9
+ status pushes. Maps come over MQTT and are parsed by `parseRoborockMap`, like the Xiaomi Cloud ones.
10
+ ([homebridge-xiaomi-roborock-vacuum#104](https://github.com/homebridge-xiaomi-roborock-vacuum/homebridge-xiaomi-roborock-vacuum/issues/104)).
11
+
12
+ ## Command line
13
+
14
+ ```sh
15
+ npx node-miio roborock login # asks for your email, then for the code Roborock emails you
16
+ npx node-miio roborock devices # name, model, protocol version, firmware, duid and (masked) local key
17
+ npx node-miio roborock rooms # the map's rooms, named as in the Roborock app
18
+ npx node-miio roborock call get_status # any method, e.g. get_consumable, get_room_mapping
19
+ npx node-miio roborock map # rooms, robot and charger positions, current room (--out: save the file)
20
+ npx node-miio roborock logout
21
+ ```
22
+
23
+ `rooms` and `call` ask the robot for its IP through Roborock's cloud, then talk to it locally (or through the cloud
24
+ if it can't be reached locally). Pass `--ip` to skip the cloud lookup. With several robots, choose one with `--duid`
25
+ (from `roborock devices`).
26
+
27
+ `devices --json` prints JSON; `--show-keys` shows the local keys in full. The session is saved, readable only by
28
+ you, in `roborock-session.json` next to the Xiaomi Cloud session (see `MIIO_CONFIG_DIR` in [Xiaomi Cloud](cloud.md)).
29
+ It's as sensitive as a password.
30
+
31
+ The **protocol version** of `roborock devices` tells which robots node-miio can control:
32
+
33
+ - `1.0` and `L01`: the same commands as miIO (`get_status`, `app_start`, `app_segment_clean`…). Most S, Q and Saros robots.
34
+ - `A01` and `B01`: different command sets (e.g. the Q7/Q10 series, Dyad/Zeo). Not planned yet.
35
+
36
+ Locally, robots speak `1.0` (AES-ECB) or `L01` (AES-GCM), which may differ from that version: the Saros 20
37
+ (`roborock.vacuum.a296`, `pv` 1.0) only answers `L01`. node-miio tries both and remembers the one that works. Robots
38
+ drop requests whose sequence number isn't higher than the previous one.
39
+
40
+ Roborock limits logins and code requests: avoid running `login` repeatedly.
41
+
42
+ ## Library
43
+
44
+ ```ts
45
+ import {
46
+ RoborockLogin,
47
+ roborockApiOf,
48
+ RoborockMqttSession,
49
+ RoborockWebApi,
50
+ roborockDevice,
51
+ roomNames,
52
+ saveRoborockSession,
53
+ toDevices,
54
+ } from "node-miio";
55
+
56
+ const login = await RoborockLogin.start("me@example.com");
57
+ await login.requestCode();
58
+ const session = await login.loginWithCode(codeFromTheEmail);
59
+ await saveRoborockSession(session);
60
+
61
+ const home = await new RoborockWebApi(session).getHomeData();
62
+ const [robot] = toDevices(home);
63
+ // { duid, name, model: "roborock.vacuum.a296", protocolVersion: "1.0", localKey, shared, … }
64
+
65
+ // One MQTT session per account, shared by its robots
66
+ const mqtt = new RoborockMqttSession(session.userData.rriot);
67
+
68
+ // A `Vacuum`, like the ones of `device()`. Local when possible (`address` is optional with `mqtt`), else MQTT.
69
+ const vacuum = await roborockDevice({
70
+ ...robot,
71
+ model: robot.model,
72
+ mqtt,
73
+ localProtocolVersion: savedVersion, // optional: what `roborockApiOf(vacuum).localProtocolVersion` was last time
74
+ });
75
+ console.log(await vacuum.batteryLevel(), vacuum.property("state"));
76
+ const rooms = roomNames(await vacuum.getRoomMap(), home.rooms); // [{ segmentId: 1, roomId, name: "Kitchen" }, …]
77
+ await vacuum.cleanRooms([rooms[0].segmentId]);
78
+
79
+ // The map (over MQTT): the same `RoborockMap` as `parseRoborockMap` (rooms, robot position, current room…)
80
+ const map = await roborockApiOf(vacuum)?.getMap();
81
+ await vacuum.destroy();
82
+ await mqtt.close();
83
+ ```
84
+
85
+ The robot's status pushes (over both connections) refresh the device's properties, so `stateChanged` events come
86
+ without waiting for the next poll.
87
+
88
+ ## Credits
89
+
90
+ The account endpoints, the message format (`1.0` and `L01`), the MQTT topics and the map responses follow [python-roborock](https://github.com/Python-roborock/python-roborock) (Apache-2.0),
91
+ which documents Roborock's API. No code is copied from it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-miio",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "license": "MIT",
5
5
  "description": "Control Mi Home devices, such as Mi Robot Vacuums, Mi Air Purifiers, Mi Smart Home Gateway (Aqara) and more",
6
6
  "author": "https://github.com/afharo",
@@ -50,6 +50,7 @@
50
50
  "chalk": "^4.1.2",
51
51
  "debug": "^4.4.3",
52
52
  "deep-equal": "^2.2.3",
53
+ "mqtt": "^5.16.1",
53
54
  "node-persist": "^4.0.4",
54
55
  "tinkerhub-discovery": "^0.6.0",
55
56
  "uqr": "^0.1.3",
Binary file