Skip to content
This repository was archived by the owner on May 29, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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".
}
]
Expand Down
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}`)
Expand All @@ -138,6 +150,7 @@ class OccupancySensor {
}

setOccupancyDetected(value) {
this.log(`Setting OccupancyDetected to ${value}`);
return this.occupancyService.setCharacteristic(Characteristic.OccupancyDetected, value)
}

Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
}
],
"dependencies": {
"rxjs": "^6.6.3",
"rxjs-compat": "^6.6.3",
"unifi-events": "^2.0.0"
}
}