Skip to content

Commit

Permalink
Create SmartLock.js
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
ebaauw committed Jul 28, 2023
1 parent 90fd639 commit a0ef276
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions lib/NbService/SmartLock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// homebridge-nb/lib/NbService/SmartLock.js
// Copyright © 2020-2023 Erik Baauw. All rights reserved.
//
// Homebridge plug-in for Nuki Bridge.

'use strict'

const { ServiceDelegate } = require('homebridge-lib')
const { NbClient } = require('hb-nb-tools')
const { dateToString } = require('../NbService')

class SmartLock extends ServiceDelegate {
constructor (nbAccessory, params = {}) {
params.name = nbAccessory.name
params.Service = nbAccessory.Services.hap.LockMechanism
params.primaryService = true
super(nbAccessory, params)

this.addCharacteristicDelegate({
key: 'currentState',
Characteristic: this.Characteristics.hap.LockCurrentState
})
this.addCharacteristicDelegate({
key: 'targetState',
Characteristic: this.Characteristics.hap.LockTargetState
}).on('didSet', async (value, fromHomeKit) => {
try {
if (!fromHomeKit) {
return
}
const response = await nbAccessory.client.lockAction(
nbAccessory.id, nbAccessory.deviceType,
value === this.Characteristics.hap.LockTargetState.UNSECURED
? NbClient.LockActions.UNLOCK
: NbClient.LockActions.LOCK
)
if (response != null && response.body.success) {
this.values.currentState = value
nbAccessory.update(response.body)
}
} catch (error) { this.error(error) }
}).on('didTouch', async (value, fromHomeKit) => {
try {
if (!fromHomeKit) {
return
}
if (value === this.Characteristics.hap.LockTargetState.UNSECURED) {
await nbAccessory.client.lockAction(
nbAccessory.id, nbAccessory.deviceType,
NbClient.LockActions.UNLATCH
)
}
} catch (error) { this.error(error) }
})
this.addCharacteristicDelegate({
key: 'unlatch',
Characteristic: this.Characteristics.my.Unlatch,
value: false
}).on('didSet', async (value, fromHomeKit) => {
try {
if (!fromHomeKit) {
return
}
if (value) {
nbAccessory.update({ state: NbClient.LockStates.UNLATCHING })
setTimeout(() => {
nbAccessory.update({ state: NbClient.LockStates.UNLATCHED })
}, 3000)
const response = await nbAccessory.client.lockAction(
nbAccessory.id, nbAccessory.deviceType,
NbClient.LockActions.UNLATCH
)
if (response != null && response.body.success) {
nbAccessory.update(response.body)
nbAccessory.update({ state: NbClient.LockStates.UNLOCKED })
}
}
} catch (error) { this.error(error) }
})
this.addCharacteristicDelegate({
key: 'lastUpdated',
Characteristic: this.Characteristics.my.LastUpdated
})
this.addCharacteristicDelegate({
key: 'statusFault',
Characteristic: this.Characteristics.hap.StatusFault,
value: this.Characteristics.hap.StatusFault.NO_FAULT
})
if (this.platform.config.latch) {
this.addCharacteristicDelegate({
key: 'label',
Characteristic: this.Characteristics.hap.ServiceLabelIndex,
value: 1
})
}
}

update (state) {
if (state.state != null) {
switch (state.state) {
case NbClient.LockStates.UNLOCKED:
case NbClient.LockStates.UNLATCHED:
case NbClient.LockStates.UNLOCKED_LOCK_N_GO:
this.values.currentState = this.Characteristics.hap.LockCurrentState.UNSECURED
this.values.targetState = this.Characteristics.hap.LockTargetState.UNSECURED
this.values.statusFault = this.Characteristics.hap.StatusFault.NO_FAULT
break
case NbClient.LockStates.LOCKING:
this.values.targetState = this.Characteristics.hap.LockTargetState.SECURED
this.values.statusFault = this.Characteristics.hap.StatusFault.NO_FAULT
break
case NbClient.LockStates.LOCKED:
this.values.currentState = this.Characteristics.hap.LockCurrentState.SECURED
this.values.targetState = this.Characteristics.hap.LockTargetState.SECURED
this.values.statusFault = this.Characteristics.hap.StatusFault.NO_FAULT
break
case NbClient.LockStates.UNLOCKING:
case NbClient.LockStates.UNLATCHING:
this.values.targetState = this.Characteristics.hap.LockTargetState.UNSECURED
this.values.statusFault = this.Characteristics.hap.StatusFault.NO_FAULT
break
case NbClient.LockStates.MOTOR_BLOCKED:
this.values.currentState = this.Characteristics.hap.LockCurrentState.JAMMED
this.values.statusFault = this.Characteristics.hap.StatusFault.GENERAL_FAULT
break
case NbClient.LockStates.UNDEFINED:
default:
this.values.currentState = this.Characteristics.hap.LockCurrentState.UNKNOWN
this.values.statusFault = this.Characteristics.hap.StatusFault.GENERAL_FAULT
break
}
this.values.unlatch = [
NbClient.LockStates.UNLATCHED, NbClient.LockStates.UNLATCHING
].includes(state.state)
}
if (state.timestamp != null) {
this.values.lastUpdated = dateToString(state.timestamp)
}
}
}

module.exports = SmartLock

0 comments on commit a0ef276

Please sign in to comment.