Skip to content

Commit

Permalink
Removed redundant Panel status updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr Balan committed Jun 10, 2019
1 parent f02a252 commit 0b3ac78
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 60 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.exports = function (homebridge) {
([pubNub, vivintApi, cachedAccessories, {DeviceSet, deviceSet}]) => {
// add any new devices
let cachedIds = cachedAccessories.map((acc) => acc.context.id)
let newAccessories = vivintApi.deviceSnapshot()
let newAccessories = vivintApi.deviceSnapshot().d
.filter((data) => data._id && ! cachedIds.includes(data._id))
.map((deviceData) => DeviceSet.createDeviceAccessory(deviceData))
.filter((dvc) => dvc)
Expand Down
99 changes: 41 additions & 58 deletions lib/device_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ const dataPatch = require("../lib/datapatch.js")
const debounce = require("debounce-promise")
const extend = require('util')._extend

const SECURITYSYSTEM_TARGET_ARMSTAY = 0
const SECURITYSYSTEM_TARGET_ARMAWAY = 1
const SECURITYSYSTEM_TARGET_ARMNIGHT = 2
const SECURITYSYSTEM_TARGET_DISARM = 3

const SECURITYSYSTEM_VIVINT_DISARM = 0
const SECURITYSYSTEM_VIVINT_ARMSTAY = 3
const SECURITYSYSTEM_VIVINT_ARMAWAY = 4
Expand All @@ -16,7 +11,6 @@ const GARAGEDOOR_VIVINT_CLOSING = 2
const GARAGEDOOR_VIVINT_OPENING = 4
const GARAGEDOOR_VIVINT_OPEN = 5


function serialNumber(ser, ser32) {
return (ser32 || 0).toString(16).padStart(8, '0') + ':' +
(ser || 0).toString(16).padStart(8, '0')
Expand Down Expand Up @@ -64,14 +58,15 @@ function DeviceSetModule(config, log, homebridge, vivintApi, ThermostatCharacter
this.lastSnapshotTime = timestamp
log("Handling incoming device snapshot for timestamp", timestamp)

// update armed status
this.handleSecurity(deviceData.s)

for (let _deviceId in this.devicesById) {
let deviceId = parseInt(_deviceId)
let d = deviceData.find((dvc) => dvc._id == deviceId)
let d = deviceData.d.find((dvc) => dvc._id == deviceId)

if (d) this.devicesById[_deviceId].handleSnapshot(d)
}

// update armed status
this.handleSecurity(0, vivintApi.systemInfo.system.s)
}

handleMessage(msg) {
Expand All @@ -87,22 +82,25 @@ function DeviceSetModule(config, log, homebridge, vivintApi, ThermostatCharacter
this.devicesById[patch._id].handlePatch(patch)
}
})
} else if (msg.message.da.seca || msg.message.da.secd) {
this.handleSecurity((msg.message.da.seca || msg.message.da.secd), 0)
//Arm
} else if (msg.message.da.seca) {
this.handleSecurity(msg.message.da.seca.s)
//Disarm
} else if (msg.message.da.secd) {
this.handleSecurity(msg.message.da.secd.s)
}
}
}

