UNPKG

21.1 kBTypeScriptView Raw
1import { EventEmitter } from "events";
2import { CharacteristicValue, Nullable, ServiceJsonObject, WithUUID } from "../types";
3import { CharacteristicWarning } from "./Accessory";
4import { Characteristic, CharacteristicChange, SerializedCharacteristic } from "./Characteristic";
5import type { AccessCode, AccessControl, AccessoryInformation, AccessoryMetrics, AccessoryRuntimeInformation, AirPurifier, AirQualitySensor, AssetUpdate, Assistant, AudioStreamManagement, Battery, CameraOperatingMode, CameraRecordingManagement, CameraRTPStreamManagement, CarbonDioxideSensor, CarbonMonoxideSensor, CloudRelay, ContactSensor, DataStreamTransportManagement, Diagnostics, Door, Doorbell, Fan, Fanv2, Faucet, FilterMaintenance, FirmwareUpdate, GarageDoorOpener, HeaterCooler, HumidifierDehumidifier, HumiditySensor, InputSource, IrrigationSystem, LeakSensor, Lightbulb, LightSensor, LockManagement, LockMechanism, Microphone, MotionSensor, NFCAccess, OccupancySensor, Outlet, Pairing, PowerManagement, ProtocolInformation, SecuritySystem, ServiceLabel, Siri, SiriEndpoint, Slats, SmartSpeaker, SmokeSensor, Speaker, StatefulProgrammableSwitch, StatelessProgrammableSwitch, Switch, TapManagement, TargetControl, TargetControlManagement, Television, TelevisionSpeaker, TemperatureSensor, Thermostat, ThreadTransport, TransferTransportManagement, Tunnel, Valve, WiFiRouter, WiFiSatellite, WiFiTransport, Window, WindowCovering } from "./definitions";
6import { IdentifierCache } from "./model/IdentifierCache";
7import { HAPConnection } from "./util/eventedhttp";
8import { HapStatusError } from "./util/hapStatusError";
9/**
10 * @group Service
11 */
12export interface SerializedService {
13 displayName: string;
14 UUID: string;
15 subtype?: string;
16 constructorName?: string;
17 hiddenService?: boolean;
18 primaryService?: boolean;
19 characteristics: SerializedCharacteristic[];
20 optionalCharacteristics?: SerializedCharacteristic[];
21}
22/**
23 * string with the format: `UUID + (subtype | "")`
24 *
25 * @group Service
26 */
27export type ServiceId = string;
28/**
29 * @group Service
30 */
31export type ServiceCharacteristicChange = CharacteristicChange & {
32 characteristic: Characteristic;
33};
34/**
35 * @group Service
36 */
37export declare const enum ServiceEventTypes {
38 CHARACTERISTIC_CHANGE = "characteristic-change",
39 SERVICE_CONFIGURATION_CHANGE = "service-configurationChange",
40 CHARACTERISTIC_WARNING = "characteristic-warning"
41}
42/**
43 * @group Service
44 */
45export declare interface Service {
46 on(event: "characteristic-change", listener: (change: ServiceCharacteristicChange) => void): this;
47 on(event: "service-configurationChange", listener: () => void): this;
48 on(event: "characteristic-warning", listener: (warning: CharacteristicWarning) => void): this;
49 emit(event: "characteristic-change", change: ServiceCharacteristicChange): boolean;
50 emit(event: "service-configurationChange"): boolean;
51 emit(event: "characteristic-warning", warning: CharacteristicWarning): boolean;
52}
53/**
54 * Service represents a set of grouped values necessary to provide a logical function. For instance, a
55 * "Door Lock Mechanism" service might contain two values, one for the "desired lock state" and one for the
56 * "current lock state". A particular Service is distinguished from others by its "type", which is a UUID.
57 * HomeKit provides a set of known Service UUIDs defined in HomeKit.ts along with a corresponding
58 * concrete subclass that you can instantiate directly to set up the necessary values. These natively-supported
59 * Services are expected to contain a particular set of Characteristics.
60 *
61 * Unlike Characteristics, where you cannot have two Characteristics with the same UUID in the same Service,
62 * you can actually have multiple Services with the same UUID in a single Accessory. For instance, imagine
63 * a Garage Door Opener with both a "security light" and a "backlight" for the display. Each light could be
64 * a "Lightbulb" Service with the same UUID. To account for this situation, we define an extra "subtype"
65 * property on Service, that can be a string or other string-convertible object that uniquely identifies the
66 * Service among its peers in an Accessory. For instance, you might have `service1.subtype = 'security_light'`
67 * for one and `service2.subtype = 'backlight'` for the other.
68 *
69 * You can also define custom Services by providing your own UUID for the type that you generate yourself.
70 * Custom Services can contain an arbitrary set of Characteristics, but Siri will likely not be able to
71 * work with these.
72 *
73 * @group Service
74 */
75export declare class Service extends EventEmitter {
76 /**
77 * @group Service Definitions
78 */
79 static AccessCode: typeof AccessCode;
80 /**
81 * @group Service Definitions
82 */
83 static AccessControl: typeof AccessControl;
84 /**
85 * @group Service Definitions
86 */
87 static AccessoryInformation: typeof AccessoryInformation;
88 /**
89 * @group Service Definitions
90 */
91 static AccessoryMetrics: typeof AccessoryMetrics;
92 /**
93 * @group Service Definitions
94 */
95 static AccessoryRuntimeInformation: typeof AccessoryRuntimeInformation;
96 /**
97 * @group Service Definitions
98 */
99 static AirPurifier: typeof AirPurifier;
100 /**
101 * @group Service Definitions
102 */
103 static AirQualitySensor: typeof AirQualitySensor;
104 /**
105 * @group Service Definitions
106 */
107 static AssetUpdate: typeof AssetUpdate;
108 /**
109 * @group Service Definitions
110 */
111 static Assistant: typeof Assistant;
112 /**
113 * @group Service Definitions
114 */
115 static AudioStreamManagement: typeof AudioStreamManagement;
116 /**
117 * @group Service Definitions
118 */
119 static Battery: typeof Battery;
120 /**
121 * @group Service Definitions
122 */
123 static CameraOperatingMode: typeof CameraOperatingMode;
124 /**
125 * @group Service Definitions
126 */
127 static CameraRecordingManagement: typeof CameraRecordingManagement;
128 /**
129 * @group Service Definitions
130 */
131 static CameraRTPStreamManagement: typeof CameraRTPStreamManagement;
132 /**
133 * @group Service Definitions
134 */
135 static CarbonDioxideSensor: typeof CarbonDioxideSensor;
136 /**
137 * @group Service Definitions
138 */
139 static CarbonMonoxideSensor: typeof CarbonMonoxideSensor;
140 /**
141 * @group Service Definitions
142 */
143 static CloudRelay: typeof CloudRelay;
144 /**
145 * @group Service Definitions
146 */
147 static ContactSensor: typeof ContactSensor;
148 /**
149 * @group Service Definitions
150 */
151 static DataStreamTransportManagement: typeof DataStreamTransportManagement;
152 /**
153 * @group Service Definitions
154 */
155 static Diagnostics: typeof Diagnostics;
156 /**
157 * @group Service Definitions
158 */
159 static Door: typeof Door;
160 /**
161 * @group Service Definitions
162 */
163 static Doorbell: typeof Doorbell;
164 /**
165 * @group Service Definitions
166 */
167 static Fan: typeof Fan;
168 /**
169 * @group Service Definitions
170 */
171 static Fanv2: typeof Fanv2;
172 /**
173 * @group Service Definitions
174 */
175 static Faucet: typeof Faucet;
176 /**
177 * @group Service Definitions
178 */
179 static FilterMaintenance: typeof FilterMaintenance;
180 /**
181 * @group Service Definitions
182 */
183 static FirmwareUpdate: typeof FirmwareUpdate;
184 /**
185 * @group Service Definitions
186 */
187 static GarageDoorOpener: typeof GarageDoorOpener;
188 /**
189 * @group Service Definitions
190 */
191 static HeaterCooler: typeof HeaterCooler;
192 /**
193 * @group Service Definitions
194 */
195 static HumidifierDehumidifier: typeof HumidifierDehumidifier;
196 /**
197 * @group Service Definitions
198 */
199 static HumiditySensor: typeof HumiditySensor;
200 /**
201 * @group Service Definitions
202 */
203 static InputSource: typeof InputSource;
204 /**
205 * @group Service Definitions
206 */
207 static IrrigationSystem: typeof IrrigationSystem;
208 /**
209 * @group Service Definitions
210 */
211 static LeakSensor: typeof LeakSensor;
212 /**
213 * @group Service Definitions
214 */
215 static Lightbulb: typeof Lightbulb;
216 /**
217 * @group Service Definitions
218 */
219 static LightSensor: typeof LightSensor;
220 /**
221 * @group Service Definitions
222 */
223 static LockManagement: typeof LockManagement;
224 /**
225 * @group Service Definitions
226 */
227 static LockMechanism: typeof LockMechanism;
228 /**
229 * @group Service Definitions
230 */
231 static Microphone: typeof Microphone;
232 /**
233 * @group Service Definitions
234 */
235 static MotionSensor: typeof MotionSensor;
236 /**
237 * @group Service Definitions
238 */
239 static NFCAccess: typeof NFCAccess;
240 /**
241 * @group Service Definitions
242 */
243 static OccupancySensor: typeof OccupancySensor;
244 /**
245 * @group Service Definitions
246 */
247 static Outlet: typeof Outlet;
248 /**
249 * @group Service Definitions
250 */
251 static Pairing: typeof Pairing;
252 /**
253 * @group Service Definitions
254 */
255 static PowerManagement: typeof PowerManagement;
256 /**
257 * @group Service Definitions
258 */
259 static ProtocolInformation: typeof ProtocolInformation;
260 /**
261 * @group Service Definitions
262 */
263 static SecuritySystem: typeof SecuritySystem;
264 /**
265 * @group Service Definitions
266 */
267 static ServiceLabel: typeof ServiceLabel;
268 /**
269 * @group Service Definitions
270 */
271 static Siri: typeof Siri;
272 /**
273 * @group Service Definitions
274 */
275 static SiriEndpoint: typeof SiriEndpoint;
276 /**
277 * @group Service Definitions
278 */
279 static Slats: typeof Slats;
280 /**
281 * @group Service Definitions
282 */
283 static SmartSpeaker: typeof SmartSpeaker;
284 /**
285 * @group Service Definitions
286 */
287 static SmokeSensor: typeof SmokeSensor;
288 /**
289 * @group Service Definitions
290 */
291 static Speaker: typeof Speaker;
292 /**
293 * @group Service Definitions
294 */
295 static StatefulProgrammableSwitch: typeof StatefulProgrammableSwitch;
296 /**
297 * @group Service Definitions
298 */
299 static StatelessProgrammableSwitch: typeof StatelessProgrammableSwitch;
300 /**
301 * @group Service Definitions
302 */
303 static Switch: typeof Switch;
304 /**
305 * @group Service Definitions
306 */
307 static TapManagement: typeof TapManagement;
308 /**
309 * @group Service Definitions
310 */
311 static TargetControl: typeof TargetControl;
312 /**
313 * @group Service Definitions
314 */
315 static TargetControlManagement: typeof TargetControlManagement;
316 /**
317 * @group Service Definitions
318 */
319 static Television: typeof Television;
320 /**
321 * @group Service Definitions
322 */
323 static TelevisionSpeaker: typeof TelevisionSpeaker;
324 /**
325 * @group Service Definitions
326 */
327 static TemperatureSensor: typeof TemperatureSensor;
328 /**
329 * @group Service Definitions
330 */
331 static Thermostat: typeof Thermostat;
332 /**
333 * @group Service Definitions
334 */
335 static ThreadTransport: typeof ThreadTransport;
336 /**
337 * @group Service Definitions
338 */
339 static TransferTransportManagement: typeof TransferTransportManagement;
340 /**
341 * @group Service Definitions
342 */
343 static Tunnel: typeof Tunnel;
344 /**
345 * @group Service Definitions
346 */
347 static Valve: typeof Valve;
348 /**
349 * @group Service Definitions
350 */
351 static WiFiRouter: typeof WiFiRouter;
352 /**
353 * @group Service Definitions
354 */
355 static WiFiSatellite: typeof WiFiSatellite;
356 /**
357 * @group Service Definitions
358 */
359 static WiFiTransport: typeof WiFiTransport;
360 /**
361 * @group Service Definitions
362 */
363 static Window: typeof Window;
364 /**
365 * @group Service Definitions
366 */
367 static WindowCovering: typeof WindowCovering;
368 displayName: string;
369 UUID: string;
370 subtype?: string;
371 iid: Nullable<number>;
372 name: Nullable<string>;
373 characteristics: Characteristic[];
374 optionalCharacteristics: Characteristic[];
375 /**
376 * @private
377 */
378 isHiddenService: boolean;
379 /**
380 * @private
381 */
382 isPrimaryService: boolean;
383 /**
384 * @private
385 */
386 linkedServices: Service[];
387 constructor(displayName: string | undefined, UUID: string, subtype?: string);
388 /**
389 * Returns an id which uniquely identifies a service on the associated accessory.
390 * The serviceId is a concatenation of the UUID for the service (defined by HAP) and the subtype (could be empty)
391 * which is programmatically defined by the programmer.
392 *
393 * @returns the serviceId
394 */
395 getServiceId(): ServiceId;
396 addCharacteristic(input: Characteristic): Characteristic;
397 addCharacteristic(input: {
398 new (...args: any[]): Characteristic;
399 }, ...constructorArgs: any[]): Characteristic;
400 /**
401 * Sets this service as the new primary service.
402 * Any currently active primary service will be reset to be not primary.
403 * This will happen immediately, if the service was already added to an accessory, or later
404 * when the service gets added to an accessory.
405 *
406 * @param isPrimary - optional boolean (default true) if the service should be the primary service
407 */
408 setPrimaryService(isPrimary?: boolean): void;
409 /**
410 * Marks the service as hidden
411 *
412 * @param isHidden - optional boolean (default true) if the service should be marked hidden
413 */
414 setHiddenService(isHidden?: boolean): void;
415 /**
416 * Adds a new link to the specified service. The service MUST be already added to
417 * the SAME accessory.
418 *
419 * @param service - The service this service should link to
420 */
421 addLinkedService(service: Service): void;
422 /**
423 * Removes a link to the specified service which was previously added with {@link addLinkedService}
424 *
425 * @param service - Previously linked service
426 */
427 removeLinkedService(service: Service): void;
428 removeCharacteristic(characteristic: Characteristic): void;
429 getCharacteristic(constructor: WithUUID<{
430 new (): Characteristic;
431 }>): Characteristic;
432 getCharacteristic(name: string | WithUUID<{
433 new (): Characteristic;
434 }>): Characteristic | undefined;
435 testCharacteristic<T extends WithUUID<typeof Characteristic>>(name: string | T): boolean;
436 /**
437 * This updates the value by calling the {@link CharacteristicEventTypes.SET} event handler associated with the characteristic.
438 * This acts the same way as when a HomeKit controller sends a `/characteristics` request to update the characteristic.
439 * An event notification will be sent to all connected HomeKit controllers which are registered
440 * to receive event notifications for this characteristic.
441 *
442 * This method behaves like a {@link Characteristic.updateValue} call with the addition that the own {@link CharacteristicEventTypes.SET}
443 * event handler is called.
444 *
445 * @param name - The name or the constructor of the desired {@link Characteristic}.
446 * @param value - The updated value.
447 *
448 * Note: If you don't want the {@link CharacteristicEventTypes.SET} to be called, refer to {@link updateCharacteristic}.
449 */
450 setCharacteristic<T extends WithUUID<{
451 new (): Characteristic;
452 }>>(name: string | T, value: CharacteristicValue): Service;
453 /**
454 * Sets the state of the characteristic to an errored state.
455 *
456 * If a {@link Characteristic.onGet} or {@link CharacteristicEventTypes.GET} handler is set up,
457 * the errored state will be ignored and the characteristic will always query the latest state by calling the provided handler.
458 *
459 * If a generic error object is supplied, the characteristic tries to extract a {@link HAPStatus} code
460 * from the error message string. If not possible a generic {@link HAPStatus.SERVICE_COMMUNICATION_FAILURE} will be used.
461 * If the supplied error object is an instance of {@link HapStatusError} the corresponding status will be used.
462 *
463 * This doesn't call any registered {@link Characteristic.onSet} or {@link CharacteristicEventTypes.SET} handlers.
464 *
465 * Have a look at the
466 * {@link https://github.com/homebridge/HAP-NodeJS/wiki/Presenting-Erroneous-Accessory-State-to-the-User Presenting Erroneous Accessory State to the User}
467 * guide for more information on how to present erroneous state to the user.
468 *
469 * @param name - The name or the constructor of the desired {@link Characteristic}.
470 * @param error - The error object
471 *
472 * Note: Erroneous state is never **pushed** to the client side. Only, if the HomeKit client requests the current
473 * state of the Characteristic, the corresponding {@link HapStatusError} is returned. As described above,
474 * any {@link Characteristic.onGet} or {@link CharacteristicEventTypes.GET} handlers have preference.
475 */
476 setCharacteristic<T extends WithUUID<{
477 new (): Characteristic;
478 }>>(name: string | T, error: HapStatusError | Error): Service;
479 /**
480 * This updates the value of the characteristic. If the value changed, an event notification will be sent to all connected
481 * HomeKit controllers which are registered to receive event notifications for this characteristic.
482 *
483 * @param name - The name or the constructor of the desired {@link Characteristic}.
484 * @param value - The new value.
485 */
486 updateCharacteristic<T extends WithUUID<{
487 new (): Characteristic;
488 }>>(name: string | T, value: Nullable<CharacteristicValue>): Service;
489 /**
490 * Sets the state of the characteristic to an errored state.
491 * If a {@link Characteristic.onGet} or {@link CharacteristicEventTypes.GET} handler is set up,
492 * the errored state will be ignored and the characteristic will always query the latest state by calling the provided handler.
493 *
494 * If a generic error object is supplied, the characteristic tries to extract a {@link HAPStatus} code
495 * from the error message string. If not possible a generic {@link HAPStatus.SERVICE_COMMUNICATION_FAILURE} will be used.
496 * If the supplied error object is an instance of {@link HapStatusError} the corresponding status will be used.
497 *
498 * @param name - The name or the constructor of the desired {@link Characteristic}.
499 * @param error - The error object
500 *
501 * Have a look at the
502 * {@link https://github.com/homebridge/HAP-NodeJS/wiki/Presenting-Erroneous-Accessory-State-to-the-User Presenting Erroneous Accessory State to the User}
503 * guide for more information on how to present erroneous state to the user.
504 *
505 * Note: Erroneous state is never **pushed** to the client side. Only, if the HomeKit client requests the current
506 * state of the Characteristic, the corresponding {@link HapStatusError} is returned. As described above,
507 * any {@link Characteristic.onGet} or {@link CharacteristicEventTypes.GET} handlers have precedence.
508 */
509 updateCharacteristic<T extends WithUUID<{
510 new (): Characteristic;
511 }>>(name: string | T, error: HapStatusError | Error): Service;
512 addOptionalCharacteristic(characteristic: Characteristic | {
513 new (): Characteristic;
514 }): void;
515 /**
516 * This method was created to copy all characteristics from another service to this.
517 * It's only adopting is currently in homebridge to merge the AccessoryInformation service. So some things
518 * may be explicitly tailored towards this use case.
519 *
520 * It will not remove characteristics which are present currently but not added on the other characteristic.
521 * It will not replace the characteristic if the value is falsy (except of '0' or 'false')
522 * @param service
523 * @private used by homebridge
524 */
525 replaceCharacteristicsFromService(service: Service): void;
526 /**
527 * @private
528 */
529 getCharacteristicByIID(iid: number): Characteristic | undefined;
530 /**
531 * @private
532 */
533 _assignIDs(identifierCache: IdentifierCache, accessoryName: string, baseIID?: number): void;
534 /**
535 * Returns a JSON representation of this service suitable for delivering to HAP clients.
536 * @private used to generate response to /accessories query
537 */
538 toHAP(connection: HAPConnection, contactGetHandlers?: boolean): Promise<ServiceJsonObject>;
539 /**
540 * Returns a JSON representation of this service without characteristic values.
541 * @private used to generate the config hash
542 */
543 internalHAPRepresentation(): ServiceJsonObject;
544 /**
545 * @private
546 */
547 private setupCharacteristicEventHandlers;
548 /**
549 * @private
550 */
551 private emitCharacteristicWarningEvent;
552 /**
553 * @private
554 */
555 private _sideloadCharacteristics;
556 /**
557 * @private
558 */
559 static serialize(service: Service): SerializedService;
560 /**
561 * @private
562 */
563 static deserialize(json: SerializedService): Service;
564}
565import "./definitions/ServiceDefinitions";
566//# sourceMappingURL=Service.d.ts.map
\No newline at end of file