UNPKG

homebridge-blaq

Version:

Control and view your garage door(s) remotely with real-time updates using Konnected's BlaQ hardware

87 lines 3.9 kB
import { BaseBlaQAccessory } from './base.js'; const LOCK_PREFIX = 'lock-'; export const label = 'Lock Remotes'; /** * Platform Accessory * An instance of this class is created for each accessory your platform registers * Each accessory may expose multiple services of different service types. */ export class BlaQGarageLockAccessory extends BaseBlaQAccessory { lockService; isLocked; lockType = 'lock'; constructor(args) { super(args); this.lockService = this.getOrAddService(this.platform.service.LockMechanism); // Set the service name. This is what is displayed as the name on the Home // app. We use what we stored in `accessory.context` in `discoverDevices`. this.lockService.setCharacteristic(this.platform.characteristic.Name, this.accessory.context.device.displayName + ' ' + label); this.lockService.getCharacteristic(this.platform.characteristic.LockCurrentState) .onGet(this.getLockState.bind(this)); this.lockService.getCharacteristic(this.platform.characteristic.LockTargetState) .onSet(this.changeLockState.bind(this)); this.logger.debug(`Initialized ${this.getSelfClassName()}!`); } getLockState() { if (this.isLocked === undefined || this.isLocked === null) { return this.platform.characteristic.LockCurrentState.UNKNOWN; } return this.isLocked ? this.platform.characteristic.LockCurrentState.SECURED : this.platform.characteristic.LockCurrentState.UNSECURED; } setLockState(isLocked) { this.isLocked = isLocked; this.lockService.setCharacteristic(this.platform.characteristic.LockTargetState, this.isLocked ? this.platform.characteristic.LockTargetState.SECURED : this.platform.characteristic.LockTargetState.UNSECURED); this.lockService.setCharacteristic(this.platform.characteristic.LockCurrentState, this.getLockState()); } async changeLockState(target) { const lockDesired = target === this.platform.characteristic.LockTargetState.SECURED; const apiTarget = lockDesired ? 'lock' : 'unlock'; if (lockDesired !== this.isLocked) { await this.authFetch(`${this.apiBaseURL}/lock/${this.lockType}/${apiTarget}`, { method: 'POST' }); } } handleStateEvent(stateEvent) { super.handleStateEvent(stateEvent); if (!this.synced) { return; } try { const stateInfo = JSON.parse(stateEvent.data); if (['lock-lock', 'lock-lock_remotes'].includes(stateInfo.id)) { const buttonEvent = stateInfo; this.lockType = stateInfo.id.split(LOCK_PREFIX).pop(); if (['UNLOCKED', 'LOCKED'].includes(buttonEvent.state?.toUpperCase() || '')) { this.setLockState(buttonEvent.state?.toUpperCase() === 'LOCKED'); } } } catch (e) { this.logger.error('Cannot deserialize message:', stateEvent); this.logger.error('Deserialization yielded:', e); } } handleLogEvent(logEvent) { super.handleLogEvent(logEvent); if (!this.synced) { return; } try { const logStr = logEvent.data; const lowercaseLogStr = logStr.toLowerCase(); if (lowercaseLogStr.includes('lock') && lowercaseLogStr.includes('state') && lowercaseLogStr.includes('unlocked')) { this.setLockState(false); } else if (lowercaseLogStr.includes('lock') && lowercaseLogStr.includes('state') && lowercaseLogStr.includes('locked')) { this.setLockState(true); } } catch (e) { this.logger.error('Log parsing error:', e); } } } //# sourceMappingURL=garage-lock.js.map