Skip to content

Commit

Permalink
Code rearrangement and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkormendy committed Jul 15, 2020
1 parent f85ac3f commit a80a51b
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ADCPlatform {
* @param api The homebridge API.
*/
constructor(log, config, api) {

this.log = log
this.config = config || { platform: PLUGIN_NAME }
this.debug = this.config.debug || false
Expand Down Expand Up @@ -205,8 +206,7 @@ class ADCPlatform {
this.log(`Logging into Alarm.com as ${this.config.username}`)
}

return nodeADC
.login(this.config.username, this.config.password)
return nodeADC.login(this.config.username, this.config.password)
.then(authOpts => {
// Cache login response and estimated expiration time
authOpts.expires = +new Date() + 1000 * 60 * this.config.authTimeoutMinutes
Expand Down Expand Up @@ -252,7 +252,12 @@ class ADCPlatform {
.then(res => fetchStateForAllSystems(res))
.then(systemStates => {

// writes systemStates payload to a file for debug/troubleshooting
if (this.logLevel > 3) {
this.writePayload(this.api.user.storagePath() + '/', 'ADC-SystemStates.json', JSON.stringify(systemStates))
}

// break out system components
systemStates.forEach(system => {

if (system.partitions) {
Expand All @@ -264,9 +269,9 @@ class ADCPlatform {
this.statPartitionState(accessory, partition)
})
} else {
throw new Error('Alarm.com: No partitions found, check configuration with security system provider')
// fatal error, we require partitions and cannot continue
throw new Error('No partitions found, check configuration with security system provider')
}
// fatal error, need to catch and report in general log output

if (system.sensors) {
system.sensors.forEach(sensor => {
Expand All @@ -276,19 +281,10 @@ class ADCPlatform {
}
this.statSensorState(accessory, sensor)
})
} else {
if (this.logLevel > 2)
this.log('No sensors found, ignore if expected, or check configuration with security system provider')
}
// need to catch and report in loglevel debug

if (system.locks) {
system.locks.forEach(lock => {
const accessory = this.accessories[lock.id]
if (!accessory) {
return this.addLock(lock)
}
this.statLockState(accessory, lock)
})
}
// need to catch and report in loglevel debug

if (system.light) {
system.light.forEach(light => {
Expand All @@ -298,8 +294,23 @@ class ADCPlatform {
}
this.statLightState(accessory, light)
})
} else {
if (this.logLevel > 2)
this.log('No lights found, ignore if expected, or check configuration with security system provider')
}

if (system.locks) {
system.locks.forEach(lock => {
const accessory = this.accessories[lock.id]
if (!accessory) {
return this.addLock(lock)
}
this.statLockState(accessory, lock)
})
} else {
if (this.logLevel > 2)
this.log('No locks found, ignore if expected, or check configuration with security system provider')
}
// need to catch and report in loglevel debug

})
})
Expand Down Expand Up @@ -422,8 +433,7 @@ class ADCPlatform {

if (desiredState !== accessory.context.desiredState) {
if (this.logLevel > 2) {
this.log(`Updating partition ${name} (${id}), desiredState=${desiredState}, prev=${accessory.context.desiredState}`
)
this.log(`Updating partition ${name} (${id}), desiredState=${desiredState}, prev=${accessory.context.desiredState}`)
}

accessory.context.desiredState = desiredState
Expand Down Expand Up @@ -615,6 +625,8 @@ class ADCPlatform {
)
const [type, characteristic, model] = getSensorType(sensor)



if (state !== accessory.context.state) {
if (this.logLevel > 2) {
this.log(`Updating sensor ${name} (${id}), state=${state}, prev=${accessory.context.state}`)
Expand All @@ -639,7 +651,7 @@ class ADCPlatform {
.updateValue(batteryLow)
}
}

/* Sensors only report state, no ability to change their state. */


Expand Down Expand Up @@ -1030,7 +1042,7 @@ class ADCPlatform {

const id = accessory.context.accID
if (this.logLevel > 2) {
this.log(`Removing ${accessory.context.name} (${id}) from HomeBridge.`)
this.log(`Removing ${accessory.context.name} (${id}) from HomeBridge`)
}
this.api.unregisterPlatformAccessories(PLUGIN_ID, PLUGIN_NAME, [accessory])
delete this.accessories[id]
Expand Down Expand Up @@ -1098,6 +1110,7 @@ function fetchStateForAllSystems(res) {
* @returns {*} The state as nodeADC defines it.
*/
function getPartitionState(state) {
// console.log(`${sensor.attributes.description} Sensor (${sensor.id}) is ${sensor.attributes.stateText}.`)
switch (state) {
case nodeADC.SYSTEM_STATES.ARMED_STAY:
return Characteristic.SecuritySystemCurrentState.STAY_ARM
Expand All @@ -1119,8 +1132,7 @@ function getPartitionState(state) {
* @returns {*} The state as nodeADC defines it.
*/
function getSensorState(sensor) {
// if (sensor.attributes.description == 'Master Motion')
// console.log(sensor)
// console.log(`${sensor.attributes.description} Sensor (${sensor.id}) is ${sensor.attributes.stateText}.`)
switch (sensor.attributes.state) {
case nodeADC.SENSOR_STATES.OPEN:
return Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
Expand Down Expand Up @@ -1210,7 +1222,7 @@ function getSensorType(sensor) {
}

/**
* Maps an Alarm.com sensor model to its type represented in homebridge.
* Maps an Alarm.com sensor model to its type represented in homebridge/homekit.
*
* @param model The model as reported by Alarm.com.
* @returns {array} An array with homebridge service and characteristic types.
Expand All @@ -1235,4 +1247,4 @@ function sensorModelToType(model) {
default:
return [undefined, undefined]
}
}
}

0 comments on commit a80a51b

Please sign in to comment.