Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// The returned "services" for this accessory is assumed to be the old style: a big array
// of JSON-style objects that will need to be parsed by HAP-NodeJS's AccessoryLoader.
// Create the actual HAP-NodeJS "Accessory" instance
return AccessoryLoader.parseAccessoryJSON({
displayName: displayName,
services: services
});
}
else {
// The returned "services" for this accessory are simply an array of new-API-style
// Service instances which we can add to a created HAP-NodeJS Accessory directly.
var accessoryUUID = uuid.generate(accessoryType + ":" + (uuid_base || displayName));
var accessory = new Accessory(displayName, accessoryUUID);
// listen for the identify event if the accessory instance has defined an identify() method
if (accessoryInstance.identify)
accessory.on('identify', function(paired, callback) { accessoryInstance.identify(callback); });
services.forEach(function(service) {
// if you returned an AccessoryInformation service, merge its values with ours
if (service instanceof Service.AccessoryInformation) {
var existingService = accessory.getService(Service.AccessoryInformation);
// pull out any values you may have defined
var manufacturer = service.getCharacteristic(Characteristic.Manufacturer).value;
var model = service.getCharacteristic(Characteristic.Model).value;
var serialNumber = service.getCharacteristic(Characteristic.SerialNumber).value;
var firmwareRevision = service.getCharacteristic(Characteristic.FirmwareRevision).value;
function setupAccessory(switchConfig) {
const outletUUID = uuid.v3({
namespace: uuid.namespace.url,
name: switchConfig.id
});
const outlet = new Accessory(switchConfig.name, outletUUID);
outlet
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, 'HALBERT')
.setCharacteristic(Characteristic.Model, `${switchConfig.type}-${switchConfig.protocol}-switch`)
.setCharacteristic(Characteristic.SerialNumber, switchConfig.id);
outlet.on('identify', (paired, callback) => {
console.logger.info(`${switchConfig.name} was identified. Paired: ${paired}.`);
callback();
});
outlet
.addService(Service.Outlet, switchConfig.name)
.getCharacteristic(Characteristic.On)
.on('set', (value, callback) => {
createAccessory(name, id) {
if (!name || !id) {
console.logger.error('An accessory wasn\'t supplied with a proper name or id. These two must be provided.');
return null;
}
const outletUUID = uuid.v3({
namespace: uuid.namespace.url,
name: `${this.moduleId}-${id}`
});
const accessory = new Accessory(name, outletUUID);
this.accessories.push(accessory);
console.logger.success(`Created accessory '${name}'.`);
return accessory;
}
}
function setupAccessory(switchConfig) {
const outletUUID = uuid.v3({
namespace: uuid.namespace.url,
name: switchConfig.id
});
const outlet = new Accessory(switchConfig.name, outletUUID);
outlet
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, 'HALBERT')
.setCharacteristic(
Characteristic.Model,
`${switchConfig.type}-${switchConfig.protocol}-switch`
)
.setCharacteristic(Characteristic.SerialNumber, switchConfig.id);
outlet.on('identify', (paired, callback) => {
console.logger.info(
`${switchConfig.name} was identified. Paired: ${paired}.`
);
callback();
});
createAccessory(name, id) {
if (!name || !id) {
console.logger.error(
"An accessory wasn't supplied with a proper name or id. These two must be provided."
);
return null;
}
const outletUUID = uuid.v3({
namespace: uuid.namespace.url,
name: `${this.moduleId}-${id}`
});
const accessory = new Accessory(name, outletUUID);
this.accessories.push(accessory);
console.logger.success(`Created accessory '${name}'.`);
return accessory;
}
buildAccessory(state) {
let accessory = new Accessory(this.name,
uuid.generate(this.constructor.name + this.name));
let charactersiticContactState = accessory
.addService(Service.ContactSensor, this.name)
.getCharacteristic(Characteristic.ContactSensorState);
charactersiticContactState.setValue(this.convertState(state));
charactersiticContactState.on('get', this.readOpenHabContact.bind(this));
return accessory;
}
buildAccessory(state) {
let accessory = new Accessory(
this.name, uuid.generate(this.constructor.name + this.name));
let singleStates = this.parseState(state);
let hue = +singleStates[0];
let saturation = +singleStates[1];
let brightness = +singleStates[2];
let service = accessory.addService(Service.Lightbulb, this.name);
let charactersiticOnOff =
service.getCharacteristic(Characteristic.On);
charactersiticOnOff.setValue(brightness > 0);
charactersiticOnOff.on('set', this.updateOpenHabBrightness.bind(this));
charactersiticOnOff.on('get', this.readOpenHabPowerState.bind(this));
let charactersiticBrightness =
buildAccessory(state) {
let accessory = new Accessory(this.name,
uuid.generate(this.constructor.name + this.name));
let charactersiticOnOff = accessory
.addService(Service.Lightbulb, this.name)
.getCharacteristic(Characteristic.On);
charactersiticOnOff.setValue(state === 'ON');
charactersiticOnOff.on('set', this.updateOpenHabItem.bind(this));
charactersiticOnOff.on('get', this.readOpenHabPowerState.bind(this));
return accessory;
};
const uuid = hap.uuid.generate(config.id + (config.uuidAddition ? config.uuidAddition : ''));
let acc;
this.bridge.bridgedAccessories.forEach(a => {
if (a.UUID === uuid) {
acc = a;
}
});
if (acc) {
this.debug('already existing accessory ' + config.id + ' ' + config.name);
} else if (this.bridge.bridgedAccessories.length >= 150) {
this.error('maximum of 150 accessories per bridge exceeded, can\'t add ' + config.id + ' ' + config.name);
} else {
this.debug('addAccessory ' + config.id + ' ' + config.name);
acc = new hap.Accessory(config.name, uuid, hap.Accessory.Categories.OTHER);
this.bridge.addBridgedAccessory(acc);
}
this.waitForAccessories();
return acc;
}
}