@petro-kushchak/homebridge-homepod-radio
Version:
Homebridge accessory for streaming radio to Homepod Mini and Apple TV
91 lines • 4.07 kB
JavaScript
import { PLUGIN_MANUFACTURER, PLUGIN_MODEL } from './platformConstants.js';
import { AirPlayDevice } from './lib/airplayDevice.js';
import * as os from 'os';
import * as path from 'path';
export class HomepodAudioSwitchAccessory {
platform;
accessory;
audioConfig;
playbackController;
device;
service;
informationService;
constructor(platform, accessory, audioConfig, playbackController) {
this.platform = platform;
this.accessory = accessory;
this.audioConfig = audioConfig;
this.playbackController = playbackController;
this.device = new AirPlayDevice(this.platform.platformConfig.homepodId, platform.logger, platform.platformConfig.verboseMode, this.streamerName(), '', '');
this.service =
this.accessory.getService(this.platform.Service.Switch) ||
this.accessory.addService(this.platform.Service.Switch);
this.service
.getCharacteristic(this.platform.Characteristic.On)
.on("get" /* CharacteristicEventTypes.GET */, (callback) => {
this.platform.logger.debug(`[${this.streamerName()}] Getting State On: ${this.isPlaying()}`);
callback(undefined, this.isPlaying());
})
.on("set" /* CharacteristicEventTypes.SET */, (value, callback) => {
this.platform.logger.info(`[${this.streamerName()}] Setting State On: ${value}`);
if (value) {
this.startPlaying();
}
else {
this.stopPlaying();
}
callback();
});
this.informationService =
this.accessory.getService(this.platform.Service.AccessoryInformation) ||
this.accessory.addService(this.platform.Service.AccessoryInformation);
this.informationService
.setCharacteristic(this.platform.Characteristic.Manufacturer, PLUGIN_MANUFACTURER)
.setCharacteristic(this.platform.Characteristic.Model, PLUGIN_MODEL)
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.platform.platformConfig.serialNumber)
.setCharacteristic(this.platform.Characteristic.Name, this.audioConfig.name);
// This will do its best to keep the actual outputs status up to date with Homekit.
setInterval(async () => {
this.service.getCharacteristic(this.platform.Characteristic.On).updateValue(this.isPlaying());
}, 3000);
this.playbackController.addStreamer(this);
this.platform.logger.info(`[${this.streamerName()}] Finished initializing`);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async volumeUpdated(homepodId, volume) {
return await Promise.resolve();
}
async stopRequested(source) {
this.platform.logger.info(`[${this.streamerName()}] Stopping playback - received stop request from ${source.streamerName()} `);
await this.device.stop();
}
async shutdownRequested() {
return await Promise.resolve();
}
async platformLaunched() {
return await Promise.resolve();
}
streamerName() {
return this.audioConfig.name;
}
isPlaying() {
return this.device.isPlaying();
}
async startPlaying() {
await this.playbackController.requestStop(this);
const mediaPath = this.platform.platformConfig.mediaPath || os.homedir();
const filePath = path.join(mediaPath, this.audioConfig.fileName);
await this.device.playFile(filePath, this.audioConfig.volume);
await this.playbackController.updateVolume(this.platform.platformConfig.homepodId, this.audioConfig.volume);
}
async stopPlaying() {
await this.device.stop();
}
/*
* This method is called directly after creation of this instance.
* It should return all services which should be added to the accessory.
*/
getServices() {
return [this.informationService, this.service];
}
}
//# sourceMappingURL=platformAudioSwitchAccessory.js.map