author | Luke Hoersten <luke@hoersten.org> |
Mon, 16 Nov 2020 13:35:10 -0600 | |
changeset 30 | 14c6ce90a512 |
parent 29 | 04bc5820407b |
permissions | -rw-r--r-- |
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
1 |
const hap = require("hap-nodejs"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
2 |
const pysh = require("python-shell"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
3 |
|
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
4 |
const Accessory = hap.Accessory; |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
5 |
const Characteristic = hap.Characteristic; |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
6 |
const CharacteristicEventTypes = hap.CharacteristicEventTypes; |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
7 |
const AccessoryEventTypes = hap.AccessoryEventTypes; |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
8 |
const Service = hap.Service; |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
9 |
const uuid = hap.uuid; |
0 | 10 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
11 |
const accessoryUuid = hap.uuid.generate("hap.accessories.doorbell-lock"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
12 |
const accessory = new Accessory("Doorbell and Lock", accessoryUuid); |
0 | 13 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
14 |
const doorbellService = new Service.Doorbell("Doorbell"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
15 |
const lockService = new Service.LockMechanism("Lock"); |
0 | 16 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
17 |
const lockTargetStateCharacteristic = lockService.getCharacteristic(Characteristic.LockTargetState); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
18 |
const lockCurrentStateCharacteristic = lockService.getCharacteristic(Characteristic.LockCurrentState); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
19 |
|
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
20 |
const doorbellSwitchCharacteristic = doorbellService.getCharacteristic(Characteristic.ProgrammableSwitchEvent); |
0 | 21 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
22 |
function Door() { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
23 |
this.lockTimeout = 10000; // milliseconds - 10 sec |
0 | 24 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
25 |
this.pyshell = new pysh.PythonShell("doord.py", { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
26 |
mode: "text", |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
27 |
pythonPath: "/usr/bin/python3", |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
28 |
pythonOptions: ["-u"], |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
29 |
scriptPath: "python/" |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
30 |
}); |
0 | 31 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
32 |
this.lock = () => { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
33 |
console.log("locking door"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
34 |
this.pyshell.send("lock"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
35 |
}; |
0 | 36 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
37 |
this.unlock = () => { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
38 |
console.log("unlocking door"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
39 |
this.pyshell.send("unlock"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
40 |
}; |
0 | 41 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
42 |
this.identify = () => { console.log("identify door"); }; |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
43 |
|
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
44 |
this.listenDoorbell = (doorbellOnCallback, doorbellOffCallback) => { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
45 |
this.pyshell.on("message", (message) => { |
0 | 46 |
console.log(message); |
47 |
switch(message) { |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
48 |
case "doorbell on": |
0 | 49 |
doorbellOnCallback(); |
50 |
break; |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
51 |
case "doorbell off": |
0 | 52 |
doorbellOffCallback(); |
53 |
break; |
|
54 |
} |
|
55 |
}); |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
56 |
}; |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
57 |
} |
0 | 58 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
59 |
const door = new Door(); |
0 | 60 |
|
61 |
function setDoorTargetState(value) { |
|
62 |
switch(value) { |
|
63 |
case Characteristic.LockTargetState.UNSECURED: |
|
64 |
unlockDoor(); |
|
65 |
break; |
|
66 |
case Characteristic.LockTargetState.SECURED: |
|
67 |
lockDoor(); |
|
68 |
break; |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
function unlockDoor() { |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
73 |
door.unlock(); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
74 |
lockCurrentStateCharacteristic.setValue(Characteristic.LockCurrentState.UNSECURED); |
0 | 75 |
scheduleUnlockTimeout(); |
76 |
} |
|
77 |
||
78 |
function lockDoor() { |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
79 |
door.lock(); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
80 |
lockCurrentStateCharacteristic.setValue(Characteristic.LockCurrentState.SECURED); |
0 | 81 |
} |
82 |
||
83 |
function scheduleUnlockTimeout() { |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
84 |
setTimeout(() => { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
85 |
console.log("unlock timeout door"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
86 |
lockTargetStateCharacteristic.setValue(Characteristic.LockTargetState.SECURED); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
87 |
}, door.lockTimeout); |
0 | 88 |
} |
89 |
||
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
90 |
door.listenDoorbell( |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
91 |
() => { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
92 |
doorbellSwitchCharacteristic.setValue(Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS); |
0 | 93 |
}, |
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
94 |
() => {} |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
95 |
); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
96 |
|
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
97 |
// set initial state |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
98 |
lockTargetStateCharacteristic.setValue(Characteristic.LockTargetState.SECURED); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
99 |
lockCurrentStateCharacteristic.setValue(Characteristic.LockCurrentState.SECURED); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
100 |
lockTargetStateCharacteristic.on(CharacteristicEventTypes.SET, (value, callback) => { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
101 |
setDoorTargetState(value); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
102 |
callback(); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
103 |
}); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
104 |
|
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
105 |
accessory.on(AccessoryEventTypes.IDENTIFY, (paired, callback) => { |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
106 |
door.identify(); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
107 |
callback(); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
108 |
}); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
109 |
|
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
110 |
accessory.addService(doorbellService); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
111 |
accessory.addService(lockService); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
112 |
|
29
04bc5820407b
Added pseudo-camera back to doorbell so the chime can be controlled. Doorbell settings are tied to the camera in home app.
Luke Hoersten <luke@hoersten.org>
parents:
26
diff
changeset
|
113 |
// pseudo-services |
04bc5820407b
Added pseudo-camera back to doorbell so the chime can be controlled. Doorbell settings are tied to the camera in home app.
Luke Hoersten <luke@hoersten.org>
parents:
26
diff
changeset
|
114 |
accessory.addService(Service.CameraRTPStreamManagement, "Pseudo-Camera"); |
04bc5820407b
Added pseudo-camera back to doorbell so the chime can be controlled. Doorbell settings are tied to the camera in home app.
Luke Hoersten <luke@hoersten.org>
parents:
26
diff
changeset
|
115 |
accessory.addService(Service.Speaker, "Pseudo-Speaker"); |
04bc5820407b
Added pseudo-camera back to doorbell so the chime can be controlled. Doorbell settings are tied to the camera in home app.
Luke Hoersten <luke@hoersten.org>
parents:
26
diff
changeset
|
116 |
accessory.addService(Service.Microphone, "Pseudo-Microphone"); |
04bc5820407b
Added pseudo-camera back to doorbell so the chime can be controlled. Doorbell settings are tied to the camera in home app.
Luke Hoersten <luke@hoersten.org>
parents:
26
diff
changeset
|
117 |
|
26
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
118 |
accessory.getService(Service.AccessoryInformation) |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
119 |
.setCharacteristic(Characteristic.Manufacturer, "Raspberry Pi") |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
120 |
.setCharacteristic(Characteristic.Model, "Zero W") |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
121 |
.setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW"); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
122 |
|
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
123 |
accessory.publish({ |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
124 |
username: "C1:5D:3A:EA:54:AB", |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
125 |
pincode: "031-45-154", |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
126 |
category: hap.Categories.DOOR_LOCK |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
127 |
}); |
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
128 |
|
87ddaf81e63c
Updated homekit door for Raspbian Buster.
Luke Hoersten <luke@hoersten.org>
parents:
22
diff
changeset
|
129 |
console.log("Accessory initialized"); |