File index.js added (mode: 100644) (index 0000000..7332306) |
|
1 |
|
/* jshint asi: true, node: true, laxbreak: true, laxcomma: true, undef: true, unused: true */ |
|
2 |
|
|
|
3 |
|
// there is no known webhook/websocket to use for events, this results in very frequent polling under the push-sensor model... |
|
4 |
|
|
|
5 |
|
var homespun = require('homespun-discovery') |
|
6 |
|
, pushsensor = homespun.utilities.pushsensor |
|
7 |
|
, PushSensor = pushsensor.Sensor |
|
8 |
|
, RingAPI = require('doorbot') |
|
9 |
|
, sensorTypes = homespun.utilities.sensortypes |
|
10 |
|
, underscore = require('underscore') |
|
11 |
|
, util = require('util') |
|
12 |
|
|
|
13 |
|
|
|
14 |
|
var Accessory |
|
15 |
|
, Service |
|
16 |
|
, Characteristic |
|
17 |
|
, CommunityTypes |
|
18 |
|
, UUIDGen |
|
19 |
|
|
|
20 |
|
module.exports = function (homebridge) { |
|
21 |
|
Accessory = homebridge.platformAccessory |
|
22 |
|
Service = homebridge.hap.Service |
|
23 |
|
Characteristic = homebridge.hap.Characteristic |
|
24 |
|
CommunityTypes = require('hap-nodejs-community-types')(homebridge) |
|
25 |
|
UUIDGen = homebridge.hap.uuid |
|
26 |
|
|
|
27 |
|
pushsensor.init(homebridge) |
|
28 |
|
homebridge.registerPlatform('homebridge-platform-ring-video-doorbell', 'ring-video-doorbell', Ring, true) |
|
29 |
|
} |
|
30 |
|
|
|
31 |
|
|
|
32 |
|
var Ring = function (log, config, api) { |
|
33 |
|
if (!(this instanceof Ring)) return new Ring(log, config, api) |
|
34 |
|
|
|
35 |
|
if (!config) return |
|
36 |
|
|
|
37 |
|
this.log = log |
|
38 |
|
this.config = config |
|
39 |
|
this.api = api |
|
40 |
|
|
|
41 |
|
this.options = underscore.defaults(this.config.options || {}, { retries: 5, ttl: 5, verboseP: false }) |
|
42 |
|
|
|
43 |
|
this.discoveries = {} |
|
44 |
|
this.doorbots = {} |
|
45 |
|
this.stickup_cams = {} |
|
46 |
|
|
|
47 |
|
if (api) this.api.on('didFinishLaunching', this._didFinishLaunching.bind(this)) |
|
48 |
|
else this._didFinishLaunching() |
|
49 |
|
} |
|
50 |
|
|
|
51 |
|
Ring.prototype._didFinishLaunching = function () { |
|
52 |
|
var self = this |
|
53 |
|
|
|
54 |
|
var refresh = function () { |
|
55 |
|
var ring = RingAPI({ email : self.config.username |
|
56 |
|
, password : self.config.password |
|
57 |
|
, retries : self.options.retries |
|
58 |
|
, userAgent : self.options.userAgent |
|
59 |
|
}) |
|
60 |
|
|
|
61 |
|
self.doorbot = ring |
|
62 |
|
self._refresh1(function (err) { |
|
63 |
|
if (err) { |
|
64 |
|
self.log.error('refresh1', underscore.extend({ username: self.config.username }, err)) |
|
65 |
|
return setTimeout(refresh, 30 * 1000) |
|
66 |
|
} |
|
67 |
|
|
|
68 |
|
self._refresh2(function (err) { |
|
69 |
|
if (err) { |
|
70 |
|
self.log.error('refresh2', underscore.extend({ username: self.config.username }, err)) |
|
71 |
|
return setTimeout(refresh, 30 * 1000) |
|
72 |
|
} |
|
73 |
|
|
|
74 |
|
return setTimeout(refresh, self.options.ttl * 1000) |
|
75 |
|
}) |
|
76 |
|
}) |
|
77 |
|
} |
|
78 |
|
|
|
79 |
|
refresh() |
|
80 |
|
|
|
81 |
|
self.log('didFinishLaunching') |
|
82 |
|
} |
|
83 |
|
|
|
84 |
|
Ring.prototype._addAccessory = function (device) { |
|
85 |
|
var self = this |
|
86 |
|
|
|
87 |
|
var accessory = new Accessory(device.name, device.uuid) |
|
88 |
|
|
|
89 |
|
accessory.on('identify', function (paired, callback) { |
|
90 |
|
self.log(accessory.displayName, ': identify request') |
|
91 |
|
callback() |
|
92 |
|
}) |
|
93 |
|
|
|
94 |
|
if (device.attachAccessory.bind(device)(accessory)) self.api.updatePlatformAccessories([ accessory ]) |
|
95 |
|
|
|
96 |
|
if (!self.discoveries[accessory.UUID]) { |
|
97 |
|
self.api.registerPlatformAccessories('homebridge-platform-ring-video-doorbell', 'ring-video-doorbell', [ accessory ]) |
|
98 |
|
self.log('addAccessory', underscore.pick(device, [ 'uuid', 'name', 'manufacturer', 'model', 'serialNumber' ])) |
|
99 |
|
} |
|
100 |
|
} |
|
101 |
|
|
|
102 |
|
Ring.prototype.configurationRequestHandler = function (context, request, callback) {/* jshint unused: false */ |
|
103 |
|
this.log('configuration request', { context: context, request: request }) |
|
104 |
|
} |
|
105 |
|
|
|
106 |
|
Ring.prototype.configureAccessory = function (accessory) { |
|
107 |
|
var self = this |
|
108 |
|
|
|
109 |
|
accessory.on('identify', function (paired, callback) { |
|
110 |
|
self.log(accessory.displayName, ': identify request') |
|
111 |
|
callback() |
|
112 |
|
}) |
|
113 |
|
|
|
114 |
|
self.discoveries[accessory.UUID] = accessory |
|
115 |
|
self.log('configureAccessory', underscore.pick(accessory, [ 'UUID', 'displayName' ])) |
|
116 |
|
} |
|
117 |
|
|
|
118 |
|
/* |
|
119 |
|
{ "doorbots" : |
|
120 |
|
{ "id" : ... |
|
121 |
|
, "description" : "Front Gate" |
|
122 |
|
, "device_id" : "..." |
|
123 |
|
, "time_zone" : "America\/Chicago" |
|
124 |
|
, "subscribed" : true |
|
125 |
|
, "subscribed_motions" : true |
|
126 |
|
, "battery_life" : 20 |
|
127 |
|
, "external_connection" : false |
|
128 |
|
, "firmware_version" : "1.8.73" |
|
129 |
|
, "kind" : "doorbell" |
|
130 |
|
, "latitude" : 39.8333333 |
|
131 |
|
, "longitude" : -98.585522 |
|
132 |
|
, "address" : ".... .... .., Lebanon, KS 66952 USA" |
|
133 |
|
, "settings" : { ... } |
|
134 |
|
, "features" : |
|
135 |
|
{ "motions_enabled" : true |
|
136 |
|
, "show_recordings" : true |
|
137 |
|
, "show_vod_settings" : true |
|
138 |
|
} |
|
139 |
|
, "owned" : true |
|
140 |
|
, "alerts" : |
|
141 |
|
{ "connection" : "offline" |
|
142 |
|
, "battery" : "low" |
|
143 |
|
} |
|
144 |
|
, "owner" : |
|
145 |
|
{ "id" : ... |
|
146 |
|
, "first_name" : null |
|
147 |
|
, "last_name" : null |
|
148 |
|
, "email" : "user@example.com" |
|
149 |
|
} |
|
150 |
|
} |
|
151 |
|
, "authorized_doorbots" : [ ... ] |
|
152 |
|
, "chimes" : [ ... ] |
|
153 |
|
, "stickup_cams" : [ ... ] |
|
154 |
|
} |
|
155 |
|
*/ |
|
156 |
|
|
|
157 |
|
Ring.prototype._refresh1 = function (callback) { |
|
158 |
|
var self = this |
|
159 |
|
|
|
160 |
|
self.doorbot.devices(function (err, result) { |
|
161 |
|
var serialNumbers = [] |
|
162 |
|
|
|
163 |
|
if (err) return callback(err) |
|
164 |
|
|
|
165 |
|
var handle_device = function(proto, devices, service) { |
|
166 |
|
var capabilities, properties |
|
167 |
|
, deviceId = service.id |
|
168 |
|
, device = devices[deviceId] |
|
169 |
|
|
|
170 |
|
if (!device) { |
|
171 |
|
console.log('type: ' + ((devices === self.doorbots) ? 'ringing' : 'floodlight') + ' ... ' + JSON.stringify(service, null, 2)) |
|
172 |
|
capabilities = underscore.pick(sensorTypes, |
|
173 |
|
[ 'battery_level', 'battery_low', 'motion_detected', 'reachability' ]) |
|
174 |
|
underscore.extend(capabilities, underscore.pick(sensorTypes, |
|
175 |
|
[ (devices === self.doorbots) ? 'ringing' : 'floodlight' ])) |
|
176 |
|
properties = { name : service.description |
|
177 |
|
, manufacturer : 'Bot Home Automation, Inc.' |
|
178 |
|
, model : service.kind |
|
179 |
|
, serialNumber : service.id.toString() |
|
180 |
|
, firmwareRevision : service.firmware_version |
|
181 |
|
, hardwareRevision : '' |
|
182 |
|
} |
|
183 |
|
|
|
184 |
|
device = new proto(self, service.id.toString(), { capabilities: capabilities, properties: properties }) |
|
185 |
|
devices[deviceId] = device |
|
186 |
|
} |
|
187 |
|
|
|
188 |
|
device.readings = { battery_level : service.battery_life |
|
189 |
|
, battery_low : (service.alerts) && (service.alerts.battery == 'low') |
|
190 |
|
? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW |
|
191 |
|
: Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL |
|
192 |
|
, reachability : (service.alerts) && (service.alerts.connection !== 'offline') |
|
193 |
|
, floodlight : !service.led_status ? undefined : service.led_status !== 'off' |
|
194 |
|
} |
|
195 |
|
device._update.bind(device)(device.readings) |
|
196 |
|
|
|
197 |
|
serialNumbers.push(service.id.toString()) |
|
198 |
|
} |
|
199 |
|
var check_devices = function (devices) { |
|
200 |
|
underscore.keys(devices).forEach(function (deviceId) { |
|
201 |
|
var device = devices[deviceId] |
|
202 |
|
var accessory = device.accessory |
|
203 |
|
|
|
204 |
|
if (serialNumbers.indexOf(device.serialNumber) !== -1) return |
|
205 |
|
|
|
206 |
|
if (accessory) { |
|
207 |
|
self.api.registerPlatformAccessories('homebridge-platform-ring-video-doorbell', 'ring-video-doorbell', [ accessory ]) |
|
208 |
|
self.log('removeAccessory', underscore.pick(device, [ 'uuid', 'name', 'manufacturer', 'model', 'serialNumber' ])) |
|
209 |
|
} |
|
210 |
|
|
|
211 |
|
delete devices[deviceId] |
|
212 |
|
}) |
|
213 |
|
} |
|
214 |
|
|
|
215 |
|
if (!result) result = {} |
|
216 |
|
|
|
217 |
|
if (!result.doorbots) result.doorbots = [] |
|
218 |
|
result.doorbots.forEach(function (service) { handle_device(Doorbot, self.doorbots, service) }) |
|
219 |
|
check_devices(self.doorbots) |
|
220 |
|
|
|
221 |
|
if (!result.stickup_cams) result.stickup_cams = [] |
|
222 |
|
result.stickup_cams.forEach(function (service) { handle_device(StickupCam, self.stickup_cams, service) }) |
|
223 |
|
check_devices(self.stickup_cams) |
|
224 |
|
|
|
225 |
|
callback() |
|
226 |
|
}) |
|
227 |
|
} |
|
228 |
|
|
|
229 |
|
/* |
|
230 |
|
[ |
|
231 |
|
{ |
|
232 |
|
"id" : ..., |
|
233 |
|
"id_str" : "...", |
|
234 |
|
"state" : "ringing", |
|
235 |
|
"protocol" : "sip", |
|
236 |
|
"doorbot_id" : ..., |
|
237 |
|
"doorbot_description" : "Front Gate", |
|
238 |
|
"device_kind" : "doorbell", |
|
239 |
|
"motion" : false, |
|
240 |
|
"snapshot_url" : "", |
|
241 |
|
"kind" : "ding", |
|
242 |
|
"sip_server_ip" : "a.b.c.d" |
|
243 |
|
"sip_server_port" : "15063", |
|
244 |
|
"sip_server_tls" : "false", |
|
245 |
|
"sip_session_id" : "...", |
|
246 |
|
"sip_from" : "sip:...@ring.com", |
|
247 |
|
"sip_to" : "sip:...@a.b.c.d:15063;transport=tcp", |
|
248 |
|
"audio_jitter_buffer_ms" : 0, |
|
249 |
|
"video_jitter_buffer_ms" : 0, |
|
250 |
|
"sip_endpoints" : null, |
|
251 |
|
"expires_in" : 171, |
|
252 |
|
"now" : 1483114179.70994, |
|
253 |
|
"optimization_level" : 3, |
|
254 |
|
"sip_token" : "..." |
|
255 |
|
"sip_ding_id" : "..." |
|
256 |
|
} |
|
257 |
|
] |
|
258 |
|
*/ |
|
259 |
|
|
|
260 |
|
Ring.prototype._refresh2 = function (callback) { |
|
261 |
|
var self = this |
|
262 |
|
|
|
263 |
|
self.doorbot.dings(function (err, result) { |
|
264 |
|
if (err) return callback(err) |
|
265 |
|
|
|
266 |
|
if (!util.isArray(result)) return callback(new Error('not an Array: ' + typeof result)) |
|
267 |
|
|
|
268 |
|
underscore.keys(self.doorbots).forEach(function (deviceId) { |
|
269 |
|
underscore.extend(self.doorbots[deviceId].readings, { motion_detected: false, ringing: false }) |
|
270 |
|
}) |
|
271 |
|
|
|
272 |
|
result.forEach(function (event) { |
|
273 |
|
var device |
|
274 |
|
|
|
275 |
|
if (event.state !== 'ringing') return |
|
276 |
|
|
|
277 |
|
device = self.doorbots[event.doorbot_id] || self.stickup_cams[event.doorbot_id] |
|
278 |
|
if (!device) return self.log.error('dings/active: no device', event) |
|
279 |
|
|
|
280 |
|
underscore.extend(device.readings, { motion_detected : (event.kind === 'motion') || (event.motion) |
|
281 |
|
, ringing : event.kind === 'ding' }) |
|
282 |
|
}) |
|
283 |
|
underscore.keys(self.doorbots).forEach(function (deviceId) { |
|
284 |
|
var device = self.doorbots[deviceId] |
|
285 |
|
|
|
286 |
|
device._update.bind(device)(device.readings) |
|
287 |
|
}) |
|
288 |
|
underscore.keys(self.stickup_cams).forEach(function (deviceId) { |
|
289 |
|
var device = self.stickup_cams[deviceId] |
|
290 |
|
|
|
291 |
|
device._update.bind(device)(device.readings) |
|
292 |
|
}) |
|
293 |
|
|
|
294 |
|
callback() |
|
295 |
|
}) |
|
296 |
|
} |
|
297 |
|
|
|
298 |
|
|
|
299 |
|
var Doorbot = function (platform, deviceId, service) { |
|
300 |
|
if (!(this instanceof Doorbot)) return new Doorbot(platform, deviceId, service) |
|
301 |
|
|
|
302 |
|
PushSensor.call(this, platform, deviceId, service) |
|
303 |
|
} |
|
304 |
|
util.inherits(Doorbot, PushSensor) |
|
305 |
|
|
|
306 |
|
var StickupCam = function (platform, deviceId, service) { |
|
307 |
|
var self = this |
|
308 |
|
|
|
309 |
|
if (!(this instanceof StickupCam)) return new StickupCam(platform, deviceId, service) |
|
310 |
|
|
|
311 |
|
var floodlight |
|
312 |
|
|
|
313 |
|
PushSensor.call(this, platform, deviceId, service) |
|
314 |
|
|
|
315 |
|
floodlight = self.getAccessoryService(Service.Lightbulb) |
|
316 |
|
if (!floodlight) return self.log.warn('StickupCam', { err: 'could not find Service.Lightbulb' }) |
|
317 |
|
|
|
318 |
|
console.log('!!! setting callback for on/off') |
|
319 |
|
floodlight.getCharacteristic(Characteristic.On).on('set', function (value, callback) { |
|
320 |
|
console.log('!!! set value to ' + JSON.stringify(value)) |
|
321 |
|
platform.doorbot[value ? 'lightOn' : 'lightOff']({ id: deviceId }, |
|
322 |
|
function (err, response, result) {/* jshint unused: false */ |
|
323 |
|
console.log('!!! result from doorbot.' + (value ? 'lightOn' : 'lightOff') + ': errP=' + (!!err)) |
|
324 |
|
if (err) { |
|
325 |
|
self.log.error('setValue', underscore.extend({ deviceId: deviceId }, err)) |
|
326 |
|
} else { |
|
327 |
|
self._update.bind(self)({ floodlight: value }) |
|
328 |
|
} |
|
329 |
|
|
|
330 |
|
callback() |
|
331 |
|
}) |
|
332 |
|
console.log('!!! setting value to ' + JSON.stringify(value)) |
|
333 |
|
}) |
|
334 |
|
console.log('!!! callback for on/off is now set') |
|
335 |
|
} |
|
336 |
|
util.inherits(StickupCam, PushSensor) |