handleSecurity(secx, systemstate) {
handleSecurity(systemstate) {
if (this.panel_Id && this.devicesById[this.panel_Id]) {

let m = {}
m.message = {
let message = {
_id: this.panel_Id,
s: (secx.s || systemstate)
s: systemstate
}

this.devicesById[this.panel_Id].handlePatch(m.message)
this.devicesById[this.panel_Id].handlePatch(message)
}
}
}
Expand Down Expand Up @@ -447,8 +445,6 @@ function DeviceSetModule(config, log, homebridge, vivintApi, ThermostatCharacter
super(accessory)
this.service = accessory.getServiceByUUIDAndSubType(Service.SecuritySystem)

this.targetPanelState = Characteristic.SecuritySystemTargetState.DISARMED

let self = this
let getter = function(fn) {
let bound = fn.bind(self)
Expand All @@ -463,10 +459,8 @@ function DeviceSetModule(config, log, homebridge, vivintApi, ThermostatCharacter
leading: true
})
return (value, next) => {
log("starting fn call value " + fn.name)
boundDebounced(value).then(
(result) => {
log("got success value " + fn.name + " " + result)
next()
},
(failure) => {
Expand All @@ -488,64 +482,53 @@ function DeviceSetModule(config, log, homebridge, vivintApi, ThermostatCharacter
}

getTargetSecuritySystemState() {
return this.targetPanelState;
return this.getCurrentSecuritySystemState()
}

setTargetSecuritySystemState(targetState) {
let vivintState = this.getVivintStateFromHomekitState(targetState)

this.targetPanelState = targetState
this.service
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.updateValue(targetState)

let vivintState = 0
switch(targetState) {
case SECURITYSYSTEM_TARGET_DISARM:
vivintState = SECURITYSYSTEM_VIVINT_DISARM
break

case SECURITYSYSTEM_TARGET_ARMAWAY:
vivintState = SECURITYSYSTEM_VIVINT_ARMAWAY
break

case SECURITYSYSTEM_TARGET_ARMSTAY:
vivintState = SECURITYSYSTEM_VIVINT_ARMSTAY
break

case SECURITYSYSTEM_TARGET_ARMNIGHT:
vivintState = SECURITYSYSTEM_VIVINT_ARMSTAY
break

default:
return Promise.reject("[WARNING] Received unknown arm state: " + targetState)
}

return vivintApi.putDevice('armedstates', null, {
armState: vivintState,
forceArm: false
})
}

getCurrentSecuritySystemState() {
switch(this.data.s){
return this.getHomekitStateFromVivintState(this.data.s)
}

getHomekitStateFromVivintState(vivintState){
switch(vivintState){
case SECURITYSYSTEM_VIVINT_DISARM:
return Characteristic.SecuritySystemCurrentState.DISARMED

case SECURITYSYSTEM_VIVINT_ARMSTAY:
if (this.targetPanelState === Characteristic.SecuritySystemTargetState.NIGHT_ARM)
return Characteristic.SecuritySystemCurrentState.NIGHT_ARM
else return Characteristic.SecuritySystemCurrentState.STAY_ARM

return Characteristic.SecuritySystemCurrentState.STAY_ARM
case SECURITYSYSTEM_VIVINT_ARMAWAY:
return Characteristic.SecuritySystemCurrentState.AWAY_ARM
}
}

default:
return Characteristic.SecuritySystemCurrentState.DISARMED
getVivintStateFromHomekitState(homekitState){
switch(homekitState){
case Characteristic.SecuritySystemCurrentState.DISARMED:
return SECURITYSYSTEM_VIVINT_DISARM
case Characteristic.SecuritySystemCurrentState.STAY_ARM:
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
return SECURITYSYSTEM_VIVINT_ARMSTAY
case Characteristic.SecuritySystemCurrentState.AWAY_ARM:
return SECURITYSYSTEM_VIVINT_ARMAWAY
}
}

notify() {
//log("Updating Security State: " + this.ssCurrentValue())
this.service
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.updateValue(this.targetPanelState)
.updateValue(this.getCurrentSecuritySystemState())

this.service
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
Expand Down Expand Up @@ -852,10 +835,8 @@ function DeviceSetModule(config, log, homebridge, vivintApi, ThermostatCharacter
leading: true
})
return (value, next) => {
log("starting fn call value " + fn.name)
boundDebounced(value).then(
(result) => {
log("got success value " + fn.name + " " + result)
next()
},
(failure) => {
Expand Down Expand Up @@ -1094,7 +1075,8 @@ function DeviceSetModule(config, log, homebridge, vivintApi, ThermostatCharacter
/**
* Handle a PubSub patch
*/
handlePatch(patch) {
/*
handlePatch(patch) {
console.log("==================")
console.log("incoming patch")
console.log(JSON.stringify(patch))
Expand All @@ -1104,6 +1086,7 @@ function DeviceSetModule(config, log, homebridge, vivintApi, ThermostatCharacter
console.log("state after")
console.log(JSON.stringify(this.data))
}
*/
}

extend(Thermostat, {
Expand Down
2 changes: 1 addition & 1 deletion lib/vivint_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function VivintApiModule(config, log) {
}

deviceSnapshot() {
return this.systemInfo.system.par[0].d
return this.systemInfo.system.par[0]
}

deviceSnapshotTs() {
Expand Down

0 comments on commit 0b3ac78

Please sign in to comment.