@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.
- package/LICENSE +19 -0
- package/README.md +22 -0
- package/bld/library/backend/client.d.ts +68 -0
- package/bld/library/backend/client.js +477 -0
- package/bld/library/backend/client.js.map +1 -0
- package/bld/library/backend/constants.d.ts +7 -0
- package/bld/library/backend/constants.js +11 -0
- package/bld/library/backend/constants.js.map +1 -0
- package/bld/library/backend/index.d.ts +3 -0
- package/bld/library/backend/index.js +4 -0
- package/bld/library/backend/index.js.map +1 -0
- package/bld/library/backend/oauth.d.ts +24 -0
- package/bld/library/backend/oauth.js +121 -0
- package/bld/library/backend/oauth.js.map +1 -0
- package/bld/library/binding.d.ts +44 -0
- package/bld/library/binding.js +212 -0
- package/bld/library/binding.js.map +1 -0
- package/bld/library/cloud/client.d.ts +27 -0
- package/bld/library/cloud/client.js +109 -0
- package/bld/library/cloud/client.js.map +1 -0
- package/bld/library/cloud/constants.d.ts +9 -0
- package/bld/library/cloud/constants.js +11 -0
- package/bld/library/cloud/constants.js.map +1 -0
- package/bld/library/cloud/device.d.ts +84 -0
- package/bld/library/cloud/device.js +686 -0
- package/bld/library/cloud/device.js.map +1 -0
- package/bld/library/cloud/index.d.ts +5 -0
- package/bld/library/cloud/index.js +6 -0
- package/bld/library/cloud/index.js.map +1 -0
- package/bld/library/cloud/mqtt.d.ts +69 -0
- package/bld/library/cloud/mqtt.js +517 -0
- package/bld/library/cloud/mqtt.js.map +1 -0
- package/bld/library/cloud/transport.d.ts +8 -0
- package/bld/library/cloud/transport.js +30 -0
- package/bld/library/cloud/transport.js.map +1 -0
- package/bld/library/command.d.ts +6 -0
- package/bld/library/command.js +6 -0
- package/bld/library/command.js.map +1 -0
- package/bld/library/configuration.d.ts +62 -0
- package/bld/library/configuration.js +303 -0
- package/bld/library/configuration.js.map +1 -0
- package/bld/library/device.d.ts +68 -0
- package/bld/library/device.js +235 -0
- package/bld/library/device.js.map +1 -0
- package/bld/library/devices/air-conditioner.d.ts +48 -0
- package/bld/library/devices/air-conditioner.js +155 -0
- package/bld/library/devices/air-conditioner.js.map +1 -0
- package/bld/library/devices/command-effect.d.ts +35 -0
- package/bld/library/devices/command-effect.js +269 -0
- package/bld/library/devices/command-effect.js.map +1 -0
- package/bld/library/devices/dehumidifier.d.ts +41 -0
- package/bld/library/devices/dehumidifier.js +124 -0
- package/bld/library/devices/dehumidifier.js.map +1 -0
- package/bld/library/devices/fan.d.ts +37 -0
- package/bld/library/devices/fan.js +135 -0
- package/bld/library/devices/fan.js.map +1 -0
- package/bld/library/devices/index.d.ts +7 -0
- package/bld/library/devices/index.js +8 -0
- package/bld/library/devices/index.js.map +1 -0
- package/bld/library/devices/light.d.ts +22 -0
- package/bld/library/devices/light.js +112 -0
- package/bld/library/devices/light.js.map +1 -0
- package/bld/library/devices/placeholder.d.ts +3 -0
- package/bld/library/devices/placeholder.js +4 -0
- package/bld/library/devices/placeholder.js.map +1 -0
- package/bld/library/devices/value-range.d.ts +2 -0
- package/bld/library/devices/value-range.js +6 -0
- package/bld/library/devices/value-range.js.map +1 -0
- package/bld/library/endpoint-connection.d.ts +154 -0
- package/bld/library/endpoint-connection.js +510 -0
- package/bld/library/endpoint-connection.js.map +1 -0
- package/bld/library/index.d.ts +26 -0
- package/bld/library/index.js +31 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/local/certificate.d.ts +31 -0
- package/bld/library/local/certificate.js +298 -0
- package/bld/library/local/certificate.js.map +1 -0
- package/bld/library/local/controller.d.ts +70 -0
- package/bld/library/local/controller.js +497 -0
- package/bld/library/local/controller.js.map +1 -0
- package/bld/library/local/discovery.d.ts +29 -0
- package/bld/library/local/discovery.js +321 -0
- package/bld/library/local/discovery.js.map +1 -0
- package/bld/library/local/index.d.ts +6 -0
- package/bld/library/local/index.js +7 -0
- package/bld/library/local/index.js.map +1 -0
- package/bld/library/local/message.d.ts +11 -0
- package/bld/library/local/message.js +128 -0
- package/bld/library/local/message.js.map +1 -0
- package/bld/library/local/mqtt.d.ts +113 -0
- package/bld/library/local/mqtt.js +1044 -0
- package/bld/library/local/mqtt.js.map +1 -0
- package/bld/library/local/routed-message-client.d.ts +49 -0
- package/bld/library/local/routed-message-client.js +326 -0
- package/bld/library/local/routed-message-client.js.map +1 -0
- package/bld/library/miot/index.d.ts +4 -0
- package/bld/library/miot/index.js +5 -0
- package/bld/library/miot/index.js.map +1 -0
- package/bld/library/miot/matcher.d.ts +65 -0
- package/bld/library/miot/matcher.js +217 -0
- package/bld/library/miot/matcher.js.map +1 -0
- package/bld/library/miot/request.d.ts +16 -0
- package/bld/library/miot/request.js +27 -0
- package/bld/library/miot/request.js.map +1 -0
- package/bld/library/miot/result.d.ts +4 -0
- package/bld/library/miot/result.js +5 -0
- package/bld/library/miot/result.js.map +1 -0
- package/bld/library/miot/spec.d.ts +74 -0
- package/bld/library/miot/spec.js +78 -0
- package/bld/library/miot/spec.js.map +1 -0
- package/bld/library/provider.d.ts +71 -0
- package/bld/library/provider.js +600 -0
- package/bld/library/provider.js.map +1 -0
- package/bld/library/session-manager.d.ts +34 -0
- package/bld/library/session-manager.js +177 -0
- package/bld/library/session-manager.js.map +1 -0
- package/bld/library/session.d.ts +36 -0
- package/bld/library/session.js +370 -0
- package/bld/library/session.js.map +1 -0
- package/bld/library/storage.d.ts +2 -0
- package/bld/library/storage.js +66 -0
- package/bld/library/storage.js.map +1 -0
- package/bld/library/terminal/bindings.d.ts +3 -0
- package/bld/library/terminal/bindings.js +280 -0
- package/bld/library/terminal/bindings.js.map +1 -0
- package/bld/library/terminal/index.d.ts +2 -0
- package/bld/library/terminal/index.js +3 -0
- package/bld/library/terminal/index.js.map +1 -0
- package/bld/library/terminal/provider-details.d.ts +3 -0
- package/bld/library/terminal/provider-details.js +477 -0
- package/bld/library/terminal/provider-details.js.map +1 -0
- package/package.json +29 -0
- package/src/library/backend/client.ts +713 -0
- package/src/library/backend/constants.ts +17 -0
- package/src/library/backend/index.ts +3 -0
- package/src/library/backend/oauth.ts +170 -0
- package/src/library/binding.ts +387 -0
- package/src/library/cloud/client.ts +155 -0
- package/src/library/cloud/constants.ts +19 -0
- package/src/library/cloud/device.ts +938 -0
- package/src/library/cloud/index.ts +5 -0
- package/src/library/cloud/mqtt.ts +671 -0
- package/src/library/cloud/transport.ts +38 -0
- package/src/library/command.ts +7 -0
- package/src/library/configuration.ts +480 -0
- package/src/library/device.ts +548 -0
- package/src/library/devices/air-conditioner.ts +171 -0
- package/src/library/devices/command-effect.ts +425 -0
- package/src/library/devices/dehumidifier.ts +131 -0
- package/src/library/devices/fan.ts +140 -0
- package/src/library/devices/index.ts +7 -0
- package/src/library/devices/light.ts +119 -0
- package/src/library/devices/placeholder.ts +3 -0
- package/src/library/devices/value-range.ts +14 -0
- package/src/library/endpoint-connection.ts +841 -0
- package/src/library/index.ts +62 -0
- package/src/library/local/certificate.ts +401 -0
- package/src/library/local/controller.ts +692 -0
- package/src/library/local/discovery.ts +485 -0
- package/src/library/local/index.ts +6 -0
- package/src/library/local/message.ts +174 -0
- package/src/library/local/mqtt.ts +1493 -0
- package/src/library/local/routed-message-client.ts +377 -0
- package/src/library/miot/index.ts +4 -0
- package/src/library/miot/matcher.ts +461 -0
- package/src/library/miot/request.ts +30 -0
- package/src/library/miot/result.ts +10 -0
- package/src/library/miot/spec.ts +103 -0
- package/src/library/provider.ts +745 -0
- package/src/library/session-manager.ts +224 -0
- package/src/library/session.ts +528 -0
- package/src/library/storage.ts +83 -0
- package/src/library/terminal/bindings.tsx +612 -0
- package/src/library/terminal/index.ts +2 -0
- package/src/library/terminal/provider-details.tsx +871 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CommandEffect,
|
|
3
|
+
CommandError,
|
|
4
|
+
type EndpointReference,
|
|
5
|
+
Temperature,
|
|
6
|
+
} from '@homelib/core';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
type MiotEndpointConnectionResolvedMetadata,
|
|
10
|
+
getMiotEndpointConnectionProperty,
|
|
11
|
+
} from '../endpoint-connection.js';
|
|
12
|
+
import {
|
|
13
|
+
type MiotResolvedSpecProperty,
|
|
14
|
+
MiotSetPropertyRequest,
|
|
15
|
+
isValidMiotSpecValueList,
|
|
16
|
+
isValidMiotSpecValueRange,
|
|
17
|
+
matchesMiotUrnPattern,
|
|
18
|
+
} from '../miot/index.js';
|
|
19
|
+
|
|
20
|
+
import {clampAndQuantizeValue} from './value-range.js';
|
|
21
|
+
|
|
22
|
+
const MIOT_BRIGHTNESS_PROPERTY_TYPE =
|
|
23
|
+
'urn:miot-spec-v2:property:brightness:0000000D';
|
|
24
|
+
const MIOT_FAN_LEVEL_PROPERTY_TYPE =
|
|
25
|
+
'urn:miot-spec-v2:property:fan-level:00000016';
|
|
26
|
+
const MIOT_RELATIVE_HUMIDITY_PROPERTY_TYPE =
|
|
27
|
+
'urn:miot-spec-v2:property:relative-humidity:0000000C';
|
|
28
|
+
const MIOT_TARGET_HUMIDITY_PROPERTY_TYPE =
|
|
29
|
+
'urn:miot-spec-v2:property:target-humidity:00000022';
|
|
30
|
+
const MIOT_NUMERIC_FORMAT_RANGES: Readonly<
|
|
31
|
+
Record<string, readonly [minimum: number, maximum: number] | undefined>
|
|
32
|
+
> = {
|
|
33
|
+
float: undefined,
|
|
34
|
+
int8: [-128, 127],
|
|
35
|
+
int16: [-32_768, 32_767],
|
|
36
|
+
int32: [-2_147_483_648, 2_147_483_647],
|
|
37
|
+
uint8: [0, 255],
|
|
38
|
+
uint16: [0, 65_535],
|
|
39
|
+
uint32: [0, 4_294_967_295],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type MiotCommandEffectValues<TName extends string = string> = Readonly<
|
|
43
|
+
Partial<Record<TName, unknown>>
|
|
44
|
+
>;
|
|
45
|
+
|
|
46
|
+
export type MiotCommandEffectConnection = {
|
|
47
|
+
readonly metadata: MiotEndpointConnectionResolvedMetadata;
|
|
48
|
+
getCommandEffectState(name: string): unknown;
|
|
49
|
+
getObservationRevision(names: Iterable<string>): number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A desired MIoT state expressed through aliases in resolved connection
|
|
54
|
+
* metadata. Property addresses and numeric conventions come from the matched
|
|
55
|
+
* MIoT spec rather than a second effect-specific schema.
|
|
56
|
+
*/
|
|
57
|
+
export class MiotCommandEffect<
|
|
58
|
+
TPropertyName extends string,
|
|
59
|
+
> implements CommandEffect {
|
|
60
|
+
private readonly properties: readonly MiotCommandEffectProperty[];
|
|
61
|
+
|
|
62
|
+
private readonly connection: MiotCommandEffectConnection;
|
|
63
|
+
|
|
64
|
+
private observationRevisions: readonly number[];
|
|
65
|
+
|
|
66
|
+
private observationRevisionValue = 0;
|
|
67
|
+
|
|
68
|
+
constructor(
|
|
69
|
+
connection: MiotCommandEffectConnection,
|
|
70
|
+
values: MiotCommandEffectValues<TPropertyName>,
|
|
71
|
+
) {
|
|
72
|
+
const {metadata} = connection;
|
|
73
|
+
const entries = Object.entries(values);
|
|
74
|
+
|
|
75
|
+
if (entries.length === 0) {
|
|
76
|
+
throw new TypeError('A MIoT command effect must contain a value.');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
this.properties = entries
|
|
80
|
+
.map(([name, value]) => {
|
|
81
|
+
if (value === undefined) {
|
|
82
|
+
throw new TypeError(
|
|
83
|
+
`MIoT command effect value is undefined: ${name}.`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const {service, property} = getMiotEndpointConnectionProperty(
|
|
88
|
+
metadata,
|
|
89
|
+
name,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
name,
|
|
94
|
+
property,
|
|
95
|
+
address: {
|
|
96
|
+
did: metadata.device.did,
|
|
97
|
+
siid: service.iid,
|
|
98
|
+
piid: property.iid,
|
|
99
|
+
},
|
|
100
|
+
value: canonicalizeMiotEffectValue(property, value),
|
|
101
|
+
};
|
|
102
|
+
})
|
|
103
|
+
.toSorted(compareMiotCommandEffectProperties);
|
|
104
|
+
this.connection = connection;
|
|
105
|
+
this.observationRevisions = this.getPropertyObservationRevisions();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get observationRevision(): number {
|
|
109
|
+
const observationRevisions = this.getPropertyObservationRevisions();
|
|
110
|
+
|
|
111
|
+
if (
|
|
112
|
+
observationRevisions.every((revision, index) => {
|
|
113
|
+
const previousRevision = this.observationRevisions[index];
|
|
114
|
+
return previousRevision !== undefined && revision > previousRevision;
|
|
115
|
+
})
|
|
116
|
+
) {
|
|
117
|
+
this.observationRevisions = observationRevisions;
|
|
118
|
+
this.observationRevisionValue++;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return this.observationRevisionValue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The request represented by a single-property effect.
|
|
126
|
+
*
|
|
127
|
+
* Multi-property effects are valid for equality and state matching, but
|
|
128
|
+
* their external execution must be defined explicitly instead of being
|
|
129
|
+
* silently expanded into non-atomic sequential writes.
|
|
130
|
+
*/
|
|
131
|
+
get request(): MiotSetPropertyRequest {
|
|
132
|
+
const [property] = this.properties;
|
|
133
|
+
|
|
134
|
+
if (property === undefined || this.properties.length !== 1) {
|
|
135
|
+
throw new TypeError(
|
|
136
|
+
'A multi-property MIoT effect does not define one property request.',
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!property.property.access.includes('write')) {
|
|
141
|
+
throw new CommandError(
|
|
142
|
+
`MIoT command effect property is not writable: ${property.name}.`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return new MiotSetPropertyRequest(property.address, property.value);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Describes the canonical values that this effect writes to the device. */
|
|
150
|
+
toLogString(): string {
|
|
151
|
+
return this.properties
|
|
152
|
+
.map(({name, property, value}) => {
|
|
153
|
+
const enumName =
|
|
154
|
+
property.enum === undefined
|
|
155
|
+
? undefined
|
|
156
|
+
: Object.entries(property.enum).find(
|
|
157
|
+
([, enumValue]) => enumValue === value,
|
|
158
|
+
)?.[0];
|
|
159
|
+
|
|
160
|
+
return `set ${name}=${value}${enumName === undefined ? '' : ` (${enumName})`}`;
|
|
161
|
+
})
|
|
162
|
+
.join(' ');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
equals(effect: CommandEffect): boolean {
|
|
166
|
+
if (!(effect instanceof MiotCommandEffect)) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return miotCommandEffectPropertiesEqual(this.properties, effect.properties);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
matches(_endpoint: EndpointReference): boolean {
|
|
174
|
+
for (const target of this.properties) {
|
|
175
|
+
const value = this.connection.getCommandEffectState(target.name);
|
|
176
|
+
|
|
177
|
+
if (
|
|
178
|
+
value === undefined ||
|
|
179
|
+
canonicalizeMiotObservedEffectValue(
|
|
180
|
+
target.name,
|
|
181
|
+
target.property,
|
|
182
|
+
value,
|
|
183
|
+
) !== target.value
|
|
184
|
+
) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
private getPropertyObservationRevisions(): readonly number[] {
|
|
192
|
+
return this.properties.map(property =>
|
|
193
|
+
this.connection.getObservationRevision([property.name]),
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
type MiotCommandEffectProperty = {
|
|
199
|
+
readonly name: string;
|
|
200
|
+
readonly property: MiotResolvedSpecProperty;
|
|
201
|
+
readonly address: {
|
|
202
|
+
readonly did: string;
|
|
203
|
+
readonly siid: number;
|
|
204
|
+
readonly piid: number;
|
|
205
|
+
};
|
|
206
|
+
readonly value: boolean | number | string;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
function canonicalizeMiotEffectValue(
|
|
210
|
+
property: MiotResolvedSpecProperty,
|
|
211
|
+
value: unknown,
|
|
212
|
+
): boolean | number | string {
|
|
213
|
+
const miotValue = getMiotEffectValue(property, value);
|
|
214
|
+
const valueList = property['value-list'];
|
|
215
|
+
|
|
216
|
+
if (
|
|
217
|
+
valueList !== undefined &&
|
|
218
|
+
isValidMiotSpecValueList(valueList) &&
|
|
219
|
+
!valueList.some(entry => entry.value === miotValue)
|
|
220
|
+
) {
|
|
221
|
+
throw new CommandError(`Unsupported MIoT property value: ${miotValue}.`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return canonicalizeMiotObservedValue(property, miotValue);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function canonicalizeMiotObservedEffectValue(
|
|
228
|
+
name: string,
|
|
229
|
+
property: MiotResolvedSpecProperty,
|
|
230
|
+
value: unknown,
|
|
231
|
+
): boolean | number | string {
|
|
232
|
+
const canonicalValue = canonicalizeMiotObservedValue(property, value);
|
|
233
|
+
|
|
234
|
+
if (
|
|
235
|
+
property.enum !== undefined &&
|
|
236
|
+
(typeof canonicalValue !== 'number' ||
|
|
237
|
+
!Object.values(property.enum).includes(canonicalValue))
|
|
238
|
+
) {
|
|
239
|
+
throw new TypeError(`Unknown MIoT enum property state: ${name}=${value}.`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return canonicalValue;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function canonicalizeMiotObservedValue(
|
|
246
|
+
property: MiotResolvedSpecProperty,
|
|
247
|
+
value: unknown,
|
|
248
|
+
): boolean | number | string {
|
|
249
|
+
const {format} = property;
|
|
250
|
+
|
|
251
|
+
if (format === 'bool') {
|
|
252
|
+
if (typeof value !== 'boolean') {
|
|
253
|
+
throw new TypeError('Invalid MIoT boolean command effect value.');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return value;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (format === 'string') {
|
|
260
|
+
if (typeof value !== 'string') {
|
|
261
|
+
throw new TypeError('Invalid MIoT string command effect value.');
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return value;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
268
|
+
throw new TypeError('Invalid MIoT numeric command effect value.');
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (!Object.hasOwn(MIOT_NUMERIC_FORMAT_RANGES, format)) {
|
|
272
|
+
throw new TypeError(`Unsupported MIoT command effect format: ${format}.`);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const valueList = property['value-list'];
|
|
276
|
+
|
|
277
|
+
if (valueList !== undefined) {
|
|
278
|
+
if (
|
|
279
|
+
!isValidMiotSpecValueList(valueList) ||
|
|
280
|
+
!valueList.some(entry => entry.value === value)
|
|
281
|
+
) {
|
|
282
|
+
throw new TypeError('Invalid MIoT value-list command effect value.');
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const valueRange = property['value-range'];
|
|
287
|
+
const canonicalValue =
|
|
288
|
+
valueRange === undefined
|
|
289
|
+
? value
|
|
290
|
+
: isValidMiotSpecValueRange(valueRange, format)
|
|
291
|
+
? clampAndQuantizeValue(value, valueRange)
|
|
292
|
+
: undefined;
|
|
293
|
+
|
|
294
|
+
if (canonicalValue === undefined) {
|
|
295
|
+
throw new TypeError('Invalid MIoT command effect value range.');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (
|
|
299
|
+
/^(?:u?int(?:8|16|32))$/.test(format) &&
|
|
300
|
+
!Number.isInteger(canonicalValue)
|
|
301
|
+
) {
|
|
302
|
+
throw new TypeError('Invalid MIoT integer command effect value.');
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const formatRange = MIOT_NUMERIC_FORMAT_RANGES[format];
|
|
306
|
+
|
|
307
|
+
if (
|
|
308
|
+
formatRange !== undefined &&
|
|
309
|
+
(canonicalValue < formatRange[0] || canonicalValue > formatRange[1])
|
|
310
|
+
) {
|
|
311
|
+
throw new TypeError('MIoT command effect value exceeds its format range.');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return canonicalValue;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function getMiotEffectValue(
|
|
318
|
+
property: MiotResolvedSpecProperty,
|
|
319
|
+
value: unknown,
|
|
320
|
+
): unknown {
|
|
321
|
+
if (property.enum !== undefined) {
|
|
322
|
+
if (typeof value !== 'string') {
|
|
323
|
+
throw new TypeError('Invalid MIoT enum command effect value.');
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (!Object.hasOwn(property.enum, value)) {
|
|
327
|
+
throw new CommandError(`Unsupported MIoT enum value: ${value}.`);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const enumValue = property.enum[value];
|
|
331
|
+
|
|
332
|
+
return enumValue;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (value instanceof Temperature) {
|
|
336
|
+
switch (property.unit) {
|
|
337
|
+
case 'celsius':
|
|
338
|
+
return value.celsius;
|
|
339
|
+
case 'fahrenheit':
|
|
340
|
+
return value.fahrenheit;
|
|
341
|
+
case 'kelvin':
|
|
342
|
+
return value.kelvin;
|
|
343
|
+
default:
|
|
344
|
+
throw new TypeError(
|
|
345
|
+
`Unsupported MIoT temperature unit: ${property.unit ?? 'none'}.`,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (typeof value !== 'number') {
|
|
351
|
+
return value;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (matchesMiotUrnPattern(property.type, MIOT_BRIGHTNESS_PROPERTY_TYPE)) {
|
|
355
|
+
const valueRange = property['value-range'];
|
|
356
|
+
|
|
357
|
+
if (!isValidMiotSpecValueRange(valueRange, property.format)) {
|
|
358
|
+
throw new TypeError('Invalid MIoT brightness value range.');
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return value * valueRange[1];
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (
|
|
365
|
+
matchesMiotUrnPattern(
|
|
366
|
+
property.type,
|
|
367
|
+
MIOT_RELATIVE_HUMIDITY_PROPERTY_TYPE,
|
|
368
|
+
) ||
|
|
369
|
+
matchesMiotUrnPattern(property.type, MIOT_TARGET_HUMIDITY_PROPERTY_TYPE)
|
|
370
|
+
) {
|
|
371
|
+
return value * 100;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (matchesMiotUrnPattern(property.type, MIOT_FAN_LEVEL_PROPERTY_TYPE)) {
|
|
375
|
+
const valueList = property['value-list'];
|
|
376
|
+
|
|
377
|
+
if (!isValidMiotSpecValueList(valueList)) {
|
|
378
|
+
throw new TypeError('Invalid MIoT fan-level value list.');
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const levels = valueList
|
|
382
|
+
.map(entry => entry.value)
|
|
383
|
+
.toSorted((a, b) => a - b);
|
|
384
|
+
const index = Math.min(
|
|
385
|
+
levels.length - 1,
|
|
386
|
+
Math.max(0, Math.round(value * levels.length) - 1),
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
return levels[index];
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return value;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function compareMiotCommandEffectProperties(
|
|
396
|
+
left: MiotCommandEffectProperty,
|
|
397
|
+
right: MiotCommandEffectProperty,
|
|
398
|
+
): number {
|
|
399
|
+
return (
|
|
400
|
+
left.address.did.localeCompare(right.address.did) ||
|
|
401
|
+
left.address.siid - right.address.siid ||
|
|
402
|
+
left.address.piid - right.address.piid
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function miotCommandEffectPropertiesEqual(
|
|
407
|
+
left: readonly MiotCommandEffectProperty[],
|
|
408
|
+
right: readonly MiotCommandEffectProperty[],
|
|
409
|
+
): boolean {
|
|
410
|
+
return (
|
|
411
|
+
left.length === right.length &&
|
|
412
|
+
left.every((property, index) => {
|
|
413
|
+
const other = right[index];
|
|
414
|
+
|
|
415
|
+
return (
|
|
416
|
+
other !== undefined &&
|
|
417
|
+
property.name === other.name &&
|
|
418
|
+
property.address.did === other.address.did &&
|
|
419
|
+
property.address.siid === other.address.siid &&
|
|
420
|
+
property.address.piid === other.address.piid &&
|
|
421
|
+
property.value === other.value
|
|
422
|
+
);
|
|
423
|
+
})
|
|
424
|
+
);
|
|
425
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CommandError,
|
|
3
|
+
type CommandExecution,
|
|
4
|
+
DehumidifierEndpoint,
|
|
5
|
+
type DehumidifierEndpointCommand,
|
|
6
|
+
type DehumidifierEndpointConnection,
|
|
7
|
+
type DehumidifierMode,
|
|
8
|
+
SetDehumidifierModeCommand,
|
|
9
|
+
SetDehumidifierOnCommand,
|
|
10
|
+
SetDehumidifierTargetHumidityCommand,
|
|
11
|
+
Temperature,
|
|
12
|
+
} from '@homelib/core';
|
|
13
|
+
import {computed} from 'mobx';
|
|
14
|
+
|
|
15
|
+
import {MiotEndpointConnection} from '../endpoint-connection.js';
|
|
16
|
+
import {
|
|
17
|
+
type MiotPropertySchema,
|
|
18
|
+
type MiotPropertySchemaProperties,
|
|
19
|
+
} from '../miot/index.js';
|
|
20
|
+
|
|
21
|
+
import {MiotCommandEffect} from './command-effect.js';
|
|
22
|
+
|
|
23
|
+
export class MiotDehumidifierEndpointConnection
|
|
24
|
+
extends MiotEndpointConnection<
|
|
25
|
+
DehumidifierEndpointCommand,
|
|
26
|
+
typeof MiotDehumidifierEndpointConnection.properties
|
|
27
|
+
>
|
|
28
|
+
implements DehumidifierEndpointConnection
|
|
29
|
+
{
|
|
30
|
+
static readonly Endpoint = DehumidifierEndpoint;
|
|
31
|
+
static readonly properties = {
|
|
32
|
+
'urn:miot-spec-v2:service:dehumidifier:00007841': {
|
|
33
|
+
'urn:miot-spec-v2:property:on:00000006': 'on',
|
|
34
|
+
'urn:miot-spec-v2:property:mode:00000008': {
|
|
35
|
+
name: 'mode',
|
|
36
|
+
enum: {'*': {auto: 0, sleep: 1, laundry: 2}},
|
|
37
|
+
optional: true,
|
|
38
|
+
},
|
|
39
|
+
'urn:miot-spec-v2:property:target-humidity:00000022': {
|
|
40
|
+
name: 'target-humidity',
|
|
41
|
+
optional: true,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
'urn:miot-spec-v2:service:environment:0000780A': {
|
|
45
|
+
'urn:miot-spec-v2:property:temperature:00000020': {
|
|
46
|
+
name: 'temperature',
|
|
47
|
+
optional: true,
|
|
48
|
+
},
|
|
49
|
+
'urn:miot-spec-v2:property:relative-humidity:0000000C': {
|
|
50
|
+
name: 'relative-humidity',
|
|
51
|
+
optional: true,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
} as const satisfies MiotPropertySchema;
|
|
55
|
+
|
|
56
|
+
@computed
|
|
57
|
+
get on(): boolean {
|
|
58
|
+
return this.getBooleanPropertyState('on', false);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@computed
|
|
62
|
+
get mode(): DehumidifierMode | undefined {
|
|
63
|
+
return this.getEnumPropertyState('mode');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@computed
|
|
67
|
+
get targetRelativeHumidity(): number | undefined {
|
|
68
|
+
const value = this.getNumberPropertyState('target-humidity', 0);
|
|
69
|
+
return value === undefined ? undefined : value / 100;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@computed
|
|
73
|
+
get temperature(): Temperature | undefined {
|
|
74
|
+
return this.getTemperaturePropertyState(
|
|
75
|
+
'temperature',
|
|
76
|
+
Temperature.fromKelvin(0),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@computed
|
|
81
|
+
get relativeHumidity(): number | undefined {
|
|
82
|
+
const value = this.getNumberPropertyState('relative-humidity', 0);
|
|
83
|
+
return value === undefined ? undefined : value / 100;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
override prepareCommand(
|
|
87
|
+
command: DehumidifierEndpointCommand,
|
|
88
|
+
): CommandExecution {
|
|
89
|
+
let effect: MiotDehumidifierCommandEffect;
|
|
90
|
+
|
|
91
|
+
if (command instanceof SetDehumidifierOnCommand) {
|
|
92
|
+
effect = new MiotDehumidifierCommandEffect(this, {on: command.value});
|
|
93
|
+
} else if (command instanceof SetDehumidifierModeCommand) {
|
|
94
|
+
if (this.properties.mode === undefined) {
|
|
95
|
+
throw new CommandError('MIoT dehumidifier does not support mode.');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
effect = new MiotDehumidifierCommandEffect(this, {
|
|
99
|
+
mode: command.value,
|
|
100
|
+
});
|
|
101
|
+
} else if (command instanceof SetDehumidifierTargetHumidityCommand) {
|
|
102
|
+
if (this.properties['target-humidity'] === undefined) {
|
|
103
|
+
throw new CommandError(
|
|
104
|
+
'MIoT dehumidifier does not support target humidity.',
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
effect = new MiotDehumidifierCommandEffect(this, {
|
|
109
|
+
'target-humidity': command.relativeHumidity,
|
|
110
|
+
});
|
|
111
|
+
} else {
|
|
112
|
+
throw new TypeError('Unsupported MIoT dehumidifier endpoint command.');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const {request} = effect;
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
effect,
|
|
119
|
+
execute: () => this.executeRequest(request),
|
|
120
|
+
toLogString: () => effect.toLogString(),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
type MiotDehumidifierEndpointProperties = MiotPropertySchemaProperties<
|
|
126
|
+
typeof MiotDehumidifierEndpointConnection.properties
|
|
127
|
+
>;
|
|
128
|
+
|
|
129
|
+
class MiotDehumidifierCommandEffect extends MiotCommandEffect<
|
|
130
|
+
keyof MiotDehumidifierEndpointProperties
|
|
131
|
+
> {}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CommandError,
|
|
3
|
+
type CommandExecution,
|
|
4
|
+
FanEndpoint,
|
|
5
|
+
type FanEndpointCommand,
|
|
6
|
+
type FanEndpointConnection,
|
|
7
|
+
type FanMode,
|
|
8
|
+
SetFanHorizontalSwingCommand,
|
|
9
|
+
SetFanModeCommand,
|
|
10
|
+
SetFanOnCommand,
|
|
11
|
+
SetFanSpeedCommand,
|
|
12
|
+
} from '@homelib/core';
|
|
13
|
+
import {computed} from 'mobx';
|
|
14
|
+
|
|
15
|
+
import {MiotEndpointConnection} from '../endpoint-connection.js';
|
|
16
|
+
import {
|
|
17
|
+
type MiotPropertySchema,
|
|
18
|
+
type MiotPropertySchemaProperties,
|
|
19
|
+
} from '../miot/index.js';
|
|
20
|
+
|
|
21
|
+
import {MiotCommandEffect} from './command-effect.js';
|
|
22
|
+
|
|
23
|
+
export class MiotFanEndpointConnection
|
|
24
|
+
extends MiotEndpointConnection<
|
|
25
|
+
FanEndpointCommand,
|
|
26
|
+
typeof MiotFanEndpointConnection.properties
|
|
27
|
+
>
|
|
28
|
+
implements FanEndpointConnection
|
|
29
|
+
{
|
|
30
|
+
static readonly Endpoint = FanEndpoint;
|
|
31
|
+
static readonly properties = {
|
|
32
|
+
'urn:miot-spec-v2:service:fan:00007808': {
|
|
33
|
+
'urn:miot-spec-v2:property:on:00000006': 'on',
|
|
34
|
+
'urn:miot-spec-v2:property:mode:00000008': {
|
|
35
|
+
name: 'mode',
|
|
36
|
+
enum: {
|
|
37
|
+
'urn:miot-spec-v2:device:fan:0000A005:zhimi-*': {
|
|
38
|
+
natural: 0,
|
|
39
|
+
normal: 1,
|
|
40
|
+
},
|
|
41
|
+
'*': {
|
|
42
|
+
normal: 0,
|
|
43
|
+
natural: 1,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
optional: true,
|
|
47
|
+
},
|
|
48
|
+
'urn:miot-spec-v2:property:fan-level:00000016': {
|
|
49
|
+
name: 'fan-level',
|
|
50
|
+
optional: true,
|
|
51
|
+
},
|
|
52
|
+
'urn:miot-spec-v2:property:horizontal-swing:00000017': {
|
|
53
|
+
name: 'horizontal-swing',
|
|
54
|
+
optional: true,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
} as const satisfies MiotPropertySchema;
|
|
58
|
+
|
|
59
|
+
@computed
|
|
60
|
+
get on(): boolean {
|
|
61
|
+
return this.getBooleanPropertyState('on', false);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@computed
|
|
65
|
+
get mode(): FanMode | undefined {
|
|
66
|
+
return this.getEnumPropertyState('mode');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@computed
|
|
70
|
+
get speed(): number | undefined {
|
|
71
|
+
const fanLevel = this.properties['fan-level'];
|
|
72
|
+
|
|
73
|
+
if (fanLevel === undefined) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const valueList = this.getPropertyValueList(fanLevel);
|
|
78
|
+
const levels = valueList
|
|
79
|
+
.map(entry => entry.value)
|
|
80
|
+
.toSorted((left, right) => left - right);
|
|
81
|
+
const value = this.getNumberPropertyState('fan-level');
|
|
82
|
+
|
|
83
|
+
if (value === undefined) {
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return (levels.indexOf(value) + 1) / levels.length;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@computed
|
|
91
|
+
get horizontalSwing(): boolean | undefined {
|
|
92
|
+
return this.getBooleanPropertyState('horizontal-swing', false);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
override prepareCommand(command: FanEndpointCommand): CommandExecution {
|
|
96
|
+
let effect: MiotFanCommandEffect;
|
|
97
|
+
|
|
98
|
+
if (command instanceof SetFanOnCommand) {
|
|
99
|
+
effect = new MiotFanCommandEffect(this, {on: command.value});
|
|
100
|
+
} else if (command instanceof SetFanModeCommand) {
|
|
101
|
+
if (this.properties.mode === undefined) {
|
|
102
|
+
throw new CommandError('MIoT fan does not support mode.');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
effect = new MiotFanCommandEffect(this, {mode: command.value});
|
|
106
|
+
} else if (command instanceof SetFanSpeedCommand) {
|
|
107
|
+
if (this.properties['fan-level'] === undefined) {
|
|
108
|
+
throw new CommandError('MIoT fan does not support speed.');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
effect = new MiotFanCommandEffect(this, {'fan-level': command.value});
|
|
112
|
+
} else if (command instanceof SetFanHorizontalSwingCommand) {
|
|
113
|
+
if (this.properties['horizontal-swing'] === undefined) {
|
|
114
|
+
throw new CommandError('MIoT fan does not support horizontal swing.');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
effect = new MiotFanCommandEffect(this, {
|
|
118
|
+
'horizontal-swing': command.value,
|
|
119
|
+
});
|
|
120
|
+
} else {
|
|
121
|
+
throw new TypeError('Unsupported MIoT fan endpoint command.');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const {request} = effect;
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
effect,
|
|
128
|
+
execute: () => this.executeRequest(request),
|
|
129
|
+
toLogString: () => effect.toLogString(),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
type MiotFanEndpointProperties = MiotPropertySchemaProperties<
|
|
135
|
+
typeof MiotFanEndpointConnection.properties
|
|
136
|
+
>;
|
|
137
|
+
|
|
138
|
+
class MiotFanCommandEffect extends MiotCommandEffect<
|
|
139
|
+
keyof MiotFanEndpointProperties
|
|
140
|
+
> {}
|