homebridge-kodi
Version:
Kodi plugin for Homebridge
71 lines • 3.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandSwitchAccessory = void 0;
const internal_1 = require("../../internal");
const kodi = require("../kodi");
// ==========================
// = CommandSwitchAccessory =
// ==========================
class CommandSwitchAccessory extends internal_1.KodiCommandAccessory {
constructor(platform, accessory, log, config, name, version, interval, sequence) {
super();
this.platform = platform;
this.accessory = accessory;
this.log = log;
this.config = config;
this.name = name;
this.version = version;
this.interval = interval;
this.sequence = sequence;
this.log.info('Adding CommandSwitchAccessory');
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Name, name)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'github.com DeutscheMark')
.setCharacteristic(this.platform.Characteristic.Model, 'Homebridge-Kodi CommandSwitch')
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.platform.api.hap.uuid.generate(name))
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, version);
this.switchService =
this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch);
this.switchService.setCharacteristic(this.platform.Characteristic.Name, name);
this.switchService.getCharacteristic(this.platform.Characteristic.On)
.onGet(async () => {
return false;
})
.onSet(async (on) => {
this.log.debug('Setting ' + this.name + ': ' + on);
if (on) {
for (let index = 0; index < this.sequence.length; index++) {
const commandarr = this.sequence[index].split(':');
const command = commandarr[0];
const params = commandarr[1];
const intervalValue = this.interval || 500;
setTimeout(() => {
kodi.input(this.config, command, params)
.then(result => {
if (result) {
this.log.info(this.name + ': "' + this.sequence[index] + '" command sent.');
if (index === this.sequence.length - 1) {
setTimeout(() => {
this.log.info(this.name + ': Command sequence successfully sent.');
this.switchService.getCharacteristic(this.platform.api.hap.Characteristic.On).updateValue(false);
}, 100);
}
}
else if (index === this.sequence.length - 1) {
setTimeout(() => {
this.log.info(this.name + ': Command sequence successfully sent.');
this.switchService.getCharacteristic(this.platform.api.hap.Characteristic.On).updateValue(false);
}, 100);
}
})
.catch(error => {
this.log.error(this.name + ' - Error: ' + error.message);
});
}, intervalValue * index);
}
}
});
}
}
exports.CommandSwitchAccessory = CommandSwitchAccessory;
//# sourceMappingURL=kodiCommandAccessory.js.map