UNPKG

@ronniepettersson/homebridge-dummy

Version:

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

88 lines 2.97 kB
import { DummyAccessory } from '../base.js'; import { strings } from '../../i18n/i18n.js'; import { Webhook } from '../../model/webhook.js'; import { storageGet_Deprecated, Storage } from '../../tools/storage.js'; import { WebhookCommand } from '../../model/enums.js'; export class OnOffAccessory extends DummyAccessory { on; constructor(Service, Characteristic, accessory, config, log, isGrouped) { super(Service, Characteristic, accessory, config, log, isGrouped); this.on = this.defaultOn; this.accessoryService.getCharacteristic(Characteristic.On) .onGet(this.getOn.bind(this)) .onSet(this.setOn.bind(this)); this.initializeOn(); } webhooks() { return [ new Webhook(this.identifier, WebhookCommand.On, (value) => { this.setOn(value); return this.logMessageForOnState(value).replace('%s', this.name); }), ]; } async initializeOn() { if (!this.isStateful) { this.accessoryService.updateCharacteristic(this.Characteristic.On, this.on); return; } const on = await storageGet_Deprecated(this.defaultStateStorageKey); if (on === undefined) { return; } await this.setOn(on); } get defaultOn() { return this.config.defaultOn ? true : false; } async getOn() { return this.on; } async setOn(value) { if (this.on !== value) { this.logIfDesired(this.logMessageForOnState(value)); if (this.config.commandOn && value) { this.executeCommand(this.config.commandOn); } else if (this.config.commandOff && !value) { this.executeCommand(this.config.commandOff); } } this.on = value; if (this.isStateful) { await Storage.set(this.defaultStateStorageKey, this.on); } if (this.on !== this.defaultOn) { this.startTimer(); } else { this.cancelTimer(); } this.accessoryService.updateCharacteristic(this.Characteristic.On, this.on); if (this.sensor) { if (!this.sensor.timerControlled) { this.sensor.active = this.on !== this.defaultOn; } else if (this.on !== this.defaultOn) { this.sensor.active = false; } } } async schedule() { if (this.on === this.defaultOn) { await this.setOn(!this.on); } } async reset() { if (this.on !== this.defaultOn) { await this.setOn(this.defaultOn); if (this.sensor?.timerControlled) { this.sensor.active = true; } } } logMessageForOnState(value) { return value ? strings.onOff.stateOn : strings.onOff.stateOff; } } //# sourceMappingURL=onoff.js.map