@homebridge/hap-nodejs 2.2.4-beta.7 → 2.3.0-beta.1
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/README.md +14 -17
- package/dist/accessories/AirConditioner_accessory.js +27 -29
- package/dist/accessories/AirConditioner_accessory.js.map +1 -1
- package/dist/accessories/AppleTVRemote_accessory.js +63 -76
- package/dist/accessories/AppleTVRemote_accessory.js.map +1 -1
- package/dist/accessories/Camera_accessory.js +151 -134
- package/dist/accessories/Camera_accessory.js.map +1 -1
- package/dist/accessories/Fan_accessory.js +18 -21
- package/dist/accessories/Fan_accessory.js.map +1 -1
- package/dist/accessories/GarageDoorOpener_accessory.js +33 -35
- package/dist/accessories/GarageDoorOpener_accessory.js.map +1 -1
- package/dist/accessories/Light-AdaptiveLighting_accessory.js +41 -44
- package/dist/accessories/Light-AdaptiveLighting_accessory.js.map +1 -1
- package/dist/accessories/Light_accessory.js +32 -34
- package/dist/accessories/Light_accessory.js.map +1 -1
- package/dist/accessories/Lock_accessory.js +24 -26
- package/dist/accessories/Lock_accessory.js.map +1 -1
- package/dist/accessories/MotionSensor_accessory.js +13 -15
- package/dist/accessories/MotionSensor_accessory.js.map +1 -1
- package/dist/accessories/Outlet_accessory.js +20 -22
- package/dist/accessories/Outlet_accessory.js.map +1 -1
- package/dist/accessories/SmartSpeaker_accessory.js +18 -20
- package/dist/accessories/SmartSpeaker_accessory.js.map +1 -1
- package/dist/accessories/Sprinkler_accessory.js +36 -38
- package/dist/accessories/Sprinkler_accessory.js.map +1 -1
- package/dist/accessories/TV_accessory.js +43 -45
- package/dist/accessories/TV_accessory.js.map +1 -1
- package/dist/accessories/TemperatureSensor_accessory.js +12 -14
- package/dist/accessories/TemperatureSensor_accessory.js.map +1 -1
- package/dist/accessories/Wi-FiRouter_accessory.d.ts +1 -1
- package/dist/accessories/Wi-FiRouter_accessory.d.ts.map +1 -1
- package/dist/accessories/Wi-FiRouter_accessory.js +9 -12
- package/dist/accessories/Wi-FiRouter_accessory.js.map +1 -1
- package/dist/accessories/Wi-FiSatellite_accessory.d.ts +1 -1
- package/dist/accessories/Wi-FiSatellite_accessory.d.ts.map +1 -1
- package/dist/accessories/Wi-FiSatellite_accessory.js +11 -14
- package/dist/accessories/Wi-FiSatellite_accessory.js.map +1 -1
- package/dist/accessories/gstreamer-audioProducer.d.ts +3 -3
- package/dist/accessories/gstreamer-audioProducer.d.ts.map +1 -1
- package/dist/accessories/gstreamer-audioProducer.js +38 -38
- package/dist/accessories/gstreamer-audioProducer.js.map +1 -1
- package/dist/accessories/types.d.ts +63 -63
- package/dist/accessories/types.d.ts.map +1 -1
- package/dist/accessories/types.js +83 -87
- package/dist/accessories/types.js.map +1 -1
- package/dist/hap.d.ts +50 -0
- package/dist/hap.d.ts.map +1 -0
- package/dist/hap.js +49 -0
- package/dist/hap.js.map +1 -0
- package/dist/index.d.ts +3 -49
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -53
- package/dist/index.js.map +1 -1
- package/dist/internal-types.d.ts +1 -1
- package/dist/internal-types.d.ts.map +1 -1
- package/dist/internal-types.js +19 -19
- package/dist/internal-types.js.map +1 -1
- package/dist/lib/Accessory.d.ts +37 -37
- package/dist/lib/Accessory.d.ts.map +1 -1
- package/dist/lib/Accessory.js +286 -280
- package/dist/lib/Accessory.js.map +1 -1
- package/dist/lib/Advertiser.d.ts +6 -6
- package/dist/lib/Advertiser.d.ts.map +1 -1
- package/dist/lib/Advertiser.js +143 -139
- package/dist/lib/Advertiser.js.map +1 -1
- package/dist/lib/Bridge.d.ts +1 -1
- package/dist/lib/Bridge.d.ts.map +1 -1
- package/dist/lib/Bridge.js +2 -6
- package/dist/lib/Bridge.js.map +1 -1
- package/dist/lib/Characteristic.d.ts +40 -41
- package/dist/lib/Characteristic.d.ts.map +1 -1
- package/dist/lib/Characteristic.js +290 -240
- package/dist/lib/Characteristic.js.map +1 -1
- package/dist/lib/HAPServer.d.ts +31 -30
- package/dist/lib/HAPServer.d.ts.map +1 -1
- package/dist/lib/HAPServer.js +245 -247
- package/dist/lib/HAPServer.js.map +1 -1
- package/dist/lib/Service.d.ts +21 -22
- package/dist/lib/Service.d.ts.map +1 -1
- package/dist/lib/Service.js +60 -68
- package/dist/lib/Service.js.map +1 -1
- package/dist/lib/camera/RTPProxy.d.ts +2 -1
- package/dist/lib/camera/RTPProxy.d.ts.map +1 -1
- package/dist/lib/camera/RTPProxy.js +35 -37
- package/dist/lib/camera/RTPProxy.js.map +1 -1
- package/dist/lib/camera/RTPStreamManagement.d.ts +34 -33
- package/dist/lib/camera/RTPStreamManagement.d.ts.map +1 -1
- package/dist/lib/camera/RTPStreamManagement.js +184 -137
- package/dist/lib/camera/RTPStreamManagement.js.map +1 -1
- package/dist/lib/camera/RecordingManagement.d.ts +18 -17
- package/dist/lib/camera/RecordingManagement.d.ts.map +1 -1
- package/dist/lib/camera/RecordingManagement.js +189 -164
- package/dist/lib/camera/RecordingManagement.js.map +1 -1
- package/dist/lib/camera/index.d.ts +3 -3
- package/dist/lib/camera/index.d.ts.map +1 -1
- package/dist/lib/camera/index.js +3 -6
- package/dist/lib/camera/index.js.map +1 -1
- package/dist/lib/controller/AdaptiveLightingController.d.ts +8 -8
- package/dist/lib/controller/AdaptiveLightingController.d.ts.map +1 -1
- package/dist/lib/controller/AdaptiveLightingController.js +112 -99
- package/dist/lib/controller/AdaptiveLightingController.js.map +1 -1
- package/dist/lib/controller/CameraController.d.ts +21 -20
- package/dist/lib/controller/CameraController.d.ts.map +1 -1
- package/dist/lib/controller/CameraController.js +92 -89
- package/dist/lib/controller/CameraController.js.map +1 -1
- package/dist/lib/controller/Controller.d.ts +1 -1
- package/dist/lib/controller/Controller.d.ts.map +1 -1
- package/dist/lib/controller/Controller.js +5 -8
- package/dist/lib/controller/Controller.js.map +1 -1
- package/dist/lib/controller/DoorbellController.d.ts +4 -4
- package/dist/lib/controller/DoorbellController.d.ts.map +1 -1
- package/dist/lib/controller/DoorbellController.js +9 -13
- package/dist/lib/controller/DoorbellController.js.map +1 -1
- package/dist/lib/controller/RemoteController.d.ts +40 -39
- package/dist/lib/controller/RemoteController.d.ts.map +1 -1
- package/dist/lib/controller/RemoteController.js +213 -186
- package/dist/lib/controller/RemoteController.js.map +1 -1
- package/dist/lib/controller/index.d.ts +5 -5
- package/dist/lib/controller/index.d.ts.map +1 -1
- package/dist/lib/controller/index.js +5 -8
- package/dist/lib/controller/index.js.map +1 -1
- package/dist/lib/datastream/DataStreamManagement.d.ts +2 -2
- package/dist/lib/datastream/DataStreamManagement.d.ts.map +1 -1
- package/dist/lib/datastream/DataStreamManagement.js +36 -35
- package/dist/lib/datastream/DataStreamManagement.js.map +1 -1
- package/dist/lib/datastream/DataStreamParser.d.ts +1 -0
- package/dist/lib/datastream/DataStreamParser.d.ts.map +1 -1
- package/dist/lib/datastream/DataStreamParser.js +53 -69
- package/dist/lib/datastream/DataStreamParser.js.map +1 -1
- package/dist/lib/datastream/DataStreamServer.d.ts +24 -23
- package/dist/lib/datastream/DataStreamServer.d.ts.map +1 -1
- package/dist/lib/datastream/DataStreamServer.js +151 -154
- package/dist/lib/datastream/DataStreamServer.js.map +1 -1
- package/dist/lib/datastream/index.d.ts +3 -3
- package/dist/lib/datastream/index.d.ts.map +1 -1
- package/dist/lib/datastream/index.js +3 -6
- package/dist/lib/datastream/index.js.map +1 -1
- package/dist/lib/definitions/CharacteristicDefinitions.d.ts +1 -1
- package/dist/lib/definitions/CharacteristicDefinitions.d.ts.map +1 -1
- package/dist/lib/definitions/CharacteristicDefinitions.js +1103 -1288
- package/dist/lib/definitions/CharacteristicDefinitions.js.map +1 -1
- package/dist/lib/definitions/ServiceDefinitions.d.ts +1 -1
- package/dist/lib/definitions/ServiceDefinitions.d.ts.map +1 -1
- package/dist/lib/definitions/ServiceDefinitions.js +644 -722
- package/dist/lib/definitions/ServiceDefinitions.js.map +1 -1
- package/dist/lib/definitions/generate-definitions.d.ts +2 -2
- package/dist/lib/definitions/generate-definitions.d.ts.map +1 -1
- package/dist/lib/definitions/generate-definitions.js +241 -241
- package/dist/lib/definitions/generate-definitions.js.map +1 -1
- package/dist/lib/definitions/generator-configuration.d.ts +1 -1
- package/dist/lib/definitions/generator-configuration.d.ts.map +1 -1
- package/dist/lib/definitions/generator-configuration.js +572 -309
- package/dist/lib/definitions/generator-configuration.js.map +1 -1
- package/dist/lib/definitions/index.d.ts +2 -2
- package/dist/lib/definitions/index.d.ts.map +1 -1
- package/dist/lib/definitions/index.js +2 -5
- package/dist/lib/definitions/index.js.map +1 -1
- package/dist/lib/gen/HomeKit.js +1 -2
- package/dist/lib/gen/HomeKit.js.map +1 -1
- package/dist/lib/model/AccessoryInfo.d.ts +4 -3
- package/dist/lib/model/AccessoryInfo.d.ts.map +1 -1
- package/dist/lib/model/AccessoryInfo.js +53 -56
- package/dist/lib/model/AccessoryInfo.js.map +1 -1
- package/dist/lib/model/ControllerStorage.d.ts +3 -3
- package/dist/lib/model/ControllerStorage.d.ts.map +1 -1
- package/dist/lib/model/ControllerStorage.js +31 -30
- package/dist/lib/model/ControllerStorage.js.map +1 -1
- package/dist/lib/model/HAPFileStorage.d.ts.map +1 -1
- package/dist/lib/model/HAPFileStorage.js +71 -50
- package/dist/lib/model/HAPFileStorage.js.map +1 -1
- package/dist/lib/model/HAPStorage.d.ts +1 -1
- package/dist/lib/model/HAPStorage.d.ts.map +1 -1
- package/dist/lib/model/HAPStorage.js +11 -14
- package/dist/lib/model/HAPStorage.js.map +1 -1
- package/dist/lib/model/IdentifierCache.d.ts +1 -1
- package/dist/lib/model/IdentifierCache.d.ts.map +1 -1
- package/dist/lib/model/IdentifierCache.js +23 -27
- package/dist/lib/model/IdentifierCache.js.map +1 -1
- package/dist/lib/tv/AccessControlManagement.d.ts +6 -6
- package/dist/lib/tv/AccessControlManagement.d.ts.map +1 -1
- package/dist/lib/tv/AccessControlManagement.js +23 -24
- package/dist/lib/tv/AccessControlManagement.js.map +1 -1
- package/dist/lib/util/checkName.d.ts +2 -2
- package/dist/lib/util/checkName.d.ts.map +1 -1
- package/dist/lib/util/checkName.js +8 -14
- package/dist/lib/util/checkName.js.map +1 -1
- package/dist/lib/util/clone.d.ts.map +1 -1
- package/dist/lib/util/clone.js +1 -5
- package/dist/lib/util/clone.js.map +1 -1
- package/dist/lib/util/color-utils.d.ts +1 -1
- package/dist/lib/util/color-utils.d.ts.map +1 -1
- package/dist/lib/util/color-utils.js +1608 -410
- package/dist/lib/util/color-utils.js.map +1 -1
- package/dist/lib/util/eventedhttp.d.ts +23 -22
- package/dist/lib/util/eventedhttp.d.ts.map +1 -1
- package/dist/lib/util/eventedhttp.js +127 -122
- package/dist/lib/util/eventedhttp.js.map +1 -1
- package/dist/lib/util/hapCrypto.d.ts +2 -1
- package/dist/lib/util/hapCrypto.d.ts.map +1 -1
- package/dist/lib/util/hapCrypto.js +72 -59
- package/dist/lib/util/hapCrypto.js.map +1 -1
- package/dist/lib/util/hapStatusError.d.ts +1 -1
- package/dist/lib/util/hapStatusError.d.ts.map +1 -1
- package/dist/lib/util/hapStatusError.js +5 -9
- package/dist/lib/util/hapStatusError.js.map +1 -1
- package/dist/lib/util/net-utils.js +13 -19
- package/dist/lib/util/net-utils.js.map +1 -1
- package/dist/lib/util/once.js +3 -6
- package/dist/lib/util/once.js.map +1 -1
- package/dist/lib/util/promise-utils.d.ts +1 -1
- package/dist/lib/util/promise-utils.d.ts.map +1 -1
- package/dist/lib/util/promise-utils.js +9 -11
- package/dist/lib/util/promise-utils.js.map +1 -1
- package/dist/lib/util/request-util.d.ts +3 -3
- package/dist/lib/util/request-util.d.ts.map +1 -1
- package/dist/lib/util/request-util.js +14 -23
- package/dist/lib/util/request-util.js.map +1 -1
- package/dist/lib/util/time.d.ts +1 -0
- package/dist/lib/util/time.d.ts.map +1 -1
- package/dist/lib/util/time.js +6 -11
- package/dist/lib/util/time.js.map +1 -1
- package/dist/lib/util/tlv.d.ts +1 -0
- package/dist/lib/util/tlv.d.ts.map +1 -1
- package/dist/lib/util/tlv.js +38 -44
- package/dist/lib/util/tlv.js.map +1 -1
- package/dist/lib/util/uuid.d.ts +1 -0
- package/dist/lib/util/uuid.d.ts.map +1 -1
- package/dist/lib/util/uuid.js +26 -38
- package/dist/lib/util/uuid.js.map +1 -1
- package/dist/types.d.ts +24 -24
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +3 -5
- package/dist/types.js.map +1 -1
- package/package.json +49 -42
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const hapStatusError_1 = require("./util/hapStatusError");
|
|
13
|
-
const once_1 = require("./util/once");
|
|
14
|
-
const request_util_1 = require("./util/request-util");
|
|
15
|
-
const uuid_1 = require("./util/uuid");
|
|
16
|
-
const debug = (0, debug_1.default)("HAP-NodeJS:Characteristic");
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { inspect } from 'node:util';
|
|
4
|
+
import createDebug from 'debug';
|
|
5
|
+
import { HAPStatus } from './HAPServer.js';
|
|
6
|
+
import { clone } from './util/clone.js';
|
|
7
|
+
import { HapStatusError } from './util/hapStatusError.js';
|
|
8
|
+
import { once } from './util/once.js';
|
|
9
|
+
import { extractHAPStatusFromError, formatOutgoingCharacteristicValue, isIntegerNumericFormat, isNumericFormat, isUnsignedNumericFormat, numericLowerBound, numericUpperBound, } from './util/request-util.js';
|
|
10
|
+
import { BASE_UUID, toShortForm } from './util/uuid.js';
|
|
11
|
+
const debug = createDebug('HAP-NodeJS:Characteristic');
|
|
17
12
|
/**
|
|
18
13
|
* @group Characteristic
|
|
19
14
|
*/
|
|
20
|
-
|
|
15
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
16
|
+
export var Formats;
|
|
21
17
|
(function (Formats) {
|
|
22
18
|
Formats["BOOL"] = "bool";
|
|
23
19
|
/**
|
|
@@ -56,11 +52,12 @@ var Formats;
|
|
|
56
52
|
* Base64 encoded tlv8 string.
|
|
57
53
|
*/
|
|
58
54
|
Formats["TLV8"] = "tlv8";
|
|
59
|
-
})(Formats || (
|
|
55
|
+
})(Formats || (Formats = {}));
|
|
60
56
|
/**
|
|
61
57
|
* @group Characteristic
|
|
62
58
|
*/
|
|
63
|
-
|
|
59
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
60
|
+
export var Units;
|
|
64
61
|
(function (Units) {
|
|
65
62
|
/**
|
|
66
63
|
* Celsius is the only temperature unit in the HomeKit Accessory Protocol.
|
|
@@ -72,22 +69,23 @@ var Units;
|
|
|
72
69
|
Units["ARC_DEGREE"] = "arcdegrees";
|
|
73
70
|
Units["LUX"] = "lux";
|
|
74
71
|
Units["SECONDS"] = "seconds";
|
|
75
|
-
})(Units || (
|
|
72
|
+
})(Units || (Units = {}));
|
|
76
73
|
/**
|
|
77
74
|
* @group Characteristic
|
|
78
75
|
*/
|
|
79
|
-
|
|
76
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
77
|
+
export var Perms;
|
|
80
78
|
(function (Perms) {
|
|
81
79
|
Perms["PAIRED_READ"] = "pr";
|
|
82
80
|
Perms["PAIRED_WRITE"] = "pw";
|
|
83
81
|
Perms["NOTIFY"] = "ev";
|
|
84
|
-
// eslint-disable-next-line
|
|
82
|
+
// eslint-disable-next-line ts/no-duplicate-enum-values
|
|
85
83
|
Perms["EVENTS"] = "ev";
|
|
86
84
|
Perms["ADDITIONAL_AUTHORIZATION"] = "aa";
|
|
87
85
|
Perms["TIMED_WRITE"] = "tw";
|
|
88
86
|
Perms["HIDDEN"] = "hd";
|
|
89
87
|
Perms["WRITE_RESPONSE"] = "wr";
|
|
90
|
-
})(Perms || (
|
|
88
|
+
})(Perms || (Perms = {}));
|
|
91
89
|
/**
|
|
92
90
|
* The wire values of {@link Perms}, as a runtime lookup. A const enum is erased during compilation and cannot be enumerated, so this set is the enum's
|
|
93
91
|
* runtime mirror and lives beside it: a permission added to the enum above must be added here as well. The members are referenced rather than retyped,
|
|
@@ -113,14 +111,14 @@ const VALID_PERMS = new Set([
|
|
|
113
111
|
*
|
|
114
112
|
* @private
|
|
115
113
|
*/
|
|
116
|
-
function isValidPerms(perms) {
|
|
114
|
+
export function isValidPerms(perms) {
|
|
117
115
|
if (!Array.isArray(perms) || perms.length === 0) {
|
|
118
116
|
return false;
|
|
119
117
|
}
|
|
120
118
|
// for...of iteration surfaces the holes of a sparse array as undefined, which Array.prototype.every would silently skip. A hole survives JSON
|
|
121
119
|
// serialization as null, the exact malformed entry this check exists to reject.
|
|
122
120
|
for (const permission of perms) {
|
|
123
|
-
if (typeof permission !==
|
|
121
|
+
if (typeof permission !== 'string' || !VALID_PERMS.has(permission)) {
|
|
124
122
|
return false;
|
|
125
123
|
}
|
|
126
124
|
}
|
|
@@ -136,12 +134,12 @@ function isValidPerms(perms) {
|
|
|
136
134
|
*
|
|
137
135
|
* @private
|
|
138
136
|
*/
|
|
139
|
-
function describePerms(perms) {
|
|
137
|
+
export function describePerms(perms) {
|
|
140
138
|
try {
|
|
141
139
|
return JSON.stringify(perms) ?? String(perms);
|
|
142
140
|
}
|
|
143
141
|
catch {
|
|
144
|
-
return
|
|
142
|
+
return inspect(perms);
|
|
145
143
|
}
|
|
146
144
|
}
|
|
147
145
|
/**
|
|
@@ -152,16 +150,18 @@ function describePerms(perms) {
|
|
|
152
150
|
*
|
|
153
151
|
* @group Characteristic
|
|
154
152
|
*/
|
|
155
|
-
|
|
153
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
154
|
+
export var Access;
|
|
156
155
|
(function (Access) {
|
|
157
156
|
Access[Access["READ"] = 0] = "READ";
|
|
158
157
|
Access[Access["WRITE"] = 1] = "WRITE";
|
|
159
158
|
Access[Access["NOTIFY"] = 2] = "NOTIFY";
|
|
160
|
-
})(Access || (
|
|
159
|
+
})(Access || (Access = {}));
|
|
161
160
|
/**
|
|
162
161
|
* @group Characteristic
|
|
163
162
|
*/
|
|
164
|
-
|
|
163
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
164
|
+
export var ChangeReason;
|
|
165
165
|
(function (ChangeReason) {
|
|
166
166
|
/**
|
|
167
167
|
* Reason used when HomeKit writes a value or the API user calls {@link Characteristic.setValue}.
|
|
@@ -179,11 +179,12 @@ var ChangeReason;
|
|
|
179
179
|
* Used when call to {@link Characteristic.sendEventNotification} was made.
|
|
180
180
|
*/
|
|
181
181
|
ChangeReason["EVENT"] = "event";
|
|
182
|
-
})(ChangeReason || (
|
|
182
|
+
})(ChangeReason || (ChangeReason = {}));
|
|
183
183
|
/**
|
|
184
184
|
* @group Characteristic
|
|
185
185
|
*/
|
|
186
|
-
|
|
186
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
187
|
+
export var CharacteristicEventTypes;
|
|
187
188
|
(function (CharacteristicEventTypes) {
|
|
188
189
|
/**
|
|
189
190
|
* This event is thrown when a HomeKit controller wants to read the current value of the characteristic.
|
|
@@ -221,14 +222,14 @@ var CharacteristicEventTypes;
|
|
|
221
222
|
* @private
|
|
222
223
|
*/
|
|
223
224
|
CharacteristicEventTypes["CHARACTERISTIC_CONFIGURATION_CHANGE"] = "characteristic-configurationChange";
|
|
224
|
-
})(CharacteristicEventTypes || (
|
|
225
|
+
})(CharacteristicEventTypes || (CharacteristicEventTypes = {}));
|
|
225
226
|
/**
|
|
226
227
|
* @group Characteristic
|
|
227
228
|
*/
|
|
228
229
|
class ValidValuesIterable {
|
|
229
230
|
props;
|
|
230
231
|
constructor(props) {
|
|
231
|
-
(
|
|
232
|
+
assert(isNumericFormat(props.format), `Cannot instantiate valid values iterable when format is not numeric. Found ${props.format}`);
|
|
232
233
|
this.props = props;
|
|
233
234
|
}
|
|
234
235
|
*[Symbol.iterator]() {
|
|
@@ -252,11 +253,11 @@ class ValidValuesIterable {
|
|
|
252
253
|
stepValue = this.props.minStep;
|
|
253
254
|
}
|
|
254
255
|
}
|
|
255
|
-
else if (
|
|
256
|
-
max =
|
|
256
|
+
else if (isUnsignedNumericFormat(this.props.format)) {
|
|
257
|
+
max = numericUpperBound(this.props.format);
|
|
257
258
|
}
|
|
258
259
|
else {
|
|
259
|
-
throw new Error(
|
|
260
|
+
throw new Error(`Could not find valid iterator strategy for props: ${JSON.stringify(this.props)}`);
|
|
260
261
|
}
|
|
261
262
|
for (let i = min; i <= max; i += stepValue) {
|
|
262
263
|
yield i;
|
|
@@ -298,8 +299,8 @@ function minWithUndefined(a, b) {
|
|
|
298
299
|
*
|
|
299
300
|
* @group Characteristic
|
|
300
301
|
*/
|
|
301
|
-
// eslint-disable-next-line
|
|
302
|
-
class Characteristic extends
|
|
302
|
+
// eslint-disable-next-line ts/no-unsafe-declaration-merging
|
|
303
|
+
export class Characteristic extends EventEmitter {
|
|
303
304
|
// Pattern below is for automatic detection of the section of defined characteristics. Used by the generator
|
|
304
305
|
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
305
306
|
/**
|
|
@@ -1332,7 +1333,7 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1332
1333
|
/**
|
|
1333
1334
|
* @private
|
|
1334
1335
|
*/
|
|
1335
|
-
statusCode =
|
|
1336
|
+
statusCode = HAPStatus.SUCCESS;
|
|
1336
1337
|
props;
|
|
1337
1338
|
/**
|
|
1338
1339
|
* The {@link Characteristic.onGet} handler
|
|
@@ -1371,8 +1372,8 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1371
1372
|
* @param handler
|
|
1372
1373
|
*/
|
|
1373
1374
|
onGet(handler) {
|
|
1374
|
-
if (typeof handler !==
|
|
1375
|
-
this.characteristicWarning(
|
|
1375
|
+
if (typeof handler !== 'function') {
|
|
1376
|
+
this.characteristicWarning('.onGet handler must be a function');
|
|
1376
1377
|
return this;
|
|
1377
1378
|
}
|
|
1378
1379
|
this.getHandler = handler;
|
|
@@ -1400,8 +1401,8 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1400
1401
|
* @param handler
|
|
1401
1402
|
*/
|
|
1402
1403
|
onSet(handler) {
|
|
1403
|
-
if (typeof handler !==
|
|
1404
|
-
this.characteristicWarning(
|
|
1404
|
+
if (typeof handler !== 'function') {
|
|
1405
|
+
this.characteristicWarning('.onSet handler must be a function');
|
|
1405
1406
|
return this;
|
|
1406
1407
|
}
|
|
1407
1408
|
this.setHandler = handler;
|
|
@@ -1422,7 +1423,7 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1422
1423
|
* @param props - Partial properties object with the desired updates.
|
|
1423
1424
|
*/
|
|
1424
1425
|
setProps(props) {
|
|
1425
|
-
(
|
|
1426
|
+
assert(props, 'props cannot be undefined when setting props');
|
|
1426
1427
|
let formatDidChange = false;
|
|
1427
1428
|
// for every value "null" can be used to reset props, except for required props
|
|
1428
1429
|
if (props.format) {
|
|
@@ -1443,37 +1444,37 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1443
1444
|
* out of {@link setupAdditionalAuthorization}. An empty array fails {@link isValidPerms} exactly as the declared value does, so the accessory is
|
|
1444
1445
|
* still quarantined - it just fails closed on the way there. The warning above quotes what the plugin actually declared.
|
|
1445
1446
|
*/
|
|
1446
|
-
this.props.perms = Array.isArray(props.perms)
|
|
1447
|
+
this.props.perms = Array.isArray(props.perms)
|
|
1448
|
+
? props.perms
|
|
1449
|
+
: [];
|
|
1447
1450
|
}
|
|
1448
1451
|
if (props.unit !== undefined) {
|
|
1449
|
-
this.props.unit = props.unit
|
|
1452
|
+
this.props.unit = props.unit ?? undefined;
|
|
1450
1453
|
}
|
|
1451
1454
|
if (props.description !== undefined) {
|
|
1452
|
-
this.props.description = props.description
|
|
1455
|
+
this.props.description = props.description ?? undefined;
|
|
1453
1456
|
}
|
|
1454
1457
|
// check minValue is valid for the format type
|
|
1455
1458
|
if (props.minValue !== undefined) {
|
|
1456
1459
|
if (props.minValue === null) {
|
|
1457
1460
|
props.minValue = undefined;
|
|
1458
1461
|
}
|
|
1459
|
-
else if (!
|
|
1460
|
-
this.characteristicWarning(
|
|
1462
|
+
else if (!isNumericFormat(this.props.format)) {
|
|
1463
|
+
this.characteristicWarning(`Characteristic Property 'minValue' can only be set for characteristics with numeric format, but not for ${this.props.format}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1461
1464
|
props.minValue = undefined;
|
|
1462
1465
|
}
|
|
1463
|
-
else if (typeof props.minValue !==
|
|
1466
|
+
else if (typeof props.minValue !== 'number' || !Number.isFinite(props.minValue)) {
|
|
1464
1467
|
this.characteristicWarning(`Characteristic Property 'minValue' must be a finite number, received "${props.minValue}" (${typeof props.minValue})`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1465
1468
|
props.minValue = undefined;
|
|
1466
1469
|
}
|
|
1467
1470
|
else {
|
|
1468
|
-
if (props.minValue <
|
|
1469
|
-
this.characteristicWarning(
|
|
1470
|
-
|
|
1471
|
-
props.minValue = (0, request_util_1.numericLowerBound)(this.props.format);
|
|
1471
|
+
if (props.minValue < numericLowerBound(this.props.format)) {
|
|
1472
|
+
this.characteristicWarning(`Characteristic Property 'minValue' was set to ${props.minValue}, but for numeric format ${this.props.format} minimum possible is ${numericLowerBound(this.props.format)}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1473
|
+
props.minValue = numericLowerBound(this.props.format);
|
|
1472
1474
|
}
|
|
1473
|
-
else if (props.minValue >
|
|
1474
|
-
this.characteristicWarning(
|
|
1475
|
-
|
|
1476
|
-
props.minValue = (0, request_util_1.numericLowerBound)(this.props.format);
|
|
1475
|
+
else if (props.minValue > numericUpperBound(this.props.format)) {
|
|
1476
|
+
this.characteristicWarning(`Characteristic Property 'minValue' was set to ${props.minValue}, but for numeric format ${this.props.format} maximum possible is ${numericUpperBound(this.props.format)}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1477
|
+
props.minValue = numericLowerBound(this.props.format);
|
|
1477
1478
|
}
|
|
1478
1479
|
}
|
|
1479
1480
|
this.props.minValue = props.minValue;
|
|
@@ -1483,24 +1484,22 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1483
1484
|
if (props.maxValue === null) {
|
|
1484
1485
|
props.maxValue = undefined;
|
|
1485
1486
|
}
|
|
1486
|
-
else if (!
|
|
1487
|
-
this.characteristicWarning(
|
|
1487
|
+
else if (!isNumericFormat(this.props.format)) {
|
|
1488
|
+
this.characteristicWarning(`Characteristic Property 'maxValue' can only be set for characteristics with numeric format, but not for ${this.props.format}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1488
1489
|
props.maxValue = undefined;
|
|
1489
1490
|
}
|
|
1490
|
-
else if (typeof props.maxValue !==
|
|
1491
|
+
else if (typeof props.maxValue !== 'number' || !Number.isFinite(props.maxValue)) {
|
|
1491
1492
|
this.characteristicWarning(`Characteristic Property 'maxValue' must be a finite number, received "${props.maxValue}" (${typeof props.maxValue})`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1492
1493
|
props.maxValue = undefined;
|
|
1493
1494
|
}
|
|
1494
1495
|
else {
|
|
1495
|
-
if (props.maxValue >
|
|
1496
|
-
this.characteristicWarning(
|
|
1497
|
-
|
|
1498
|
-
props.maxValue = (0, request_util_1.numericUpperBound)(this.props.format);
|
|
1496
|
+
if (props.maxValue > numericUpperBound(this.props.format)) {
|
|
1497
|
+
this.characteristicWarning(`Characteristic Property 'maxValue' was set to ${props.maxValue}, but for numeric format ${this.props.format} maximum possible is ${numericUpperBound(this.props.format)}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1498
|
+
props.maxValue = numericUpperBound(this.props.format);
|
|
1499
1499
|
}
|
|
1500
|
-
else if (props.maxValue <
|
|
1501
|
-
this.characteristicWarning(
|
|
1502
|
-
|
|
1503
|
-
props.maxValue = (0, request_util_1.numericUpperBound)(this.props.format);
|
|
1500
|
+
else if (props.maxValue < numericLowerBound(this.props.format)) {
|
|
1501
|
+
this.characteristicWarning(`Characteristic Property 'maxValue' was set to ${props.maxValue}, but for numeric format ${this.props.format} minimum possible is ${numericLowerBound(this.props.format)}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1502
|
+
props.maxValue = numericUpperBound(this.props.format);
|
|
1504
1503
|
}
|
|
1505
1504
|
}
|
|
1506
1505
|
this.props.maxValue = props.maxValue;
|
|
@@ -1509,13 +1508,13 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1509
1508
|
if (props.minStep === null) {
|
|
1510
1509
|
this.props.minStep = undefined;
|
|
1511
1510
|
}
|
|
1512
|
-
else if (!
|
|
1513
|
-
this.characteristicWarning(
|
|
1511
|
+
else if (!isNumericFormat(this.props.format)) {
|
|
1512
|
+
this.characteristicWarning(`Characteristic Property \`minStep\` can only be set for characteristics with numeric format, but not for ${this.props.format}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1514
1513
|
}
|
|
1515
1514
|
else {
|
|
1516
|
-
if (props.minStep < 1 &&
|
|
1517
|
-
this.characteristicWarning(
|
|
1518
|
-
|
|
1515
|
+
if (props.minStep < 1 && isIntegerNumericFormat(this.props.format)) {
|
|
1516
|
+
this.characteristicWarning('Characteristic Property `minStep` was set to a value lower than 1, '
|
|
1517
|
+
+ `this will have no effect on format \`${this.props.format}`);
|
|
1519
1518
|
}
|
|
1520
1519
|
this.props.minStep = props.minStep;
|
|
1521
1520
|
}
|
|
@@ -1525,11 +1524,11 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1525
1524
|
this.props.maxLen = undefined;
|
|
1526
1525
|
}
|
|
1527
1526
|
else if (this.props.format !== "string" /* Formats.STRING */) {
|
|
1528
|
-
this.characteristicWarning(
|
|
1527
|
+
this.characteristicWarning(`Characteristic Property \`maxLen\` can only be set for characteristics with format \`STRING\`, but not for ${this.props.format}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1529
1528
|
}
|
|
1530
1529
|
else {
|
|
1531
1530
|
if (props.maxLen > 256) {
|
|
1532
|
-
this.characteristicWarning(
|
|
1531
|
+
this.characteristicWarning('Characteristic Property string `maxLen` cannot be bigger than 256');
|
|
1533
1532
|
props.maxLen = 256;
|
|
1534
1533
|
}
|
|
1535
1534
|
this.props.maxLen = props.maxLen;
|
|
@@ -1540,7 +1539,7 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1540
1539
|
this.props.maxDataLen = undefined;
|
|
1541
1540
|
}
|
|
1542
1541
|
else if (this.props.format !== "data" /* Formats.DATA */) {
|
|
1543
|
-
this.characteristicWarning(
|
|
1542
|
+
this.characteristicWarning(`Characteristic Property \`maxDataLen\` can only be set for characteristics with format \`DATA\`, but not for ${this.props.format}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
1544
1543
|
}
|
|
1545
1544
|
else {
|
|
1546
1545
|
this.props.maxDataLen = props.maxDataLen;
|
|
@@ -1550,11 +1549,11 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1550
1549
|
if (props.validValues === null) {
|
|
1551
1550
|
this.props.validValues = undefined;
|
|
1552
1551
|
}
|
|
1553
|
-
else if (!
|
|
1554
|
-
this.characteristicWarning(
|
|
1552
|
+
else if (!isNumericFormat(this.props.format)) {
|
|
1553
|
+
this.characteristicWarning(`Characteristic Property \`validValues\` was supplied for non numeric format ${this.props.format}`);
|
|
1555
1554
|
}
|
|
1556
1555
|
else {
|
|
1557
|
-
(
|
|
1556
|
+
assert(props.validValues.length, 'characteristic prop validValues cannot be empty array');
|
|
1558
1557
|
this.props.validValues = props.validValues;
|
|
1559
1558
|
}
|
|
1560
1559
|
}
|
|
@@ -1562,31 +1561,31 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1562
1561
|
if (props.validValueRanges === null) {
|
|
1563
1562
|
this.props.validValueRanges = undefined;
|
|
1564
1563
|
}
|
|
1565
|
-
else if (!
|
|
1566
|
-
this.characteristicWarning(
|
|
1564
|
+
else if (!isNumericFormat(this.props.format)) {
|
|
1565
|
+
this.characteristicWarning(`Characteristic Property \`validValueRanges\` was supplied for non numeric format ${this.props.format}`);
|
|
1567
1566
|
}
|
|
1568
1567
|
else {
|
|
1569
|
-
(
|
|
1568
|
+
assert(props.validValueRanges.length === 2, 'characteristic prop validValueRanges must have a length of 2');
|
|
1570
1569
|
this.props.validValueRanges = props.validValueRanges;
|
|
1571
1570
|
}
|
|
1572
1571
|
}
|
|
1573
1572
|
if (props.adminOnlyAccess !== undefined) {
|
|
1574
|
-
this.props.adminOnlyAccess = props.adminOnlyAccess
|
|
1573
|
+
this.props.adminOnlyAccess = props.adminOnlyAccess ?? undefined;
|
|
1575
1574
|
}
|
|
1576
1575
|
if (this.props.minValue != null && this.props.maxValue != null) { // the eqeq instead of eqeqeq is important here
|
|
1577
1576
|
if (this.props.minValue > this.props.maxValue) { // see https://github.com/homebridge/HAP-NodeJS/issues/690
|
|
1578
1577
|
this.props.minValue = undefined;
|
|
1579
1578
|
this.props.maxValue = undefined;
|
|
1580
|
-
throw new Error(
|
|
1579
|
+
throw new Error(`Error setting CharacteristicsProps for '${this.displayName}': 'minValue' cannot be greater or equal the 'maxValue'!`);
|
|
1581
1580
|
}
|
|
1582
1581
|
}
|
|
1583
1582
|
// the properties are part of the configuration HomeKit is served, so a published accessory must recompute its configuration number;
|
|
1584
1583
|
// a call that changes nothing leaves the configuration hash and therefore the number as it was
|
|
1585
1584
|
this.emit("characteristic-configurationChange" /* CharacteristicEventTypes.CHARACTERISTIC_CONFIGURATION_CHANGE */);
|
|
1586
|
-
if ((
|
|
1585
|
+
if ((isNumericFormat(this.props.format) || this.props.format === "string" /* Formats.STRING */)
|
|
1587
1586
|
&& this.value != null
|
|
1588
1587
|
&& !formatDidChange
|
|
1589
|
-
&& this.statusCode ===
|
|
1588
|
+
&& this.statusCode === HAPStatus.SUCCESS
|
|
1590
1589
|
&& this.UUID !== Characteristic.ProgrammableSwitchEvent.UUID) {
|
|
1591
1590
|
// explaining the if statement above:
|
|
1592
1591
|
// - We only do a check for numeric and string formats as they are the only ones affected by characteristic property restrictions.
|
|
@@ -1608,11 +1607,11 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1608
1607
|
*
|
|
1609
1608
|
* The range of valid values can be defined using three different ways via the {@link CharacteristicProps} object
|
|
1610
1609
|
* (set via the {@link setProps} method):
|
|
1611
|
-
*
|
|
1612
|
-
*
|
|
1610
|
+
* First method is to specifically list every valid value inside {@link CharacteristicProps.validValues}
|
|
1611
|
+
* Second you can specify a range via {@link CharacteristicProps.minValue} and {@link CharacteristicProps.maxValue} (with optionally defining
|
|
1613
1612
|
* {@link CharacteristicProps.minStep})
|
|
1614
|
-
*
|
|
1615
|
-
*
|
|
1613
|
+
* And lastly you can specify a range via {@link CharacteristicProps.validValueRanges}
|
|
1614
|
+
* Implicitly a valid value range is predefined for characteristics with Format {@link Formats.UINT8}, {@link Formats.UINT16},
|
|
1616
1615
|
* {@link Formats.UINT32} and {@link Formats.UINT64}: starting by zero to their respective maximum number
|
|
1617
1616
|
*
|
|
1618
1617
|
* The method will automatically detect which type of valid values definition is used and provide
|
|
@@ -1659,13 +1658,15 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1659
1658
|
}
|
|
1660
1659
|
setValue(value, callback, context) {
|
|
1661
1660
|
if (value instanceof Error) {
|
|
1662
|
-
this.statusCode = value instanceof
|
|
1661
|
+
this.statusCode = value instanceof HapStatusError
|
|
1662
|
+
? value.hapStatus
|
|
1663
|
+
: extractHAPStatusFromError(value);
|
|
1663
1664
|
if (callback) {
|
|
1664
1665
|
callback();
|
|
1665
1666
|
}
|
|
1666
1667
|
return this;
|
|
1667
1668
|
}
|
|
1668
|
-
if (callback && !context && typeof callback !==
|
|
1669
|
+
if (callback && !context && typeof callback !== 'function') {
|
|
1669
1670
|
context = callback;
|
|
1670
1671
|
callback = undefined;
|
|
1671
1672
|
}
|
|
@@ -1673,13 +1674,13 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1673
1674
|
value = this.validateUserInput(value);
|
|
1674
1675
|
}
|
|
1675
1676
|
catch (error) {
|
|
1676
|
-
this.characteristicWarning(error?.message ||
|
|
1677
|
+
this.characteristicWarning(error?.message || 'Unknown error', "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
|
|
1677
1678
|
if (callback) {
|
|
1678
1679
|
callback(error);
|
|
1679
1680
|
}
|
|
1680
1681
|
return this;
|
|
1681
1682
|
}
|
|
1682
|
-
this.handleSetRequest(value, undefined, context).then(value => {
|
|
1683
|
+
this.handleSetRequest(value, undefined, context).then((value) => {
|
|
1683
1684
|
if (callback) {
|
|
1684
1685
|
if (value) { // possible write response
|
|
1685
1686
|
callback(null, value);
|
|
@@ -1688,7 +1689,7 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1688
1689
|
callback(null);
|
|
1689
1690
|
}
|
|
1690
1691
|
}
|
|
1691
|
-
}, reason => {
|
|
1692
|
+
}, (reason) => {
|
|
1692
1693
|
if (callback) {
|
|
1693
1694
|
callback(reason);
|
|
1694
1695
|
}
|
|
@@ -1697,13 +1698,15 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1697
1698
|
}
|
|
1698
1699
|
updateValue(value, callback, context) {
|
|
1699
1700
|
if (value instanceof Error) {
|
|
1700
|
-
this.statusCode = value instanceof
|
|
1701
|
+
this.statusCode = value instanceof HapStatusError
|
|
1702
|
+
? value.hapStatus
|
|
1703
|
+
: extractHAPStatusFromError(value);
|
|
1701
1704
|
if (callback) {
|
|
1702
1705
|
callback();
|
|
1703
1706
|
}
|
|
1704
1707
|
return this;
|
|
1705
1708
|
}
|
|
1706
|
-
if (callback && !context && typeof callback !==
|
|
1709
|
+
if (callback && !context && typeof callback !== 'function') {
|
|
1707
1710
|
context = callback;
|
|
1708
1711
|
callback = undefined;
|
|
1709
1712
|
}
|
|
@@ -1711,19 +1714,25 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1711
1714
|
value = this.validateUserInput(value);
|
|
1712
1715
|
}
|
|
1713
1716
|
catch (error) {
|
|
1714
|
-
this.characteristicWarning(error?.message ||
|
|
1717
|
+
this.characteristicWarning(error?.message || 'Unknown error', "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
|
|
1715
1718
|
if (callback) {
|
|
1716
1719
|
callback();
|
|
1717
1720
|
}
|
|
1718
1721
|
return this;
|
|
1719
1722
|
}
|
|
1720
|
-
this.statusCode =
|
|
1723
|
+
this.statusCode = HAPStatus.SUCCESS;
|
|
1721
1724
|
const oldValue = this.value;
|
|
1722
1725
|
this.value = value;
|
|
1723
1726
|
if (callback) {
|
|
1724
1727
|
callback();
|
|
1725
1728
|
}
|
|
1726
|
-
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1729
|
+
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1730
|
+
originator: undefined,
|
|
1731
|
+
oldValue,
|
|
1732
|
+
newValue: value,
|
|
1733
|
+
reason: "update" /* ChangeReason.UPDATE */,
|
|
1734
|
+
context,
|
|
1735
|
+
});
|
|
1727
1736
|
return this; // for chaining
|
|
1728
1737
|
}
|
|
1729
1738
|
/**
|
|
@@ -1736,11 +1745,17 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1736
1745
|
* @param context - Passed to the {@link CharacteristicEventTypes.CHANGE} event handler.
|
|
1737
1746
|
*/
|
|
1738
1747
|
sendEventNotification(value, context) {
|
|
1739
|
-
this.statusCode =
|
|
1748
|
+
this.statusCode = HAPStatus.SUCCESS;
|
|
1740
1749
|
value = this.validateUserInput(value);
|
|
1741
1750
|
const oldValue = this.value;
|
|
1742
1751
|
this.value = value;
|
|
1743
|
-
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1752
|
+
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1753
|
+
originator: undefined,
|
|
1754
|
+
oldValue,
|
|
1755
|
+
newValue: value,
|
|
1756
|
+
reason: "event" /* ChangeReason.EVENT */,
|
|
1757
|
+
context,
|
|
1758
|
+
});
|
|
1744
1759
|
return this; // for chaining
|
|
1745
1760
|
}
|
|
1746
1761
|
/**
|
|
@@ -1748,11 +1763,11 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1748
1763
|
*
|
|
1749
1764
|
* @param connection - The HAP connection from which the request originated from.
|
|
1750
1765
|
* @param context - Deprecated parameter. There for backwards compatibility.
|
|
1751
|
-
* @private
|
|
1766
|
+
* @private
|
|
1752
1767
|
*/
|
|
1753
1768
|
async handleGetRequest(connection, context) {
|
|
1754
1769
|
if (!this.props.perms.includes("pr" /* Perms.PAIRED_READ */)) { // check if we are allowed to read from this characteristic
|
|
1755
|
-
throw
|
|
1770
|
+
throw HAPStatus.WRITE_ONLY_CHARACTERISTIC;
|
|
1756
1771
|
}
|
|
1757
1772
|
if (this.UUID === Characteristic.ProgrammableSwitchEvent.UUID) {
|
|
1758
1773
|
// special workaround for event only programmable switch event, which must always return null
|
|
@@ -1760,37 +1775,43 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1760
1775
|
}
|
|
1761
1776
|
if (this.getHandler) {
|
|
1762
1777
|
if (this.listeners("get" /* CharacteristicEventTypes.GET */).length > 0) {
|
|
1763
|
-
this.characteristicWarning(
|
|
1778
|
+
this.characteristicWarning('Ignoring on(\'get\') handler as onGet handler was defined instead');
|
|
1764
1779
|
}
|
|
1765
1780
|
try {
|
|
1766
1781
|
let value = await this.getHandler(context, connection);
|
|
1767
|
-
this.statusCode =
|
|
1782
|
+
this.statusCode = HAPStatus.SUCCESS;
|
|
1768
1783
|
try {
|
|
1769
1784
|
value = this.validateUserInput(value);
|
|
1770
1785
|
}
|
|
1771
1786
|
catch (error) {
|
|
1772
1787
|
this.characteristicWarning(`An illegal value was supplied by the read handler for characteristic: ${error?.message}`, "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */, error?.stack);
|
|
1773
|
-
this.statusCode =
|
|
1774
|
-
return Promise.reject(
|
|
1788
|
+
this.statusCode = HAPStatus.SERVICE_COMMUNICATION_FAILURE;
|
|
1789
|
+
return Promise.reject(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
1775
1790
|
}
|
|
1776
1791
|
const oldValue = this.value;
|
|
1777
1792
|
this.value = value;
|
|
1778
1793
|
if (oldValue !== value) { // emit a change event if necessary
|
|
1779
|
-
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1794
|
+
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1795
|
+
originator: connection,
|
|
1796
|
+
oldValue,
|
|
1797
|
+
newValue: value,
|
|
1798
|
+
reason: "read" /* ChangeReason.READ */,
|
|
1799
|
+
context,
|
|
1800
|
+
});
|
|
1780
1801
|
}
|
|
1781
1802
|
return value;
|
|
1782
1803
|
}
|
|
1783
1804
|
catch (error) {
|
|
1784
|
-
if (typeof error ===
|
|
1785
|
-
const hapStatusError = new
|
|
1805
|
+
if (typeof error === 'number') {
|
|
1806
|
+
const hapStatusError = new HapStatusError(error);
|
|
1786
1807
|
this.statusCode = hapStatusError.hapStatus;
|
|
1787
1808
|
}
|
|
1788
|
-
else if (error instanceof
|
|
1809
|
+
else if (error instanceof HapStatusError) {
|
|
1789
1810
|
this.statusCode = error.hapStatus;
|
|
1790
1811
|
}
|
|
1791
1812
|
else {
|
|
1792
1813
|
this.characteristicWarning(`Unhandled error thrown inside read handler for characteristic: ${error?.message}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
|
|
1793
|
-
this.statusCode =
|
|
1814
|
+
this.statusCode = HAPStatus.SERVICE_COMMUNICATION_FAILURE;
|
|
1794
1815
|
}
|
|
1795
1816
|
throw this.statusCode;
|
|
1796
1817
|
}
|
|
@@ -1804,50 +1825,56 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1804
1825
|
}
|
|
1805
1826
|
catch (error) {
|
|
1806
1827
|
this.characteristicWarning(`An illegal value was supplied by setting \`value\` for characteristic: ${error?.message}`, "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */, error?.stack);
|
|
1807
|
-
return Promise.reject(
|
|
1828
|
+
return Promise.reject(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
1808
1829
|
}
|
|
1809
1830
|
}
|
|
1810
1831
|
return new Promise((resolve, reject) => {
|
|
1811
1832
|
try {
|
|
1812
|
-
this.emit("get" /* CharacteristicEventTypes.GET */,
|
|
1833
|
+
this.emit("get" /* CharacteristicEventTypes.GET */, once((status, value) => {
|
|
1813
1834
|
if (status) {
|
|
1814
|
-
if (typeof status ===
|
|
1815
|
-
const hapStatusError = new
|
|
1835
|
+
if (typeof status === 'number') {
|
|
1836
|
+
const hapStatusError = new HapStatusError(status);
|
|
1816
1837
|
this.statusCode = hapStatusError.hapStatus;
|
|
1817
1838
|
}
|
|
1818
|
-
else if (status instanceof
|
|
1839
|
+
else if (status instanceof HapStatusError) {
|
|
1819
1840
|
this.statusCode = status.hapStatus;
|
|
1820
1841
|
}
|
|
1821
1842
|
else {
|
|
1822
|
-
debug(
|
|
1823
|
-
this.statusCode =
|
|
1843
|
+
debug('[%s] Received error from get handler %s', this.displayName, status.stack);
|
|
1844
|
+
this.statusCode = extractHAPStatusFromError(status);
|
|
1824
1845
|
}
|
|
1825
1846
|
reject(this.statusCode);
|
|
1826
1847
|
return;
|
|
1827
1848
|
}
|
|
1828
|
-
this.statusCode =
|
|
1849
|
+
this.statusCode = HAPStatus.SUCCESS;
|
|
1829
1850
|
try {
|
|
1830
1851
|
value = this.validateUserInput(value);
|
|
1831
1852
|
}
|
|
1832
1853
|
catch (error) {
|
|
1833
1854
|
// this runs inside the plugin's callback; a throw would land in the plugin and leave the request unanswered
|
|
1834
1855
|
this.characteristicWarning(`An illegal value was supplied by the read handler for characteristic: ${error?.message}`, "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */, error?.stack);
|
|
1835
|
-
this.statusCode =
|
|
1836
|
-
reject(
|
|
1856
|
+
this.statusCode = HAPStatus.SERVICE_COMMUNICATION_FAILURE;
|
|
1857
|
+
reject(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
1837
1858
|
return;
|
|
1838
1859
|
}
|
|
1839
1860
|
const oldValue = this.value;
|
|
1840
1861
|
this.value = value;
|
|
1841
1862
|
resolve(value);
|
|
1842
1863
|
if (oldValue !== value) { // emit a change event if necessary
|
|
1843
|
-
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1864
|
+
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1865
|
+
originator: connection,
|
|
1866
|
+
oldValue,
|
|
1867
|
+
newValue: value,
|
|
1868
|
+
reason: "read" /* ChangeReason.READ */,
|
|
1869
|
+
context,
|
|
1870
|
+
});
|
|
1844
1871
|
}
|
|
1845
1872
|
}), context, connection);
|
|
1846
1873
|
}
|
|
1847
1874
|
catch (error) {
|
|
1848
1875
|
this.characteristicWarning(`Unhandled error thrown inside read handler for characteristic: ${error?.message}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
|
|
1849
|
-
this.statusCode =
|
|
1850
|
-
reject(
|
|
1876
|
+
this.statusCode = HAPStatus.SERVICE_COMMUNICATION_FAILURE;
|
|
1877
|
+
reject(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
1851
1878
|
}
|
|
1852
1879
|
});
|
|
1853
1880
|
}
|
|
@@ -1858,12 +1885,12 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1858
1885
|
* @param connection - The connection from which the request originated from
|
|
1859
1886
|
* @param context - Deprecated parameter. There for backwards compatibility.
|
|
1860
1887
|
* @returns Promise resolve to void in normal operation. When characteristic supports write-response, HAP
|
|
1861
|
-
*
|
|
1862
|
-
*
|
|
1888
|
+
* requests a write-response and the set handler returns a write-response value, the respective
|
|
1889
|
+
* write response value is resolved.
|
|
1863
1890
|
* @private
|
|
1864
1891
|
*/
|
|
1865
1892
|
async handleSetRequest(value, connection, context) {
|
|
1866
|
-
this.statusCode =
|
|
1893
|
+
this.statusCode = HAPStatus.SUCCESS;
|
|
1867
1894
|
if (connection !== undefined) {
|
|
1868
1895
|
// if connection is undefined, the set "request" comes from the setValue method.
|
|
1869
1896
|
// for setValue a value of "null" is allowed and checked via validateUserInput.
|
|
@@ -1872,70 +1899,82 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1872
1899
|
}
|
|
1873
1900
|
catch (e) {
|
|
1874
1901
|
debug(`[${this.displayName}]`, e.message);
|
|
1875
|
-
return Promise.reject(
|
|
1902
|
+
return Promise.reject(HAPStatus.INVALID_VALUE_IN_REQUEST);
|
|
1876
1903
|
}
|
|
1877
1904
|
}
|
|
1878
1905
|
const oldValue = this.value;
|
|
1879
1906
|
if (this.setHandler) {
|
|
1880
1907
|
if (this.listeners("set" /* CharacteristicEventTypes.SET */).length > 0) {
|
|
1881
|
-
this.characteristicWarning(
|
|
1908
|
+
this.characteristicWarning('Ignoring on(\'set\') handler as onSet handler was defined instead');
|
|
1882
1909
|
}
|
|
1883
1910
|
try {
|
|
1884
1911
|
const writeResponse = await this.setHandler(value, context, connection);
|
|
1885
|
-
this.statusCode =
|
|
1912
|
+
this.statusCode = HAPStatus.SUCCESS;
|
|
1886
1913
|
if (writeResponse != null && this.props.perms.includes("wr" /* Perms.WRITE_RESPONSE */)) {
|
|
1887
1914
|
this.value = this.validateUserInput(writeResponse);
|
|
1888
1915
|
return this.value;
|
|
1889
1916
|
}
|
|
1890
1917
|
else {
|
|
1891
1918
|
if (writeResponse != null) {
|
|
1892
|
-
this.characteristicWarning(
|
|
1919
|
+
this.characteristicWarning('SET handler returned write response value, though the characteristic doesn\'t support write response', "debug-message" /* CharacteristicWarningType.DEBUG_MESSAGE */);
|
|
1893
1920
|
}
|
|
1894
1921
|
this.value = value;
|
|
1895
|
-
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1922
|
+
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1923
|
+
originator: connection,
|
|
1924
|
+
oldValue,
|
|
1925
|
+
newValue: value,
|
|
1926
|
+
reason: "write" /* ChangeReason.WRITE */,
|
|
1927
|
+
context,
|
|
1928
|
+
});
|
|
1896
1929
|
return;
|
|
1897
1930
|
}
|
|
1898
1931
|
}
|
|
1899
1932
|
catch (error) {
|
|
1900
|
-
if (typeof error ===
|
|
1901
|
-
const hapStatusError = new
|
|
1933
|
+
if (typeof error === 'number') {
|
|
1934
|
+
const hapStatusError = new HapStatusError(error);
|
|
1902
1935
|
this.statusCode = hapStatusError.hapStatus;
|
|
1903
1936
|
}
|
|
1904
|
-
else if (error instanceof
|
|
1937
|
+
else if (error instanceof HapStatusError) {
|
|
1905
1938
|
this.statusCode = error.hapStatus;
|
|
1906
1939
|
}
|
|
1907
1940
|
else {
|
|
1908
1941
|
this.characteristicWarning(`Unhandled error thrown inside write handler for characteristic: ${error?.message}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
|
|
1909
|
-
this.statusCode =
|
|
1942
|
+
this.statusCode = HAPStatus.SERVICE_COMMUNICATION_FAILURE;
|
|
1910
1943
|
}
|
|
1911
1944
|
throw this.statusCode;
|
|
1912
1945
|
}
|
|
1913
1946
|
}
|
|
1914
1947
|
if (this.listeners("set" /* CharacteristicEventTypes.SET */).length === 0) {
|
|
1915
1948
|
this.value = value;
|
|
1916
|
-
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1949
|
+
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1950
|
+
originator: connection,
|
|
1951
|
+
oldValue,
|
|
1952
|
+
newValue: value,
|
|
1953
|
+
reason: "write" /* ChangeReason.WRITE */,
|
|
1954
|
+
context,
|
|
1955
|
+
});
|
|
1917
1956
|
return Promise.resolve();
|
|
1918
1957
|
}
|
|
1919
1958
|
else {
|
|
1920
1959
|
return new Promise((resolve, reject) => {
|
|
1921
1960
|
try {
|
|
1922
|
-
this.emit("set" /* CharacteristicEventTypes.SET */, value,
|
|
1961
|
+
this.emit("set" /* CharacteristicEventTypes.SET */, value, once((status, writeResponse) => {
|
|
1923
1962
|
if (status) {
|
|
1924
|
-
if (typeof status ===
|
|
1925
|
-
const hapStatusError = new
|
|
1963
|
+
if (typeof status === 'number') {
|
|
1964
|
+
const hapStatusError = new HapStatusError(status);
|
|
1926
1965
|
this.statusCode = hapStatusError.hapStatus;
|
|
1927
1966
|
}
|
|
1928
|
-
else if (status instanceof
|
|
1967
|
+
else if (status instanceof HapStatusError) {
|
|
1929
1968
|
this.statusCode = status.hapStatus;
|
|
1930
1969
|
}
|
|
1931
1970
|
else {
|
|
1932
|
-
debug(
|
|
1933
|
-
this.statusCode =
|
|
1971
|
+
debug('[%s] Received error from set handler %s', this.displayName, status.stack);
|
|
1972
|
+
this.statusCode = extractHAPStatusFromError(status);
|
|
1934
1973
|
}
|
|
1935
1974
|
reject(this.statusCode);
|
|
1936
1975
|
return;
|
|
1937
1976
|
}
|
|
1938
|
-
this.statusCode =
|
|
1977
|
+
this.statusCode = HAPStatus.SUCCESS;
|
|
1939
1978
|
if (writeResponse != null && this.props.perms.includes("wr" /* Perms.WRITE_RESPONSE */)) {
|
|
1940
1979
|
// support write response simply by letting the implementor pass the response as second argument to the callback
|
|
1941
1980
|
try {
|
|
@@ -1944,26 +1983,32 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1944
1983
|
catch (error) {
|
|
1945
1984
|
// this runs inside the plugin's callback; a throw would land in the plugin and leave the request unanswered
|
|
1946
1985
|
this.characteristicWarning(`An illegal write response value was supplied by the write handler for characteristic: ${error?.message}`, "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */, error?.stack);
|
|
1947
|
-
this.statusCode =
|
|
1948
|
-
reject(
|
|
1986
|
+
this.statusCode = HAPStatus.SERVICE_COMMUNICATION_FAILURE;
|
|
1987
|
+
reject(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
1949
1988
|
return;
|
|
1950
1989
|
}
|
|
1951
1990
|
resolve(this.value);
|
|
1952
1991
|
}
|
|
1953
1992
|
else {
|
|
1954
1993
|
if (writeResponse != null) {
|
|
1955
|
-
this.characteristicWarning(
|
|
1994
|
+
this.characteristicWarning('SET handler returned write response value, though the characteristic doesn\'t support write response', "debug-message" /* CharacteristicWarningType.DEBUG_MESSAGE */);
|
|
1956
1995
|
}
|
|
1957
1996
|
this.value = value;
|
|
1958
1997
|
resolve();
|
|
1959
|
-
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1998
|
+
this.emit("change" /* CharacteristicEventTypes.CHANGE */, {
|
|
1999
|
+
originator: connection,
|
|
2000
|
+
oldValue,
|
|
2001
|
+
newValue: value,
|
|
2002
|
+
reason: "write" /* ChangeReason.WRITE */,
|
|
2003
|
+
context,
|
|
2004
|
+
});
|
|
1960
2005
|
}
|
|
1961
2006
|
}), context, connection);
|
|
1962
2007
|
}
|
|
1963
2008
|
catch (error) {
|
|
1964
2009
|
this.characteristicWarning(`Unhandled error thrown inside write handler for characteristic: ${error?.message}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
|
|
1965
|
-
this.statusCode =
|
|
1966
|
-
reject(
|
|
2010
|
+
this.statusCode = HAPStatus.SERVICE_COMMUNICATION_FAILURE;
|
|
2011
|
+
reject(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
1967
2012
|
}
|
|
1968
2013
|
});
|
|
1969
2014
|
}
|
|
@@ -1999,20 +2044,20 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
1999
2044
|
case "string" /* Formats.STRING */:
|
|
2000
2045
|
switch (this.UUID) {
|
|
2001
2046
|
case Characteristic.Manufacturer.UUID:
|
|
2002
|
-
return
|
|
2047
|
+
return 'Default-Manufacturer';
|
|
2003
2048
|
case Characteristic.Model.UUID:
|
|
2004
|
-
return
|
|
2049
|
+
return 'Default-Model';
|
|
2005
2050
|
case Characteristic.SerialNumber.UUID:
|
|
2006
|
-
return
|
|
2051
|
+
return 'Default-SerialNumber';
|
|
2007
2052
|
case Characteristic.FirmwareRevision.UUID:
|
|
2008
|
-
return
|
|
2053
|
+
return '0.0.0';
|
|
2009
2054
|
default:
|
|
2010
|
-
return
|
|
2055
|
+
return '';
|
|
2011
2056
|
}
|
|
2012
2057
|
case "data" /* Formats.DATA */:
|
|
2013
|
-
return
|
|
2058
|
+
return ''; // who knows!
|
|
2014
2059
|
case "tlv8" /* Formats.TLV8 */:
|
|
2015
|
-
return
|
|
2060
|
+
return ''; // who knows!
|
|
2016
2061
|
case "int" /* Formats.INT */:
|
|
2017
2062
|
case "float" /* Formats.FLOAT */:
|
|
2018
2063
|
case "uint8" /* Formats.UINT8 */:
|
|
@@ -2023,10 +2068,10 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2023
2068
|
case Characteristic.CurrentTemperature.UUID:
|
|
2024
2069
|
return 0; // some existing integrations expect this to be 0 by default
|
|
2025
2070
|
default: {
|
|
2026
|
-
if (this.props.validValues?.length && typeof this.props.validValues[0] ===
|
|
2071
|
+
if (this.props.validValues?.length && typeof this.props.validValues[0] === 'number') {
|
|
2027
2072
|
return this.props.validValues[0];
|
|
2028
2073
|
}
|
|
2029
|
-
if (typeof this.props.minValue ===
|
|
2074
|
+
if (typeof this.props.minValue === 'number' && Number.isFinite(this.props.minValue)) {
|
|
2030
2075
|
return this.props.minValue;
|
|
2031
2076
|
}
|
|
2032
2077
|
return 0;
|
|
@@ -2048,10 +2093,10 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2048
2093
|
}
|
|
2049
2094
|
switch (this.props.format) {
|
|
2050
2095
|
case "bool" /* Formats.BOOL */: {
|
|
2051
|
-
if (typeof value ===
|
|
2096
|
+
if (typeof value === 'boolean') {
|
|
2052
2097
|
return value;
|
|
2053
2098
|
}
|
|
2054
|
-
if (typeof value ===
|
|
2099
|
+
if (typeof value === 'number' && (value === 1 || value === 0)) {
|
|
2055
2100
|
return Boolean(value);
|
|
2056
2101
|
}
|
|
2057
2102
|
throw new Error(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
|
|
@@ -2062,18 +2107,18 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2062
2107
|
case "uint16" /* Formats.UINT16 */:
|
|
2063
2108
|
case "uint32" /* Formats.UINT32 */:
|
|
2064
2109
|
case "uint64" /* Formats.UINT64 */: {
|
|
2065
|
-
if (typeof value ===
|
|
2110
|
+
if (typeof value === 'boolean') {
|
|
2066
2111
|
value = value ? 1 : 0;
|
|
2067
2112
|
}
|
|
2068
|
-
if (typeof value !==
|
|
2069
|
-
throw new
|
|
2113
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
2114
|
+
throw new TypeError(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
|
|
2070
2115
|
}
|
|
2071
|
-
const numericMin = maxWithUndefined(this.props.minValue,
|
|
2072
|
-
const numericMax = minWithUndefined(this.props.maxValue,
|
|
2073
|
-
if (typeof numericMin ===
|
|
2116
|
+
const numericMin = maxWithUndefined(this.props.minValue, numericLowerBound(this.props.format));
|
|
2117
|
+
const numericMax = minWithUndefined(this.props.maxValue, numericUpperBound(this.props.format));
|
|
2118
|
+
if (typeof numericMin === 'number' && value < numericMin) {
|
|
2074
2119
|
throw new Error(`Client supplied value of ${value} is less than the minimum allowed value of ${numericMin}`);
|
|
2075
2120
|
}
|
|
2076
|
-
if (typeof numericMax ===
|
|
2121
|
+
if (typeof numericMax === 'number' && value > numericMax) {
|
|
2077
2122
|
throw new Error(`Client supplied value of ${value} is greater than the maximum allowed value of ${numericMax}`);
|
|
2078
2123
|
}
|
|
2079
2124
|
if (this.props.validValues && !this.props.validValues.includes(value)) {
|
|
@@ -2090,29 +2135,29 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2090
2135
|
return value;
|
|
2091
2136
|
}
|
|
2092
2137
|
case "string" /* Formats.STRING */: {
|
|
2093
|
-
if (typeof value !==
|
|
2094
|
-
throw new
|
|
2138
|
+
if (typeof value !== 'string') {
|
|
2139
|
+
throw new TypeError(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
|
|
2095
2140
|
}
|
|
2096
|
-
const maxLength = this.props.maxLen
|
|
2141
|
+
const maxLength = this.props.maxLen ?? 64; // default is 64; max is 256 which is set in setProps
|
|
2097
2142
|
if (value.length > maxLength) {
|
|
2098
2143
|
throw new Error(`Client supplied value length of ${value.length} exceeds maximum length allowed of ${maxLength}`);
|
|
2099
2144
|
}
|
|
2100
2145
|
return value;
|
|
2101
2146
|
}
|
|
2102
2147
|
case "data" /* Formats.DATA */: {
|
|
2103
|
-
if (typeof value !==
|
|
2104
|
-
throw new
|
|
2148
|
+
if (typeof value !== 'string') {
|
|
2149
|
+
throw new TypeError(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
|
|
2105
2150
|
}
|
|
2106
2151
|
// we don't validate base64 here
|
|
2107
|
-
const maxLength = this.props.maxDataLen
|
|
2152
|
+
const maxLength = this.props.maxDataLen ?? 0x200000; // default is 0x200000
|
|
2108
2153
|
if (value.length > maxLength) {
|
|
2109
2154
|
throw new Error(`Client supplied value length of ${value.length} exceeds maximum length allowed of ${maxLength}`);
|
|
2110
2155
|
}
|
|
2111
2156
|
return value;
|
|
2112
2157
|
}
|
|
2113
2158
|
case "tlv8" /* Formats.TLV8 */:
|
|
2114
|
-
if (typeof value !==
|
|
2115
|
-
throw new
|
|
2159
|
+
if (typeof value !== 'string') {
|
|
2160
|
+
throw new TypeError(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
|
|
2116
2161
|
}
|
|
2117
2162
|
return value;
|
|
2118
2163
|
}
|
|
@@ -2129,7 +2174,7 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2129
2174
|
validateUserInput(value, warningType = "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */) {
|
|
2130
2175
|
if (value === null) {
|
|
2131
2176
|
if (this.UUID === Characteristic.Model.UUID || this.UUID === Characteristic.SerialNumber.UUID) { // mirrors the statement in case: Formats.STRING
|
|
2132
|
-
this.characteristicWarning(
|
|
2177
|
+
this.characteristicWarning('characteristic must have a non null value otherwise HomeKit will reject this accessory, ignoring new value', "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
2133
2178
|
return this.value; // don't change the value
|
|
2134
2179
|
}
|
|
2135
2180
|
if (this.props.format === "data" /* Formats.DATA */ || this.props.format === "tlv8" /* Formats.TLV8 */) {
|
|
@@ -2145,11 +2190,11 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2145
2190
|
* As an intermediate step we kept the behavior but added a warning printed to the console.
|
|
2146
2191
|
* In a future update we will do the breaking change of return null below!
|
|
2147
2192
|
*/
|
|
2148
|
-
if (this.UUID.endsWith(
|
|
2193
|
+
if (this.UUID.endsWith(BASE_UUID)) { // we have an apple defined characteristic (at least assuming nobody else uses the UUID namespace)
|
|
2149
2194
|
if (this.UUID === Characteristic.ProgrammableSwitchEvent.UUID) {
|
|
2150
2195
|
return value; // null is allowed as a value for ProgrammableSwitchEvent
|
|
2151
2196
|
}
|
|
2152
|
-
this.characteristicWarning(
|
|
2197
|
+
this.characteristicWarning('characteristic was supplied illegal value: null! Home App will reject null for Apple defined characteristics', warningType);
|
|
2153
2198
|
// if the value has been set previously, return it now, otherwise continue with validation to have a default value set.
|
|
2154
2199
|
if (this.value !== null) {
|
|
2155
2200
|
return this.value;
|
|
@@ -2162,16 +2207,16 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2162
2207
|
}
|
|
2163
2208
|
switch (this.props.format) {
|
|
2164
2209
|
case "bool" /* Formats.BOOL */: {
|
|
2165
|
-
if (typeof value ===
|
|
2210
|
+
if (typeof value === 'boolean') {
|
|
2166
2211
|
return value;
|
|
2167
2212
|
}
|
|
2168
|
-
if (typeof value ===
|
|
2213
|
+
if (typeof value === 'number') {
|
|
2169
2214
|
return value === 1;
|
|
2170
2215
|
}
|
|
2171
|
-
if (typeof value ===
|
|
2172
|
-
return value ===
|
|
2216
|
+
if (typeof value === 'string') {
|
|
2217
|
+
return value === '1' || value === 'true';
|
|
2173
2218
|
}
|
|
2174
|
-
this.characteristicWarning(
|
|
2219
|
+
this.characteristicWarning(`characteristic value expected boolean and received ${typeof value}`, warningType);
|
|
2175
2220
|
return false;
|
|
2176
2221
|
}
|
|
2177
2222
|
case "int" /* Formats.INT */:
|
|
@@ -2180,18 +2225,22 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2180
2225
|
case "uint16" /* Formats.UINT16 */:
|
|
2181
2226
|
case "uint32" /* Formats.UINT32 */:
|
|
2182
2227
|
case "uint64" /* Formats.UINT64 */: {
|
|
2183
|
-
if (typeof value ===
|
|
2228
|
+
if (typeof value === 'boolean') {
|
|
2184
2229
|
value = value ? 1 : 0;
|
|
2185
2230
|
}
|
|
2186
|
-
if (typeof value ===
|
|
2187
|
-
value = this.props.format === "float" /* Formats.FLOAT */
|
|
2231
|
+
if (typeof value === 'string') {
|
|
2232
|
+
value = this.props.format === "float" /* Formats.FLOAT */
|
|
2233
|
+
? Number.parseFloat(value)
|
|
2234
|
+
: parseInt(value, 10);
|
|
2188
2235
|
}
|
|
2189
|
-
if (typeof value !==
|
|
2236
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
2190
2237
|
this.characteristicWarning(`characteristic value expected valid finite number and received "${value}" (${typeof value})`, warningType);
|
|
2191
|
-
value = typeof this.value ===
|
|
2238
|
+
value = typeof this.value === 'number'
|
|
2239
|
+
? this.value
|
|
2240
|
+
: this.props.minValue || 0;
|
|
2192
2241
|
}
|
|
2193
|
-
const numericMin = maxWithUndefined(this.props.minValue,
|
|
2194
|
-
let numericMax = minWithUndefined(this.props.maxValue,
|
|
2242
|
+
const numericMin = maxWithUndefined(this.props.minValue, numericLowerBound(this.props.format));
|
|
2243
|
+
let numericMax = minWithUndefined(this.props.maxValue, numericUpperBound(this.props.format));
|
|
2195
2244
|
let stepValue;
|
|
2196
2245
|
if (this.props.format === "float" /* Formats.FLOAT */) {
|
|
2197
2246
|
stepValue = this.props.minStep;
|
|
@@ -2200,7 +2249,7 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2200
2249
|
stepValue = maxWithUndefined(this.props.minStep, 1);
|
|
2201
2250
|
}
|
|
2202
2251
|
if (stepValue != null && stepValue > 0) {
|
|
2203
|
-
const minValue = this.props.minValue
|
|
2252
|
+
const minValue = this.props.minValue ?? 0;
|
|
2204
2253
|
if (numericMax != null) {
|
|
2205
2254
|
numericMax = stepValue * Math.floor((numericMax - minValue) / stepValue) + minValue;
|
|
2206
2255
|
}
|
|
@@ -2216,7 +2265,9 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2216
2265
|
}
|
|
2217
2266
|
if (this.props.validValues && !this.props.validValues.includes(value)) {
|
|
2218
2267
|
this.characteristicWarning(`characteristic value ${value} is not contained in valid values array`, warningType);
|
|
2219
|
-
return this.props.validValues.includes(this.value)
|
|
2268
|
+
return this.props.validValues.includes(this.value)
|
|
2269
|
+
? this.value
|
|
2270
|
+
: (this.props.validValues[0] || 0);
|
|
2220
2271
|
}
|
|
2221
2272
|
if (this.props.validValueRanges && this.props.validValueRanges.length === 2) {
|
|
2222
2273
|
if (value < this.props.validValueRanges[0]) {
|
|
@@ -2233,14 +2284,16 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2233
2284
|
return value;
|
|
2234
2285
|
}
|
|
2235
2286
|
case "string" /* Formats.STRING */: {
|
|
2236
|
-
if (typeof value ===
|
|
2237
|
-
this.characteristicWarning(
|
|
2238
|
-
|
|
2287
|
+
if (typeof value === 'number') {
|
|
2288
|
+
this.characteristicWarning('characteristic was supplied illegal value: number instead of string, '
|
|
2289
|
+
+ 'supplying illegal values will throw errors in the future', warningType);
|
|
2239
2290
|
value = String(value);
|
|
2240
2291
|
}
|
|
2241
|
-
if (typeof value !==
|
|
2242
|
-
this.characteristicWarning(
|
|
2243
|
-
value = typeof this.value ===
|
|
2292
|
+
if (typeof value !== 'string') {
|
|
2293
|
+
this.characteristicWarning(`characteristic value expected string and received ${typeof value}`, warningType);
|
|
2294
|
+
value = typeof this.value === 'string'
|
|
2295
|
+
? this.value
|
|
2296
|
+
: `${value}`;
|
|
2244
2297
|
}
|
|
2245
2298
|
// mirrors the case value = null at the beginning
|
|
2246
2299
|
if (value.length <= 1 && (this.UUID === Characteristic.Model.UUID || this.UUID === Characteristic.SerialNumber.UUID)) {
|
|
@@ -2256,30 +2309,30 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2256
2309
|
return value;
|
|
2257
2310
|
}
|
|
2258
2311
|
case "data" /* Formats.DATA */:
|
|
2259
|
-
if (typeof value !==
|
|
2260
|
-
throw new
|
|
2312
|
+
if (typeof value !== 'string') {
|
|
2313
|
+
throw new TypeError('characteristic with DATA format must have string value');
|
|
2261
2314
|
}
|
|
2262
2315
|
if (this.props.maxDataLen != null && value.length > this.props.maxDataLen) {
|
|
2263
2316
|
// can't cut it as we would basically set binary rubbish afterwards
|
|
2264
|
-
throw new Error(
|
|
2317
|
+
throw new Error('characteristic with DATA format exceeds specified maxDataLen');
|
|
2265
2318
|
}
|
|
2266
2319
|
return value;
|
|
2267
2320
|
case "tlv8" /* Formats.TLV8 */:
|
|
2268
2321
|
if (value === undefined) {
|
|
2269
|
-
this.characteristicWarning(
|
|
2322
|
+
this.characteristicWarning('characteristic was supplied illegal value: undefined', warningType);
|
|
2270
2323
|
return this.value;
|
|
2271
2324
|
}
|
|
2272
2325
|
return value; // we trust that this is valid tlv8
|
|
2273
2326
|
}
|
|
2274
2327
|
// hopefully it shouldn't get to this point
|
|
2275
2328
|
if (value === undefined) {
|
|
2276
|
-
this.characteristicWarning(
|
|
2329
|
+
this.characteristicWarning('characteristic was supplied illegal value: undefined', "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
|
|
2277
2330
|
return this.value;
|
|
2278
2331
|
}
|
|
2279
2332
|
return value;
|
|
2280
2333
|
}
|
|
2281
2334
|
/**
|
|
2282
|
-
* @private
|
|
2335
|
+
* @private
|
|
2283
2336
|
*/
|
|
2284
2337
|
_assignID(identifierCache, accessoryName, serviceUUID, serviceSubtype) {
|
|
2285
2338
|
// generate our IID based on our UUID
|
|
@@ -2295,12 +2348,14 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2295
2348
|
* (see {@link validateUserInput}), and on something like {@link Characteristic.PasswordSetting} that value is the secret itself, so those keep the
|
|
2296
2349
|
* long-standing behaviour of going nowhere until an accessory is listening.
|
|
2297
2350
|
*/
|
|
2298
|
-
characteristicWarning(message, type = "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */,
|
|
2351
|
+
characteristicWarning(message, type = "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */,
|
|
2352
|
+
// eslint-disable-next-line unicorn/error-message -- only here to capture the stack
|
|
2353
|
+
stack = new Error().stack, reportWhenUnheard = false) {
|
|
2299
2354
|
const heard = this.emit("characteristic-warning" /* CharacteristicEventTypes.CHARACTERISTIC_WARNING */, type, message, stack);
|
|
2300
2355
|
if (!heard && reportWhenUnheard) {
|
|
2301
2356
|
// The stack the subscribers would have received goes with it: a characteristic with no service carries no accessory or service name, so it is
|
|
2302
2357
|
// the only thing that locates the fault.
|
|
2303
|
-
console.warn(`HAP-NodeJS WARNING: [${this.displayName} (${this.UUID})] ${message}\nThrown at: ${stack ??
|
|
2358
|
+
console.warn(`HAP-NodeJS WARNING: [${this.displayName} (${this.UUID})] ${message}\nThrown at: ${stack ?? 'unknown'}`);
|
|
2304
2359
|
}
|
|
2305
2360
|
}
|
|
2306
2361
|
/**
|
|
@@ -2344,7 +2399,7 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2344
2399
|
}
|
|
2345
2400
|
/**
|
|
2346
2401
|
* Returns a JSON representation of this characteristic suitable for delivering to HAP clients.
|
|
2347
|
-
* @private
|
|
2402
|
+
* @private
|
|
2348
2403
|
*/
|
|
2349
2404
|
async toHAP(connection, contactGetHandlers = true) {
|
|
2350
2405
|
const object = this.internalHAPRepresentation();
|
|
@@ -2359,63 +2414,63 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2359
2414
|
const value = contactGetHandlers
|
|
2360
2415
|
? await this.handleGetRequest(connection).catch(() => {
|
|
2361
2416
|
const value = this.getDefaultValue();
|
|
2362
|
-
debug(
|
|
2417
|
+
debug('[%s] Error getting value for characteristic on /accessories request. Returning default value instead: %s', this.displayName, `${value}`);
|
|
2363
2418
|
return value; // use default value
|
|
2364
2419
|
})
|
|
2365
2420
|
: this.value;
|
|
2366
|
-
object.value =
|
|
2421
|
+
object.value = formatOutgoingCharacteristicValue(value, this.props);
|
|
2367
2422
|
}
|
|
2368
2423
|
return object;
|
|
2369
2424
|
}
|
|
2370
2425
|
/**
|
|
2371
2426
|
* Returns a JSON representation of this characteristic without the value.
|
|
2372
|
-
* @private
|
|
2427
|
+
* @private
|
|
2373
2428
|
*/
|
|
2374
2429
|
internalHAPRepresentation() {
|
|
2375
|
-
(
|
|
2430
|
+
assert(this.iid, `iid cannot be undefined for characteristic '${this.displayName}'`);
|
|
2376
2431
|
// TODO include the value for characteristics of the AccessoryInformation service
|
|
2377
2432
|
return {
|
|
2378
|
-
type:
|
|
2379
|
-
iid: this.iid,
|
|
2380
|
-
value: null,
|
|
2381
|
-
perms: this.props.perms,
|
|
2382
|
-
description: this.props.description || this.displayName,
|
|
2383
|
-
format: this.props.format,
|
|
2384
|
-
unit: this.props.unit,
|
|
2385
|
-
minValue: this.props.minValue,
|
|
2386
|
-
maxValue: this.props.maxValue,
|
|
2387
|
-
minStep: this.props.minStep,
|
|
2388
|
-
maxLen: this.props.maxLen,
|
|
2389
|
-
maxDataLen: this.props.maxDataLen,
|
|
2390
|
-
|
|
2391
|
-
|
|
2433
|
+
'type': toShortForm(this.UUID),
|
|
2434
|
+
'iid': this.iid,
|
|
2435
|
+
'value': null,
|
|
2436
|
+
'perms': this.props.perms,
|
|
2437
|
+
'description': this.props.description || this.displayName,
|
|
2438
|
+
'format': this.props.format,
|
|
2439
|
+
'unit': this.props.unit,
|
|
2440
|
+
'minValue': this.props.minValue,
|
|
2441
|
+
'maxValue': this.props.maxValue,
|
|
2442
|
+
'minStep': this.props.minStep,
|
|
2443
|
+
'maxLen': this.props.maxLen,
|
|
2444
|
+
'maxDataLen': this.props.maxDataLen,
|
|
2445
|
+
'valid-values': this.props.validValues,
|
|
2446
|
+
'valid-values-range': this.props.validValueRanges,
|
|
2392
2447
|
};
|
|
2393
2448
|
}
|
|
2394
2449
|
/**
|
|
2395
2450
|
* Serialize characteristic into json string.
|
|
2396
2451
|
*
|
|
2397
2452
|
* @param characteristic - Characteristic object.
|
|
2398
|
-
* @private
|
|
2453
|
+
* @private
|
|
2399
2454
|
*/
|
|
2400
2455
|
static serialize(characteristic) {
|
|
2401
2456
|
let constructorName;
|
|
2402
|
-
if (characteristic.constructor.name !==
|
|
2457
|
+
if (characteristic.constructor.name !== 'Characteristic') {
|
|
2403
2458
|
constructorName = characteristic.constructor.name;
|
|
2404
2459
|
}
|
|
2405
2460
|
return {
|
|
2406
2461
|
displayName: characteristic.displayName,
|
|
2407
2462
|
UUID: characteristic.UUID,
|
|
2408
2463
|
eventOnlyCharacteristic: characteristic.UUID === Characteristic.ProgrammableSwitchEvent.UUID, // support downgrades for now
|
|
2409
|
-
constructorName
|
|
2464
|
+
constructorName,
|
|
2410
2465
|
value: characteristic.value,
|
|
2411
|
-
props:
|
|
2466
|
+
props: clone({}, characteristic.props),
|
|
2412
2467
|
};
|
|
2413
2468
|
}
|
|
2414
2469
|
/**
|
|
2415
2470
|
* Deserialize characteristic from json string.
|
|
2416
2471
|
*
|
|
2417
2472
|
* @param json - Json string representing a characteristic.
|
|
2418
|
-
* @private
|
|
2473
|
+
* @private
|
|
2419
2474
|
*/
|
|
2420
2475
|
static deserialize(json) {
|
|
2421
2476
|
let characteristic;
|
|
@@ -2433,9 +2488,4 @@ class Characteristic extends events_1.EventEmitter {
|
|
|
2433
2488
|
return characteristic;
|
|
2434
2489
|
}
|
|
2435
2490
|
}
|
|
2436
|
-
exports.Characteristic = Characteristic;
|
|
2437
|
-
// We have a cyclic dependency problem. Within this file we have the definitions of "./definitions" as
|
|
2438
|
-
// type imports only (in order to define the static properties). Setting those properties is done outside
|
|
2439
|
-
// this file, within the definition files. Therefore, we import it at the end of this file. Seems weird, but is important.
|
|
2440
|
-
require("./definitions/CharacteristicDefinitions");
|
|
2441
2491
|
//# sourceMappingURL=Characteristic.js.map
|