UNPKG

@ronniepettersson/homebridge-dummy

Version:

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

163 lines 6.92 kB
import { OnOffAccessory } from './onoff.js'; import { strings } from '../../i18n/i18n.js'; import { AccessoryType, WebhookCommand } from '../../model/enums.js'; import { Webhook } from '../../model/webhook.js'; import { storageGet_Deprecated, Storage } from '../../tools/storage.js'; const NO_BRIGHTNESS = -1; export class LightbulbAccessory extends OnOffAccessory { brightness; hue; saturation; colorTemperature; constructor(Service, Characteristic, accessory, config, log, isGrouped) { super(Service, Characteristic, accessory, config, log, isGrouped); this.brightness = this.config.defaultBrightness ?? NO_BRIGHTNESS; this.hue = this.config.defaultHue ?? 0; this.saturation = this.config.defaultSaturation ?? 0; this.colorTemperature = this.config.defaultColorTemperature ?? 0; if (this.isDimmer) { this.accessoryService.getCharacteristic(this.Characteristic.Brightness) .onGet(this.getBrightness.bind(this)) .onSet(this.setBrightness.bind(this)); if (this.isStateful && Storage.has(this.defaultBrightnessStorageKey)) { this.brightness = Storage.get(this.defaultBrightnessStorageKey) ?? this.brightness; } else { this.initializeBrightness_Deprecated(); } this.accessoryService.getCharacteristic(this.Characteristic.Hue) .onGet(this.getHue.bind(this)) .onSet(this.setHue.bind(this)); if (this.isStateful && Storage.has(this.defaultHueStorageKey)) { this.hue = Storage.get(this.defaultHueStorageKey) ?? this.hue; } this.accessoryService.getCharacteristic(this.Characteristic.Saturation) .onGet(this.getSaturation.bind(this)) .onSet(this.setSaturation.bind(this)); if (this.isStateful && Storage.has(this.defaultSaturationStorageKey)) { this.saturation = Storage.get(this.defaultSaturationStorageKey) ?? this.saturation; } this.accessoryService.getCharacteristic(this.Characteristic.ColorTemperature) .onGet(this.getColorTemperature.bind(this)) .onSet(this.setColorTemperature.bind(this)); if (this.isStateful && Storage.has(this.defaultColorTemperatureStorageKey)) { this.colorTemperature = Storage.get(this.defaultColorTemperatureStorageKey) ?? this.colorTemperature; } } } get isDimmer() { return this.brightness !== NO_BRIGHTNESS; } get defaultBrightnessStorageKey() { return `${this.identifier}:Brightness`; } get defaultHueStorageKey() { return `${this.identifier}:Hue`; } get defaultSaturationStorageKey() { return `${this.identifier}:Saturation`; } get defaultColorTemperatureStorageKey() { return `${this.identifier}:ColorTemperature`; } getAccessoryType() { return AccessoryType.Lightbulb; } webhooks() { return [ ...super.webhooks(), new Webhook(this.identifier, WebhookCommand.Brightness, (value) => { this.setBrightness(value); return strings.lightbulb.brightness.replace('%s', this.name).replace('%d', value.toString()); }), new Webhook(this.identifier, WebhookCommand.Hue, (value) => { this.setHue(value); return strings.lightbulb.hue.replace('%s', this.name).replace('%d', value.toString()); }), new Webhook(this.identifier, WebhookCommand.Saturation, (value) => { this.setSaturation(value); return strings.lightbulb.saturation.replace('%s', this.name).replace('%d', value.toString()); }), new Webhook(this.identifier, WebhookCommand.ColorTemperature, (value) => { this.setColorTemperature(value); return strings.lightbulb.colorTemperature.replace('%s', this.name).replace('%d', value.toString()); }), ]; } async initializeBrightness_Deprecated() { if (!this.isStateful) { this.accessoryService.updateCharacteristic(this.Characteristic.Brightness, this.brightness); return; } const brightness = await storageGet_Deprecated(this.defaultBrightnessStorageKey); if (brightness === undefined) { return; } await this.setBrightness(brightness); } logMessageForOnState(value) { if (this.isDimmer && value) { return strings.lightbulb.stateOn.replace('%d', this.brightness.toLocaleString()); } else { return super.logMessageForOnState(value); } } async getBrightness() { return this.brightness; } async setBrightness(value) { if (this.brightness === value) { return; } this.brightness = value; this.logIfDesired(strings.lightbulb.brightness, this.brightness.toString()); if (this.isStateful) { await Storage.set(this.defaultBrightnessStorageKey, this.brightness); } this.accessoryService.updateCharacteristic(this.Characteristic.Brightness, this.brightness); } async getHue() { return this.hue; } async setHue(value) { if (this.hue === value) { return; } this.hue = value; this.logIfDesired(strings.lightbulb.hue, this.hue.toString()); if (this.isStateful) { await Storage.set(this.defaultHueStorageKey, this.hue); } this.accessoryService.updateCharacteristic(this.Characteristic.Hue, this.hue); } async getSaturation() { return this.saturation; } async setSaturation(value) { if (this.saturation === value) { return; } this.saturation = value; this.logIfDesired(strings.lightbulb.saturation, this.saturation.toString()); if (this.isStateful) { await Storage.set(this.defaultSaturationStorageKey, this.saturation); } this.accessoryService.updateCharacteristic(this.Characteristic.Saturation, this.saturation); } async getColorTemperature() { return this.colorTemperature; } async setColorTemperature(value) { if (this.colorTemperature === value) { return; } this.colorTemperature = value; this.logIfDesired(strings.lightbulb.colorTemperature, this.colorTemperature.toString()); if (this.isStateful) { await Storage.set(this.defaultColorTemperatureStorageKey, this.colorTemperature); } this.accessoryService.updateCharacteristic(this.Characteristic.ColorTemperature, this.colorTemperature); } } //# sourceMappingURL=lightbulb.js.map