@homelib/xiaomi 0.0.0-wip.01265bf1cc90

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 (183) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +26 -0
  3. package/bld/library/backend/client.d.ts +73 -0
  4. package/bld/library/backend/client.js +490 -0
  5. package/bld/library/backend/client.js.map +1 -0
  6. package/bld/library/backend/constants.d.ts +7 -0
  7. package/bld/library/backend/constants.js +11 -0
  8. package/bld/library/backend/constants.js.map +1 -0
  9. package/bld/library/backend/index.d.ts +3 -0
  10. package/bld/library/backend/index.js +4 -0
  11. package/bld/library/backend/index.js.map +1 -0
  12. package/bld/library/backend/oauth.d.ts +24 -0
  13. package/bld/library/backend/oauth.js +121 -0
  14. package/bld/library/backend/oauth.js.map +1 -0
  15. package/bld/library/binding.d.ts +44 -0
  16. package/bld/library/binding.js +212 -0
  17. package/bld/library/binding.js.map +1 -0
  18. package/bld/library/cloud/client.d.ts +27 -0
  19. package/bld/library/cloud/client.js +109 -0
  20. package/bld/library/cloud/client.js.map +1 -0
  21. package/bld/library/cloud/constants.d.ts +9 -0
  22. package/bld/library/cloud/constants.js +11 -0
  23. package/bld/library/cloud/constants.js.map +1 -0
  24. package/bld/library/cloud/device.d.ts +84 -0
  25. package/bld/library/cloud/device.js +686 -0
  26. package/bld/library/cloud/device.js.map +1 -0
  27. package/bld/library/cloud/index.d.ts +5 -0
  28. package/bld/library/cloud/index.js +6 -0
  29. package/bld/library/cloud/index.js.map +1 -0
  30. package/bld/library/cloud/mqtt.d.ts +69 -0
  31. package/bld/library/cloud/mqtt.js +517 -0
  32. package/bld/library/cloud/mqtt.js.map +1 -0
  33. package/bld/library/cloud/transport.d.ts +8 -0
  34. package/bld/library/cloud/transport.js +34 -0
  35. package/bld/library/cloud/transport.js.map +1 -0
  36. package/bld/library/command.d.ts +6 -0
  37. package/bld/library/command.js +6 -0
  38. package/bld/library/command.js.map +1 -0
  39. package/bld/library/configuration.d.ts +62 -0
  40. package/bld/library/configuration.js +303 -0
  41. package/bld/library/configuration.js.map +1 -0
  42. package/bld/library/device.d.ts +69 -0
  43. package/bld/library/device.js +266 -0
  44. package/bld/library/device.js.map +1 -0
  45. package/bld/library/devices/air-conditioner.d.ts +48 -0
  46. package/bld/library/devices/air-conditioner.js +155 -0
  47. package/bld/library/devices/air-conditioner.js.map +1 -0
  48. package/bld/library/devices/command-effect.d.ts +35 -0
  49. package/bld/library/devices/command-effect.js +269 -0
  50. package/bld/library/devices/command-effect.js.map +1 -0
  51. package/bld/library/devices/dehumidifier.d.ts +41 -0
  52. package/bld/library/devices/dehumidifier.js +124 -0
  53. package/bld/library/devices/dehumidifier.js.map +1 -0
  54. package/bld/library/devices/fan.d.ts +37 -0
  55. package/bld/library/devices/fan.js +135 -0
  56. package/bld/library/devices/fan.js.map +1 -0
  57. package/bld/library/devices/index.d.ts +9 -0
  58. package/bld/library/devices/index.js +10 -0
  59. package/bld/library/devices/index.js.map +1 -0
  60. package/bld/library/devices/light.d.ts +22 -0
  61. package/bld/library/devices/light.js +112 -0
  62. package/bld/library/devices/light.js.map +1 -0
  63. package/bld/library/devices/pet-feeder.d.ts +36 -0
  64. package/bld/library/devices/pet-feeder.js +116 -0
  65. package/bld/library/devices/pet-feeder.js.map +1 -0
  66. package/bld/library/devices/placeholder.d.ts +3 -0
  67. package/bld/library/devices/placeholder.js +4 -0
  68. package/bld/library/devices/placeholder.js.map +1 -0
  69. package/bld/library/devices/temperature-humidity-sensor.d.ts +14 -0
  70. package/bld/library/devices/temperature-humidity-sensor.js +56 -0
  71. package/bld/library/devices/temperature-humidity-sensor.js.map +1 -0
  72. package/bld/library/devices/value-range.d.ts +2 -0
  73. package/bld/library/devices/value-range.js +6 -0
  74. package/bld/library/devices/value-range.js.map +1 -0
  75. package/bld/library/endpoint-connection.d.ts +168 -0
  76. package/bld/library/endpoint-connection.js +510 -0
  77. package/bld/library/endpoint-connection.js.map +1 -0
  78. package/bld/library/index.d.ts +26 -0
  79. package/bld/library/index.js +33 -0
  80. package/bld/library/index.js.map +1 -0
  81. package/bld/library/local/certificate.d.ts +31 -0
  82. package/bld/library/local/certificate.js +298 -0
  83. package/bld/library/local/certificate.js.map +1 -0
  84. package/bld/library/local/controller.d.ts +70 -0
  85. package/bld/library/local/controller.js +498 -0
  86. package/bld/library/local/controller.js.map +1 -0
  87. package/bld/library/local/discovery.d.ts +29 -0
  88. package/bld/library/local/discovery.js +321 -0
  89. package/bld/library/local/discovery.js.map +1 -0
  90. package/bld/library/local/index.d.ts +6 -0
  91. package/bld/library/local/index.js +7 -0
  92. package/bld/library/local/index.js.map +1 -0
  93. package/bld/library/local/message.d.ts +11 -0
  94. package/bld/library/local/message.js +128 -0
  95. package/bld/library/local/message.js.map +1 -0
  96. package/bld/library/local/mqtt.d.ts +113 -0
  97. package/bld/library/local/mqtt.js +1074 -0
  98. package/bld/library/local/mqtt.js.map +1 -0
  99. package/bld/library/local/routed-message-client.d.ts +49 -0
  100. package/bld/library/local/routed-message-client.js +326 -0
  101. package/bld/library/local/routed-message-client.js.map +1 -0
  102. package/bld/library/miot/index.d.ts +4 -0
  103. package/bld/library/miot/index.js +5 -0
  104. package/bld/library/miot/index.js.map +1 -0
  105. package/bld/library/miot/matcher.d.ts +84 -0
  106. package/bld/library/miot/matcher.js +326 -0
  107. package/bld/library/miot/matcher.js.map +1 -0
  108. package/bld/library/miot/request.d.ts +33 -0
  109. package/bld/library/miot/request.js +57 -0
  110. package/bld/library/miot/request.js.map +1 -0
  111. package/bld/library/miot/result.d.ts +8 -0
  112. package/bld/library/miot/result.js +5 -0
  113. package/bld/library/miot/result.js.map +1 -0
  114. package/bld/library/miot/spec.d.ts +96 -0
  115. package/bld/library/miot/spec.js +86 -0
  116. package/bld/library/miot/spec.js.map +1 -0
  117. package/bld/library/provider.d.ts +78 -0
  118. package/bld/library/provider.js +600 -0
  119. package/bld/library/provider.js.map +1 -0
  120. package/bld/library/session-manager.d.ts +34 -0
  121. package/bld/library/session-manager.js +177 -0
  122. package/bld/library/session-manager.js.map +1 -0
  123. package/bld/library/session.d.ts +36 -0
  124. package/bld/library/session.js +370 -0
  125. package/bld/library/session.js.map +1 -0
  126. package/bld/library/storage.d.ts +2 -0
  127. package/bld/library/storage.js +66 -0
  128. package/bld/library/storage.js.map +1 -0
  129. package/bld/library/terminal/bindings.d.ts +3 -0
  130. package/bld/library/terminal/bindings.js +306 -0
  131. package/bld/library/terminal/bindings.js.map +1 -0
  132. package/bld/library/terminal/index.d.ts +2 -0
  133. package/bld/library/terminal/index.js +3 -0
  134. package/bld/library/terminal/index.js.map +1 -0
  135. package/bld/library/terminal/provider-details.d.ts +3 -0
  136. package/bld/library/terminal/provider-details.js +477 -0
  137. package/bld/library/terminal/provider-details.js.map +1 -0
  138. package/package.json +29 -0
  139. package/src/library/backend/client.ts +737 -0
  140. package/src/library/backend/constants.ts +17 -0
  141. package/src/library/backend/index.ts +3 -0
  142. package/src/library/backend/oauth.ts +170 -0
  143. package/src/library/binding.ts +387 -0
  144. package/src/library/cloud/client.ts +155 -0
  145. package/src/library/cloud/constants.ts +19 -0
  146. package/src/library/cloud/device.ts +938 -0
  147. package/src/library/cloud/index.ts +5 -0
  148. package/src/library/cloud/mqtt.ts +671 -0
  149. package/src/library/cloud/transport.ts +46 -0
  150. package/src/library/command.ts +7 -0
  151. package/src/library/configuration.ts +480 -0
  152. package/src/library/device.ts +601 -0
  153. package/src/library/devices/air-conditioner.ts +171 -0
  154. package/src/library/devices/command-effect.ts +425 -0
  155. package/src/library/devices/dehumidifier.ts +131 -0
  156. package/src/library/devices/fan.ts +140 -0
  157. package/src/library/devices/index.ts +9 -0
  158. package/src/library/devices/light.ts +119 -0
  159. package/src/library/devices/pet-feeder.ts +129 -0
  160. package/src/library/devices/placeholder.ts +3 -0
  161. package/src/library/devices/temperature-humidity-sensor.ts +49 -0
  162. package/src/library/devices/value-range.ts +14 -0
  163. package/src/library/endpoint-connection.ts +841 -0
  164. package/src/library/index.ts +71 -0
  165. package/src/library/local/certificate.ts +401 -0
  166. package/src/library/local/controller.ts +693 -0
  167. package/src/library/local/discovery.ts +485 -0
  168. package/src/library/local/index.ts +6 -0
  169. package/src/library/local/message.ts +174 -0
  170. package/src/library/local/mqtt.ts +1555 -0
  171. package/src/library/local/routed-message-client.ts +377 -0
  172. package/src/library/miot/index.ts +4 -0
  173. package/src/library/miot/matcher.ts +683 -0
  174. package/src/library/miot/request.ts +70 -0
  175. package/src/library/miot/result.ts +14 -0
  176. package/src/library/miot/spec.ts +114 -0
  177. package/src/library/provider.ts +745 -0
  178. package/src/library/session-manager.ts +224 -0
  179. package/src/library/session.ts +528 -0
  180. package/src/library/storage.ts +83 -0
  181. package/src/library/terminal/bindings.tsx +692 -0
  182. package/src/library/terminal/index.ts +2 -0
  183. package/src/library/terminal/provider-details.tsx +871 -0
