homebridge-unifi-smartpower
Version:
UniFi SmartPower plugin for Homebridge.
64 lines • 2.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniFiControlSwitchPlatformAccessory = void 0;
class UniFiControlSwitchPlatformAccessory {
platform;
accessory;
log;
context;
switchName;
manufacturer;
model;
serialNumber;
timeout;
id;
enabled = false;
onCharacteristic;
constructor(platform, accessory) {
this.platform = platform;
this.accessory = accessory;
this.log = this.platform.log;
this.context = this.accessory.context;
this.switchName = this.context.switchName;
this.manufacturer = this.context.manufacturer;
this.model = this.context.model;
this.serialNumber = this.context.serialNumber;
this.timeout = this.context.timeout ?? 0;
this.id = this.serialNumber;
this.accessory
.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(this.platform.Characteristic.Model, this.model)
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.serialNumber);
this.onCharacteristic = (this.accessory.getService(this.platform.Service.Switch) ||
this.accessory.addService(this.platform.Service.Switch, this.switchName, this.id))
.setCharacteristic(this.platform.Characteristic.Name, this.switchName)
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
}
setOn(value) {
this.log.debug('[%s] Set Characteristic On ->', this.switchName, value);
this.enabled = !!value;
if (this.enabled && this.timeout > 0) {
setTimeout(() => {
// Ignore if it has already been re-disabled.
if (!this.enabled) {
return;
}
this.enabled = false;
this.log.debug('[%s] Update Characteristic On due to enabled timeout ->', this.switchName, this.enabled);
this.onCharacteristic.updateValue(this.enabled);
}, this.timeout * 1000);
}
}
getOn() {
this.log.debug('[%s] Get Characteristic On ->', this.switchName, this.enabled);
return this.isEnabled();
}
isEnabled() {
return this.enabled;
}
}
exports.UniFiControlSwitchPlatformAccessory = UniFiControlSwitchPlatformAccessory;
//# sourceMappingURL=platformAccessoryControlSwitch.js.map