UNPKG

4.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.DoorbellController = void 0;
4const Characteristic_1 = require("../Characteristic");
5const Service_1 = require("../Service");
6const CameraController_1 = require("./CameraController");
7/**
8 * The `DoorbellController` to efficiently manage doorbell implementations with HAP-NodeJS.
9 *
10 * NOTICE: We subclass from the {@link CameraController} here and deliberately do not introduce/set an
11 * own/custom ControllerType for Doorbells, as Cameras and Doorbells are pretty much the same thing
12 * and would collide otherwise.
13 * As the possibility exists, both the CameraController and DoorbellController are written to support migration
14 * from one to another. Meaning a serialized CameraController can be initialized as a DoorbellController
15 * (on startup in {@link initWithServices}) and vice versa.
16 *
17 * @group Doorbell
18 */
19class DoorbellController extends CameraController_1.CameraController {
20 doorbellService;
21 doorbellServiceExternallySupplied = false;
22 /**
23 * Temporary storage. Erased after init.
24 */
25 doorbellOptions;
26 /**
27 * Initializes a new `DoorbellController`.
28 * @param options - The {@link CameraControllerOptions} and optional {@link DoorbellOptions}.
29 */
30 constructor(options) {
31 super(options);
32 this.doorbellOptions = {
33 name: options.name,
34 externalDoorbellService: options.externalDoorbellService,
35 };
36 }
37 /**
38 * Call this method to signal a doorbell button press.
39 */
40 ringDoorbell() {
41 this.doorbellService.updateCharacteristic(Characteristic_1.Characteristic.ProgrammableSwitchEvent, Characteristic_1.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
42 }
43 constructServices() {
44 if (this.doorbellOptions?.externalDoorbellService) {
45 this.doorbellService = this.doorbellOptions.externalDoorbellService;
46 this.doorbellServiceExternallySupplied = true;
47 }
48 else {
49 this.doorbellService = new Service_1.Service.Doorbell(this.doorbellOptions?.name ?? "", "");
50 }
51 this.doorbellService.setPrimaryService();
52 const serviceMap = super.constructServices();
53 if (!this.doorbellServiceExternallySupplied) {
54 serviceMap.doorbell = this.doorbellService;
55 }
56 return serviceMap;
57 }
58 initWithServices(serviceMap) {
59 const result = super._initWithServices(serviceMap);
60 if (this.doorbellOptions?.externalDoorbellService) {
61 this.doorbellService = this.doorbellOptions.externalDoorbellService;
62 this.doorbellServiceExternallySupplied = true;
63 if (result.serviceMap.doorbell) {
64 delete result.serviceMap.doorbell;
65 result.updated = true;
66 }
67 }
68 else {
69 this.doorbellService = result.serviceMap.doorbell;
70 if (!this.doorbellService) { // see NOTICE above
71 this.doorbellService = new Service_1.Service.Doorbell(this.doorbellOptions?.name ?? "", "");
72 result.serviceMap.doorbell = this.doorbellService;
73 result.updated = true;
74 }
75 }
76 this.doorbellService.setPrimaryService();
77 if (result.updated) {
78 return result.serviceMap;
79 }
80 }
81 // eslint-disable-next-line @typescript-eslint/no-unused-vars
82 migrateFromDoorbell(serviceMap) {
83 return false;
84 }
85 retrieveEventTriggerOptions() {
86 const result = super.retrieveEventTriggerOptions();
87 result.add(2 /* EventTriggerOption.DOORBELL */);
88 return result;
89 }
90 handleControllerRemoved() {
91 super.handleControllerRemoved();
92 this.doorbellService = undefined;
93 }
94 configureServices() {
95 super.configureServices();
96 this.doorbellService.getCharacteristic(Characteristic_1.Characteristic.ProgrammableSwitchEvent)
97 .onGet(() => null); // a value of null represent nothing is pressed
98 this.doorbellOptions = undefined;
99 }
100}
101exports.DoorbellController = DoorbellController;
102//# sourceMappingURL=DoorbellController.js.map
\No newline at end of file