@@ -0,0 +1,70 @@
1
+ export abstract class MiotRequest {
2
+ declare private readonly requestBrand: void;
3
+
4
+ toLogString(): string {
5
+ return this.constructor.name;
6
+ }
7
+ }
8
+
9
+ export class MiotSetPropertyRequest extends MiotRequest {
10
+ constructor(
11
+ readonly property: MiotProperty,
12
+ readonly value: unknown,
13
+ ) {
14
+ super();
15
+ }
16
+
17
+ override toLogString(): string {
18
+ const {did, siid, piid} = this.property;
19
+
20
+ return `miot set did=${did} siid=${siid} piid=${piid}`;
21
+ }
22
+ }
23
+
24
+ export class MiotInvokeActionRequest extends MiotRequest {
25
+ /** Inputs must follow the order of the corresponding `MiotSpecAction.in`. */
26
+ constructor(
27
+ readonly action: MiotAction,
28
+ readonly inputs: readonly MiotActionInput[],
29
+ ) {
30
+ super();
31
+
32
+ if (new Set(inputs.map(input => input.piid)).size !== inputs.length) {
33
+ throw new TypeError('Duplicate MIoT action input property.');
34
+ }
35
+ }
36
+
37
+ override toLogString(): string {
38
+ const {did, siid, aiid} = this.action;
39
+
40
+ return `miot action did=${did} siid=${siid} aiid=${aiid}`;
41
+ }
42
+ }
43
+
44
+ export type MiotExecutionRequest =
45
+ MiotSetPropertyRequest | MiotInvokeActionRequest;
46
+
47
+ export type MiotAction = {
48
+ readonly did: string;
49
+ readonly siid: number;
50
+ readonly aiid: number;
51
+ };
52
+
53
+ export type MiotActionInput = {
54
+ readonly piid: number;
55
+ readonly value: unknown;
56
+ };
57
+
58
+ export function getMiotExecutionRequestDid(
59
+ request: MiotExecutionRequest,
60
+ ): string {
61
+ return request instanceof MiotSetPropertyRequest
62
+ ? request.property.did
63
+ : request.action.did;
64
+ }
65
+
66
+ export type MiotProperty = {
67
+ readonly did: string;
68
+ readonly siid: number;
69
+ readonly piid: number;
70
+ };
@@ -0,0 +1,14 @@
1
+ // Xiaomi's official HA client treats both codes as successful execution results.
2
+ export function isSuccessfulMiotExecutionResult(
3
+ result: MiotExecutionResult,
4
+ ): boolean {
5
+ return result.code === 0 || result.code === 1;
6
+ }
7
+
8
+ /**
9
+ * The common execution status. MIoT action outputs are intentionally not
10
+ * exposed until a HomeLib command has a concrete consumer for them.
11
+ */
12
+ export type MiotExecutionResult = {
13
+ readonly code: number;
14
+ };
@@ -0,0 +1,114 @@
1
+ import * as x from 'x-value';
2
+
3
+ const MIOT_SPEC_INSTANCE_URL = 'https://miot-spec.org/miot-spec-v2/instance';
4
+ const MIOT_SPEC_REQUEST_TIMEOUT = 30_000;
5
+
6
+ export class MiotSpecClient {
7
+ private readonly instancePromiseMap = new Map<
8
+ string,
9
+ Promise<MiotSpecInstance>
10
+ >();
11
+
12
+ getInstance(urn: string): Promise<MiotSpecInstance> {
13
+ const cachedInstancePromise = this.instancePromiseMap.get(urn);
14
+
15
+ if (cachedInstancePromise !== undefined) {
16
+ return cachedInstancePromise;
17
+ }
18
+
19
+ const instancePromise = withRequestTimeout(this.requestInstance(urn));
20
+
21
+ this.instancePromiseMap.set(urn, instancePromise);
22
+ void instancePromise.catch(() => {
23
+ if (this.instancePromiseMap.get(urn) === instancePromise) {
24
+ this.instancePromiseMap.delete(urn);
25
+ }
26
+ });
27
+
28
+ return instancePromise;
29
+ }
30
+
31
+ private async requestInstance(urn: string): Promise<MiotSpecInstance> {
32
+ const url = new URL(MIOT_SPEC_INSTANCE_URL);
33
+ url.searchParams.set('type', urn);
34
+ const response = await fetch(url);
35
+
36
+ if (!response.ok) {
37
+ throw new Error(`Failed to get MIoT spec instance: ${response.status}.`);
38
+ }
39
+
40
+ return MiotSpecInstance.satisfies(await response.json());
41
+ }
42
+ }
43
+
44
+ export const MiotSpecValueRange = x.tuple([x.number, x.number, x.number]);
45
+
46
+ export type MiotSpecValueRange = x.TypeOf<typeof MiotSpecValueRange>;
47
+
48
+ export const MiotSpecValueListEntry = x.object({
49
+ value: x.number,
50
+ description: x.string,
51
+ });
52
+
53
+ export type MiotSpecValueListEntry = x.TypeOf<typeof MiotSpecValueListEntry>;
54
+
55
+ export const MiotSpecValueList = x.array(MiotSpecValueListEntry);
56
+
57
+ export type MiotSpecValueList = x.TypeOf<typeof MiotSpecValueList>;
58
+
59
+ export const MiotSpecProperty = x.object({
60
+ iid: x.number,
61
+ type: x.string,
62
+ description: x.string,
63
+ format: x.string,
64
+ access: x.array(x.string),
65
+ unit: x.string.optional(),
66
+ 'value-range': MiotSpecValueRange.optional(),
67
+ 'value-list': MiotSpecValueList.optional(),
68
+ });
69
+
70
+ export type MiotSpecProperty = x.TypeOf<typeof MiotSpecProperty>;
71
+
72
+ export const MiotSpecAction = x.object({
73
+ iid: x.number,
74
+ type: x.string,
75
+ description: x.string,
76
+ in: x.array(x.number),
77
+ out: x.array(x.number),
78
+ });
79
+
80
+ export type MiotSpecAction = x.TypeOf<typeof MiotSpecAction>;
81
+
82
+ export const MiotSpecService = x.object({
83
+ iid: x.number,
84
+ type: x.string,
85
+ description: x.string,
86
+ properties: x.array(MiotSpecProperty).optional(),
87
+ actions: x.array(MiotSpecAction).optional(),
88
+ });
89
+
90
+ export type MiotSpecService = x.TypeOf<typeof MiotSpecService>;
91
+
92
+ export const MiotSpecInstance = x.object({
93
+ type: x.string,
94
+ description: x.string,
95
+ services: x.array(MiotSpecService),
96
+ });
97
+
98
+ export type MiotSpecInstance = x.TypeOf<typeof MiotSpecInstance>;
99
+
100
+ function withRequestTimeout<T>(request: Promise<T>): Promise<T> {
101
+ let timeout: ReturnType<typeof setTimeout> | undefined;
102
+ const timeoutPromise = new Promise<never>((_resolve, reject) => {
103
+ timeout = setTimeout(() => {
104
+ reject(new Error('MIoT spec request timed out.'));
105
+ }, MIOT_SPEC_REQUEST_TIMEOUT);
106
+ });
107
+
108
+ // This bounds the cached request's wait; the underlying fetch keeps running.
109
+ return Promise.race([request, timeoutPromise]).finally(() => {
110
+ if (timeout !== undefined) {
111
+ clearTimeout(timeout);
112
+ }
113
+ });
114
+ }