Skip to content

Instantly share code, notes, and snippets.

@legotheboss
Created November 26, 2016 05:20
Show Gist options
  • Save legotheboss/b92ffd29495109a5c409c638307aa00f to your computer and use it in GitHub Desktop.
Save legotheboss/b92ffd29495109a5c409c638307aa00f to your computer and use it in GitHub Desktop.
motionaccessory
var Accessory = require('../').Accessory;
var Service = require('../').Service;
var Characteristic = require('../').Characteristic;
var uuid = require('../').uuid;
// here's a fake hardware device that we'll expose to HomeKit
var MOTION_SENSOR = {
motionDetected: false,
getStatus: function() {
//set the boolean here, this will be returned to the device
MOTION_SENSOR.motionDetected = false;
},
identify: function() {
console.log("Identify the motion sensor!");
}
}
// Generate a consistent UUID for our Motion Sensor Accessory that will remain the same even when
// restarting our server. We use the `uuid.generate` helper function to create a deterministic
// UUID based on an arbitrary "namespace" and the word "motionSensor".
var motionSensorUUID = uuid.generate('hap-nodejs:accessories:motion');
// This is the Accessory that we'll return to HAP-NodeJS that represents our fake motionSensor.
var motionSensor = exports.accessory = new Accessory('Motion Sensor', motionSensorUUID);
// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
motionSensor.username = "1A:2B:3C:4D:5E:FF";
motionSensor.pincode = "031-45-154";
// set some basic properties (these values are arbitrary and setting them is optional)
motionSensor
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Oltica")
.setCharacteristic(Characteristic.Model, "Rev-1")
.setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW");
// listen for the "identify" event for this Accessory
motionSensor.on('identify', function(paired, callback) {
MOTION_SENSOR.identify();
callback(); // success
});
motionSensor
.addService(Service.motionSensor, "Motion Sensor") // services exposed to the user should have "names" like "Fake Light" for us
.getCharacteristic(Characteristic.MotionDetected)
.setCharacteristic(Characteristic.MotionDetected)
.on('get', function(callback) {
// this event is emitted when you ask Siri directly whether your motionSensor is on or not. you might query
// the motion sensor hardware itself to find this out, then call the callback. But if you take longer than a
// few seconds to respond, Siri will give up.
var err = null; // in case there were any problems
if (MOTION_SENSOR.motionDetected) {
console.log("Was there motion?");
callback(err, true);
}
else {
console.log("Are we on? No.");
callback(err, false);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment