homebridge-chuango-h4
Version:
A homebridge plugin for the Chuango H4 home security system.
142 lines • 7.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChuangoH4PlatformAccessory = void 0;
const chuango_h4_client_1 = require("chuango-h4-client");
/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
class ChuangoH4PlatformAccessory {
constructor(platform, accessory, connection) {
this.platform = platform;
this.accessory = accessory;
this.connection = connection;
this.targetState = 0;
this.detectors = {};
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Chuango')
.setCharacteristic(this.platform.Characteristic.Model, 'H4');
this.service = this.accessory.getService(this.platform.Service.SecuritySystem) || this.accessory.addService(this.platform.Service.SecuritySystem);
this.service.setCharacteristic(this.platform.Characteristic.Name, 'H4');
// create handlers for required characteristics
this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState)
.on('get', this.handleSecuritySystemCurrentStateGet.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemTargetState)
.on('get', this.handleSecuritySystemTargetStateGet.bind(this))
.on('set', this.handleSecuritySystemTargetStateSet.bind(this));
this.connection.on('alarm', (alarm) => {
this.platform.log.info('Got alarm: ' + JSON.stringify(alarm));
if (alarm.itemEvent === chuango_h4_client_1.ItemEventType.Alarm || alarm.itemEvent === chuango_h4_client_1.ItemEventType.AbnormalEvent || alarm.itemEvent === chuango_h4_client_1.ItemEventType.SOSAlarm) {
this.service.setCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.platform.Characteristic.SecuritySystemCurrentState.ALARM_TRIGGERED);
const detector = this.detectors[alarm.deviceID];
if (detector) {
const node = detector.device.NodesList[0];
switch (node.FuncType) {
case 'SS':
detector.service.setCharacteristic(this.platform.Characteristic.MotionDetected, true);
break;
case 'OD':
detector.service.setCharacteristic(this.platform.Characteristic.ContactSensorState, true);
break;
}
}
}
});
this.connection.on('state', (state) => {
this.platform.log.info('Alarm state changed: ' + JSON.stringify(state));
const newState = this.alarmStateToHomebridge(state);
this.service.setCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, newState);
if (!state.Alarm) {
this.targetState = newState;
this.service.setCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, newState);
}
});
this.connection.getAllDevices().then((devices) => {
for (const device of devices) {
let acc = this.accessory.getService(device.DevName.trim());
if (!acc) {
// homebridge does not know about this service, so we need to build it
const node = device.NodesList[0];
switch (node.FuncType) {
case 'SS':
acc = this.accessory.addService(this.platform.Service.MotionSensor, device.DevName.trim(), node.UUID);
break;
case 'OD':
acc = this.accessory.addService(this.platform.Service.ContactSensor, device.DevName.trim(), node.UUID);
break;
}
}
if (acc) {
// save off the device service for later
this.detectors[device.DevId] = { device: device, service: acc };
}
}
// this.platform.log.info('Connected devices:\n', JSON.stringify(devices, null, 2));
});
}
handleSecuritySystemCurrentStateGet(callback) {
this.platform.log.debug('Triggered GET SecuritySystemCurrentState');
this.connection.getCurrentAlarmState().then((state) => {
const currentValue = this.alarmStateToHomebridge(state);
this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState).updateValue(currentValue);
});
callback(null);
}
/**
* Handle requests to get the current value of the 'Security System Target State' characteristic
*/
handleSecuritySystemTargetStateGet(callback) {
this.platform.log.debug('Triggered GET SecuritySystemTargetState');
callback(null, this.targetState);
}
/**
* Handle requests to set the 'Security System Target State' characteristic
*/
handleSecuritySystemTargetStateSet(value, callback) {
this.platform.log.debug('Triggered SET SecuritySystemTargetState:', value);
if (this.targetState !== value) {
this.targetState = value;
const newState = this.homebridgeStateToArmState(value);
if (newState) {
this.connection.setAlarmState(newState);
}
}
callback(null);
}
homebridgeStateToArmState(value) {
switch (value) {
case this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM:
return chuango_h4_client_1.ArmState.Home;
case this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM:
return chuango_h4_client_1.ArmState.Armed;
case this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM:
return chuango_h4_client_1.ArmState.Home;
case this.platform.Characteristic.SecuritySystemTargetState.DISARM:
return chuango_h4_client_1.ArmState.Disarmed;
}
return null;
}
alarmStateToHomebridge(state) {
if (state.Alarm) {
return this.platform.Characteristic.SecuritySystemCurrentState.ALARM_TRIGGERED;
}
else {
switch (state.State) {
case chuango_h4_client_1.ArmState.Home:
return this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM;
case chuango_h4_client_1.ArmState.Disarmed:
return this.platform.Characteristic.SecuritySystemCurrentState.DISARMED;
case chuango_h4_client_1.ArmState.Armed:
return this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM;
case chuango_h4_client_1.ArmState.SOS:
return this.platform.Characteristic.SecuritySystemCurrentState.ALARM_TRIGGERED;
}
}
// this should never happen
throw 'Invalid alarm state encountered: ' + JSON.stringify(state);
}
}
exports.ChuangoH4PlatformAccessory = ChuangoH4PlatformAccessory;
//# sourceMappingURL=platformAccessory.js.map