@homelib/xiaomi 0.0.0-wip.ba4eafa051eb

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 (175) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +22 -0
  3. package/bld/library/backend/client.d.ts +68 -0
  4. package/bld/library/backend/client.js +477 -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 +30 -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 +68 -0
  43. package/bld/library/device.js +235 -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 +7 -0
  58. package/bld/library/devices/index.js +8 -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/placeholder.d.ts +3 -0
  64. package/bld/library/devices/placeholder.js +4 -0
  65. package/bld/library/devices/placeholder.js.map +1 -0
  66. package/bld/library/devices/value-range.d.ts +2 -0
  67. package/bld/library/devices/value-range.js +6 -0
  68. package/bld/library/devices/value-range.js.map +1 -0
  69. package/bld/library/endpoint-connection.d.ts +154 -0
  70. package/bld/library/endpoint-connection.js +510 -0
  71. package/bld/library/endpoint-connection.js.map +1 -0
  72. package/bld/library/index.d.ts +26 -0
  73. package/bld/library/index.js +31 -0
  74. package/bld/library/index.js.map +1 -0
  75. package/bld/library/local/certificate.d.ts +31 -0
  76. package/bld/library/local/certificate.js +298 -0
  77. package/bld/library/local/certificate.js.map +1 -0
  78. package/bld/library/local/controller.d.ts +70 -0
  79. package/bld/library/local/controller.js +497 -0
  80. package/bld/library/local/controller.js.map +1 -0
  81. package/bld/library/local/discovery.d.ts +29 -0
  82. package/bld/library/local/discovery.js +321 -0
  83. package/bld/library/local/discovery.js.map +1 -0
  84. package/bld/library/local/index.d.ts +6 -0
  85. package/bld/library/local/index.js +7 -0
  86. package/bld/library/local/index.js.map +1 -0
  87. package/bld/library/local/message.d.ts +11 -0
  88. package/bld/library/local/message.js +128 -0
  89. package/bld/library/local/message.js.map +1 -0
  90. package/bld/library/local/mqtt.d.ts +113 -0
  91. package/bld/library/local/mqtt.js +1044 -0
  92. package/bld/library/local/mqtt.js.map +1 -0
  93. package/bld/library/local/routed-message-client.d.ts +49 -0
  94. package/bld/library/local/routed-message-client.js +326 -0
  95. package/bld/library/local/routed-message-client.js.map +1 -0
  96. package/bld/library/miot/index.d.ts +4 -0
  97. package/bld/library/miot/index.js +5 -0
  98. package/bld/library/miot/index.js.map +1 -0
  99. package/bld/library/miot/matcher.d.ts +65 -0
  100. package/bld/library/miot/matcher.js +217 -0
  101. package/bld/library/miot/matcher.js.map +1 -0
  102. package/bld/library/miot/request.d.ts +16 -0
  103. package/bld/library/miot/request.js +27 -0
  104. package/bld/library/miot/request.js.map +1 -0
  105. package/bld/library/miot/result.d.ts +4 -0
  106. package/bld/library/miot/result.js +5 -0
  107. package/bld/library/miot/result.js.map +1 -0
  108. package/bld/library/miot/spec.d.ts +74 -0
  109. package/bld/library/miot/spec.js +78 -0
  110. package/bld/library/miot/spec.js.map +1 -0
  111. package/bld/library/provider.d.ts +71 -0
  112. package/bld/library/provider.js +600 -0
  113. package/bld/library/provider.js.map +1 -0
  114. package/bld/library/session-manager.d.ts +34 -0
  115. package/bld/library/session-manager.js +177 -0
  116. package/bld/library/session-manager.js.map +1 -0
  117. package/bld/library/session.d.ts +36 -0
  118. package/bld/library/session.js +370 -0
  119. package/bld/library/session.js.map +1 -0
  120. package/bld/library/storage.d.ts +2 -0
  121. package/bld/library/storage.js +66 -0
  122. package/bld/library/storage.js.map +1 -0
  123. package/bld/library/terminal/bindings.d.ts +3 -0
  124. package/bld/library/terminal/bindings.js +280 -0
  125. package/bld/library/terminal/bindings.js.map +1 -0
  126. package/bld/library/terminal/index.d.ts +2 -0
  127. package/bld/library/terminal/index.js +3 -0
  128. package/bld/library/terminal/index.js.map +1 -0
  129. package/bld/library/terminal/provider-details.d.ts +3 -0
  130. package/bld/library/terminal/provider-details.js +477 -0
  131. package/bld/library/terminal/provider-details.js.map +1 -0
  132. package/package.json +29 -0
  133. package/src/library/backend/client.ts +713 -0
  134. package/src/library/backend/constants.ts +17 -0
  135. package/src/library/backend/index.ts +3 -0
  136. package/src/library/backend/oauth.ts +170 -0
  137. package/src/library/binding.ts +387 -0
  138. package/src/library/cloud/client.ts +155 -0
  139. package/src/library/cloud/constants.ts +19 -0
  140. package/src/library/cloud/device.ts +938 -0
  141. package/src/library/cloud/index.ts +5 -0
  142. package/src/library/cloud/mqtt.ts +671 -0
  143. package/src/library/cloud/transport.ts +38 -0
  144. package/src/library/command.ts +7 -0
  145. package/src/library/configuration.ts +480 -0
  146. package/src/library/device.ts +548 -0
  147. package/src/library/devices/air-conditioner.ts +171 -0
  148. package/src/library/devices/command-effect.ts +425 -0
  149. package/src/library/devices/dehumidifier.ts +131 -0
  150. package/src/library/devices/fan.ts +140 -0
  151. package/src/library/devices/index.ts +7 -0
  152. package/src/library/devices/light.ts +119 -0
  153. package/src/library/devices/placeholder.ts +3 -0
  154. package/src/library/devices/value-range.ts +14 -0
  155. package/src/library/endpoint-connection.ts +841 -0
  156. package/src/library/index.ts +62 -0
  157. package/src/library/local/certificate.ts +401 -0
  158. package/src/library/local/controller.ts +692 -0
  159. package/src/library/local/discovery.ts +485 -0
  160. package/src/library/local/index.ts +6 -0
  161. package/src/library/local/message.ts +174 -0
  162. package/src/library/local/mqtt.ts +1493 -0
  163. package/src/library/local/routed-message-client.ts +377 -0
  164. package/src/library/miot/index.ts +4 -0
  165. package/src/library/miot/matcher.ts +461 -0
  166. package/src/library/miot/request.ts +30 -0
  167. package/src/library/miot/result.ts +10 -0
  168. package/src/library/miot/spec.ts +103 -0
  169. package/src/library/provider.ts +745 -0
  170. package/src/library/session-manager.ts +224 -0
  171. package/src/library/session.ts +528 -0
  172. package/src/library/storage.ts +83 -0
  173. package/src/library/terminal/bindings.tsx +612 -0
  174. package/src/library/terminal/index.ts +2 -0
  175. package/src/library/terminal/provider-details.tsx +871 -0
