diff --git a/README.md b/README.md index 9679f5b..9d07d8d 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ The easiest way to configure this plugin is via [Homebridge Config UI X](https:/ ], "watchGuests": true, // Optional. Set false to not monitor guest networks. "interval": 1800, // Optional. Polling interval used to query Unifi in seconds + "debounceTime": 10000, // Optional. Discard changes to occupancy that occur within the threshold in seconds "mode": "any" // Optional. Set to "any", "all" or "none". } ] diff --git a/index.js b/index.js index fd84b6c..424c903 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ 'use strict'; -const UnifiEvents = require('unifi-events') +const events = require('events'); +const Rx = require('rxjs'); +const RxOp = require('rxjs/operators'); +const UnifiEvents = require('unifi-events'); const manifest = require('./package.json'); const url = require('url'); @@ -57,6 +60,15 @@ class OccupancySensor { return this.checkOccupancy() }); + this.emitter = new events.EventEmitter(); + this.observable = Rx.fromEvent(this.emitter, 'data'); + this.observable + .pipe( + RxOp.debounceTime((config.debounceTime || 0) * 1000), + RxOp.distinctUntilChanged() + ).subscribe(value => { + this.setOccupancyDetected(value); + }); this.occupancyDetected = Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED; this.checkOccupancy(); setInterval(this.checkOccupancy.bind(this), this.interval * 1000) @@ -126,7 +138,7 @@ class OccupancySensor { } } - this.setOccupancyDetected(this.occupancyDetected) + this.emitter.emit('data', this.occupancyDetected); }) .catch((err) => { this.log(`ERROR: Failed to check occupancy: ${err.message}`) @@ -138,6 +150,7 @@ class OccupancySensor { } setOccupancyDetected(value) { + this.log(`Setting OccupancyDetected to ${value}`); return this.occupancyService.setCharacteristic(Characteristic.OccupancyDetected, value) } diff --git a/package-lock.json b/package-lock.json index dbad6d7..2c15d85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -303,6 +303,19 @@ "lodash": "^4.17.19" } }, + "rxjs": { + "version": "6.6.3", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz", + "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==", + "requires": { + "tslib": "^1.9.0" + } + }, + "rxjs-compat": { + "version": "6.6.3", + "resolved": "https://registry.npmjs.org/rxjs-compat/-/rxjs-compat-6.6.3.tgz", + "integrity": "sha512-y+wUqq7bS2dG+7rH2fNMoxsDiJ32RQzFxZQE/JdtpnmEZmwLQrb1tCiItyHxdXJHXjmHnnzFscn3b6PEmORGKw==" + }, "safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -343,6 +356,11 @@ "punycode": "^2.1.1" } }, + "tslib": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" + }, "tunnel-agent": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", diff --git a/package.json b/package.json index 987dfd0..26f44e6 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,8 @@ } ], "dependencies": { + "rxjs": "^6.6.3", + "rxjs-compat": "^6.6.3", "unifi-events": "^2.0.0" } }