Skip to content

Commit

Permalink
Refactor garage door handling;
Browse files Browse the repository at this point in the history
Fix error logging
  • Loading branch information
balansse committed Jun 29, 2022
1 parent 6e40d98 commit 9614d3f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 45 deletions.
6 changes: 3 additions & 3 deletions lib/accessories/dimmer_switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DimmerSwitch extends Device {
.then(
(success) => next(),
(failure) => {
log.error("Failure setting dimmer switch state:", failure)
this.log.error("Failure setting dimmer switch state:", failure)
next(failure)
})
} else {
Expand All @@ -67,7 +67,7 @@ class DimmerSwitch extends Device {
.then(
(success) => next(),
(failure) => {
log.error("Failure setting dimmer switch state:", failure)
this.log.error("Failure setting dimmer switch state:", failure)
next(failure)
})
}
Expand All @@ -85,7 +85,7 @@ class DimmerSwitch extends Device {
.then(
(success) => next(),
(failure) => {
log.error("Failure setting dimmer brightness state:", failure)
this.log.error("Failure setting dimmer brightness state:", failure)
next(failure)
})
}
Expand Down
56 changes: 18 additions & 38 deletions lib/accessories/garage_door.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,29 @@ class GarageDoor extends Device {

this.service
.getCharacteristic(this.Characteristic.CurrentDoorState)
.on('get', (next) => next(null, this.doorCurrentValue()));
.on('get', (callback) => callback(null, this.getGarageDoorState()));

this.service
.getCharacteristic(this.Characteristic.TargetDoorState)
.on('get', (next) => next(null, this.doorCurrentValue()))
.on('set', this.setDoorTargetStateCharacteristic.bind(this))
}

//TODO: refactor this
setDoorTargetStateCharacteristic(targetState, next) {
if (targetState) {
this.service
.getCharacteristic(this.Characteristic.TargetDoorState)
.updateValue(this.Characteristic.TargetDoorState.CLOSED)
//.on('get', (callback) => callback(null, this.doorCurrentValue()))
.on('set', async (state, callback) => this.setTargetState(state, callback))

this.vivintApi.putDevice('door', this.id, {
s: VivintDict.GarageDoorStates.Closing,
_id: this.id
})
.then(
(success) => next(),
(failure) => {
log.error("Failure setting garage door state:", failure)
next(failure)
})
} else {
this.service
.getCharacteristic(this.Characteristic.TargetDoorState)
.updateValue(this.Characteristic.TargetDoorState.OPEN)
this.notify()
}

this.vivintApi.putDevice('door', this.id, {
s: VivintDict.GarageDoorStates.Opening,
_id: this.id
})
.then(
(success) => next(),
(failure) => {
log.error("Failure setting garage door state:", failure)
next(failure)
})
async setTargetState(targetState, callback) {
let locked = (targetState == this.Characteristic.LockTargetState.SECURED)
try {
callback()
await this.vivintApi.setGarageDoorState(this.id, locked)
}
catch (err) {
this.log.error("Failure setting garage door state:", err)
callback(new Error(`An error occurred while setting the garage door state: ${err}`))
}
}

doorCurrentValue() {
getGarageDoorState() {
switch(this.data.Status){
case VivintDict.GarageDoorStates.Unknown: // unknown state but this eliminates double notification
case VivintDict.GarageDoorStates.Closed:
Expand All @@ -74,8 +53,9 @@ class GarageDoor extends Device {
notify() {
super.notify()
if (this.service) {
this.service.getCharacteristic(this.Characteristic.CurrentDoorState)
.updateValue(this.doorCurrentValue())
let state = this.getGarageDoorState()
this.service.updateCharacteristic(this.Characteristic.CurrentDoorState, state)
this.service.updateCharacteristic(this.Characteristic.TargetDoorState, state == this.Characteristic.TargetDoorState.CLOSED)
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/accessories/light_switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class LightSwitch extends Device {
.then(
(success) => next(),
(failure) => {
log.error("Failure setting switch state:", failure)
this.log.error("Failure setting switch state:", failure)
next(failure)
})
} else {
Expand All @@ -39,7 +39,7 @@ class LightSwitch extends Device {
.then(
(success) => next(),
(failure) => {
log.error("Failure setting switch state:", failure)
this.log.error("Failure setting switch state:", failure)
next(failure)
})
}
Expand Down
3 changes: 1 addition & 2 deletions lib/accessories/lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class Lock extends Device {
constructor(accessory, data, config, log, homebridge, vivintApi) {
super(accessory, data, config, log, homebridge, vivintApi)

this.log = log
this.service = accessory.getService(this.Service.LockMechanism)

this.batteryService.updateCharacteristic(this.Characteristic.ChargingState, this.Characteristic.ChargingState.NOT_CHARGEABLE)
Expand All @@ -23,7 +22,7 @@ class Lock extends Device {
}

async setTargetState(targetState, callback) {
let locked = (targetState == this.Characteristic.LockCurrentState.SECURED)
let locked = (targetState == this.Characteristic.LockTargetState.SECURED)
try {
callback()
await this.vivintApi.setLockState(this.id, locked)
Expand Down
4 changes: 4 additions & 0 deletions lib/vivint_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ function VivintApiModule(config, log) {
return mapObject(message, VivintDict)
}

async setGarageDoorState(garageDoorId, newState) {
return await this.putDevice('door', garageDoorId, { [VivintDict.Fields.Id]: garageDoorId, [VivintDict.Fields.Status]: newState } )
}

async setLockState(lockId, newState) {
return await this.putDevice('locks', lockId, { [VivintDict.Fields.Id]: lockId, [VivintDict.Fields.Status]: newState } )
}
Expand Down

0 comments on commit 9614d3f

Please sign in to comment.