UNPKG

homebridge-virtual-accessories

Version:
122 lines 5.03 kB
import { ExternalAccessory } from './externalAccessory.js'; /** * Speaker - Accessory implementation */ export class Speaker extends ExternalAccessory { static ACCESSORY_TYPE_NAME = 'Speaker'; static INACTIVE = 0; // Characteristic.Active.INACTIVE static ACTIVE = 1; // Characteristic.Active.ACTIVE static MUTED = true; // Characteristic.Mute static UNMUTED = false; // Characteristic.Mute stateStorageKey = 'SpeakerState'; muteStorageKey = 'SpeakerMuteState'; volumeStorageKey = 'SpeakerVolume'; audioAccessoryConfiguration; states = { Active: Speaker.INACTIVE, Mute: Speaker.UNMUTED, Volume: 100, }; constructor(platform, accessory, accessoryConfiguration) { super(platform, accessory, accessoryConfiguration); // First configure the device based on the accessory details this.audioAccessoryConfiguration = this.accessoryConfiguration.speaker; const mute = (this.audioAccessoryConfiguration.mute !== undefined) ? this.audioAccessoryConfiguration.mute : Speaker.UNMUTED; const volume = this.audioAccessoryConfiguration.volume; this.states.Active = Speaker.INACTIVE; this.states.Mute = mute; this.states.Volume = volume; // If the accessory is stateful retrieve stored state if (this.accessoryConfiguration.accessoryIsStateful) { const accessoryState = this.loadAccessoryState(this.storagePath); const cachedState = accessoryState[this.stateStorageKey]; const cachedMute = accessoryState[this.muteStorageKey]; const cachedVolume = accessoryState[this.volumeStorageKey]; if (cachedState !== undefined) { this.states.Active = cachedState; } if (cachedMute !== undefined) { this.states.Mute = cachedMute; } if (cachedVolume !== undefined) { this.states.Volume = cachedVolume; } } // set accessory information this.service = this.accessory.getService(this.platform.Service.Speaker) || this.accessory.addService(this.platform.Service.Speaker); this.service.setCharacteristic(this.platform.Characteristic.Name, this.accessoryConfiguration.accessoryName); // register handlers this.service.getCharacteristic(this.platform.Characteristic.Active) .onSet(this.setActive.bind(this)) .onGet(this.getActive.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.Mute) .onSet(this.setMute.bind(this)) .onGet(this.getMute.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.Volume) .onSet(this.setVolume.bind(this)) .onGet(this.getVolume.bind(this)); } // Handlers async setActive(value) { this.states.Active = value; this.storeState(); this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting State: ${Speaker.getStateName(this.states.Active)}`); } async getActive() { const speakerState = this.states.Active; this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting State: ${Speaker.getStateName(speakerState)}`); return speakerState; } async setVolume(value) { this.states.Volume = value; this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting Volume: ${this.states.Volume}`); } async getVolume() { const volume = this.states.Volume; this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting Volume: ${volume}`); return volume; } async setMute(value) { this.states.Mute = value; this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting Mute: ${this.states.Mute}`); } async getMute() { const mute = this.states.Mute; this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting Mute: ${mute}`); return mute; } getJsonState() { const jsonState = { [this.stateStorageKey]: this.states.Active, [this.muteStorageKey]: this.states.Mute, [this.volumeStorageKey]: this.states.Volume, }; const json = JSON.stringify(jsonState); return json; } getAccessoryTypeName() { return Speaker.ACCESSORY_TYPE_NAME; } static getStateName(state) { let stateName; switch (state) { case undefined: { stateName = 'undefined'; break; } case Speaker.INACTIVE: { stateName = 'INACTIVE'; break; } case Speaker.ACTIVE: { stateName = 'ACTIVE'; break; } default: { stateName = state.toString(); } } return stateName; } } //# sourceMappingURL=virtualAccessorySpeaker.js.map