homebridge-connect-my-pool-home-automation
Version:
HomeKit integration for Astral Viron Gateway via Home Automation
125 lines • 5.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelAccessory = void 0;
const action_1 = require("../action");
const status_1 = require("../status");
const accessory_1 = require("./accessory");
const settings_1 = require("../settings");
/**
* Channel Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
class ChannelAccessory extends accessory_1.Accessory {
constructor(platform, accessory, device, status) {
super(platform, accessory, device, status);
this.stateOn = false;
this.stateContactOn = this.Characteristic.ContactSensorState.CONTACT_DETECTED;
this.channelsModes = [];
this.stateName = this.deviceName;
this.channelConfigStatus = this.getChannelsConfigStatus(status);
this.channelsModes.push({ key: 0, value: 'Off' });
this.channelsModes.push({ key: 1, value: 'Auto' });
this.channelsModes.push({ key: 2, value: 'On' });
this.channelsModes.push({ key: 3, value: 'Low Speed' });
this.channelsModes.push({ key: 4, value: 'Medium Speed' });
this.channelsModes.push({ key: 5, value: 'High Speed' });
}
setUpServices() {
super.setUpServices();
this.createChannelServices();
this.services[0].setPrimaryService(true);
this.createChannelSensorService();
super.updatePlatform();
}
createChannelServices() {
this.log.debug('Creating %s service for controller', this.deviceName);
const zoneService = this.accessory.getServiceById(this.service.Switch, this.deviceType)
|| this.accessory.addService(this.service.Switch, this.deviceName, this.deviceType);
zoneService.getCharacteristic(this.Characteristic.On)
.onGet(this.getOnState.bind(this))
.onSet(this.setOnState.bind(this));
this.services.push(zoneService);
return zoneService;
}
createChannelSensorService() {
this.log.debug('Creating %s sensor service for controller', this.deviceName);
const zoneService = this.accessory.getServiceById(this.service.ContactSensor, this.deviceType)
|| this.accessory.addService(this.service.ContactSensor, this.getNameState, this.deviceType);
zoneService.getCharacteristic(this.Characteristic.ContactSensorState)
.onGet(this.getContactSensorOnState.bind(this));
zoneService.getCharacteristic(this.Characteristic.Name)
.onGet(this.getNameState.bind(this));
this.services.push(zoneService);
return zoneService;
}
/// /////////////////////
// GET AND SET CONFIG STATUS
/// /////////////////////
getChannelsConfigStatus(status) {
const currentStatus = status;
const currentDevice = this.device;
const currentConfig = currentDevice.data;
if (currentStatus) {
const channelStatus = currentStatus.channels.find(h => h.channel_number === currentConfig.channel_number);
const configStatus = Object.assign({}, new status_1.ChannelStatus(currentConfig.channel_number, true, currentConfig.function), currentConfig, channelStatus);
this.debugLog('channelConfigStatus', configStatus);
return configStatus;
}
else {
const configStatus = Object.assign({}, currentConfig, new status_1.ChannelStatus(currentConfig.channel_number, false, currentConfig.function));
this.debugLog('channelConfigStatus', configStatus);
return configStatus;
}
}
setConfigStatus(status) {
this.channelConfigStatus = this.getChannelsConfigStatus(status);
}
async updateStatus(status) {
await super.updateStatus(status);
this.services[0].getCharacteristic(this.Characteristic.On)
.updateValue(this.getOnState());
this.services[1].getCharacteristic(this.Characteristic.ContactSensorState)
.updateValue(this.getContactSensorOnState());
this.services[1].updateCharacteristic(this.Characteristic.Name, this.getNameState());
}
/// /////////////////////
// GET AND SET FUNCTIONS
/// /////////////////////
getContactSensorOnState() {
const value = this.getOnState() ?
this.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED :
this.Characteristic.ContactSensorState.CONTACT_DETECTED;
this.debugLog('getContactSensorOnState', value);
this.stateContactOn = value;
return this.stateContactOn;
}
getOnState() {
const channelConfigStatus = this.channelConfigStatus;
const value = channelConfigStatus.mode <= settings_1.ChannelsMode.AUTO ? false : true;
this.debugLog('getOnState', value);
this.stateOn = value;
return this.stateOn;
}
setOnState(value) {
this.debugLog('setOnState', value);
if (this.stateOn === value) {
return;
}
this.stateOn = value;
this.platform.setPoolAction(action_1.PoolAction.CycleChanelMode, this.device.deviceTypeNumber).then((res) => {
this.debugLog('setOnState Result', res);
});
}
getNameState() {
var _a;
const channelConfigStatus = this.channelConfigStatus;
const value = !this.getOnState() ?
this.deviceName : this.deviceName + '-' + ((_a = this.channelsModes.find(kv => kv.key === channelConfigStatus.mode)) === null || _a === void 0 ? void 0 : _a.value);
this.debugLog('getNameState', value);
this.stateName = value;
return this.stateName;
}
}
exports.ChannelAccessory = ChannelAccessory;
//# sourceMappingURL=channelAccessory.js.map