UNPKG

@ronniepettersson/homebridge-dummy

Version:

Create Homebridge accessories to help with automation and control — scheduling, delays, sensors, commands, webhooks, and more

104 lines 4.36 kB
import { DummyAccessory } from './base.js'; import { strings } from '../i18n/i18n.js'; import { AccessoryType, DefaultLockState, isValidLockState, printableValues, WebhookCommand } from '../model/enums.js'; import { Webhook } from '../model/webhook.js'; import { storageGet_Deprecated, Storage } from '../tools/storage.js'; export class LockAccessory extends DummyAccessory { state; constructor(Service, Characteristic, accessory, config, log, isGrouped) { super(Service, Characteristic, accessory, config, log, isGrouped); if (!isValidLockState(this.config.defaultLockState)) { this.log.warning(strings.lock.badDefault, this.name, `'${config.defaultLockState}'`, printableValues(DefaultLockState)); } this.state = this.defaultLockState; this.accessoryService.getCharacteristic(Characteristic.LockTargetState) .onGet(this.getState.bind(this)) .onSet(this.setState.bind(this)); this.accessoryService.getCharacteristic(Characteristic.LockCurrentState) .onGet(this.getState.bind(this)); this.initializeState(); } async initializeState() { if (!this.isStateful) { this.accessoryService.updateCharacteristic(this.Characteristic.LockTargetState, this.state); this.accessoryService.updateCharacteristic(this.Characteristic.LockCurrentState, this.state); return; } const state = await storageGet_Deprecated(this.defaultStateStorageKey); if (state === undefined) { return; } await this.setState(state); } getAccessoryType() { return AccessoryType.LockMechanism; } webhooks() { return [ new Webhook(this.identifier, WebhookCommand.LockTargetState, (value) => { this.setState(value); return this.logTemplateForCV(value).replace('%s', this.name); }), ]; } get defaultLockState() { return this.config.defaultLockState === DefaultLockState.UNLOCKED ? this.Characteristic.LockTargetState.UNSECURED : this.Characteristic.LockTargetState.SECURED; } async getState() { return this.state; } async setState(value) { if (this.state !== value) { this.logLockState(value); if (this.config.commandLock && value === this.Characteristic.LockTargetState.SECURED) { this.executeCommand(this.config.commandLock); } else if (this.config.commandUnlock && value === this.Characteristic.LockTargetState.UNSECURED) { this.executeCommand(this.config.commandUnlock); } } this.state = value; if (this.isStateful) { await Storage.set(this.defaultStateStorageKey, this.state); } if (this.state !== this.defaultLockState) { this.startTimer(); } else { this.cancelTimer(); } this.accessoryService.updateCharacteristic(this.Characteristic.LockTargetState, this.state); this.accessoryService.updateCharacteristic(this.Characteristic.LockCurrentState, this.state); if (this.sensor) { if (!this.sensor.timerControlled) { this.sensor.active = this.state !== this.defaultLockState; } else if (this.state !== this.defaultLockState) { this.sensor.active = false; } } } async schedule() { if (this.state === this.defaultLockState) { const opposite = this.state === this.Characteristic.LockTargetState.SECURED ? this.Characteristic.LockTargetState.UNSECURED : this.Characteristic.LockTargetState.SECURED; await this.setState(opposite); } } async reset() { if (this.state !== this.defaultLockState) { await this.setState(this.defaultLockState); if (this.sensor?.timerControlled) { this.sensor.active = true; } } } logTemplateForCV(value) { return value === this.Characteristic.LockTargetState.SECURED ? strings.lock.secured : strings.lock.unsecured; } logLockState(value) { this.logIfDesired(this.logTemplateForCV(value)); } } //# sourceMappingURL=lock.js.map