From 4ac028deb4afb6c83095d82ba0306b869738993c Mon Sep 17 00:00:00 2001 From: Jacky Liang Date: Wed, 27 Nov 2019 17:41:41 -0500 Subject: [PATCH] Code enhancements - 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". --- adt-pulse.js | 84 ++++++++++++---------------------------------------- index.js | 31 +++++++++---------- package.json | 2 +- 3 files changed, 36 insertions(+), 81 deletions(-) diff --git a/adt-pulse.js b/adt-pulse.js index 463aafb..0557943 100644 --- a/adt-pulse.js +++ b/adt-pulse.js @@ -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. * @@ -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", @@ -135,10 +142,6 @@ Pulse.prototype.login = function () { "info": { "error": error, "message": that.getErrorMessage(body), - "response": { - "path": responsePath, - "valid": regex.test(responsePath), - }, }, }); } else { @@ -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", @@ -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. @@ -293,10 +286,6 @@ Pulse.prototype.getDeviceStatus = function () { "info": { "error": error, "message": that.getErrorMessage(body), - "response": { - "path": responsePath, - "valid": regex.test(responsePath), - }, }, }); } else { @@ -338,10 +327,6 @@ Pulse.prototype.getDeviceStatus = function () { "info": { "error": error, "message": that.getErrorMessage(body), - "response": { - "path": responsePath, - "valid": regex.test(responsePath), - }, }, }); } else { @@ -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. * @@ -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 { @@ -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 { @@ -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( @@ -631,10 +598,6 @@ Pulse.prototype.getZoneStatus = function () { "info": { "error": error, "message": that.getErrorMessage(body), - "response": { - "path": responsePath, - "valid": regex.test(responsePath), - }, }, }); } else { @@ -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( @@ -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 diff --git a/index.js b/index.js index 9879b35..9d8eb64 100644 --- a/index.js +++ b/index.js @@ -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 = []; @@ -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(); }); } @@ -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. @@ -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. @@ -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) { @@ -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, @@ -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) { @@ -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); } }); @@ -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) { @@ -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; @@ -1150,7 +1151,7 @@ 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; } @@ -1158,14 +1159,14 @@ ADTPulsePlatform.prototype.formatZoneStatus = function (type, state) { 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; diff --git a/package.json b/package.json index 599594c..adafe63 100644 --- a/package.json +++ b/package.json @@ -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": {