@homebridge/hap-nodejs 2.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (265) hide show
  1. package/@types/bonjour-hap.d.ts +66 -0
  2. package/@types/simple-plist.d.ts +4 -0
  3. package/LICENSE +176 -0
  4. package/README.md +86 -0
  5. package/dist/accessories/AirConditioner_accessory.d.ts +2 -0
  6. package/dist/accessories/AirConditioner_accessory.d.ts.map +1 -0
  7. package/dist/accessories/AirConditioner_accessory.js +128 -0
  8. package/dist/accessories/AirConditioner_accessory.js.map +1 -0
  9. package/dist/accessories/AppleTVRemote_accessory.d.ts +2 -0
  10. package/dist/accessories/AppleTVRemote_accessory.d.ts.map +1 -0
  11. package/dist/accessories/AppleTVRemote_accessory.js +159 -0
  12. package/dist/accessories/AppleTVRemote_accessory.js.map +1 -0
  13. package/dist/accessories/Camera_accessory.d.ts +2 -0
  14. package/dist/accessories/Camera_accessory.d.ts.map +1 -0
  15. package/dist/accessories/Camera_accessory.js +545 -0
  16. package/dist/accessories/Camera_accessory.js.map +1 -0
  17. package/dist/accessories/Fan_accessory.d.ts +2 -0
  18. package/dist/accessories/Fan_accessory.d.ts.map +1 -0
  19. package/dist/accessories/Fan_accessory.js +74 -0
  20. package/dist/accessories/Fan_accessory.js.map +1 -0
  21. package/dist/accessories/GarageDoorOpener_accessory.d.ts +2 -0
  22. package/dist/accessories/GarageDoorOpener_accessory.d.ts.map +1 -0
  23. package/dist/accessories/GarageDoorOpener_accessory.js +78 -0
  24. package/dist/accessories/GarageDoorOpener_accessory.js.map +1 -0
  25. package/dist/accessories/Light-AdaptiveLighting_accessory.d.ts +2 -0
  26. package/dist/accessories/Light-AdaptiveLighting_accessory.d.ts.map +1 -0
  27. package/dist/accessories/Light-AdaptiveLighting_accessory.js +106 -0
  28. package/dist/accessories/Light-AdaptiveLighting_accessory.js.map +1 -0
  29. package/dist/accessories/Light_accessory.d.ts +2 -0
  30. package/dist/accessories/Light_accessory.d.ts.map +1 -0
  31. package/dist/accessories/Light_accessory.js +145 -0
  32. package/dist/accessories/Light_accessory.js.map +1 -0
  33. package/dist/accessories/Lock_accessory.d.ts +2 -0
  34. package/dist/accessories/Lock_accessory.d.ts.map +1 -0
  35. package/dist/accessories/Lock_accessory.js +76 -0
  36. package/dist/accessories/Lock_accessory.js.map +1 -0
  37. package/dist/accessories/MotionSensor_accessory.d.ts +2 -0
  38. package/dist/accessories/MotionSensor_accessory.d.ts.map +1 -0
  39. package/dist/accessories/MotionSensor_accessory.js +45 -0
  40. package/dist/accessories/MotionSensor_accessory.js.map +1 -0
  41. package/dist/accessories/Outlet_accessory.d.ts +2 -0
  42. package/dist/accessories/Outlet_accessory.d.ts.map +1 -0
  43. package/dist/accessories/Outlet_accessory.js +79 -0
  44. package/dist/accessories/Outlet_accessory.js.map +1 -0
  45. package/dist/accessories/SmartSpeaker_accessory.d.ts +2 -0
  46. package/dist/accessories/SmartSpeaker_accessory.d.ts.map +1 -0
  47. package/dist/accessories/SmartSpeaker_accessory.js +44 -0
  48. package/dist/accessories/SmartSpeaker_accessory.js.map +1 -0
  49. package/dist/accessories/Sprinkler_accessory.d.ts +2 -0
  50. package/dist/accessories/Sprinkler_accessory.d.ts.map +1 -0
  51. package/dist/accessories/Sprinkler_accessory.js +128 -0
  52. package/dist/accessories/Sprinkler_accessory.js.map +1 -0
  53. package/dist/accessories/TV_accessory.d.ts +2 -0
  54. package/dist/accessories/TV_accessory.d.ts.map +1 -0
  55. package/dist/accessories/TV_accessory.js +101 -0
  56. package/dist/accessories/TV_accessory.js.map +1 -0
  57. package/dist/accessories/TemperatureSensor_accessory.d.ts +2 -0
  58. package/dist/accessories/TemperatureSensor_accessory.d.ts.map +1 -0
  59. package/dist/accessories/TemperatureSensor_accessory.js +44 -0
  60. package/dist/accessories/TemperatureSensor_accessory.js.map +1 -0
  61. package/dist/accessories/Wi-FiRouter_accessory.d.ts +3 -0
  62. package/dist/accessories/Wi-FiRouter_accessory.d.ts.map +1 -0
  63. package/dist/accessories/Wi-FiRouter_accessory.js +17 -0
  64. package/dist/accessories/Wi-FiRouter_accessory.js.map +1 -0
  65. package/dist/accessories/Wi-FiSatellite_accessory.d.ts +3 -0
  66. package/dist/accessories/Wi-FiSatellite_accessory.d.ts.map +1 -0
  67. package/dist/accessories/Wi-FiSatellite_accessory.js +19 -0
  68. package/dist/accessories/Wi-FiSatellite_accessory.js.map +1 -0
  69. package/dist/accessories/gstreamer-audioProducer.d.ts +25 -0
  70. package/dist/accessories/gstreamer-audioProducer.d.ts.map +1 -0
  71. package/dist/accessories/gstreamer-audioProducer.js +145 -0
  72. package/dist/accessories/gstreamer-audioProducer.js.map +1 -0
  73. package/dist/accessories/types.d.ts +78 -0
  74. package/dist/accessories/types.d.ts.map +1 -0
  75. package/dist/accessories/types.js +88 -0
  76. package/dist/accessories/types.js.map +1 -0
  77. package/dist/index.d.ts +49 -0
  78. package/dist/index.d.ts.map +1 -0
  79. package/dist/index.js +53 -0
  80. package/dist/index.js.map +1 -0
  81. package/dist/internal-types.d.ts +76 -0
  82. package/dist/internal-types.d.ts.map +1 -0
  83. package/dist/internal-types.js +80 -0
  84. package/dist/internal-types.js.map +1 -0
  85. package/dist/lib/Accessory.d.ts +492 -0
  86. package/dist/lib/Accessory.d.ts.map +1 -0
  87. package/dist/lib/Accessory.js +1642 -0
  88. package/dist/lib/Accessory.js.map +1 -0
  89. package/dist/lib/Advertiser.d.ts +190 -0
  90. package/dist/lib/Advertiser.d.ts.map +1 -0
  91. package/dist/lib/Advertiser.js +594 -0
  92. package/dist/lib/Advertiser.js.map +1 -0
  93. package/dist/lib/Bridge.d.ts +12 -0
  94. package/dist/lib/Bridge.d.ts.map +1 -0
  95. package/dist/lib/Bridge.js +19 -0
  96. package/dist/lib/Bridge.js.map +1 -0
  97. package/dist/lib/Characteristic.d.ts +1604 -0
  98. package/dist/lib/Characteristic.d.ts.map +1 -0
  99. package/dist/lib/Characteristic.js +2318 -0
  100. package/dist/lib/Characteristic.js.map +1 -0
  101. package/dist/lib/HAPServer.d.ts +302 -0
  102. package/dist/lib/HAPServer.d.ts.map +1 -0
  103. package/dist/lib/HAPServer.js +849 -0
  104. package/dist/lib/HAPServer.js.map +1 -0
  105. package/dist/lib/Service.d.ts +568 -0
  106. package/dist/lib/Service.d.ts.map +1 -0
  107. package/dist/lib/Service.js +764 -0
  108. package/dist/lib/Service.js.map +1 -0
  109. package/dist/lib/camera/RTPProxy.d.ts +59 -0
  110. package/dist/lib/camera/RTPProxy.d.ts.map +1 -0
  111. package/dist/lib/camera/RTPProxy.js +267 -0
  112. package/dist/lib/camera/RTPProxy.js.map +1 -0
  113. package/dist/lib/camera/RTPStreamManagement.d.ts +378 -0
  114. package/dist/lib/camera/RTPStreamManagement.d.ts.map +1 -0
  115. package/dist/lib/camera/RTPStreamManagement.js +1007 -0
  116. package/dist/lib/camera/RTPStreamManagement.js.map +1 -0
  117. package/dist/lib/camera/RecordingManagement.d.ts +325 -0
  118. package/dist/lib/camera/RecordingManagement.d.ts.map +1 -0
  119. package/dist/lib/camera/RecordingManagement.js +766 -0
  120. package/dist/lib/camera/RecordingManagement.js.map +1 -0
  121. package/dist/lib/camera/index.d.ts +4 -0
  122. package/dist/lib/camera/index.d.ts.map +1 -0
  123. package/dist/lib/camera/index.js +7 -0
  124. package/dist/lib/camera/index.js.map +1 -0
  125. package/dist/lib/controller/AdaptiveLightingController.d.ts +455 -0
  126. package/dist/lib/controller/AdaptiveLightingController.d.ts.map +1 -0
  127. package/dist/lib/controller/AdaptiveLightingController.js +868 -0
  128. package/dist/lib/controller/AdaptiveLightingController.js.map +1 -0
  129. package/dist/lib/controller/CameraController.d.ts +411 -0
  130. package/dist/lib/controller/CameraController.d.ts.map +1 -0
  131. package/dist/lib/controller/CameraController.js +658 -0
  132. package/dist/lib/controller/CameraController.js.map +1 -0
  133. package/dist/lib/controller/Controller.d.ts +175 -0
  134. package/dist/lib/controller/Controller.d.ts.map +1 -0
  135. package/dist/lib/controller/Controller.js +24 -0
  136. package/dist/lib/controller/Controller.js.map +1 -0
  137. package/dist/lib/controller/DoorbellController.d.ts +59 -0
  138. package/dist/lib/controller/DoorbellController.d.ts.map +1 -0
  139. package/dist/lib/controller/DoorbellController.js +102 -0
  140. package/dist/lib/controller/DoorbellController.js.map +1 -0
  141. package/dist/lib/controller/RemoteController.d.ts +440 -0
  142. package/dist/lib/controller/RemoteController.d.ts.map +1 -0
  143. package/dist/lib/controller/RemoteController.js +1178 -0
  144. package/dist/lib/controller/RemoteController.js.map +1 -0
  145. package/dist/lib/controller/index.d.ts +6 -0
  146. package/dist/lib/controller/index.d.ts.map +1 -0
  147. package/dist/lib/controller/index.js +9 -0
  148. package/dist/lib/controller/index.js.map +1 -0
  149. package/dist/lib/datastream/DataStreamManagement.d.ts +73 -0
  150. package/dist/lib/datastream/DataStreamManagement.d.ts.map +1 -0
  151. package/dist/lib/datastream/DataStreamManagement.js +205 -0
  152. package/dist/lib/datastream/DataStreamManagement.js.map +1 -0
  153. package/dist/lib/datastream/DataStreamParser.d.ts +186 -0
  154. package/dist/lib/datastream/DataStreamParser.d.ts.map +1 -0
  155. package/dist/lib/datastream/DataStreamParser.js +879 -0
  156. package/dist/lib/datastream/DataStreamParser.js.map +1 -0
  157. package/dist/lib/datastream/DataStreamServer.d.ts +357 -0
  158. package/dist/lib/datastream/DataStreamServer.d.ts.map +1 -0
  159. package/dist/lib/datastream/DataStreamServer.js +878 -0
  160. package/dist/lib/datastream/DataStreamServer.js.map +1 -0
  161. package/dist/lib/datastream/index.d.ts +4 -0
  162. package/dist/lib/datastream/index.d.ts.map +1 -0
  163. package/dist/lib/datastream/index.js +7 -0
  164. package/dist/lib/datastream/index.js.map +1 -0
  165. package/dist/lib/definitions/CharacteristicDefinitions.d.ts +1984 -0
  166. package/dist/lib/definitions/CharacteristicDefinitions.d.ts.map +1 -0
  167. package/dist/lib/definitions/CharacteristicDefinitions.js +4422 -0
  168. package/dist/lib/definitions/CharacteristicDefinitions.js.map +1 -0
  169. package/dist/lib/definitions/ServiceDefinitions.d.ts +519 -0
  170. package/dist/lib/definitions/ServiceDefinitions.d.ts.map +1 -0
  171. package/dist/lib/definitions/ServiceDefinitions.js +1361 -0
  172. package/dist/lib/definitions/ServiceDefinitions.js.map +1 -0
  173. package/dist/lib/definitions/generate-definitions.d.ts +34 -0
  174. package/dist/lib/definitions/generate-definitions.d.ts.map +1 -0
  175. package/dist/lib/definitions/generate-definitions.js +624 -0
  176. package/dist/lib/definitions/generate-definitions.js.map +1 -0
  177. package/dist/lib/definitions/generator-configuration.d.ts +22 -0
  178. package/dist/lib/definitions/generator-configuration.d.ts.map +1 -0
  179. package/dist/lib/definitions/generator-configuration.js +371 -0
  180. package/dist/lib/definitions/generator-configuration.js.map +1 -0
  181. package/dist/lib/definitions/index.d.ts +3 -0
  182. package/dist/lib/definitions/index.d.ts.map +1 -0
  183. package/dist/lib/definitions/index.js +6 -0
  184. package/dist/lib/definitions/index.js.map +1 -0
  185. package/dist/lib/gen/HomeKit.d.ts +7 -0
  186. package/dist/lib/gen/HomeKit.d.ts.map +1 -0
  187. package/dist/lib/gen/HomeKit.js +8 -0
  188. package/dist/lib/gen/HomeKit.js.map +1 -0
  189. package/dist/lib/model/AccessoryInfo.d.ts +82 -0
  190. package/dist/lib/model/AccessoryInfo.d.ts.map +1 -0
  191. package/dist/lib/model/AccessoryInfo.js +277 -0
  192. package/dist/lib/model/AccessoryInfo.js.map +1 -0
  193. package/dist/lib/model/ControllerStorage.d.ts +45 -0
  194. package/dist/lib/model/ControllerStorage.d.ts.map +1 -0
  195. package/dist/lib/model/ControllerStorage.js +245 -0
  196. package/dist/lib/model/ControllerStorage.js.map +1 -0
  197. package/dist/lib/model/HAPStorage.d.ts +15 -0
  198. package/dist/lib/model/HAPStorage.d.ts.map +1 -0
  199. package/dist/lib/model/HAPStorage.js +43 -0
  200. package/dist/lib/model/HAPStorage.js.map +1 -0
  201. package/dist/lib/model/IdentifierCache.d.ts +33 -0
  202. package/dist/lib/model/IdentifierCache.d.ts.map +1 -0
  203. package/dist/lib/model/IdentifierCache.js +114 -0
  204. package/dist/lib/model/IdentifierCache.js.map +1 -0
  205. package/dist/lib/tv/AccessControlManagement.d.ts +86 -0
  206. package/dist/lib/tv/AccessControlManagement.d.ts.map +1 -0
  207. package/dist/lib/tv/AccessControlManagement.js +127 -0
  208. package/dist/lib/tv/AccessControlManagement.js.map +1 -0
  209. package/dist/lib/util/checkName.d.ts +8 -0
  210. package/dist/lib/util/checkName.d.ts.map +1 -0
  211. package/dist/lib/util/checkName.js +17 -0
  212. package/dist/lib/util/checkName.js.map +1 -0
  213. package/dist/lib/util/clone.d.ts +7 -0
  214. package/dist/lib/util/clone.d.ts.map +1 -0
  215. package/dist/lib/util/clone.js +22 -0
  216. package/dist/lib/util/clone.js.map +1 -0
  217. package/dist/lib/util/color-utils.d.ts +18 -0
  218. package/dist/lib/util/color-utils.d.ts.map +1 -0
  219. package/dist/lib/util/color-utils.js +442 -0
  220. package/dist/lib/util/color-utils.js.map +1 -0
  221. package/dist/lib/util/eventedhttp.d.ts +251 -0
  222. package/dist/lib/util/eventedhttp.d.ts.map +1 -0
  223. package/dist/lib/util/eventedhttp.js +706 -0
  224. package/dist/lib/util/eventedhttp.js.map +1 -0
  225. package/dist/lib/util/hapCrypto.d.ts +42 -0
  226. package/dist/lib/util/hapCrypto.d.ts.map +1 -0
  227. package/dist/lib/util/hapCrypto.js +144 -0
  228. package/dist/lib/util/hapCrypto.js.map +1 -0
  229. package/dist/lib/util/hapStatusError.d.ts +16 -0
  230. package/dist/lib/util/hapStatusError.d.ts.map +1 -0
  231. package/dist/lib/util/hapStatusError.js +29 -0
  232. package/dist/lib/util/hapStatusError.js.map +1 -0
  233. package/dist/lib/util/net-utils.d.ts +21 -0
  234. package/dist/lib/util/net-utils.d.ts.map +1 -0
  235. package/dist/lib/util/net-utils.js +79 -0
  236. package/dist/lib/util/net-utils.js.map +1 -0
  237. package/dist/lib/util/once.d.ts +7 -0
  238. package/dist/lib/util/once.d.ts.map +1 -0
  239. package/dist/lib/util/once.js +22 -0
  240. package/dist/lib/util/once.js.map +1 -0
  241. package/dist/lib/util/promise-utils.d.ts +14 -0
  242. package/dist/lib/util/promise-utils.d.ts.map +1 -0
  243. package/dist/lib/util/promise-utils.js +29 -0
  244. package/dist/lib/util/promise-utils.js.map +1 -0
  245. package/dist/lib/util/request-util.d.ts +36 -0
  246. package/dist/lib/util/request-util.d.ts.map +1 -0
  247. package/dist/lib/util/request-util.js +108 -0
  248. package/dist/lib/util/request-util.js.map +1 -0
  249. package/dist/lib/util/time.d.ts +9 -0
  250. package/dist/lib/util/time.d.ts.map +1 -0
  251. package/dist/lib/util/time.js +23 -0
  252. package/dist/lib/util/time.js.map +1 -0
  253. package/dist/lib/util/tlv.d.ts +90 -0
  254. package/dist/lib/util/tlv.d.ts.map +1 -0
  255. package/dist/lib/util/tlv.js +312 -0
  256. package/dist/lib/util/tlv.js.map +1 -0
  257. package/dist/lib/util/uuid.d.ts +23 -0
  258. package/dist/lib/util/uuid.d.ts.map +1 -0
  259. package/dist/lib/util/uuid.js +78 -0
  260. package/dist/lib/util/uuid.js.map +1 -0
  261. package/dist/types.d.ts +276 -0
  262. package/dist/types.d.ts.map +1 -0
  263. package/dist/types.js +11 -0
  264. package/dist/types.js.map +1 -0
  265. package/package.json +91 -0
