Skip to content

Commit

Permalink
Code enhancements
Browse files Browse the repository at this point in the history
- Unified "internet-available" settings in ADT Pulse script.
- Rejected promises from the ADT Pulse script will not include "responsePath" information,
- Removed "Cached accessories loaded" success message.
- Devices prepared to be added will now show device or zone ID.
- Re-defined logic on obsolete accessory removal.
- Updated comments.
- Updated zone status detection logic for "formatZoneStatus".
  • Loading branch information
mrjackyliang committed Nov 27, 2019
1 parent 53f8494 commit 4ac028d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 81 deletions.
84 changes: 19 additions & 65 deletions adt-pulse.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ const accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/w

let jar;

/**
* Settings for internet-available.
*
* @since 1.0.0
*/
const hasInternetSettings = {
timeout: 5000,
retries: 3,
domainName: "portal.adtpulse.com",
port: 53,
};

/**
* Track login, login statuses, portal versions.
*
Expand Down Expand Up @@ -61,12 +73,7 @@ Pulse.prototype.login = function () {
let deferred = q.defer();
let that = this;

hasInternet({
timeout: 5000,
retries: 3,
domainName: "portal.adtpulse.com",
port: 53,
}).then(function () {
hasInternet(hasInternetSettings).then(function () {
if (authenticated) {
deferred.resolve({
"action": "LOGIN",
Expand Down Expand Up @@ -135,10 +142,6 @@ Pulse.prototype.login = function () {
"info": {
"error": error,
"message": that.getErrorMessage(body),
"response": {
"path": responsePath,
"valid": regex.test(responsePath),
},
},
});
} else {
Expand Down Expand Up @@ -190,12 +193,7 @@ Pulse.prototype.logout = function () {
let deferred = q.defer();
let that = this;

hasInternet({
timeout: 5000,
retries: 3,
domainName: "portal.adtpulse.com",
port: 53,
}).then(function () {
hasInternet(hasInternetSettings).then(function () {
if (!authenticated) {
deferred.resolve({
"action": "LOGOUT",
Expand Down Expand Up @@ -254,12 +252,7 @@ Pulse.prototype.getDeviceStatus = function () {
let deferred = q.defer();
let that = this;

hasInternet({
timeout: 5000,
retries: 3,
domainName: "portal.adtpulse.com",
port: 53,
}).then(function () {
hasInternet(hasInternetSettings).then(function () {
that.consoleLogger("ADT Pulse: Getting device information...", "log");

// Get security panel information, first.
Expand Down Expand Up @@ -293,10 +286,6 @@ Pulse.prototype.getDeviceStatus = function () {
"info": {
"error": error,
"message": that.getErrorMessage(body),
"response": {
"path": responsePath,
"valid": regex.test(responsePath),
},
},
});
} else {
Expand Down Expand Up @@ -338,10 +327,6 @@ Pulse.prototype.getDeviceStatus = function () {
"info": {
"error": error,
"message": that.getErrorMessage(body),
"response": {
"path": responsePath,
"valid": regex.test(responsePath),
},
},
});
} else {
Expand Down Expand Up @@ -419,12 +404,7 @@ Pulse.prototype.setDeviceStatus = function (armState, arm) {
let deferred = q.defer();
let that = this;

hasInternet({
timeout: 5000,
retries: 3,
domainName: "portal.adtpulse.com",
port: 53,
}).then(function () {
hasInternet(hasInternetSettings).then(function () {
/**
* Pulse URLs to set device status.
*
Expand Down Expand Up @@ -483,10 +463,6 @@ Pulse.prototype.setDeviceStatus = function (armState, arm) {
"info": {
"error": error,
"message": that.getErrorMessage(body),
"response": {
"path": responsePath,
"valid": regex.test(responsePath),
},
},
});
} else {
Expand Down Expand Up @@ -533,10 +509,6 @@ Pulse.prototype.setDeviceStatus = function (armState, arm) {
"info": {
"error": error,
"message": that.getErrorMessage(body),
"response": {
"path": responsePath,
"valid": regex.test(responsePath),
},
},
});
} else {
Expand Down Expand Up @@ -592,12 +564,7 @@ Pulse.prototype.getZoneStatus = function () {
let deferred = q.defer();
let that = this;

hasInternet({
timeout: 5000,
retries: 3,
domainName: "portal.adtpulse.com",
port: 53,
}).then(function () {
hasInternet(hasInternetSettings).then(function () {
that.consoleLogger("ADT Pulse: Getting zone status...", "log");

request.get(
Expand Down Expand Up @@ -631,10 +598,6 @@ Pulse.prototype.getZoneStatus = function () {
"info": {
"error": error,
"message": that.getErrorMessage(body),
"response": {
"path": responsePath,
"valid": regex.test(responsePath),
},
},
});
} else {
Expand Down Expand Up @@ -702,12 +665,7 @@ Pulse.prototype.performPortalSync = function () {
let deferred = q.defer();
let that = this;

hasInternet({
timeout: 5000,
retries: 3,
domainName: "portal.adtpulse.com",
port: 53,
}).then(function () {
hasInternet(hasInternetSettings).then(function () {
that.consoleLogger("ADT Pulse: Performing portal sync...", "log");

request.get(
Expand Down Expand Up @@ -742,15 +700,11 @@ Pulse.prototype.performPortalSync = function () {
"info": {
"error": error,
"message": that.getErrorMessage(body),
"response": {
"path": responsePath,
"valid": regex.test(responsePath),
},
},
});
} else {
/**
* May return status codes like this:
* May return sync codes like this:
* 1-0-0
* 2-0-0
* [integer]-0-0
Expand Down
31 changes: 16 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function ADTPulsePlatform(log, config, api) {
this.log = log;
this.config = config;

// Where the security panel and sensors are cached.
// Where the security panel and sensors are held.
this.accessories = [];
this.deviceStatus = [];
this.zoneStatus = [];
Expand Down Expand Up @@ -108,8 +108,6 @@ function ADTPulsePlatform(log, config, api) {
* and register new accessories.
*/
this.api.on("didFinishLaunching", function () {
that.logMessage("Cached accessories loaded successfully...", 40);

that.portalSync();
});
}
Expand All @@ -128,7 +126,7 @@ function ADTPulsePlatform(log, config, api) {
ADTPulsePlatform.prototype.configureAccessory = function (accessory) {
let that = this;

// Update the name to remove round brackets.
// Remove round brackets from accessory name.
accessory.displayName = accessory.displayName.replace(/[()]/gi, "");

// Get accessory information.
Expand All @@ -140,7 +138,7 @@ ADTPulsePlatform.prototype.configureAccessory = function (accessory) {
this.logMessage(`Configuring cached accessory... ${name} (${id})`, 30);
this.logMessage(accessory, 40);

// Accessory is always reachable.
// Always reachable.
accessory.updateReachability(true);

// When "Identify Accessory" is tapped.
Expand Down Expand Up @@ -300,7 +298,7 @@ ADTPulsePlatform.prototype.addAccessory = function (type, id, name, make, model,
let newAccessory = new Accessory(name, uuid);
let validAccessory = true;

// Accessory is always reachable.
// Always reachable.
newAccessory.updateReachability(true);

switch (type) {
Expand Down Expand Up @@ -481,16 +479,17 @@ ADTPulsePlatform.prototype.prepareAddAccessory = function (type, accessory) {
const deviceModel = deviceType.substr(deviceType.indexOf("-") + 2);
const deviceSummary = `"${deviceState}" / "${deviceStatus}"`.toLowerCase();

const deviceUUID = UUIDGen.generate("system-1");
const deviceId = "system-1";
const deviceUUID = UUIDGen.generate(deviceId);
const deviceLoaded = _.find(this.accessories, ["UUID", deviceUUID]);

this.logMessage("Preparing to add device accessory...", 30);
this.logMessage(`Preparing to add device (${deviceId}) accessory...`, 30);
this.logMessage(accessory, 40);

if (deviceLoaded === undefined) {
this.addAccessory(
"system",
"system-1",
deviceId,
deviceName,
deviceMake,
deviceModel,
Expand Down Expand Up @@ -530,7 +529,7 @@ ADTPulsePlatform.prototype.prepareAddAccessory = function (type, accessory) {
break;
}

this.logMessage("Preparing to add zone accessory...", 30);
this.logMessage(`Preparing to add zone (${zoneId}) accessory...`, 30);
this.logMessage(accessory, 40);

if (zoneLoaded === undefined) {
Expand Down Expand Up @@ -684,7 +683,7 @@ ADTPulsePlatform.prototype.portalSync = function () {
const zone = _.find(this.zoneStatus, {"id": id});

// Do not remove security panel(s).
if (type !== "system" && zone === undefined) {
if (zone === undefined && type !== "system") {
await this.removeAccessory(accessory);
}
});
Expand Down Expand Up @@ -1124,6 +1123,8 @@ ADTPulsePlatform.prototype.getZoneStatus = function (type, id) {
* @param {string} type - Can be "doorWindow", "glass", "motion", "co", "fire".
* @param {string} state - Can be "devStatOK", "devStatOpen", "devStatMotion", "devStatTamper", "devStatAlarm".
*
* @returns {(null|number)}
*
* @since 1.0.0
*/
ADTPulsePlatform.prototype.formatZoneStatus = function (type, state) {
Expand All @@ -1133,7 +1134,7 @@ ADTPulsePlatform.prototype.formatZoneStatus = function (type, state) {
case "doorWindow":
if (state === "devStatOK") {
status = Characteristic.ContactSensorState.CONTACT_DETECTED;
} else if (state === "devStatOpen" || state === "devStatTamper") {
} else if (["devStatOpen", "devStatTamper"].includes(state)) {
status = Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
}
break;
Expand All @@ -1150,22 +1151,22 @@ ADTPulsePlatform.prototype.formatZoneStatus = function (type, state) {
if (state === "devStatOK") {
// No motion detected.
status = false;
} else if (state === "devStatMotion" || state === "devStatTamper") {
} else if (["devStatMotion", "devStatTamper"].includes(state)) {
// Motion detected.
status = true;
}
break;
case "co":
if (state === "devStatOK") {
status = Characteristic.CarbonMonoxideDetected.CO_LEVELS_NORMAL;
} else if (state === "devStatAlarm" || state === "devStatTamper") {
} else if (["devStatAlarm", "devStatTamper"].includes(state)) {
status = Characteristic.CarbonMonoxideDetected.CO_LEVELS_ABNORMAL;
}
break;
case "fire":
if (state === "devStatOK") {
status = Characteristic.SmokeDetected.SMOKE_NOT_DETECTED;
} else if (state === "devStatAlarm" || state === "devStatTamper") {
} else if (["devStatAlarm", "devStatTamper"].includes(state)) {
status = Characteristic.SmokeDetected.SMOKE_DETECTED;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-adt-pulse",
"version": "1.4.1",
"version": "1.4.2",
"description": "Homebridge security system platform for ADT Pulse",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4ac028d

Please sign in to comment.