homebridge-unifi-smartpower
Version:
UniFi SmartPower plugin for Homebridge.
264 lines • 13.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniFiRpsPortPlatformAccessory = exports.UniFiSwitchPortPlatformAccessory = exports.UniFiSmartPowerOutletPlatformAccessory = void 0;
const uniFiSmartPower_1 = require("./uniFiSmartPower");
class UniFiSmartPowerOutletPlatformAccessory {
platform;
accessory;
outlet;
isEnabled;
log;
hap;
uniFiSmartPower;
context;
outletIndex;
outletName;
serialNumber;
id;
status = uniFiSmartPower_1.UniFiSmartPowerOutletState.UNKNOWN;
inUse = uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN;
constructor(platform, accessory, outlet, isEnabled) {
this.platform = platform;
this.accessory = accessory;
this.outlet = outlet;
this.isEnabled = isEnabled;
this.log = this.platform.log;
this.hap = this.platform.api.hap;
this.uniFiSmartPower = this.platform.uniFiSmartPower;
this.context = this.accessory.context;
this.serialNumber = this.context.device.serialNumber;
this.outletIndex = this.outlet.index;
this.outletName = this.outlet.name;
this.id = `${this.serialNumber}.${this.outletIndex}`;
const outletService = (this.accessory.getServiceById(this.platform.Service.Outlet, this.id) ||
this.accessory.addService(this.platform.Service.Outlet, this.outletName, this.id))
.setCharacteristic(this.platform.Characteristic.Name, this.outletName)
.setCharacteristic(this.platform.Characteristic.ConfiguredName, this.outletName);
const statusCharacteristic = outletService
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
const inUseCharacteristic = outlet.inUse !== uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN
? outletService
.getCharacteristic(this.platform.Characteristic.OutletInUse)
.onGet(this.getInUse.bind(this))
: null;
this.uniFiSmartPower.subscribe(this.context.device, uniFiSmartPower_1.UniFiDeviceKind.OUTLET, this.outletIndex, (({ relayState, inUse }) => {
if (this.status !== relayState && relayState !== uniFiSmartPower_1.UniFiSmartPowerOutletState.UNKNOWN) {
this.log.debug('[%s] Received outlet subscription status update: %s -> %s', this.outletName, uniFiSmartPower_1.UniFiSmartPowerOutletState[this.status], uniFiSmartPower_1.UniFiSmartPowerOutletState[relayState]);
this.status = relayState;
statusCharacteristic.updateValue(!!relayState);
}
if (inUseCharacteristic !== null &&
this.inUse !== inUse &&
inUse !== uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN) {
this.log.debug('[%s] Received outlet subscription InUse update: %s -> %s', this.outletName, uniFiSmartPower_1.UniFiPortOrOutletInUse[this.inUse], uniFiSmartPower_1.UniFiPortOrOutletInUse[inUse]);
this.inUse = inUse;
inUseCharacteristic.updateValue(!!inUse);
}
}));
}
async setOn(value) {
if (!this.isEnabled()) {
this.log.info('[%s] Cannot set Characteristic On when control is disabled', this.outletName);
throw new this.hap.HapStatusError(-70404 /* this.hap.HAPStatus.READ_ONLY_CHARACTERISTIC */);
}
this.log.debug('[%s] Set Characteristic On ->', this.outletName, value);
try {
await this.uniFiSmartPower.commandOutlet(this.context.device, this.outletIndex, value ? uniFiSmartPower_1.UniFiSmartPowerOutletAction.ON : uniFiSmartPower_1.UniFiSmartPowerOutletAction.OFF);
}
catch (error) {
this.log.error('[%s] An error occurred setting Characteristic On; %s', this.outletName, error.message);
throw new this.hap.HapStatusError(-70402 /* this.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
}
}
getOn() {
if (this.status === uniFiSmartPower_1.UniFiSmartPowerOutletState.UNKNOWN) {
throw new this.hap.HapStatusError(-70412 /* this.hap.HAPStatus.NOT_ALLOWED_IN_CURRENT_STATE */);
}
this.log.debug('[%s] Get Characteristic On ->', this.outletName, uniFiSmartPower_1.UniFiSmartPowerOutletState[this.status]);
return this.status === uniFiSmartPower_1.UniFiSmartPowerOutletState.ON;
}
getInUse() {
if (this.inUse === uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN) {
throw new this.hap.HapStatusError(-70412 /* this.hap.HAPStatus.NOT_ALLOWED_IN_CURRENT_STATE */);
}
this.log.debug('[%s] Get Characteristic InUse ->', this.outletName, uniFiSmartPower_1.UniFiPortOrOutletInUse[this.inUse]);
return this.inUse === uniFiSmartPower_1.UniFiPortOrOutletInUse.YES;
}
}
exports.UniFiSmartPowerOutletPlatformAccessory = UniFiSmartPowerOutletPlatformAccessory;
class UniFiSwitchPortPlatformAccessory {
platform;
accessory;
port;
isEnabled;
log;
hap;
uniFiSmartPower;
context;
portIndex;
portName;
portPoeOnAction;
serialNumber;
id;
poeMode = 'unknown';
inUse = uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN;
constructor(platform, accessory, port, isEnabled) {
this.platform = platform;
this.accessory = accessory;
this.port = port;
this.isEnabled = isEnabled;
this.log = this.platform.log;
this.hap = this.platform.api.hap;
this.uniFiSmartPower = this.platform.uniFiSmartPower;
this.context = this.accessory.context;
this.serialNumber = this.context.device.serialNumber;
this.portIndex = this.port.index;
this.portName = this.port.name;
this.portPoeOnAction = this.port.poeOnAction;
this.id = `${this.serialNumber}.${this.portIndex}`;
const outletService = (this.accessory.getServiceById(this.platform.Service.Outlet, this.id) ||
this.accessory.addService(this.platform.Service.Outlet, this.portName, this.id))
.setCharacteristic(this.platform.Characteristic.Name, this.portName)
.setCharacteristic(this.platform.Characteristic.ConfiguredName, this.portName);
const statusCharacteristic = outletService
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
const inUseCharacteristic = port.inUse !== uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN
? outletService
.getCharacteristic(this.platform.Characteristic.OutletInUse)
.onGet(this.getInUse.bind(this))
: null;
this.uniFiSmartPower.subscribe(this.context.device, uniFiSmartPower_1.UniFiDeviceKind.PORT, this.portIndex, (({ poeMode, inUse, }) => {
if (this.poeMode !== poeMode && poeMode !== 'unknown') {
this.log.debug('[%s] Received port subscription status update: %s -> %s', this.portName, this.poeMode.toUpperCase(), poeMode.toUpperCase());
this.poeMode = poeMode;
statusCharacteristic.updateValue(this.poeMode !== 'off');
}
if (inUseCharacteristic !== null &&
this.inUse !== inUse &&
inUse !== uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN) {
this.log.debug('[%s] Received port subscription InUse update: %s -> %s', this.portName, uniFiSmartPower_1.UniFiPortOrOutletInUse[this.inUse], uniFiSmartPower_1.UniFiPortOrOutletInUse[inUse]);
this.inUse = inUse;
inUseCharacteristic.updateValue(!!inUse);
}
}));
}
async setOn(value) {
if (!this.isEnabled()) {
this.log.info('[%s] Cannot set Characteristic On when control is disabled', this.portName);
throw new this.hap.HapStatusError(-70404 /* this.hap.HAPStatus.READ_ONLY_CHARACTERISTIC */);
}
this.log.debug('[%s] Set Characteristic On ->', this.portName, value);
try {
await this.uniFiSmartPower.commandPort(this.context.device, this.portIndex, value ? this.portPoeOnAction : 'off');
}
catch (error) {
this.log.error('[%s] An error occurred setting Characteristic On; %s', this.portName, error.message);
throw new this.hap.HapStatusError(-70402 /* this.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
}
}
getOn() {
if (this.poeMode === 'unknown') {
throw new this.hap.HapStatusError(-70412 /* this.hap.HAPStatus.NOT_ALLOWED_IN_CURRENT_STATE */);
}
const isOn = this.poeMode !== 'off';
this.log.debug('[%s] Get Characteristic On ->', this.portName, isOn ? 'ON' : 'OFF');
return isOn;
}
getInUse() {
if (this.inUse === uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN) {
throw new this.hap.HapStatusError(-70412 /* this.hap.HAPStatus.NOT_ALLOWED_IN_CURRENT_STATE */);
}
this.log.debug('[%s] Get Characteristic InUse ->', this.portName, uniFiSmartPower_1.UniFiPortOrOutletInUse[this.inUse]);
return this.inUse === uniFiSmartPower_1.UniFiPortOrOutletInUse.YES;
}
}
exports.UniFiSwitchPortPlatformAccessory = UniFiSwitchPortPlatformAccessory;
class UniFiRpsPortPlatformAccessory {
platform;
accessory;
rpsPort;
isEnabled;
log;
hap;
uniFiSmartPower;
context;
portIndex;
portName;
serialNumber;
id;
portMode = 'unknown';
inUse = uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN;
constructor(platform, accessory, rpsPort, isEnabled) {
this.platform = platform;
this.accessory = accessory;
this.rpsPort = rpsPort;
this.isEnabled = isEnabled;
this.log = this.platform.log;
this.hap = this.platform.api.hap;
this.uniFiSmartPower = this.platform.uniFiSmartPower;
this.context = this.accessory.context;
this.serialNumber = this.context.device.serialNumber;
this.portIndex = this.rpsPort.index;
this.portName = this.rpsPort.name;
this.id = `${this.serialNumber}.${this.portIndex}`;
const outletService = (this.accessory.getServiceById(this.platform.Service.Outlet, this.id) ||
this.accessory.addService(this.platform.Service.Outlet, this.portName, this.id))
.setCharacteristic(this.platform.Characteristic.Name, this.portName)
.setCharacteristic(this.platform.Characteristic.ConfiguredName, this.portName);
const statusCharacteristic = outletService
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
const inUseCharacteristic = rpsPort.inUse !== uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN
? outletService
.getCharacteristic(this.platform.Characteristic.OutletInUse)
.onGet(this.getInUse.bind(this))
: null;
this.uniFiSmartPower.subscribe(this.context.device, uniFiSmartPower_1.UniFiDeviceKind.RPS_PORT, this.portIndex, (({ portMode, inUse }) => {
if (this.portMode !== portMode) {
this.log.debug('[%s] Received RPS port subscription status update: %s -> %s', this.portName, this.portMode.toUpperCase(), portMode.toUpperCase());
this.portMode = portMode;
statusCharacteristic.updateValue(this.portMode !== 'disabled');
}
if (inUseCharacteristic !== null &&
this.inUse !== inUse &&
inUse !== uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN) {
this.log.debug('[%s] Received RPS port subscription InUse update: %s -> %s', this.portName, uniFiSmartPower_1.UniFiPortOrOutletInUse[this.inUse], uniFiSmartPower_1.UniFiPortOrOutletInUse[inUse]);
this.inUse = inUse;
inUseCharacteristic.updateValue(!!inUse);
}
}));
}
async setOn(value) {
if (!this.isEnabled()) {
this.log.info('[%s] Cannot set Characteristic On when control is disabled', this.portName);
throw new this.hap.HapStatusError(-70404 /* this.hap.HAPStatus.READ_ONLY_CHARACTERISTIC */);
}
this.log.debug('[%s] Set Characteristic On ->', this.portName, value);
try {
await this.uniFiSmartPower.commandRpsPort(this.context.device, this.portIndex, value ? uniFiSmartPower_1.UniFiRpsPortAction.ON : uniFiSmartPower_1.UniFiRpsPortAction.OFF);
}
catch (error) {
this.log.error('[%s] An error occurred setting Characteristic On; %s', this.portName, error.message);
throw new this.hap.HapStatusError(-70402 /* this.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
}
}
getOn() {
const isOn = this.portMode !== 'disabled';
this.log.debug('[%s] Get Characteristic On ->', this.portName, isOn ? 'ON' : 'OFF');
return isOn;
}
getInUse() {
if (this.inUse === uniFiSmartPower_1.UniFiPortOrOutletInUse.UNKNOWN) {
throw new this.hap.HapStatusError(-70412 /* this.hap.HAPStatus.NOT_ALLOWED_IN_CURRENT_STATE */);
}
this.log.debug('[%s] Get Characteristic InUse ->', this.portName, uniFiSmartPower_1.UniFiPortOrOutletInUse[this.inUse]);
return this.inUse === uniFiSmartPower_1.UniFiPortOrOutletInUse.YES;
}
}
exports.UniFiRpsPortPlatformAccessory = UniFiRpsPortPlatformAccessory;
//# sourceMappingURL=platformAccessory.js.map