@@ -0,0 +1,612 @@
1
+ import {
2
+ type EndpointPath,
3
+ type ProviderBindingRequest,
4
+ getEndpointPathKey,
5
+ } from '@homelib/core';
6
+ import {type ProviderBindingComponentProps} from '@homelib/terminal';
7
+ import {Box, Text, useInput} from 'ink';
8
+ import {useCallback, useEffect, useRef, useState} from 'react';
9
+
10
+ import type {BackendDevice} from '../backend/index.js';
11
+ import {
12
+ type MiotBindingDeviceCandidate,
13
+ type MiotBindingDeviceProposal,
14
+ type MiotBindingDiscovery,
15
+ type MiotBindingEndpointCandidate,
16
+ type MiotBindingEndpointProposal,
17
+ discoverMiotBindingDevices,
18
+ resolveMiotBindingDeviceProposal,
19
+ } from '../binding.js';
20
+ import type {MiotProvider} from '../provider.js';
21
+
22
+ const PHYSICAL_DEVICE_PAGE_SIZE = 12;
23
+
24
+ export function MiotProviderBindings({
25
+ provider,
26
+ device,
27
+ providerBindings,
28
+ onBind,
29
+ onBack,
30
+ onComplete,
31
+ }: ProviderBindingComponentProps<MiotProvider>): React.JSX.Element {
32
+ const [state, setState] = useState<BindingState>({type: 'loading'});
33
+ const operationReference = useRef<object | undefined>(undefined);
34
+ const deviceReference = useRef(device);
35
+ deviceReference.current = device;
36
+
37
+ const load = useCallback(
38
+ (notice?: DevicesState['notice']): void => {
39
+ const operation = {};
40
+
41
+ operationReference.current = operation;
42
+ setState({type: 'loading'});
43
+ void discoverMiotBindingDevices(provider, deviceReference.current).then(
44
+ discovery => {
45
+ if (operationReference.current !== operation) {
46
+ return;
47
+ }
48
+
49
+ operationReference.current = undefined;
50
+ setState({type: 'devices', discovery, cursor: 0, notice});
51
+ },
52
+ error => {
53
+ if (operationReference.current !== operation) {
54
+ return;
55
+ }
56
+
57
+ operationReference.current = undefined;
58
+ setState({type: 'load-error', message: getErrorMessage(error)});
59
+ },
60
+ );
61
+ },
62
+ [provider],
63
+ );
64
+
65
+ useEffect(() => {
66
+ load();
67
+
68
+ return () => {
69
+ operationReference.current = undefined;
70
+ };
71
+ }, [load]);
72
+
73
+ const saveBindings = (
74
+ source: DeviceMatchState,
75
+ requests: readonly ProviderBindingRequest[],
76
+ ): void => {
77
+ const operation = {};
78
+
79
+ operationReference.current = operation;
80
+ setState({type: 'saving', source, requests});
81
+ void onBind(requests).then(
82
+ () => {
83
+ if (operationReference.current !== operation) {
84
+ return;
85
+ }
86
+
87
+ operationReference.current = undefined;
88
+ onComplete();
89
+ },
90
+ error => {
91
+ if (operationReference.current !== operation) {
92
+ return;
93
+ }
94
+
95
+ operationReference.current = undefined;
96
+ setState({
97
+ type: 'save-error',
98
+ source,
99
+ requests,
100
+ message: getErrorMessage(error),
101
+ });
102
+ },
103
+ );
104
+ };
105
+
106
+ const prepareSave = (source: DeviceMatchState): void => {
107
+ const proposal = getProposal(source, providerBindings);
108
+
109
+ if (proposal.status === 'unavailable') {
110
+ return;
111
+ } else if (proposal.status === 'existing') {
112
+ onComplete();
113
+ return;
114
+ }
115
+
116
+ const requests = proposal.bindings.map(({endpoint, metadata}) => ({
117
+ endpoint,
118
+ metadata,
119
+ replaceExisting:
120
+ findCandidateEndpoint(proposal.device, endpoint)?.endpoint.binding !==
121
+ undefined && !hasProviderBinding(endpoint, providerBindings),
122
+ }));
123
+
124
+ if (requests.some(request => request.replaceExisting)) {
125
+ setState({type: 'confirm-save', source, requests});
126
+ } else {
127
+ saveBindings(source, requests);
128
+ }
129
+ };
130
+
131
+ useInput((input, key) => {
132
+ if (state.type === 'loading') {
133
+ if (key.escape) {
134
+ operationReference.current = undefined;
135
+ onBack();
136
+ }
137
+ } else if (state.type === 'load-error') {
138
+ if (key.escape) {
139
+ onBack();
140
+ } else if (key.return || input === 'r') {
141
+ load();
142
+ }
143
+ } else if (state.type === 'devices') {
144
+ const deviceCount = state.discovery.devices.length;
145
+ const clearedState =
146
+ state.notice === undefined ? state : {...state, notice: undefined};
147
+
148
+ if (key.escape) {
149
+ onBack();
150
+ } else if (key.upArrow && deviceCount > 0) {
151
+ setState({
152
+ ...clearedState,
153
+ cursor: wrapIndex(state.cursor - 1, deviceCount),
154
+ });
155
+ } else if (key.downArrow && deviceCount > 0) {
156
+ setState({
157
+ ...clearedState,
158
+ cursor: wrapIndex(state.cursor + 1, deviceCount),
159
+ });
160
+ } else if (key.return) {
161
+ const selectedDevice = state.discovery.devices.at(state.cursor);
162
+
163
+ if (selectedDevice !== undefined) {
164
+ setState({
165
+ type: 'device-match',
166
+ discovery: state.discovery,
167
+ deviceKey: selectedDevice.key,
168
+ });
169
+ }
170
+ } else if (input === 'r') {
171
+ load('reloaded');
172
+ } else if (state.notice !== undefined) {
173
+ setState(clearedState);
174
+ }
175
+ } else if (state.type === 'device-match') {
176
+ const proposal = getProposal(state, providerBindings);
177
+
178
+ if (key.escape) {
179
+ setState({
180
+ type: 'devices',
181
+ discovery: state.discovery,
182
+ cursor: getCandidateDeviceIndex(state.discovery, state.deviceKey),
183
+ });
184
+ } else if (key.return && proposal.status !== 'unavailable') {
185
+ prepareSave(state);
186
+ }
187
+ } else if (state.type === 'confirm-save') {
188
+ if (key.escape) {
189
+ setState(state.source);
190
+ } else if (input === 'y' || key.return) {
191
+ saveBindings(state.source, state.requests);
192
+ }
193
+ } else if (state.type === 'saving') {
194
+ return;
195
+ } else if (key.escape) {
196
+ setState(state.source);
197
+ } else if (key.return || input === 'r') {
198
+ saveBindings(state.source, state.requests);
199
+ }
200
+ });
201
+
202
+ return <BindingView providerBindings={providerBindings} state={state} />;
203
+ }
204
+
205
+ type BindingViewProps = {
206
+ readonly state: BindingState;
207
+ readonly providerBindings: readonly {
208
+ readonly endpoint: EndpointPath;
209
+ readonly metadata: unknown;
210
+ }[];
211
+ };
212
+
213
+ function BindingView({
214
+ state,
215
+ providerBindings,
216
+ }: BindingViewProps): React.JSX.Element {
217
+ if (state.type === 'loading') {
218
+ return (
219
+ <Box flexDirection="column">
220
+ <Text>finding matching mi home devices…</Text>
221
+ <Hint>esc back</Hint>
222
+ </Box>
223
+ );
224
+ } else if (state.type === 'load-error') {
225
+ return (
226
+ <ErrorView message={state.message}>enter/r retry · esc back</ErrorView>
227
+ );
228
+ } else if (state.type === 'devices') {
229
+ return <DevicesView providerBindings={providerBindings} state={state} />;
230
+ } else if (state.type === 'device-match') {
231
+ return <DeviceMatchView proposal={getProposal(state, providerBindings)} />;
232
+ } else if (state.type === 'confirm-save') {
233
+ const replacementCount = state.requests.filter(
234
+ request => request.replaceExisting,
235
+ ).length;
236
+
237
+ return (
238
+ <Box flexDirection="column">
239
+ <Text>
240
+ replace {replacementCount} existing{' '}
241
+ {replacementCount === 1 ? 'binding' : 'bindings'} and match this
242
+ device?
243
+ </Text>
244
+ <Hint>enter/y confirm · esc cancel</Hint>
245
+ </Box>
246
+ );
247
+ } else if (state.type === 'saving') {
248
+ return (
249
+ <Box flexDirection="column">
250
+ <Text>saving device match…</Text>
251
+ </Box>
252
+ );
253
+ }
254
+
255
+ return (
256
+ <ErrorView message={state.message}>enter/r retry · esc back</ErrorView>
257
+ );
258
+ }
259
+
260
+ function DevicesView({
261
+ state,
262
+ providerBindings,
263
+ }: {
264
+ readonly state: DevicesState;
265
+ readonly providerBindings: BindingViewProps['providerBindings'];
266
+ }): React.JSX.Element {
267
+ const {discovery} = state;
268
+ const visibleDevices = getVisibleItems(
269
+ discovery.devices,
270
+ state.cursor,
271
+ PHYSICAL_DEVICE_PAGE_SIZE,
272
+ );
273
+
274
+ return (
275
+ <Box flexDirection="column">
276
+ <Text>choose a mi home device</Text>
277
+
278
+ <Box flexDirection="column" marginTop={1}>
279
+ {discovery.devices.length === 0 ? (
280
+ <Text dimColor>no matching devices found.</Text>
281
+ ) : (
282
+ visibleDevices.items.map((device, index) => (
283
+ <ListItem
284
+ key={device.key}
285
+ details={getDeviceMatchDetails(device, providerBindings)}
286
+ label={getDeviceLabel(device.device)}
287
+ selected={visibleDevices.startIndex + index === state.cursor}
288
+ sublabel={getDeviceLocation(device.device)}
289
+ />
290
+ ))
291
+ )}
292
+ </Box>
293
+
294
+ {discovery.devices.length <= PHYSICAL_DEVICE_PAGE_SIZE ? null : (
295
+ <Text dimColor>
296
+ {visibleDevices.startIndex + 1}–
297
+ {visibleDevices.startIndex + visibleDevices.items.length} of{' '}
298
+ {discovery.devices.length}
299
+ </Text>
300
+ )}
301
+
302
+ {discovery.failedDeviceCount === 0 ? null : (
303
+ <Text color="yellow">
304
+ {discovery.failedDeviceCount} devices could not be checked.
305
+ </Text>
306
+ )}
307
+
308
+ {discovery.incompleteDeviceCount === 0 ? null : (
309
+ <Text dimColor>
310
+ {discovery.incompleteDeviceCount} devices do not expose enough
311
+ information.
312
+ </Text>
313
+ )}
314
+
315
+ {state.notice === 'reloaded' ? (
316
+ <Text color="green">devices reloaded.</Text>
317
+ ) : null}
318
+
319
+ <Hint>
320
+ {discovery.devices.length === 0
321
+ ? 'r reload · esc back'
322
+ : '↑↓ select · enter match device · r reload · esc back'}
323
+ </Hint>
324
+ </Box>
325
+ );
326
+ }
327
+
328
+ function DeviceMatchView({
329
+ proposal,
330
+ }: {
331
+ readonly proposal: MiotBindingDeviceProposal;
332
+ }): React.JSX.Element {
333
+ return (
334
+ <Box flexDirection="column">
335
+ <Text bold>{getDeviceLabel(proposal.device.device)}</Text>
336
+ {getDeviceLocation(proposal.device.device) === undefined ? null : (
337
+ <Text dimColor>{getDeviceLocation(proposal.device.device)}</Text>
338
+ )}
339
+
340
+ <Box flexDirection="column" marginTop={1}>
341
+ <Text>device match</Text>
342
+ {proposal.endpoints.map(endpoint => (
343
+ <EndpointProposalRow
344
+ key={getEndpointPathKey(endpoint.endpoint.endpoint.path)}
345
+ proposal={endpoint}
346
+ />
347
+ ))}
348
+ </Box>
349
+
350
+ {proposal.status === 'unavailable' ? (
351
+ <Text color="yellow">
352
+ this device mapping uses resources already bound elsewhere.
353
+ </Text>
354
+ ) : null}
355
+
356
+ <Box marginTop={1}>
357
+ {proposal.status === 'automatic' ? (
358
+ <Text bold color="cyan">
359
+ › bind device · {proposal.bindings.length}{' '}
360
+ {proposal.bindings.length === 1 ? 'endpoint' : 'endpoints'}
361
+ </Text>
362
+ ) : proposal.status === 'existing' ? (
363
+ <Text bold color="cyan">
364
+ › done · already bound
365
+ </Text>
366
+ ) : (
367
+ <Text color="yellow">device match unavailable.</Text>
368
+ )}
369
+ </Box>
370
+
371
+ <Hint>
372
+ {proposal.status === 'unavailable' ? '' : 'enter confirm · '}esc choose
373
+ another device
374
+ </Hint>
375
+ </Box>
376
+ );
377
+ }
378
+
379
+ function EndpointProposalRow({
380
+ proposal,
381
+ }: {
382
+ readonly proposal: MiotBindingEndpointProposal;
383
+ }): React.JSX.Element {
384
+ const color = proposal.status === 'unavailable' ? 'yellow' : 'green';
385
+ const status =
386
+ proposal.status === 'automatic'
387
+ ? 'matched automatically'
388
+ : proposal.status === 'existing'
389
+ ? 'already bound'
390
+ : 'mapping unavailable';
391
+
392
+ return (
393
+ <Box>
394
+ <Box width={28}>
395
+ <Text color={color}>
396
+ {proposal.status === 'unavailable' ? '!' : '●'}{' '}
397
+ {getEndpointLabel(proposal.endpoint.endpoint)}
398
+ </Text>
399
+ </Box>
400
+ <Text color={color}>{status}</Text>
401
+ <Text dimColor> · {proposal.endpoint.label}</Text>
402
+ </Box>
403
+ );
404
+ }
405
+
406
+ function ListItem({
407
+ label,
408
+ sublabel,
409
+ details,
410
+ selected,
411
+ }: {
412
+ readonly label: string;
413
+ readonly sublabel?: string;
414
+ readonly details: string;
415
+ readonly selected: boolean;
416
+ }): React.JSX.Element {
417
+ return (
418
+ <Box>
419
+ <Box width={32}>
420
+ <Text bold={selected} color={selected ? 'cyan' : undefined}>
421
+ {selected ? '› ' : ' '}
422
+ {label}
423
+ {sublabel === undefined ? null : <Text dimColor> · {sublabel}</Text>}
424
+ </Text>
425
+ </Box>
426
+ <Text dimColor>{details}</Text>
427
+ </Box>
428
+ );
429
+ }
430
+
431
+ function ErrorView({
432
+ message,
433
+ children,
434
+ }: {
435
+ readonly message: string;
436
+ readonly children: React.ReactNode;
437
+ }): React.JSX.Element {
438
+ return (
439
+ <Box flexDirection="column">
440
+ <Text color="red">{message}</Text>
441
+ <Hint>{children}</Hint>
442
+ </Box>
443
+ );
444
+ }
445
+
446
+ function Hint({
447
+ children,
448
+ }: {
449
+ readonly children: React.ReactNode;
450
+ }): React.JSX.Element {
451
+ return (
452
+ <Box marginTop={1}>
453
+ <Text dimColor>{children}</Text>
454
+ </Box>
455
+ );
456
+ }
457
+
458
+ type BindingState =
459
+ | {readonly type: 'loading'}
460
+ | {readonly type: 'load-error'; readonly message: string}
461
+ | DevicesState
462
+ | DeviceMatchState
463
+ | ConfirmSaveState
464
+ | SavingState
465
+ | SaveErrorState;
466
+
467
+ type DevicesState = {
468
+ readonly type: 'devices';
469
+ readonly discovery: MiotBindingDiscovery;
470
+ readonly cursor: number;
471
+ readonly notice?: 'reloaded';
472
+ };
473
+
474
+ type DeviceMatchState = {
475
+ readonly type: 'device-match';
476
+ readonly discovery: MiotBindingDiscovery;
477
+ readonly deviceKey: string;
478
+ };
479
+
480
+ type ConfirmSaveState = {
481
+ readonly type: 'confirm-save';
482
+ readonly source: DeviceMatchState;
483
+ readonly requests: readonly ProviderBindingRequest[];
484
+ };
485
+
486
+ type SavingState = {
487
+ readonly type: 'saving';
488
+ readonly source: DeviceMatchState;
489
+ readonly requests: readonly ProviderBindingRequest[];
490
+ };
491
+
492
+ type SaveErrorState = {
493
+ readonly type: 'save-error';
494
+ readonly source: DeviceMatchState;
495
+ readonly requests: readonly ProviderBindingRequest[];
496
+ readonly message: string;
497
+ };
498
+
499
+ function getProposal(
500
+ state: DeviceMatchState,
501
+ providerBindings: BindingViewProps['providerBindings'],
502
+ ): MiotBindingDeviceProposal {
503
+ const device = getCandidateDevice(state.discovery, state.deviceKey);
504
+
505
+ if (device === undefined) {
506
+ throw new TypeError('Unknown MIoT binding device.');
507
+ }
508
+
509
+ return resolveMiotBindingDeviceProposal(device, providerBindings);
510
+ }
511
+
512
+ function getCandidateDevice(
513
+ discovery: MiotBindingDiscovery,
514
+ key: string,
515
+ ): MiotBindingDeviceCandidate | undefined {
516
+ return discovery.devices.find(device => device.key === key);
517
+ }
518
+
519
+ function getCandidateDeviceIndex(
520
+ discovery: MiotBindingDiscovery,
521
+ key: string,
522
+ ): number {
523
+ const index = discovery.devices.findIndex(device => device.key === key);
524
+
525
+ return index === -1 ? 0 : index;
526
+ }
527
+
528
+ function findCandidateEndpoint(
529
+ device: MiotBindingDeviceCandidate,
530
+ endpointPath: EndpointPath,
531
+ ): MiotBindingEndpointCandidate | undefined {
532
+ const endpointPathKey = getEndpointPathKey(endpointPath);
533
+
534
+ return device.endpoints.find(
535
+ endpoint => getEndpointPathKey(endpoint.endpoint.path) === endpointPathKey,
536
+ );
537
+ }
538
+
539
+ function hasProviderBinding(
540
+ endpointPath: EndpointPath,
541
+ bindings: readonly {readonly endpoint: EndpointPath}[],
542
+ ): boolean {
543
+ const endpointPathKey = getEndpointPathKey(endpointPath);
544
+
545
+ return bindings.some(
546
+ binding => getEndpointPathKey(binding.endpoint) === endpointPathKey,
547
+ );
548
+ }
549
+
550
+ function getDeviceLabel(device: BackendDevice): string {
551
+ return device.name ?? device.model ?? device.did;
552
+ }
553
+
554
+ function getDeviceLocation(device: BackendDevice): string | undefined {
555
+ const location = [device.homeName, device.roomName]
556
+ .filter(value => value !== undefined)
557
+ .join(' › ');
558
+
559
+ if (device.online === false) {
560
+ return location === '' ? 'offline' : `${location} · offline`;
561
+ }
562
+
563
+ return location === '' ? undefined : location;
564
+ }
565
+
566
+ function getDeviceMatchDetails(
567
+ device: MiotBindingDeviceCandidate,
568
+ providerBindings: BindingViewProps['providerBindings'],
569
+ ): string {
570
+ const proposal = resolveMiotBindingDeviceProposal(device, providerBindings);
571
+
572
+ if (proposal.status === 'existing') {
573
+ return 'already bound';
574
+ } else if (proposal.status === 'unavailable') {
575
+ return 'resources already used';
576
+ }
577
+
578
+ const count = proposal.bindings.length;
579
+
580
+ return `${count} ${count === 1 ? 'endpoint' : 'endpoints'} ready`;
581
+ }
582
+
583
+ function getEndpointLabel(
584
+ endpoint: MiotBindingEndpointProposal['endpoint']['endpoint'],
585
+ ): string {
586
+ return endpoint.endpoint.name === '' ? 'main' : endpoint.endpoint.name;
587
+ }
588
+
589
+ function wrapIndex(index: number, length: number): number {
590
+ return (index + length) % length;
591
+ }
592
+
593
+ function getVisibleItems<T>(
594
+ items: readonly T[],
595
+ selectedIndex: number,
596
+ pageSize: number,
597
+ ): {readonly items: readonly T[]; readonly startIndex: number} {
598
+ const maximumStartIndex = Math.max(0, items.length - pageSize);
599
+ const startIndex = Math.min(
600
+ maximumStartIndex,
601
+ Math.max(0, selectedIndex - Math.floor(pageSize / 2)),
602
+ );
603
+
604
+ return {
605
+ items: items.slice(startIndex, startIndex + pageSize),
606
+ startIndex,
607
+ };
608
+ }
609
+
610
+ function getErrorMessage(error: unknown): string {
611
+ return error instanceof Error ? error.message : String(error);
612
+ }
@@ -0,0 +1,2 @@
1
+ export * from './bindings.js';
2
+ export * from './provider-details.js';