@@ -0,0 +1,2318 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Characteristic = exports.CharacteristicEventTypes = exports.ChangeReason = exports.Access = exports.Perms = exports.Units = exports.Formats = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const assert_1 = tslib_1.__importDefault(require("assert"));
6
+ const debug_1 = tslib_1.__importDefault(require("debug"));
7
+ const events_1 = require("events");
8
+ const HAPServer_1 = require("./HAPServer");
9
+ const clone_1 = require("./util/clone");
10
+ const hapStatusError_1 = require("./util/hapStatusError");
11
+ const once_1 = require("./util/once");
12
+ const request_util_1 = require("./util/request-util");
13
+ const uuid_1 = require("./util/uuid");
14
+ const checkName_1 = require("./util/checkName");
15
+ const debug = (0, debug_1.default)("HAP-NodeJS:Characteristic");
16
+ /**
17
+ * @group Characteristic
18
+ */
19
+ var Formats;
20
+ (function (Formats) {
21
+ Formats["BOOL"] = "bool";
22
+ /**
23
+ * Signed 32-bit integer
24
+ */
25
+ Formats["INT"] = "int";
26
+ /**
27
+ * Signed 64-bit floating point
28
+ */
29
+ Formats["FLOAT"] = "float";
30
+ /**
31
+ * String encoded in utf8
32
+ */
33
+ Formats["STRING"] = "string";
34
+ /**
35
+ * Unsigned 8-bit integer.
36
+ */
37
+ Formats["UINT8"] = "uint8";
38
+ /**
39
+ * Unsigned 16-bit integer.
40
+ */
41
+ Formats["UINT16"] = "uint16";
42
+ /**
43
+ * Unsigned 32-bit integer.
44
+ */
45
+ Formats["UINT32"] = "uint32";
46
+ /**
47
+ * Unsigned 64-bit integer.
48
+ */
49
+ Formats["UINT64"] = "uint64";
50
+ /**
51
+ * Data is base64 encoded string.
52
+ */
53
+ Formats["DATA"] = "data";
54
+ /**
55
+ * Base64 encoded tlv8 string.
56
+ */
57
+ Formats["TLV8"] = "tlv8";
58
+ })(Formats || (exports.Formats = Formats = {}));
59
+ /**
60
+ * @group Characteristic
61
+ */
62
+ var Units;
63
+ (function (Units) {
64
+ /**
65
+ * Celsius is the only temperature unit in the HomeKit Accessory Protocol.
66
+ * Unit conversion is always done on the client side e.g. on the iPhone in the Home App depending on
67
+ * the configured unit on the device itself.
68
+ */
69
+ Units["CELSIUS"] = "celsius";
70
+ Units["PERCENTAGE"] = "percentage";
71
+ Units["ARC_DEGREE"] = "arcdegrees";
72
+ Units["LUX"] = "lux";
73
+ Units["SECONDS"] = "seconds";
74
+ })(Units || (exports.Units = Units = {}));
75
+ /**
76
+ * @group Characteristic
77
+ */
78
+ var Perms;
79
+ (function (Perms) {
80
+ Perms["PAIRED_READ"] = "pr";
81
+ Perms["PAIRED_WRITE"] = "pw";
82
+ Perms["NOTIFY"] = "ev";
83
+ // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
84
+ Perms["EVENTS"] = "ev";
85
+ Perms["ADDITIONAL_AUTHORIZATION"] = "aa";
86
+ Perms["TIMED_WRITE"] = "tw";
87
+ Perms["HIDDEN"] = "hd";
88
+ Perms["WRITE_RESPONSE"] = "wr";
89
+ })(Perms || (exports.Perms = Perms = {}));
90
+ /**
91
+ * Describes the abstract access to a {@link Characteristic}.
92
+ * It abstracts the more granular access described by {@link Perms}.
93
+ *
94
+ * It is used in {@link CharacteristicProps.adminOnlyAccess}.
95
+ *
96
+ * @group Characteristic
97
+ */
98
+ var Access;
99
+ (function (Access) {
100
+ Access[Access["READ"] = 0] = "READ";
101
+ Access[Access["WRITE"] = 1] = "WRITE";
102
+ Access[Access["NOTIFY"] = 2] = "NOTIFY";
103
+ })(Access || (exports.Access = Access = {}));
104
+ /**
105
+ * @group Characteristic
106
+ */
107
+ var ChangeReason;
108
+ (function (ChangeReason) {
109
+ /**
110
+ * Reason used when HomeKit writes a value or the API user calls {@link Characteristic.setValue}.
111
+ */
112
+ ChangeReason["WRITE"] = "write";
113
+ /**
114
+ * Reason used when the API user calls the method {@link Characteristic.updateValue}.
115
+ */
116
+ ChangeReason["UPDATE"] = "update";
117
+ /**
118
+ * Used when HomeKit reads a value or the API user calls the deprecated method `Characteristic.getValue`.
119
+ */
120
+ ChangeReason["READ"] = "read";
121
+ /**
122
+ * Used when call to {@link Characteristic.sendEventNotification} was made.
123
+ */
124
+ ChangeReason["EVENT"] = "event";
125
+ })(ChangeReason || (exports.ChangeReason = ChangeReason = {}));
126
+ /**
127
+ * @group Characteristic
128
+ */
129
+ var CharacteristicEventTypes;
130
+ (function (CharacteristicEventTypes) {
131
+ /**
132
+ * This event is thrown when a HomeKit controller wants to read the current value of the characteristic.
133
+ * The event handler should call the supplied callback as fast as possible.
134
+ *
135
+ * HAP-NodeJS will complain about slow running get handlers after 3 seconds and terminate the request after 10 seconds.
136
+ */
137
+ CharacteristicEventTypes["GET"] = "get";
138
+ /**
139
+ * This event is thrown when a HomeKit controller wants to write a new value to the characteristic.
140
+ * The event handler should call the supplied callback as fast as possible.
141
+ *
142
+ * HAP-NodeJS will complain about slow running set handlers after 3 seconds and terminate the request after 10 seconds.
143
+ */
144
+ CharacteristicEventTypes["SET"] = "set";
145
+ /**
146
+ * Emitted after a new value is set for the characteristic.
147
+ * The new value can be set via a request by a HomeKit controller or via an API call.
148
+ */
149
+ CharacteristicEventTypes["CHANGE"] = "change";
150
+ /**
151
+ * @private
152
+ */
153
+ CharacteristicEventTypes["SUBSCRIBE"] = "subscribe";
154
+ /**
155
+ * @private
156
+ */
157
+ CharacteristicEventTypes["UNSUBSCRIBE"] = "unsubscribe";
158
+ /**
159
+ * @private
160
+ */
161
+ CharacteristicEventTypes["CHARACTERISTIC_WARNING"] = "characteristic-warning";
162
+ })(CharacteristicEventTypes || (exports.CharacteristicEventTypes = CharacteristicEventTypes = {}));
163
+ /**
164
+ * @group Characteristic
165
+ */
166
+ class ValidValuesIterable {
167
+ props;
168
+ constructor(props) {
169
+ (0, assert_1.default)((0, request_util_1.isNumericFormat)(props.format), "Cannot instantiate valid values iterable when format is not numeric. Found " + props.format);
170
+ this.props = props;
171
+ }
172
+ *[Symbol.iterator]() {
173
+ if (this.props.validValues) {
174
+ for (const value of this.props.validValues) {
175
+ yield value;
176
+ }
177
+ }
178
+ else {
179
+ let min = 0; // default is zero for all the uint types
180
+ let max;
181
+ let stepValue = 1;
182
+ if (this.props.validValueRanges) {
183
+ min = this.props.validValueRanges[0];
184
+ max = this.props.validValueRanges[1];
185
+ }
186
+ else if (this.props.minValue != null && this.props.maxValue != null) {
187
+ min = this.props.minValue;
188
+ max = this.props.maxValue;
189
+ if (this.props.minStep != null) {
190
+ stepValue = this.props.minStep;
191
+ }
192
+ }
193
+ else if ((0, request_util_1.isUnsignedNumericFormat)(this.props.format)) {
194
+ max = (0, request_util_1.numericUpperBound)(this.props.format);
195
+ }
196
+ else {
197
+ throw new Error("Could not find valid iterator strategy for props: " + JSON.stringify(this.props));
198
+ }
199
+ for (let i = min; i <= max; i += stepValue) {
200
+ yield i;
201
+ }
202
+ }
203
+ }
204
+ }
205
+ const numberPattern = /^-?\d+$/;
206
+ function extractHAPStatusFromError(error) {
207
+ let errorValue = -70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */;
208
+ if (numberPattern.test(error.message)) {
209
+ const value = parseInt(error.message, 10);
210
+ if ((0, HAPServer_1.IsKnownHAPStatusError)(value)) {
211
+ errorValue = value;
212
+ }
213
+ }
214
+ return errorValue;
215
+ }
216
+ function maxWithUndefined(a, b) {
217
+ if (a == null) {
218
+ return b;
219
+ }
220
+ else if (b == null) {
221
+ return a;
222
+ }
223
+ else {
224
+ return Math.max(a, b);
225
+ }
226
+ }
227
+ function minWithUndefined(a, b) {
228
+ if (a == null) {
229
+ return b;
230
+ }
231
+ else if (b == null) {
232
+ return a;
233
+ }
234
+ else {
235
+ return Math.min(a, b);
236
+ }
237
+ }
238
+ /**
239
+ * Characteristic represents a particular typed variable that can be assigned to a Service. For instance, a
240
+ * "Hue" Characteristic might store a 'float' value of type 'arcdegrees'. You could add the Hue Characteristic
241
+ * to a {@link Service} in order to store that value. A particular Characteristic is distinguished from others by its
242
+ * UUID. HomeKit provides a set of known Characteristic UUIDs defined in HomeKit.ts along with a
243
+ * corresponding concrete subclass.
244
+ *
245
+ * You can also define custom Characteristics by providing your own UUID. Custom Characteristics can be added
246
+ * to any native or custom Services, but Siri will likely not be able to work with these.
247
+ *
248
+ * @group Characteristic
249
+ */
250
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
251
+ class Characteristic extends events_1.EventEmitter {
252
+ // Pattern below is for automatic detection of the section of defined characteristics. Used by the generator
253
+ // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-
254
+ /**
255
+ * @group Characteristic Definitions
256
+ */
257
+ static AccessCodeControlPoint;
258
+ /**
259
+ * @group Characteristic Definitions
260
+ */
261
+ static AccessCodeSupportedConfiguration;
262
+ /**
263
+ * @group Characteristic Definitions
264
+ */
265
+ static AccessControlLevel;
266
+ /**
267
+ * @group Characteristic Definitions
268
+ */
269
+ static AccessoryFlags;
270
+ /**
271
+ * @group Characteristic Definitions
272
+ */
273
+ static AccessoryIdentifier;
274
+ /**
275
+ * @group Characteristic Definitions
276
+ */
277
+ static Active;
278
+ /**
279
+ * @group Characteristic Definitions
280
+ */
281
+ static ActiveIdentifier;
282
+ /**
283
+ * @group Characteristic Definitions
284
+ */
285
+ static ActivityInterval;
286
+ /**
287
+ * @group Characteristic Definitions
288
+ */
289
+ static AdministratorOnlyAccess;
290
+ /**
291
+ * @group Characteristic Definitions
292
+ */
293
+ static AirParticulateDensity;
294
+ /**
295
+ * @group Characteristic Definitions
296
+ */
297
+ static AirParticulateSize;
298
+ /**
299
+ * @group Characteristic Definitions
300
+ */
301
+ static AirPlayEnable;
302
+ /**
303
+ * @group Characteristic Definitions
304
+ */
305
+ static AirQuality;
306
+ /**
307
+ * @group Characteristic Definitions
308
+ */
309
+ static AppMatchingIdentifier;
310
+ /**
311
+ * @group Characteristic Definitions
312
+ */
313
+ static AssetUpdateReadiness;
314
+ /**
315
+ * @group Characteristic Definitions
316
+ */
317
+ static AudioFeedback;
318
+ /**
319
+ * @group Characteristic Definitions
320
+ */
321
+ static BatteryLevel;
322
+ /**
323
+ * @group Characteristic Definitions
324
+ */
325
+ static Brightness;
326
+ /**
327
+ * @group Characteristic Definitions
328
+ */
329
+ static ButtonEvent;
330
+ /**
331
+ * @group Characteristic Definitions
332
+ */
333
+ static CameraOperatingModeIndicator;
334
+ /**
335
+ * @group Characteristic Definitions
336
+ */
337
+ static CarbonDioxideDetected;
338
+ /**
339
+ * @group Characteristic Definitions
340
+ */
341
+ static CarbonDioxideLevel;
342
+ /**
343
+ * @group Characteristic Definitions
344
+ */
345
+ static CarbonDioxidePeakLevel;
346
+ /**
347
+ * @group Characteristic Definitions
348
+ */
349
+ static CarbonMonoxideDetected;
350
+ /**
351
+ * @group Characteristic Definitions
352
+ */
353
+ static CarbonMonoxideLevel;
354
+ /**
355
+ * @group Characteristic Definitions
356
+ */
357
+ static CarbonMonoxidePeakLevel;
358
+ /**
359
+ * @group Characteristic Definitions
360
+ */
361
+ static CCAEnergyDetectThreshold;
362
+ /**
363
+ * @group Characteristic Definitions
364
+ */
365
+ static CCASignalDetectThreshold;
366
+ /**
367
+ * @group Characteristic Definitions
368
+ */
369
+ static CharacteristicValueActiveTransitionCount;
370
+ /**
371
+ * @group Characteristic Definitions
372
+ */
373
+ static CharacteristicValueTransitionControl;
374
+ /**
375
+ * @group Characteristic Definitions
376
+ */
377
+ static ChargingState;
378
+ /**
379
+ * @group Characteristic Definitions
380
+ */
381
+ static ClosedCaptions;
382
+ /**
383
+ * @group Characteristic Definitions
384
+ */
385
+ static ColorTemperature;
386
+ /**
387
+ * @group Characteristic Definitions
388
+ */
389
+ static ConfigurationState;
390
+ /**
391
+ * @group Characteristic Definitions
392
+ */
393
+ static ConfiguredName;
394
+ /**
395
+ * @group Characteristic Definitions
396
+ */
397
+ static ContactSensorState;
398
+ /**
399
+ * @group Characteristic Definitions
400
+ */
401
+ static CoolingThresholdTemperature;
402
+ /**
403
+ * @group Characteristic Definitions
404
+ */
405
+ static CryptoHash;
406
+ /**
407
+ * @group Characteristic Definitions
408
+ */
409
+ static CurrentAirPurifierState;
410
+ /**
411
+ * @group Characteristic Definitions
412
+ */
413
+ static CurrentAmbientLightLevel;
414
+ /**
415
+ * @group Characteristic Definitions
416
+ */
417
+ static CurrentDoorState;
418
+ /**
419
+ * @group Characteristic Definitions
420
+ */
421
+ static CurrentFanState;
422
+ /**
423
+ * @group Characteristic Definitions
424
+ */
425
+ static CurrentHeaterCoolerState;
426
+ /**
427
+ * @group Characteristic Definitions
428
+ */
429
+ static CurrentHeatingCoolingState;
430
+ /**
431
+ * @group Characteristic Definitions
432
+ */
433
+ static CurrentHorizontalTiltAngle;
434
+ /**
435
+ * @group Characteristic Definitions
436
+ */
437
+ static CurrentHumidifierDehumidifierState;
438
+ /**
439
+ * @group Characteristic Definitions
440
+ */
441
+ static CurrentMediaState;
442
+ /**
443
+ * @group Characteristic Definitions
444
+ */
445
+ static CurrentPosition;
446
+ /**
447
+ * @group Characteristic Definitions
448
+ */
449
+ static CurrentRelativeHumidity;
450
+ /**
451
+ * @group Characteristic Definitions
452
+ */
453
+ static CurrentSlatState;
454
+ /**
455
+ * @group Characteristic Definitions
456
+ */
457
+ static CurrentTemperature;
458
+ /**
459
+ * @group Characteristic Definitions
460
+ */
461
+ static CurrentTiltAngle;
462
+ /**
463
+ * @group Characteristic Definitions
464
+ */
465
+ static CurrentTransport;
466
+ /**
467
+ * @group Characteristic Definitions
468
+ */
469
+ static CurrentVerticalTiltAngle;
470
+ /**
471
+ * @group Characteristic Definitions
472
+ */
473
+ static CurrentVisibilityState;
474
+ /**
475
+ * @group Characteristic Definitions
476
+ */
477
+ static DataStreamHAPTransport;
478
+ /**
479
+ * @group Characteristic Definitions
480
+ */
481
+ static DataStreamHAPTransportInterrupt;
482
+ /**
483
+ * @group Characteristic Definitions
484
+ */
485
+ static DiagonalFieldOfView;
486
+ /**
487
+ * @group Characteristic Definitions
488
+ */
489
+ static DigitalZoom;
490
+ /**
491
+ * @group Characteristic Definitions
492
+ */
493
+ static DisplayOrder;
494
+ /**
495
+ * @group Characteristic Definitions
496
+ */
497
+ static EventRetransmissionMaximum;
498
+ /**
499
+ * @group Characteristic Definitions
500
+ */
501
+ static EventSnapshotsActive;
502
+ /**
503
+ * @group Characteristic Definitions
504
+ */
505
+ static EventTransmissionCounters;
506
+ /**
507
+ * @group Characteristic Definitions
508
+ */
509
+ static FilterChangeIndication;
510
+ /**
511
+ * @group Characteristic Definitions
512
+ */
513
+ static FilterLifeLevel;
514
+ /**
515
+ * @group Characteristic Definitions
516
+ */
517
+ static FirmwareRevision;
518
+ /**
519
+ * @group Characteristic Definitions
520
+ */
521
+ static FirmwareUpdateReadiness;
522
+ /**
523
+ * @group Characteristic Definitions
524
+ */
525
+ static FirmwareUpdateStatus;
526
+ /**
527
+ * @group Characteristic Definitions
528
+ */
529
+ static HardwareFinish;
530
+ /**
531
+ * @group Characteristic Definitions
532
+ */
533
+ static HardwareRevision;
534
+ /**
535
+ * @group Characteristic Definitions
536
+ */
537
+ static HeartBeat;
538
+ /**
539
+ * @group Characteristic Definitions
540
+ */
541
+ static HeatingThresholdTemperature;
542
+ /**
543
+ * @group Characteristic Definitions
544
+ */
545
+ static HoldPosition;
546
+ /**
547
+ * @group Characteristic Definitions
548
+ */
549
+ static HomeKitCameraActive;
550
+ /**
551
+ * @group Characteristic Definitions
552
+ */
553
+ static Hue;
554
+ /**
555
+ * @group Characteristic Definitions
556
+ */
557
+ static Identifier;
558
+ /**
559
+ * @group Characteristic Definitions
560
+ */
561
+ static Identify;
562
+ /**
563
+ * @group Characteristic Definitions
564
+ */
565
+ static ImageMirroring;
566
+ /**
567
+ * @group Characteristic Definitions
568
+ */
569
+ static ImageRotation;
570
+ /**
571
+ * @group Characteristic Definitions
572
+ */
573
+ static InputDeviceType;
574
+ /**
575
+ * @group Characteristic Definitions
576
+ */
577
+ static InputSourceType;
578
+ /**
579
+ * @group Characteristic Definitions
580
+ */
581
+ static InUse;
582
+ /**
583
+ * @group Characteristic Definitions
584
+ */
585
+ static IsConfigured;
586
+ /**
587
+ * @group Characteristic Definitions
588
+ */
589
+ static LeakDetected;
590
+ /**
591
+ * @group Characteristic Definitions
592
+ */
593
+ static ListPairings;
594
+ /**
595
+ * @group Characteristic Definitions
596
+ */
597
+ static LockControlPoint;
598
+ /**
599
+ * @group Characteristic Definitions
600
+ */
601
+ static LockCurrentState;
602
+ /**
603
+ * @group Characteristic Definitions
604
+ */
605
+ static LockLastKnownAction;
606
+ /**
607
+ * @group Characteristic Definitions
608
+ */
609
+ static LockManagementAutoSecurityTimeout;
610
+ /**
611
+ * @group Characteristic Definitions
612
+ */
613
+ static LockPhysicalControls;
614
+ /**
615
+ * @group Characteristic Definitions
616
+ */
617
+ static LockTargetState;
618
+ /**
619
+ * @group Characteristic Definitions
620
+ */
621
+ static Logs;
622
+ /**
623
+ * @group Characteristic Definitions
624
+ */
625
+ static MACRetransmissionMaximum;
626
+ /**
627
+ * @group Characteristic Definitions
628
+ */
629
+ static MACTransmissionCounters;
630
+ /**
631
+ * @group Characteristic Definitions
632
+ */
633
+ static ManagedNetworkEnable;
634
+ /**
635
+ * @group Characteristic Definitions
636
+ */
637
+ static ManuallyDisabled;
638
+ /**
639
+ * @group Characteristic Definitions
640
+ */
641
+ static Manufacturer;
642
+ /**
643
+ * @group Characteristic Definitions
644
+ */
645
+ static MatterFirmwareRevisionNumber;
646
+ /**
647
+ * @group Characteristic Definitions
648
+ */
649
+ static MatterFirmwareUpdateStatus;
650
+ /**
651
+ * @group Characteristic Definitions
652
+ */
653
+ static MaximumTransmitPower;
654
+ /**
655
+ * @group Characteristic Definitions
656
+ */
657
+ static MetricsBufferFullState;
658
+ /**
659
+ * @group Characteristic Definitions
660
+ */
661
+ static Model;
662
+ /**
663
+ * @group Characteristic Definitions
664
+ */
665
+ static MotionDetected;
666
+ /**
667
+ * @group Characteristic Definitions
668
+ */
669
+ static MultifunctionButton;
670
+ /**
671
+ * @group Characteristic Definitions
672
+ */
673
+ static Mute;
674
+ /**
675
+ * @group Characteristic Definitions
676
+ */
677
+ static Name;
678
+ /**
679
+ * @group Characteristic Definitions
680
+ */
681
+ static NetworkAccessViolationControl;
682
+ /**
683
+ * @group Characteristic Definitions
684
+ */
685
+ static NetworkClientProfileControl;
686
+ /**
687
+ * @group Characteristic Definitions
688
+ */
689
+ static NetworkClientStatusControl;
690
+ /**
691
+ * @group Characteristic Definitions
692
+ */
693
+ static NFCAccessControlPoint;
694
+ /**
695
+ * @group Characteristic Definitions
696
+ */
697
+ static NFCAccessSupportedConfiguration;
698
+ /**
699
+ * @group Characteristic Definitions
700
+ */
701
+ static NightVision;
702
+ /**
703
+ * @group Characteristic Definitions
704
+ */
705
+ static NitrogenDioxideDensity;
706
+ /**
707
+ * @group Characteristic Definitions
708
+ */
709
+ static ObstructionDetected;
710
+ /**
711
+ * @group Characteristic Definitions
712
+ */
713
+ static OccupancyDetected;
714
+ /**
715
+ * @group Characteristic Definitions
716
+ */
717
+ static On;
718
+ /**
719
+ * @group Characteristic Definitions
720
+ */
721
+ static OperatingStateResponse;
722
+ /**
723
+ * @group Characteristic Definitions
724
+ */
725
+ static OpticalZoom;
726
+ /**
727
+ * @group Characteristic Definitions
728
+ */
729
+ static OutletInUse;
730
+ /**
731
+ * @group Characteristic Definitions
732
+ */
733
+ static OzoneDensity;
734
+ /**
735
+ * @group Characteristic Definitions
736
+ */
737
+ static PairingFeatures;
738
+ /**
739
+ * @group Characteristic Definitions
740
+ */
741
+ static PairSetup;
742
+ /**
743
+ * @group Characteristic Definitions
744
+ */
745
+ static PairVerify;
746
+ /**
747
+ * @group Characteristic Definitions
748
+ */
749
+ static PasswordSetting;
750
+ /**
751
+ * @group Characteristic Definitions
752
+ */
753
+ static PeriodicSnapshotsActive;
754
+ /**
755
+ * @group Characteristic Definitions
756
+ */
757
+ static PictureMode;
758
+ /**
759
+ * @group Characteristic Definitions
760
+ */
761
+ static Ping;
762
+ /**
763
+ * @group Characteristic Definitions
764
+ */
765
+ static PM10Density;
766
+ /**
767
+ * @group Characteristic Definitions
768
+ */
769
+ static PM2_5Density;
770
+ /**
771
+ * @group Characteristic Definitions
772
+ */
773
+ static PositionState;
774
+ /**
775
+ * @group Characteristic Definitions
776
+ */
777
+ static PowerModeSelection;
778
+ /**
779
+ * @group Characteristic Definitions
780
+ */
781
+ static ProductData;
782
+ /**
783
+ * @group Characteristic Definitions
784
+ */
785
+ static ProgrammableSwitchEvent;
786
+ /**
787
+ * @group Characteristic Definitions
788
+ */
789
+ static ProgrammableSwitchOutputState;
790
+ /**
791
+ * @group Characteristic Definitions
792
+ */
793
+ static ProgramMode;
794
+ /**
795
+ * @group Characteristic Definitions
796
+ */
797
+ static ReceivedSignalStrengthIndication;
798
+ /**
799
+ * @group Characteristic Definitions
800
+ */
801
+ static ReceiverSensitivity;
802
+ /**
803
+ * @group Characteristic Definitions
804
+ */
805
+ static RecordingAudioActive;
806
+ /**
807
+ * @group Characteristic Definitions
808
+ */
809
+ static RelativeHumidityDehumidifierThreshold;
810
+ /**
811
+ * @group Characteristic Definitions
812
+ */
813
+ static RelativeHumidityHumidifierThreshold;
814
+ /**
815
+ * @group Characteristic Definitions
816
+ * @deprecated Removed
817
+ */
818
+ static RelayControlPoint;
819
+ /**
820
+ * @group Characteristic Definitions
821
+ * @deprecated Removed
822
+ */
823
+ static RelayEnabled;
824
+ /**
825
+ * @group Characteristic Definitions
826
+ * @deprecated Removed
827
+ */
828
+ static RelayState;
829
+ /**
830
+ * @group Characteristic Definitions
831
+ */
832
+ static RemainingDuration;
833
+ /**
834
+ * @group Characteristic Definitions
835
+ */
836
+ static RemoteKey;
837
+ /**
838
+ * @group Characteristic Definitions
839
+ */
840
+ static ResetFilterIndication;
841
+ /**
842
+ * @group Characteristic Definitions
843
+ */
844
+ static RotationDirection;
845
+ /**
846
+ * @group Characteristic Definitions
847
+ */
848
+ static RotationSpeed;
849
+ /**
850
+ * @group Characteristic Definitions
851
+ */
852
+ static RouterStatus;
853
+ /**
854
+ * @group Characteristic Definitions
855
+ */
856
+ static Saturation;
857
+ /**
858
+ * @group Characteristic Definitions
859
+ */
860
+ static SecuritySystemAlarmType;
861
+ /**
862
+ * @group Characteristic Definitions
863
+ */
864
+ static SecuritySystemCurrentState;
865
+ /**
866
+ * @group Characteristic Definitions
867
+ */
868
+ static SecuritySystemTargetState;
869
+ /**
870
+ * @group Characteristic Definitions
871
+ */
872
+ static SelectedAudioStreamConfiguration;
873
+ /**
874
+ * @group Characteristic Definitions
875
+ */
876
+ static SelectedCameraRecordingConfiguration;
877
+ /**
878
+ * @group Characteristic Definitions
879
+ */
880
+ static SelectedDiagnosticsModes;
881
+ /**
882
+ * @group Characteristic Definitions
883
+ */
884
+ static SelectedRTPStreamConfiguration;
885
+ /**
886
+ * @group Characteristic Definitions
887
+ */
888
+ static SelectedSleepConfiguration;
889
+ /**
890
+ * @group Characteristic Definitions
891
+ */
892
+ static SerialNumber;
893
+ /**
894
+ * @group Characteristic Definitions
895
+ */
896
+ static ServiceLabelIndex;
897
+ /**
898
+ * @group Characteristic Definitions
899
+ */
900
+ static ServiceLabelNamespace;
901
+ /**
902
+ * @group Characteristic Definitions
903
+ */
904
+ static SetDuration;
905
+ /**
906
+ * @group Characteristic Definitions
907
+ */
908
+ static SetupDataStreamTransport;
909
+ /**
910
+ * @group Characteristic Definitions
911
+ */
912
+ static SetupEndpoints;
913
+ /**
914
+ * @group Characteristic Definitions
915
+ */
916
+ static SetupTransferTransport;
917
+ /**
918
+ * @group Characteristic Definitions
919
+ */
920
+ static SignalToNoiseRatio;
921
+ /**
922
+ * @group Characteristic Definitions
923
+ */
924
+ static SiriEnable;
925
+ /**
926
+ * @group Characteristic Definitions
927
+ */
928
+ static SiriEndpointSessionStatus;
929
+ /**
930
+ * @group Characteristic Definitions
931
+ */
932
+ static SiriEngineVersion;
933
+ /**
934
+ * @group Characteristic Definitions
935
+ */
936
+ static SiriInputType;
937
+ /**
938
+ * @group Characteristic Definitions
939
+ */
940
+ static SiriLightOnUse;
941
+ /**
942
+ * @group Characteristic Definitions
943
+ */
944
+ static SiriListening;
945
+ /**
946
+ * @group Characteristic Definitions
947
+ */
948
+ static SiriTouchToUse;
949
+ /**
950
+ * @group Characteristic Definitions
951
+ */
952
+ static SlatType;
953
+ /**
954
+ * @group Characteristic Definitions
955
+ */
956
+ static SleepDiscoveryMode;
957
+ /**
958
+ * @group Characteristic Definitions
959
+ */
960
+ static SleepInterval;
961
+ /**
962
+ * @group Characteristic Definitions
963
+ */
964
+ static SmokeDetected;
965
+ /**
966
+ * @group Characteristic Definitions
967
+ */
968
+ static SoftwareRevision;
969
+ /**
970
+ * @group Characteristic Definitions
971
+ */
972
+ static StagedFirmwareVersion;
973
+ /**
974
+ * @group Characteristic Definitions
975
+ */
976
+ static StatusActive;
977
+ /**
978
+ * @group Characteristic Definitions
979
+ */
980
+ static StatusFault;
981
+ /**
982
+ * @group Characteristic Definitions
983
+ */
984
+ static StatusJammed;
985
+ /**
986
+ * @group Characteristic Definitions
987
+ */
988
+ static StatusLowBattery;
989
+ /**
990
+ * @group Characteristic Definitions
991
+ */
992
+ static StatusTampered;
993
+ /**
994
+ * @group Characteristic Definitions
995
+ */
996
+ static StreamingStatus;
997
+ /**
998
+ * @group Characteristic Definitions
999
+ */
1000
+ static SulphurDioxideDensity;
1001
+ /**
1002
+ * @group Characteristic Definitions
1003
+ */
1004
+ static SupportedAssetTypes;
1005
+ /**
1006
+ * @group Characteristic Definitions
1007
+ */
1008
+ static SupportedAudioRecordingConfiguration;
1009
+ /**
1010
+ * @group Characteristic Definitions
1011
+ */
1012
+ static SupportedAudioStreamConfiguration;
1013
+ /**
1014
+ * @group Characteristic Definitions
1015
+ */
1016
+ static SupportedCameraRecordingConfiguration;
1017
+ /**
1018
+ * @group Characteristic Definitions
1019
+ */
1020
+ static SupportedCharacteristicValueTransitionConfiguration;
1021
+ /**
1022
+ * @group Characteristic Definitions
1023
+ */
1024
+ static SupportedDataStreamTransportConfiguration;
1025
+ /**
1026
+ * @group Characteristic Definitions
1027
+ */
1028
+ static SupportedDiagnosticsModes;
1029
+ /**
1030
+ * @group Characteristic Definitions
1031
+ */
1032
+ static SupportedDiagnosticsSnapshot;
1033
+ /**
1034
+ * @group Characteristic Definitions
1035
+ */
1036
+ static SupportedFirmwareUpdateConfiguration;
1037
+ /**
1038
+ * @group Characteristic Definitions
1039
+ */
1040
+ static SupportedMetrics;
1041
+ /**
1042
+ * @group Characteristic Definitions
1043
+ */
1044
+ static SupportedRouterConfiguration;
1045
+ /**
1046
+ * @group Characteristic Definitions
1047
+ */
1048
+ static SupportedRTPConfiguration;
1049
+ /**
1050
+ * @group Characteristic Definitions
1051
+ */
1052
+ static SupportedSleepConfiguration;
1053
+ /**
1054
+ * @group Characteristic Definitions
1055
+ */
1056
+ static SupportedTransferTransportConfiguration;
1057
+ /**
1058
+ * @group Characteristic Definitions
1059
+ */
1060
+ static SupportedVideoRecordingConfiguration;
1061
+ /**
1062
+ * @group Characteristic Definitions
1063
+ */
1064
+ static SupportedVideoStreamConfiguration;
1065
+ /**
1066
+ * @group Characteristic Definitions
1067
+ */
1068
+ static SwingMode;
1069
+ /**
1070
+ * @group Characteristic Definitions
1071
+ */
1072
+ static TapType;
1073
+ /**
1074
+ * @group Characteristic Definitions
1075
+ */
1076
+ static TargetAirPurifierState;
1077
+ /**
1078
+ * @group Characteristic Definitions
1079
+ */
1080
+ static TargetControlList;
1081
+ /**
1082
+ * @group Characteristic Definitions
1083
+ */
1084
+ static TargetControlSupportedConfiguration;
1085
+ /**
1086
+ * @group Characteristic Definitions
1087
+ */
1088
+ static TargetDoorState;
1089
+ /**
1090
+ * @group Characteristic Definitions
1091
+ */
1092
+ static TargetFanState;
1093
+ /**
1094
+ * @group Characteristic Definitions
1095
+ */
1096
+ static TargetHeaterCoolerState;
1097
+ /**
1098
+ * @group Characteristic Definitions
1099
+ */
1100
+ static TargetHeatingCoolingState;
1101
+ /**
1102
+ * @group Characteristic Definitions
1103
+ */
1104
+ static TargetHorizontalTiltAngle;
1105
+ /**
1106
+ * @group Characteristic Definitions
1107
+ */
1108
+ static TargetHumidifierDehumidifierState;
1109
+ /**
1110
+ * @group Characteristic Definitions
1111
+ */
1112
+ static TargetMediaState;
1113
+ /**
1114
+ * @group Characteristic Definitions
1115
+ */
1116
+ static TargetPosition;
1117
+ /**
1118
+ * @group Characteristic Definitions
1119
+ */
1120
+ static TargetRelativeHumidity;
1121
+ /**
1122
+ * @group Characteristic Definitions
1123
+ */
1124
+ static TargetTemperature;
1125
+ /**
1126
+ * @group Characteristic Definitions
1127
+ */
1128
+ static TargetTiltAngle;
1129
+ /**
1130
+ * @group Characteristic Definitions
1131
+ */
1132
+ static TargetVerticalTiltAngle;
1133
+ /**
1134
+ * @group Characteristic Definitions
1135
+ */
1136
+ static TargetVisibilityState;
1137
+ /**
1138
+ * @group Characteristic Definitions
1139
+ */
1140
+ static TemperatureDisplayUnits;
1141
+ /**
1142
+ * @group Characteristic Definitions
1143
+ */
1144
+ static ThirdPartyCameraActive;
1145
+ /**
1146
+ * @group Characteristic Definitions
1147
+ */
1148
+ static ThreadControlPoint;
1149
+ /**
1150
+ * @group Characteristic Definitions
1151
+ */
1152
+ static ThreadNodeCapabilities;
1153
+ /**
1154
+ * @group Characteristic Definitions
1155
+ */
1156
+ static ThreadOpenThreadVersion;
1157
+ /**
1158
+ * @group Characteristic Definitions
1159
+ */
1160
+ static ThreadStatus;
1161
+ /**
1162
+ * @group Characteristic Definitions
1163
+ */
1164
+ static Token;
1165
+ /**
1166
+ * @group Characteristic Definitions
1167
+ */
1168
+ static TransmitPower;
1169
+ /**
1170
+ * @group Characteristic Definitions
1171
+ * @deprecated Removed
1172
+ */
1173
+ static TunnelConnectionTimeout;
1174
+ /**
1175
+ * @group Characteristic Definitions
1176
+ * @deprecated Removed
1177
+ */
1178
+ static TunneledAccessoryAdvertising;
1179
+ /**
1180
+ * @group Characteristic Definitions
1181
+ * @deprecated Removed
1182
+ */
1183
+ static TunneledAccessoryConnected;
1184
+ /**
1185
+ * @group Characteristic Definitions
1186
+ * @deprecated Removed
1187
+ */
1188
+ static TunneledAccessoryStateNumber;
1189
+ /**
1190
+ * @group Characteristic Definitions
1191
+ */
1192
+ static ValveType;
1193
+ /**
1194
+ * @group Characteristic Definitions
1195
+ */
1196
+ static Version;
1197
+ /**
1198
+ * @group Characteristic Definitions
1199
+ */
1200
+ static VideoAnalysisActive;
1201
+ /**
1202
+ * @group Characteristic Definitions
1203
+ */
1204
+ static VOCDensity;
1205
+ /**
1206
+ * @group Characteristic Definitions
1207
+ */
1208
+ static Volume;
1209
+ /**
1210
+ * @group Characteristic Definitions
1211
+ */
1212
+ static VolumeControlType;
1213
+ /**
1214
+ * @group Characteristic Definitions
1215
+ */
1216
+ static VolumeSelector;
1217
+ /**
1218
+ * @group Characteristic Definitions
1219
+ */
1220
+ static WakeConfiguration;
1221
+ /**
1222
+ * @group Characteristic Definitions
1223
+ */
1224
+ static WANConfigurationList;
1225
+ /**
1226
+ * @group Characteristic Definitions
1227
+ */
1228
+ static WANStatusList;
1229
+ /**
1230
+ * @group Characteristic Definitions
1231
+ */
1232
+ static WaterLevel;
1233
+ /**
1234
+ * @group Characteristic Definitions
1235
+ */
1236
+ static WiFiCapabilities;
1237
+ /**
1238
+ * @group Characteristic Definitions
1239
+ */
1240
+ static WiFiConfigurationControl;
1241
+ /**
1242
+ * @group Characteristic Definitions
1243
+ */
1244
+ static WiFiSatelliteStatus;
1245
+ // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
1246
+ // NOTICE: when adding/changing properties, remember to possibly adjust the serialize/deserialize functions
1247
+ displayName;
1248
+ UUID;
1249
+ iid = null;
1250
+ value = null;
1251
+ /**
1252
+ * @private
1253
+ */
1254
+ statusCode = 0 /* HAPStatus.SUCCESS */;
1255
+ props;
1256
+ /**
1257
+ * The {@link Characteristic.onGet} handler
1258
+ */
1259
+ getHandler;
1260
+ /**
1261
+ * The {@link Characteristic.onSet} handler
1262
+ */
1263
+ setHandler;
1264
+ subscriptions = 0;
1265
+ /**
1266
+ * @private
1267
+ */
1268
+ additionalAuthorizationHandler;
1269
+ constructor(displayName, UUID, props) {
1270
+ super();
1271
+ this.displayName = displayName;
1272
+ this.UUID = UUID;
1273
+ this.props = {
1274
+ format: "int" /* Formats.INT */,
1275
+ perms: ["ev" /* Perms.NOTIFY */],
1276
+ };
1277
+ this.setProps(props || {}); // ensure sanity checks are called
1278
+ }
1279
+ /**
1280
+ * Accepts a function that will be called to retrieve the current value of a Characteristic.
1281
+ * The function must return a valid Characteristic value for the Characteristic type.
1282
+ * May optionally return a promise.
1283
+ *
1284
+ * @example
1285
+ * ```ts
1286
+ * Characteristic.onGet(async () => {
1287
+ * return true;
1288
+ * });
1289
+ * ```
1290
+ * @param handler
1291
+ */
1292
+ onGet(handler) {
1293
+ if (typeof handler !== "function") {
1294
+ this.characteristicWarning(".onGet handler must be a function");
1295
+ return this;
1296
+ }
1297
+ this.getHandler = handler;
1298
+ return this;
1299
+ }
1300
+ /**
1301
+ * Removes the {@link CharacteristicGetHandler} handler which was configured using {@link onGet}.
1302
+ */
1303
+ removeOnGet() {
1304
+ this.getHandler = undefined;
1305
+ return this;
1306
+ }
1307
+ /**
1308
+ * Accepts a function that will be called when setting the value of a Characteristic.
1309
+ * If the characteristic supports {@link Perms.WRITE_RESPONSE} and the request requests a write-response value,
1310
+ * the returned value will be used.
1311
+ * May optionally return a promise.
1312
+ *
1313
+ * @example
1314
+ * ```ts
1315
+ * Characteristic.onSet(async (value: CharacteristicValue) => {
1316
+ * console.log(value);
1317
+ * });
1318
+ * ```
1319
+ * @param handler
1320
+ */
1321
+ onSet(handler) {
1322
+ if (typeof handler !== "function") {
1323
+ this.characteristicWarning(".onSet handler must be a function");
1324
+ return this;
1325
+ }
1326
+ this.setHandler = handler;
1327
+ return this;
1328
+ }
1329
+ /**
1330
+ * Removes the {@link CharacteristicSetHandler} which was configured using {@link onSet}.
1331
+ */
1332
+ removeOnSet() {
1333
+ this.setHandler = undefined;
1334
+ return this;
1335
+ }
1336
+ /**
1337
+ * Updates the properties of this characteristic.
1338
+ * Properties passed via the parameter will be set. Any parameter set to null will be deleted.
1339
+ * See {@link CharacteristicProps}.
1340
+ *
1341
+ * @param props - Partial properties object with the desired updates.
1342
+ */
1343
+ setProps(props) {
1344
+ (0, assert_1.default)(props, "props cannot be undefined when setting props");
1345
+ // TODO calling setProps after publish doesn't lead to a increment in the current configuration number
1346
+ let formatDidChange = false;
1347
+ // for every value "null" can be used to reset props, except for required props
1348
+ if (props.format) {
1349
+ formatDidChange = this.props.format !== props.format;
1350
+ this.props.format = props.format;
1351
+ }
1352
+ if (props.perms) {
1353
+ (0, assert_1.default)(props.perms.length > 0, "characteristic prop perms cannot be empty array");
1354
+ this.props.perms = props.perms;
1355
+ }
1356
+ if (props.unit !== undefined) {
1357
+ this.props.unit = props.unit != null ? props.unit : undefined;
1358
+ }
1359
+ if (props.description !== undefined) {
1360
+ this.props.description = props.description != null ? props.description : undefined;
1361
+ }
1362
+ // check minValue is valid for the format type
1363
+ if (props.minValue !== undefined) {
1364
+ if (props.minValue === null) {
1365
+ props.minValue = undefined;
1366
+ }
1367
+ else if (!(0, request_util_1.isNumericFormat)(this.props.format)) {
1368
+ 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 */);
1369
+ props.minValue = undefined;
1370
+ }
1371
+ else if (typeof props.minValue !== "number" || !Number.isFinite(props.minValue)) {
1372
+ this.characteristicWarning(`Characteristic Property 'minValue' must be a finite number, received "${props.minValue}" (${typeof props.minValue})`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
1373
+ props.minValue = undefined;
1374
+ }
1375
+ else {
1376
+ if (props.minValue < (0, request_util_1.numericLowerBound)(this.props.format)) {
1377
+ this.characteristicWarning("Characteristic Property 'minValue' was set to " + props.minValue + ", but for numeric format " +
1378
+ this.props.format + " minimum possible is " + (0, request_util_1.numericLowerBound)(this.props.format), "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
1379
+ props.minValue = (0, request_util_1.numericLowerBound)(this.props.format);
1380
+ }
1381
+ else if (props.minValue > (0, request_util_1.numericUpperBound)(this.props.format)) {
1382
+ this.characteristicWarning("Characteristic Property 'minValue' was set to " + props.minValue + ", but for numeric format " +
1383
+ this.props.format + " maximum possible is " + (0, request_util_1.numericUpperBound)(this.props.format), "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
1384
+ props.minValue = (0, request_util_1.numericLowerBound)(this.props.format);
1385
+ }
1386
+ }
1387
+ this.props.minValue = props.minValue;
1388
+ }
1389
+ // check maxValue is valid for the format type
1390
+ if (props.maxValue !== undefined) {
1391
+ if (props.maxValue === null) {
1392
+ props.maxValue = undefined;
1393
+ }
1394
+ else if (!(0, request_util_1.isNumericFormat)(this.props.format)) {
1395
+ 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 */);
1396
+ props.maxValue = undefined;
1397
+ }
1398
+ else if (typeof props.maxValue !== "number" || !Number.isFinite(props.maxValue)) {
1399
+ this.characteristicWarning(`Characteristic Property 'maxValue' must be a finite number, received "${props.maxValue}" (${typeof props.maxValue})`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
1400
+ props.maxValue = undefined;
1401
+ }
1402
+ else {
1403
+ if (props.maxValue > (0, request_util_1.numericUpperBound)(this.props.format)) {
1404
+ this.characteristicWarning("Characteristic Property 'maxValue' was set to " + props.maxValue + ", but for numeric format " +
1405
+ this.props.format + " maximum possible is " + (0, request_util_1.numericUpperBound)(this.props.format), "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
1406
+ props.maxValue = (0, request_util_1.numericUpperBound)(this.props.format);
1407
+ }
1408
+ else if (props.maxValue < (0, request_util_1.numericLowerBound)(this.props.format)) {
1409
+ this.characteristicWarning("Characteristic Property 'maxValue' was set to " + props.maxValue + ", but for numeric format " +
1410
+ this.props.format + " minimum possible is " + (0, request_util_1.numericLowerBound)(this.props.format), "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
1411
+ props.maxValue = (0, request_util_1.numericUpperBound)(this.props.format);
1412
+ }
1413
+ }
1414
+ this.props.maxValue = props.maxValue;
1415
+ }
1416
+ if (props.minStep !== undefined) {
1417
+ if (props.minStep === null) {
1418
+ this.props.minStep = undefined;
1419
+ }
1420
+ else if (!(0, request_util_1.isNumericFormat)(this.props.format)) {
1421
+ 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 */);
1422
+ }
1423
+ else {
1424
+ if (props.minStep < 1 && (0, request_util_1.isIntegerNumericFormat)(this.props.format)) {
1425
+ this.characteristicWarning("Characteristic Property `minStep` was set to a value lower than 1, " +
1426
+ "this will have no effect on format `" + this.props.format);
1427
+ }
1428
+ this.props.minStep = props.minStep;
1429
+ }
1430
+ }
1431
+ if (props.maxLen !== undefined) {
1432
+ if (props.maxLen === null) {
1433
+ this.props.maxLen = undefined;
1434
+ }
1435
+ else if (this.props.format !== "string" /* Formats.STRING */) {
1436
+ 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 */);
1437
+ }
1438
+ else {
1439
+ if (props.maxLen > 256) {
1440
+ this.characteristicWarning("Characteristic Property string `maxLen` cannot be bigger than 256");
1441
+ props.maxLen = 256;
1442
+ }
1443
+ this.props.maxLen = props.maxLen;
1444
+ }
1445
+ }
1446
+ if (props.maxDataLen !== undefined) {
1447
+ if (props.maxDataLen === null) {
1448
+ this.props.maxDataLen = undefined;
1449
+ }
1450
+ else if (this.props.format !== "data" /* Formats.DATA */) {
1451
+ 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 */);
1452
+ }
1453
+ else {
1454
+ this.props.maxDataLen = props.maxDataLen;
1455
+ }
1456
+ }
1457
+ if (props.validValues !== undefined) {
1458
+ if (props.validValues === null) {
1459
+ this.props.validValues = undefined;
1460
+ }
1461
+ else if (!(0, request_util_1.isNumericFormat)(this.props.format)) {
1462
+ this.characteristicWarning("Characteristic Property `validValues` was supplied for non numeric format " + this.props.format);
1463
+ }
1464
+ else {
1465
+ (0, assert_1.default)(props.validValues.length, "characteristic prop validValues cannot be empty array");
1466
+ this.props.validValues = props.validValues;
1467
+ }
1468
+ }
1469
+ if (props.validValueRanges !== undefined) {
1470
+ if (props.validValueRanges === null) {
1471
+ this.props.validValueRanges = undefined;
1472
+ }
1473
+ else if (!(0, request_util_1.isNumericFormat)(this.props.format)) {
1474
+ this.characteristicWarning("Characteristic Property `validValueRanges` was supplied for non numeric format " + this.props.format);
1475
+ }
1476
+ else {
1477
+ (0, assert_1.default)(props.validValueRanges.length === 2, "characteristic prop validValueRanges must have a length of 2");
1478
+ this.props.validValueRanges = props.validValueRanges;
1479
+ }
1480
+ }
1481
+ if (props.adminOnlyAccess !== undefined) {
1482
+ this.props.adminOnlyAccess = props.adminOnlyAccess != null ? props.adminOnlyAccess : undefined;
1483
+ }
1484
+ if (this.props.minValue != null && this.props.maxValue != null) { // the eqeq instead of eqeqeq is important here
1485
+ if (this.props.minValue > this.props.maxValue) { // see https://github.com/homebridge/HAP-NodeJS/issues/690
1486
+ this.props.minValue = undefined;
1487
+ this.props.maxValue = undefined;
1488
+ throw new Error("Error setting CharacteristicsProps for '" + this.displayName + "': 'minValue' cannot be greater or equal the 'maxValue'!");
1489
+ }
1490
+ }
1491
+ if (((0, request_util_1.isNumericFormat)(this.props.format) || this.props.format === "string" /* Formats.STRING */)
1492
+ && this.value != null
1493
+ && !formatDidChange
1494
+ && this.statusCode === 0 /* HAPStatus.SUCCESS */
1495
+ && this.UUID !== Characteristic.ProgrammableSwitchEvent.UUID) {
1496
+ // explaining the if statement above:
1497
+ // - We only do a check for numeric and string formats as they are the only ones affected by characteristic property restrictions.
1498
+ // - There must be a value to begin with. Otherwise, it should just stay not having a value at all (anything else is guess work).
1499
+ // - If the format is changed through `setProps` we rely on the user to supply a valid value after the `setProps` call!
1500
+ // - If the characteristic is marked as erroneous the value is not considered valid anyway, and we must not remove the `statusCode`.
1501
+ // - Special case for `ProgrammableSwitchEvent` where every change in value is considered an event which would result in ghost button presses
1502
+ // validateUserInput when called from setProps is intended to clamp value withing allowed range. It is why warnings should not be displayed.
1503
+ const correctedValue = this.validateUserInput(this.value, "debug-message" /* CharacteristicWarningType.DEBUG_MESSAGE */);
1504
+ if (correctedValue !== this.value) {
1505
+ // we don't want to emit a CHANGE event if the value didn't change at all!
1506
+ this.updateValue(correctedValue);
1507
+ }
1508
+ }
1509
+ return this;
1510
+ }
1511
+ /**
1512
+ * This method can be used to gain an Iterator to loop over all valid values defined for this characteristic.
1513
+ *
1514
+ * The range of valid values can be defined using three different ways via the {@link CharacteristicProps} object
1515
+ * (set via the {@link setProps} method):
1516
+ * * First method is to specifically list every valid value inside {@link CharacteristicProps.validValues}
1517
+ * * Second you can specify a range via {@link CharacteristicProps.minValue} and {@link CharacteristicProps.maxValue} (with optionally defining
1518
+ * {@link CharacteristicProps.minStep})
1519
+ * * And lastly you can specify a range via {@link CharacteristicProps.validValueRanges}
1520
+ * * Implicitly a valid value range is predefined for characteristics with Format {@link Formats.UINT8}, {@link Formats.UINT16},
1521
+ * {@link Formats.UINT32} and {@link Formats.UINT64}: starting by zero to their respective maximum number
1522
+ *
1523
+ * The method will automatically detect which type of valid values definition is used and provide
1524
+ * the correct Iterator for that case.
1525
+ *
1526
+ * Note: This method is (obviously) only valid for numeric characteristics.
1527
+ *
1528
+ * @example
1529
+ * ```ts
1530
+ * // use the iterator to loop over every valid value...
1531
+ * for (const value of characteristic.validValuesIterator()) {
1532
+ * // Insert logic to run for every
1533
+ * }
1534
+ *
1535
+ * // ... or collect them in an array for storage or manipulation
1536
+ * const validValues = Array.from(characteristic.validValuesIterator());
1537
+ * ```
1538
+ */
1539
+ validValuesIterator() {
1540
+ return new ValidValuesIterable(this.props);
1541
+ }
1542
+ // noinspection JSUnusedGlobalSymbols
1543
+ /**
1544
+ * This method can be used to set up additional authorization for a characteristic.
1545
+ * For one, it adds the {@link Perms.ADDITIONAL_AUTHORIZATION} permission to the characteristic
1546
+ * (if it wasn't already) to signal support for additional authorization to HomeKit.
1547
+ * Additionally, an {@link AdditionalAuthorizationHandler} is set up which is called
1548
+ * before a write request is performed.
1549
+ *
1550
+ * Additional Authorization Data can be added to SET request via a custom iOS App.
1551
+ * Before hap-nodejs executes a write request it will call the {@link AdditionalAuthorizationHandler}
1552
+ * with 'authData' supplied in the write request. The 'authData' is a base64 encoded string
1553
+ * (or undefined if no authData was supplied).
1554
+ * The {@link AdditionalAuthorizationHandler} must then return true or false to indicate if the write request
1555
+ * is authorized and should be accepted.
1556
+ *
1557
+ * @param handler - Handler called to check additional authorization data.
1558
+ */
1559
+ setupAdditionalAuthorization(handler) {
1560
+ if (!this.props.perms.includes("aa" /* Perms.ADDITIONAL_AUTHORIZATION */)) {
1561
+ this.props.perms.push("aa" /* Perms.ADDITIONAL_AUTHORIZATION */);
1562
+ }
1563
+ this.additionalAuthorizationHandler = handler;
1564
+ }
1565
+ setValue(value, callback, context) {
1566
+ if (value instanceof Error) {
1567
+ this.statusCode = value instanceof hapStatusError_1.HapStatusError ? value.hapStatus : extractHAPStatusFromError(value);
1568
+ if (callback) {
1569
+ callback();
1570
+ }
1571
+ return this;
1572
+ }
1573
+ if (callback && !context && typeof callback !== "function") {
1574
+ context = callback;
1575
+ callback = undefined;
1576
+ }
1577
+ try {
1578
+ value = this.validateUserInput(value);
1579
+ }
1580
+ catch (error) {
1581
+ this.characteristicWarning(error?.message + "", "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
1582
+ if (callback) {
1583
+ callback(error);
1584
+ }
1585
+ return this;
1586
+ }
1587
+ this.handleSetRequest(value, undefined, context).then(value => {
1588
+ if (callback) {
1589
+ if (value) { // possible write response
1590
+ callback(null, value);
1591
+ }
1592
+ else {
1593
+ callback(null);
1594
+ }
1595
+ }
1596
+ }, reason => {
1597
+ if (callback) {
1598
+ callback(reason);
1599
+ }
1600
+ });
1601
+ return this;
1602
+ }
1603
+ updateValue(value, callback, context) {
1604
+ if (value instanceof Error) {
1605
+ this.statusCode = value instanceof hapStatusError_1.HapStatusError ? value.hapStatus : extractHAPStatusFromError(value);
1606
+ if (callback) {
1607
+ callback();
1608
+ }
1609
+ return this;
1610
+ }
1611
+ if (callback && !context && typeof callback !== "function") {
1612
+ context = callback;
1613
+ callback = undefined;
1614
+ }
1615
+ try {
1616
+ value = this.validateUserInput(value);
1617
+ }
1618
+ catch (error) {
1619
+ this.characteristicWarning(error?.message + "", "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
1620
+ if (callback) {
1621
+ callback();
1622
+ }
1623
+ return this;
1624
+ }
1625
+ this.statusCode = 0 /* HAPStatus.SUCCESS */;
1626
+ const oldValue = this.value;
1627
+ this.value = value;
1628
+ if (callback) {
1629
+ callback();
1630
+ }
1631
+ this.emit("change" /* CharacteristicEventTypes.CHANGE */, { originator: undefined, oldValue: oldValue, newValue: value, reason: "update" /* ChangeReason.UPDATE */, context: context });
1632
+ return this; // for chaining
1633
+ }
1634
+ /**
1635
+ * This method acts similarly to {@link updateValue} by setting the current value of the characteristic
1636
+ * without calling any {@link CharacteristicEventTypes.SET} or {@link onSet} handlers.
1637
+ * The difference is that this method forces an event notification sent (updateValue only sends one if the value changed).
1638
+ * This is especially useful for characteristics like {@link Characteristic.ButtonEvent} or {@link Characteristic.ProgrammableSwitchEvent}.
1639
+ *
1640
+ * @param value - The new value.
1641
+ * @param context - Passed to the {@link CharacteristicEventTypes.CHANGE} event handler.
1642
+ */
1643
+ sendEventNotification(value, context) {
1644
+ this.statusCode = 0 /* HAPStatus.SUCCESS */;
1645
+ value = this.validateUserInput(value);
1646
+ const oldValue = this.value;
1647
+ this.value = value;
1648
+ this.emit("change" /* CharacteristicEventTypes.CHANGE */, { originator: undefined, oldValue: oldValue, newValue: value, reason: "event" /* ChangeReason.EVENT */, context: context });
1649
+ return this; // for chaining
1650
+ }
1651
+ /**
1652
+ * Called when a HAP requests wants to know the current value of the characteristic.
1653
+ *
1654
+ * @param connection - The HAP connection from which the request originated from.
1655
+ * @param context - Deprecated parameter. There for backwards compatibility.
1656
+ * @private Used by the Accessory to load the characteristic value
1657
+ */
1658
+ async handleGetRequest(connection, context) {
1659
+ if (!this.props.perms.includes("pr" /* Perms.PAIRED_READ */)) { // check if we are allowed to read from this characteristic
1660
+ throw -70405 /* HAPStatus.WRITE_ONLY_CHARACTERISTIC */;
1661
+ }
1662
+ if (this.UUID === Characteristic.ProgrammableSwitchEvent.UUID) {
1663
+ // special workaround for event only programmable switch event, which must always return null
1664
+ return null;
1665
+ }
1666
+ if (this.getHandler) {
1667
+ if (this.listeners("get" /* CharacteristicEventTypes.GET */).length > 0) {
1668
+ this.characteristicWarning("Ignoring on('get') handler as onGet handler was defined instead");
1669
+ }
1670
+ try {
1671
+ let value = await this.getHandler(context, connection);
1672
+ this.statusCode = 0 /* HAPStatus.SUCCESS */;
1673
+ try {
1674
+ value = this.validateUserInput(value);
1675
+ }
1676
+ catch (error) {
1677
+ this.characteristicWarning(`An illegal value was supplied by the read handler for characteristic: ${error?.message}`, "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */, error?.stack);
1678
+ this.statusCode = -70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */;
1679
+ return Promise.reject(-70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
1680
+ }
1681
+ const oldValue = this.value;
1682
+ this.value = value;
1683
+ if (oldValue !== value) { // emit a change event if necessary
1684
+ this.emit("change" /* CharacteristicEventTypes.CHANGE */, { originator: connection, oldValue: oldValue, newValue: value, reason: "read" /* ChangeReason.READ */, context: context });
1685
+ }
1686
+ return value;
1687
+ }
1688
+ catch (error) {
1689
+ if (typeof error === "number") {
1690
+ const hapStatusError = new hapStatusError_1.HapStatusError(error);
1691
+ this.statusCode = hapStatusError.hapStatus;
1692
+ }
1693
+ else if (error instanceof hapStatusError_1.HapStatusError) {
1694
+ this.statusCode = error.hapStatus;
1695
+ }
1696
+ else {
1697
+ this.characteristicWarning(`Unhandled error thrown inside read handler for characteristic: ${error?.message}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
1698
+ this.statusCode = -70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */;
1699
+ }
1700
+ throw this.statusCode;
1701
+ }
1702
+ }
1703
+ if (this.listeners("get" /* CharacteristicEventTypes.GET */).length === 0) {
1704
+ if (this.statusCode) {
1705
+ throw this.statusCode;
1706
+ }
1707
+ try {
1708
+ return this.validateUserInput(this.value);
1709
+ }
1710
+ catch (error) {
1711
+ this.characteristicWarning(`An illegal value was supplied by setting \`value\` for characteristic: ${error?.message}`, "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */, error?.stack);
1712
+ return Promise.reject(-70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
1713
+ }
1714
+ }
1715
+ return new Promise((resolve, reject) => {
1716
+ try {
1717
+ this.emit("get" /* CharacteristicEventTypes.GET */, (0, once_1.once)((status, value) => {
1718
+ if (status) {
1719
+ if (typeof status === "number") {
1720
+ const hapStatusError = new hapStatusError_1.HapStatusError(status);
1721
+ this.statusCode = hapStatusError.hapStatus;
1722
+ }
1723
+ else if (status instanceof hapStatusError_1.HapStatusError) {
1724
+ this.statusCode = status.hapStatus;
1725
+ }
1726
+ else {
1727
+ debug("[%s] Received error from get handler %s", this.displayName, status.stack);
1728
+ this.statusCode = extractHAPStatusFromError(status);
1729
+ }
1730
+ reject(this.statusCode);
1731
+ return;
1732
+ }
1733
+ this.statusCode = 0 /* HAPStatus.SUCCESS */;
1734
+ value = this.validateUserInput(value);
1735
+ const oldValue = this.value;
1736
+ this.value = value;
1737
+ resolve(value);
1738
+ if (oldValue !== value) { // emit a change event if necessary
1739
+ this.emit("change" /* CharacteristicEventTypes.CHANGE */, { originator: connection, oldValue: oldValue, newValue: value, reason: "read" /* ChangeReason.READ */, context: context });
1740
+ }
1741
+ }), context, connection);
1742
+ }
1743
+ catch (error) {
1744
+ this.characteristicWarning(`Unhandled error thrown inside read handler for characteristic: ${error?.message}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
1745
+ this.statusCode = -70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */;
1746
+ reject(-70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
1747
+ }
1748
+ });
1749
+ }
1750
+ /**
1751
+ * Called when a HAP requests update the current value of the characteristic.
1752
+ *
1753
+ * @param value - The updated value
1754
+ * @param connection - The connection from which the request originated from
1755
+ * @param context - Deprecated parameter. There for backwards compatibility.
1756
+ * @returns Promise resolve to void in normal operation. When characteristic supports write-response, HAP
1757
+ * requests a write-response and the set handler returns a write-response value, the respective
1758
+ * write response value is resolved.
1759
+ * @private
1760
+ */
1761
+ async handleSetRequest(value, connection, context) {
1762
+ this.statusCode = 0 /* HAPStatus.SUCCESS */;
1763
+ if (connection !== undefined) {
1764
+ // if connection is undefined, the set "request" comes from the setValue method.
1765
+ // for setValue a value of "null" is allowed and checked via validateUserInput.
1766
+ try {
1767
+ value = this.validateClientSuppliedValue(value);
1768
+ }
1769
+ catch (e) {
1770
+ debug(`[${this.displayName}]`, e.message);
1771
+ return Promise.reject(-70410 /* HAPStatus.INVALID_VALUE_IN_REQUEST */);
1772
+ }
1773
+ }
1774
+ const oldValue = this.value;
1775
+ if (this.setHandler) {
1776
+ if (this.listeners("set" /* CharacteristicEventTypes.SET */).length > 0) {
1777
+ this.characteristicWarning("Ignoring on('set') handler as onSet handler was defined instead");
1778
+ }
1779
+ try {
1780
+ const writeResponse = await this.setHandler(value, context, connection);
1781
+ this.statusCode = 0 /* HAPStatus.SUCCESS */;
1782
+ if (writeResponse != null && this.props.perms.includes("wr" /* Perms.WRITE_RESPONSE */)) {
1783
+ this.value = this.validateUserInput(writeResponse);
1784
+ return this.value;
1785
+ }
1786
+ else {
1787
+ if (writeResponse != null) {
1788
+ this.characteristicWarning("SET handler returned write response value, though the characteristic doesn't support write response", "debug-message" /* CharacteristicWarningType.DEBUG_MESSAGE */);
1789
+ }
1790
+ this.value = value;
1791
+ this.emit("change" /* CharacteristicEventTypes.CHANGE */, { originator: connection, oldValue: oldValue, newValue: value, reason: "write" /* ChangeReason.WRITE */, context: context });
1792
+ return;
1793
+ }
1794
+ }
1795
+ catch (error) {
1796
+ if (typeof error === "number") {
1797
+ const hapStatusError = new hapStatusError_1.HapStatusError(error);
1798
+ this.statusCode = hapStatusError.hapStatus;
1799
+ }
1800
+ else if (error instanceof hapStatusError_1.HapStatusError) {
1801
+ this.statusCode = error.hapStatus;
1802
+ }
1803
+ else {
1804
+ this.characteristicWarning(`Unhandled error thrown inside write handler for characteristic: ${error?.message}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
1805
+ this.statusCode = -70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */;
1806
+ }
1807
+ throw this.statusCode;
1808
+ }
1809
+ }
1810
+ if (this.listeners("set" /* CharacteristicEventTypes.SET */).length === 0) {
1811
+ this.value = value;
1812
+ this.emit("change" /* CharacteristicEventTypes.CHANGE */, { originator: connection, oldValue: oldValue, newValue: value, reason: "write" /* ChangeReason.WRITE */, context: context });
1813
+ return Promise.resolve();
1814
+ }
1815
+ else {
1816
+ return new Promise((resolve, reject) => {
1817
+ try {
1818
+ this.emit("set" /* CharacteristicEventTypes.SET */, value, (0, once_1.once)((status, writeResponse) => {
1819
+ if (status) {
1820
+ if (typeof status === "number") {
1821
+ const hapStatusError = new hapStatusError_1.HapStatusError(status);
1822
+ this.statusCode = hapStatusError.hapStatus;
1823
+ }
1824
+ else if (status instanceof hapStatusError_1.HapStatusError) {
1825
+ this.statusCode = status.hapStatus;
1826
+ }
1827
+ else {
1828
+ debug("[%s] Received error from set handler %s", this.displayName, status.stack);
1829
+ this.statusCode = extractHAPStatusFromError(status);
1830
+ }
1831
+ reject(this.statusCode);
1832
+ return;
1833
+ }
1834
+ this.statusCode = 0 /* HAPStatus.SUCCESS */;
1835
+ if (writeResponse != null && this.props.perms.includes("wr" /* Perms.WRITE_RESPONSE */)) {
1836
+ // support write response simply by letting the implementor pass the response as second argument to the callback
1837
+ this.value = this.validateUserInput(writeResponse);
1838
+ resolve(this.value);
1839
+ }
1840
+ else {
1841
+ if (writeResponse != null) {
1842
+ this.characteristicWarning("SET handler returned write response value, though the characteristic doesn't support write response", "debug-message" /* CharacteristicWarningType.DEBUG_MESSAGE */);
1843
+ }
1844
+ this.value = value;
1845
+ resolve();
1846
+ this.emit("change" /* CharacteristicEventTypes.CHANGE */, { originator: connection, oldValue: oldValue, newValue: value, reason: "write" /* ChangeReason.WRITE */, context: context });
1847
+ }
1848
+ }), context, connection);
1849
+ }
1850
+ catch (error) {
1851
+ this.characteristicWarning(`Unhandled error thrown inside write handler for characteristic: ${error?.message}`, "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */, error?.stack);
1852
+ this.statusCode = -70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */;
1853
+ reject(-70402 /* HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
1854
+ }
1855
+ });
1856
+ }
1857
+ }
1858
+ /**
1859
+ * Called once a HomeKit controller subscribes to events of this characteristic.
1860
+ * @private
1861
+ */
1862
+ subscribe() {
1863
+ if (this.subscriptions === 0) {
1864
+ this.emit("subscribe" /* CharacteristicEventTypes.SUBSCRIBE */);
1865
+ }
1866
+ this.subscriptions++;
1867
+ }
1868
+ /**
1869
+ * Called once a HomeKit controller unsubscribe to events of this characteristic or a HomeKit controller
1870
+ * which was subscribed to this characteristic disconnects.
1871
+ * @private
1872
+ */
1873
+ unsubscribe() {
1874
+ const wasOne = this.subscriptions === 1;
1875
+ this.subscriptions--;
1876
+ this.subscriptions = Math.max(this.subscriptions, 0);
1877
+ if (wasOne) {
1878
+ this.emit("unsubscribe" /* CharacteristicEventTypes.UNSUBSCRIBE */);
1879
+ }
1880
+ }
1881
+ getDefaultValue() {
1882
+ // noinspection JSDeprecatedSymbols
1883
+ switch (this.props.format) {
1884
+ case "bool" /* Formats.BOOL */:
1885
+ return false;
1886
+ case "string" /* Formats.STRING */:
1887
+ switch (this.UUID) {
1888
+ case Characteristic.Manufacturer.UUID:
1889
+ return "Default-Manufacturer";
1890
+ case Characteristic.Model.UUID:
1891
+ return "Default-Model";
1892
+ case Characteristic.SerialNumber.UUID:
1893
+ return "Default-SerialNumber";
1894
+ case Characteristic.FirmwareRevision.UUID:
1895
+ return "0.0.0";
1896
+ default:
1897
+ return "";
1898
+ }
1899
+ case "data" /* Formats.DATA */:
1900
+ return ""; // who knows!
1901
+ case "tlv8" /* Formats.TLV8 */:
1902
+ return ""; // who knows!
1903
+ case "int" /* Formats.INT */:
1904
+ case "float" /* Formats.FLOAT */:
1905
+ case "uint8" /* Formats.UINT8 */:
1906
+ case "uint16" /* Formats.UINT16 */:
1907
+ case "uint32" /* Formats.UINT32 */:
1908
+ case "uint64" /* Formats.UINT64 */:
1909
+ switch (this.UUID) {
1910
+ case Characteristic.CurrentTemperature.UUID:
1911
+ return 0; // some existing integrations expect this to be 0 by default
1912
+ default: {
1913
+ if (this.props.validValues?.length && typeof this.props.validValues[0] === "number") {
1914
+ return this.props.validValues[0];
1915
+ }
1916
+ if (typeof this.props.minValue === "number" && Number.isFinite(this.props.minValue)) {
1917
+ return this.props.minValue;
1918
+ }
1919
+ return 0;
1920
+ }
1921
+ }
1922
+ default:
1923
+ return 0;
1924
+ }
1925
+ }
1926
+ /**
1927
+ * Checks if the value received from the HAP request is valid.
1928
+ * If returned false the received value is not valid and {@link HAPStatus.INVALID_VALUE_IN_REQUEST}
1929
+ * must be returned.
1930
+ * @param value - Value supplied by the HomeKit controller
1931
+ */
1932
+ validateClientSuppliedValue(value) {
1933
+ if (value == null) {
1934
+ throw new Error(`Client supplied invalid value for ${this.props.format}: ${value}`);
1935
+ }
1936
+ switch (this.props.format) {
1937
+ case "bool" /* Formats.BOOL */: {
1938
+ if (typeof value === "boolean") {
1939
+ return value;
1940
+ }
1941
+ if (typeof value === "number" && (value === 1 || value === 0)) {
1942
+ return Boolean(value);
1943
+ }
1944
+ throw new Error(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
1945
+ }
1946
+ case "int" /* Formats.INT */:
1947
+ case "float" /* Formats.FLOAT */:
1948
+ case "uint8" /* Formats.UINT8 */:
1949
+ case "uint16" /* Formats.UINT16 */:
1950
+ case "uint32" /* Formats.UINT32 */:
1951
+ case "uint64" /* Formats.UINT64 */: {
1952
+ if (typeof value === "boolean") {
1953
+ value = value ? 1 : 0;
1954
+ }
1955
+ if (typeof value !== "number" || !Number.isFinite(value)) {
1956
+ throw new Error(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
1957
+ }
1958
+ const numericMin = maxWithUndefined(this.props.minValue, (0, request_util_1.numericLowerBound)(this.props.format));
1959
+ const numericMax = minWithUndefined(this.props.maxValue, (0, request_util_1.numericUpperBound)(this.props.format));
1960
+ if (typeof numericMin === "number" && value < numericMin) {
1961
+ throw new Error(`Client supplied value of ${value} is less than the minimum allowed value of ${numericMin}`);
1962
+ }
1963
+ if (typeof numericMax === "number" && value > numericMax) {
1964
+ throw new Error(`Client supplied value of ${value} is greater than the maximum allowed value of ${numericMax}`);
1965
+ }
1966
+ if (this.props.validValues && !this.props.validValues.includes(value)) {
1967
+ throw new Error(`Client supplied value of ${value} is not in ${this.props.validValues.toString()}`);
1968
+ }
1969
+ if (this.props.validValueRanges && this.props.validValueRanges.length === 2) {
1970
+ if (value < this.props.validValueRanges[0]) {
1971
+ throw new Error(`Client supplied value of ${value} is less than the minimum allowed value of ${this.props.validValueRanges[0]}`);
1972
+ }
1973
+ if (value > this.props.validValueRanges[1]) {
1974
+ throw new Error(`Client supplied value of ${value} is greater than the maximum allowed value of ${this.props.validValueRanges[1]}`);
1975
+ }
1976
+ }
1977
+ return value;
1978
+ }
1979
+ case "string" /* Formats.STRING */: {
1980
+ if (typeof value !== "string") {
1981
+ throw new Error(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
1982
+ }
1983
+ const maxLength = this.props.maxLen != null ? this.props.maxLen : 64; // default is 64; max is 256 which is set in setProps
1984
+ if (value.length > maxLength) {
1985
+ throw new Error(`Client supplied value length of ${value.length} exceeds maximum length allowed of ${maxLength}`);
1986
+ }
1987
+ return value;
1988
+ }
1989
+ case "data" /* Formats.DATA */: {
1990
+ if (typeof value !== "string") {
1991
+ throw new Error(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
1992
+ }
1993
+ // we don't validate base64 here
1994
+ const maxLength = this.props.maxDataLen != null ? this.props.maxDataLen : 0x200000; // default is 0x200000
1995
+ if (value.length > maxLength) {
1996
+ throw new Error(`Client supplied value length of ${value.length} exceeds maximum length allowed of ${maxLength}`);
1997
+ }
1998
+ return value;
1999
+ }
2000
+ case "tlv8" /* Formats.TLV8 */:
2001
+ if (typeof value !== "string") {
2002
+ throw new Error(`Client supplied invalid type for ${this.props.format}: "${value}" (${typeof value})`);
2003
+ }
2004
+ return value;
2005
+ }
2006
+ return value;
2007
+ }
2008
+ /**
2009
+ * Checks if the value received from the API call is valid.
2010
+ * It adjusts the value where it makes sense, prints a warning where values may be rejected with an error
2011
+ * in the future and throws an error which can't be converted to a valid value.
2012
+ *
2013
+ * @param value - The value received from the API call
2014
+ * @param warningType - Optionally defines the warning type to use when raising a {@link CharacteristicEventTypes.CHARACTERISTIC_WARNING}.
2015
+ */
2016
+ validateUserInput(value, warningType = "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */) {
2017
+ if (value === null) {
2018
+ if (this.UUID === Characteristic.Model.UUID || this.UUID === Characteristic.SerialNumber.UUID) { // mirrors the statement in case: Formats.STRING
2019
+ this.characteristicWarning("characteristic must have a non null value otherwise HomeKit will reject this accessory, ignoring new value", "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
2020
+ return this.value; // don't change the value
2021
+ }
2022
+ if (this.props.format === "data" /* Formats.DATA */ || this.props.format === "tlv8" /* Formats.TLV8 */) {
2023
+ return value; // TLV8 and DATA formats are allowed to have null as a value
2024
+ }
2025
+ /**
2026
+ * A short disclaimer here.
2027
+ * null is actually a perfectly valid value for characteristics to have.
2028
+ * The Home app will show "no response" for some characteristics for which it can't handle null
2029
+ * but ultimately its valid and the developers decision what the return.
2030
+ * BUT: out of history hap-nodejs did replace null with the last known value and thus
2031
+ * homebridge devs started to adopting this method as a way of not changing the value in a GET handler.
2032
+ * As an intermediate step we kept the behavior but added a warning printed to the console.
2033
+ * In a future update we will do the breaking change of return null below!
2034
+ */
2035
+ if (this.UUID.endsWith(uuid_1.BASE_UUID)) { // we have an apple defined characteristic (at least assuming nobody else uses the UUID namespace)
2036
+ if (this.UUID === Characteristic.ProgrammableSwitchEvent.UUID) {
2037
+ return value; // null is allowed as a value for ProgrammableSwitchEvent
2038
+ }
2039
+ this.characteristicWarning("characteristic was supplied illegal value: null! Home App will reject null for Apple defined characteristics", warningType);
2040
+ // if the value has been set previously, return it now, otherwise continue with validation to have a default value set.
2041
+ if (this.value !== null) {
2042
+ return this.value;
2043
+ }
2044
+ }
2045
+ else {
2046
+ // we currently allow null for any non-custom defined characteristics
2047
+ return value;
2048
+ }
2049
+ }
2050
+ switch (this.props.format) {
2051
+ case "bool" /* Formats.BOOL */: {
2052
+ if (typeof value === "boolean") {
2053
+ return value;
2054
+ }
2055
+ if (typeof value === "number") {
2056
+ return value === 1;
2057
+ }
2058
+ if (typeof value === "string") {
2059
+ return value === "1" || value === "true";
2060
+ }
2061
+ this.characteristicWarning("characteristic value expected boolean and received " + typeof value, warningType);
2062
+ return false;
2063
+ }
2064
+ case "int" /* Formats.INT */:
2065
+ case "float" /* Formats.FLOAT */:
2066
+ case "uint8" /* Formats.UINT8 */:
2067
+ case "uint16" /* Formats.UINT16 */:
2068
+ case "uint32" /* Formats.UINT32 */:
2069
+ case "uint64" /* Formats.UINT64 */: {
2070
+ if (typeof value === "boolean") {
2071
+ value = value ? 1 : 0;
2072
+ }
2073
+ if (typeof value === "string") {
2074
+ value = this.props.format === "float" /* Formats.FLOAT */ ? parseFloat(value) : parseInt(value, 10);
2075
+ }
2076
+ if (typeof value !== "number" || !Number.isFinite(value)) {
2077
+ this.characteristicWarning(`characteristic value expected valid finite number and received "${value}" (${typeof value})`, warningType);
2078
+ value = typeof this.value === "number" ? this.value : this.props.minValue || 0;
2079
+ }
2080
+ const numericMin = maxWithUndefined(this.props.minValue, (0, request_util_1.numericLowerBound)(this.props.format));
2081
+ let numericMax = minWithUndefined(this.props.maxValue, (0, request_util_1.numericUpperBound)(this.props.format));
2082
+ let stepValue = undefined;
2083
+ if (this.props.format === "float" /* Formats.FLOAT */) {
2084
+ stepValue = this.props.minStep;
2085
+ }
2086
+ else {
2087
+ stepValue = maxWithUndefined(this.props.minStep, 1);
2088
+ }
2089
+ if (stepValue != null && stepValue > 0) {
2090
+ const minValue = this.props.minValue != null ? this.props.minValue : 0;
2091
+ if (numericMax != null) {
2092
+ numericMax = stepValue * Math.floor((numericMax - minValue) / stepValue) + minValue;
2093
+ }
2094
+ value = stepValue * Math.round((value - minValue) / stepValue) + minValue;
2095
+ }
2096
+ if (numericMin != null && value < numericMin) {
2097
+ this.characteristicWarning(`characteristic was supplied illegal value: number ${value} exceeded minimum of ${numericMin}`, warningType);
2098
+ value = numericMin;
2099
+ }
2100
+ if (numericMax != null && value > numericMax) {
2101
+ this.characteristicWarning(`characteristic was supplied illegal value: number ${value} exceeded maximum of ${numericMax}`, warningType);
2102
+ value = numericMax;
2103
+ }
2104
+ if (this.props.validValues && !this.props.validValues.includes(value)) {
2105
+ this.characteristicWarning(`characteristic value ${value} is not contained in valid values array`, warningType);
2106
+ return this.props.validValues.includes(this.value) ? this.value : (this.props.validValues[0] || 0);
2107
+ }
2108
+ if (this.props.validValueRanges && this.props.validValueRanges.length === 2) {
2109
+ if (value < this.props.validValueRanges[0]) {
2110
+ this.characteristicWarning(`characteristic was supplied illegal value: number ${value} not contained in valid value range of `
2111
+ + `${this.props.validValueRanges}, supplying illegal values will throw errors in the future`, warningType);
2112
+ value = this.props.validValueRanges[0];
2113
+ }
2114
+ else if (value > this.props.validValueRanges[1]) {
2115
+ this.characteristicWarning(`characteristic was supplied illegal value: number ${value} not contained in valid value range of `
2116
+ + `${this.props.validValueRanges}, supplying illegal values will throw errors in the future`, warningType);
2117
+ value = this.props.validValueRanges[1];
2118
+ }
2119
+ }
2120
+ return value;
2121
+ }
2122
+ case "string" /* Formats.STRING */: {
2123
+ if (typeof value === "number") {
2124
+ this.characteristicWarning("characteristic was supplied illegal value: number instead of string, " +
2125
+ "supplying illegal values will throw errors in the future", warningType);
2126
+ value = String(value);
2127
+ }
2128
+ if (typeof value !== "string") {
2129
+ this.characteristicWarning("characteristic value expected string and received " + (typeof value), warningType);
2130
+ value = typeof this.value === "string" ? this.value : value + "";
2131
+ }
2132
+ // mirrors the case value = null at the beginning
2133
+ if (value.length <= 1 && (this.UUID === Characteristic.Model.UUID || this.UUID === Characteristic.SerialNumber.UUID)) {
2134
+ this.characteristicWarning(`[${this.displayName}] characteristic must have a length of more than 1 character otherwise`
2135
+ + ` HomeKit will reject this accessory, ignoring new value ${warningType}`);
2136
+ return this.value; // just return the current value
2137
+ }
2138
+ const maxLength = this.props.maxLen ?? 64; // default is 64 (max is 256 which is set in setProps)
2139
+ if (value.length > maxLength) {
2140
+ this.characteristicWarning(`characteristic was supplied illegal value: string '${value}' exceeded max length of ${maxLength}`, warningType);
2141
+ value = value.substring(0, maxLength);
2142
+ }
2143
+ if (value.length > 0 && this.UUID === Characteristic.ConfiguredName.UUID) {
2144
+ (0, checkName_1.checkName)(this.displayName, "ConfiguredName", value);
2145
+ }
2146
+ return value;
2147
+ }
2148
+ case "data" /* Formats.DATA */:
2149
+ if (typeof value !== "string") {
2150
+ throw new Error("characteristic with DATA format must have string value");
2151
+ }
2152
+ if (this.props.maxDataLen != null && value.length > this.props.maxDataLen) {
2153
+ // can't cut it as we would basically set binary rubbish afterwards
2154
+ throw new Error("characteristic with DATA format exceeds specified maxDataLen");
2155
+ }
2156
+ return value;
2157
+ case "tlv8" /* Formats.TLV8 */:
2158
+ if (value === undefined) {
2159
+ this.characteristicWarning("characteristic was supplied illegal value: undefined", warningType);
2160
+ return this.value;
2161
+ }
2162
+ return value; // we trust that this is valid tlv8
2163
+ }
2164
+ // hopefully it shouldn't get to this point
2165
+ if (value === undefined) {
2166
+ this.characteristicWarning("characteristic was supplied illegal value: undefined", "error-message" /* CharacteristicWarningType.ERROR_MESSAGE */);
2167
+ return this.value;
2168
+ }
2169
+ return value;
2170
+ }
2171
+ /**
2172
+ * @private used to assign iid to characteristic
2173
+ */
2174
+ _assignID(identifierCache, accessoryName, serviceUUID, serviceSubtype) {
2175
+ // generate our IID based on our UUID
2176
+ this.iid = identifierCache.getIID(accessoryName, serviceUUID, serviceSubtype, this.UUID);
2177
+ }
2178
+ characteristicWarning(message, type = "warn-message" /* CharacteristicWarningType.WARN_MESSAGE */, stack = new Error().stack) {
2179
+ this.emit("characteristic-warning" /* CharacteristicEventTypes.CHARACTERISTIC_WARNING */, type, message, stack);
2180
+ }
2181
+ /**
2182
+ * @param event
2183
+ * @private
2184
+ */
2185
+ removeAllListeners(event) {
2186
+ if (!event) {
2187
+ this.removeOnGet();
2188
+ this.removeOnSet();
2189
+ }
2190
+ return super.removeAllListeners(event);
2191
+ }
2192
+ /**
2193
+ * @param characteristic
2194
+ * @private
2195
+ */
2196
+ replaceBy(characteristic) {
2197
+ this.props = characteristic.props;
2198
+ this.updateValue(characteristic.value);
2199
+ const getListeners = characteristic.listeners("get" /* CharacteristicEventTypes.GET */);
2200
+ if (getListeners.length) {
2201
+ // the callback can only be called once, so we remove all old listeners
2202
+ this.removeAllListeners("get" /* CharacteristicEventTypes.GET */);
2203
+ // @ts-expect-error: force type
2204
+ getListeners.forEach(listener => this.addListener("get" /* CharacteristicEventTypes.GET */, listener));
2205
+ }
2206
+ this.removeOnGet();
2207
+ if (characteristic.getHandler) {
2208
+ this.onGet(characteristic.getHandler);
2209
+ }
2210
+ const setListeners = characteristic.listeners("set" /* CharacteristicEventTypes.SET */);
2211
+ if (setListeners.length) {
2212
+ // the callback can only be called once, so we remove all old listeners
2213
+ this.removeAllListeners("set" /* CharacteristicEventTypes.SET */);
2214
+ // @ts-expect-error: force type
2215
+ setListeners.forEach(listener => this.addListener("set" /* CharacteristicEventTypes.SET */, listener));
2216
+ }
2217
+ this.removeOnSet();
2218
+ if (characteristic.setHandler) {
2219
+ this.onSet(characteristic.setHandler);
2220
+ }
2221
+ }
2222
+ /**
2223
+ * Returns a JSON representation of this characteristic suitable for delivering to HAP clients.
2224
+ * @private used to generate response to /accessories query
2225
+ */
2226
+ async toHAP(connection, contactGetHandlers = true) {
2227
+ const object = this.internalHAPRepresentation();
2228
+ if (!this.props.perms.includes("pr" /* Perms.PAIRED_READ */)) {
2229
+ object.value = undefined;
2230
+ }
2231
+ else if (this.UUID === Characteristic.ProgrammableSwitchEvent.UUID) {
2232
+ // special workaround for event only programmable switch event, which must always return null
2233
+ object.value = null;
2234
+ }
2235
+ else { // query the current value
2236
+ const value = contactGetHandlers
2237
+ ? await this.handleGetRequest(connection).catch(() => {
2238
+ const value = this.getDefaultValue();
2239
+ debug("[%s] Error getting value for characteristic on /accessories request. Returning default value instead: %s", this.displayName, `${value}`);
2240
+ return value; // use default value
2241
+ })
2242
+ : this.value;
2243
+ object.value = (0, request_util_1.formatOutgoingCharacteristicValue)(value, this.props);
2244
+ }
2245
+ return object;
2246
+ }
2247
+ /**
2248
+ * Returns a JSON representation of this characteristic without the value.
2249
+ * @private used to generate the config hash
2250
+ */
2251
+ internalHAPRepresentation() {
2252
+ (0, assert_1.default)(this.iid, "iid cannot be undefined for characteristic '" + this.displayName + "'");
2253
+ // TODO include the value for characteristics of the AccessoryInformation service
2254
+ return {
2255
+ type: (0, uuid_1.toShortForm)(this.UUID),
2256
+ iid: this.iid,
2257
+ value: null,
2258
+ perms: this.props.perms,
2259
+ description: this.props.description || this.displayName,
2260
+ format: this.props.format,
2261
+ unit: this.props.unit,
2262
+ minValue: this.props.minValue,
2263
+ maxValue: this.props.maxValue,
2264
+ minStep: this.props.minStep,
2265
+ maxLen: this.props.maxLen,
2266
+ maxDataLen: this.props.maxDataLen,
2267
+ "valid-values": this.props.validValues,
2268
+ "valid-values-range": this.props.validValueRanges,
2269
+ };
2270
+ }
2271
+ /**
2272
+ * Serialize characteristic into json string.
2273
+ *
2274
+ * @param characteristic - Characteristic object.
2275
+ * @private used to store characteristic on disk
2276
+ */
2277
+ static serialize(characteristic) {
2278
+ let constructorName;
2279
+ if (characteristic.constructor.name !== "Characteristic") {
2280
+ constructorName = characteristic.constructor.name;
2281
+ }
2282
+ return {
2283
+ displayName: characteristic.displayName,
2284
+ UUID: characteristic.UUID,
2285
+ eventOnlyCharacteristic: characteristic.UUID === Characteristic.ProgrammableSwitchEvent.UUID, // support downgrades for now
2286
+ constructorName: constructorName,
2287
+ value: characteristic.value,
2288
+ props: (0, clone_1.clone)({}, characteristic.props),
2289
+ };
2290
+ }
2291
+ /**
2292
+ * Deserialize characteristic from json string.
2293
+ *
2294
+ * @param json - Json string representing a characteristic.
2295
+ * @private used to recreate characteristic from disk
2296
+ */
2297
+ static deserialize(json) {
2298
+ let characteristic;
2299
+ if (json.constructorName && json.constructorName.charAt(0).toUpperCase() === json.constructorName.charAt(0)
2300
+ && Characteristic[json.constructorName]) { // MUST start with uppercase character and must exist on Characteristic object
2301
+ const constructor = Characteristic[json.constructorName];
2302
+ characteristic = new constructor();
2303
+ characteristic.displayName = json.displayName;
2304
+ characteristic.setProps(json.props);
2305
+ }
2306
+ else {
2307
+ characteristic = new Characteristic(json.displayName, json.UUID, json.props);
2308
+ }
2309
+ characteristic.value = json.value;
2310
+ return characteristic;
2311
+ }
2312
+ }
2313
+ exports.Characteristic = Characteristic;
2314
+ // We have a cyclic dependency problem. Within this file we have the definitions of "./definitions" as
2315
+ // type imports only (in order to define the static properties). Setting those properties is done outside
2316
+ // this file, within the definition files. Therefore, we import it at the end of this file. Seems weird, but is important.
2317
+ require("./definitions/CharacteristicDefinitions");
2318
+ //# sourceMappingURL=Characteristic.js.map