homebridge-virtual-accessories
Version:
Virtual HomeKit accessories for Homebridge.
63 lines • 2.6 kB
JavaScript
import { Accessory } from './accessory.js';
/**
* Microphone - Accessory implementation
*/
export class Microphone extends Accessory {
static ACCESSORY_TYPE_NAME = 'Microphone';
muteStorageKey = 'MicrophoneMute';
states = {
Mute: false,
Volume: 100,
};
constructor(platform, accessory, accessoryConfiguration) {
super(platform, accessory, accessoryConfiguration);
// First configure the device based on the accessory details
this.states.Volume = this.accessoryConfiguration.microphone.volume;
const accessoryState = this.loadAccessoryState(this.storagePath);
if (!this.isEmptyAccessoryState(accessoryState)) {
const cachedDoorbellMute = accessoryState[this.muteStorageKey];
if (cachedDoorbellMute !== undefined) {
this.states.Mute = cachedDoorbellMute;
}
}
this.service = this.accessory.getService(this.platform.Service.Microphone) || this.accessory.addService(this.platform.Service.Microphone);
this.service.setCharacteristic(this.platform.Characteristic.Name, this.accessoryConfiguration.accessoryName);
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 setMute(value) {
this.states.Mute = value;
this.storeState();
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;
}
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;
}
getJsonState() {
const jsonState = {
[this.muteStorageKey]: this.states.Mute,
};
const json = JSON.stringify(jsonState);
return json;
}
getAccessoryTypeName() {
return Microphone.ACCESSORY_TYPE_NAME;
}
}
//# sourceMappingURL=virtualAccessoryMicrophone.js.map