UNPKG

homebridge-connect-my-pool-home-automation

Version:
239 lines 11.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HeaterAccessory = void 0; const action_1 = require("../action"); const settings_1 = require("../settings"); const status_1 = require("../status"); const accessory_1 = require("./accessory"); /** * Heater 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 HeaterAccessory extends accessory_1.Accessory { constructor(platform, accessory, device, status) { super(platform, accessory, device, status); this.platform = platform; this.accessory = accessory; this.currentTemperature = settings_1.MIN_TEMP; this.currentTargetTemperature = settings_1.MIN_TARGET_TEMP; this.stateSpaOn = false; this.stateCurrentHeatingCooling = this.Characteristic.CurrentHeatingCoolingState.OFF; this.stateTargetHeatingCooling = this.Characteristic.TargetHeatingCoolingState.OFF; this.heaterConfigStatus = this.getHeaterConfigStatus(status); this.statePoolTemperatureName = 'Pool Temperature'; } setUpServices() { super.setUpServices(); this.createHeaterService(); this.services[0].setPrimaryService(true); this.createPoolTemperatureService(); if (this.device.hasPoolSpaSelectionEnabled) { this.createPoolSpaService(); } super.updatePlatform(); } createHeaterService() { this.log.debug('Creating %s service for controller', this.deviceName); const zoneService = this.accessory.getServiceById(this.service.Thermostat, this.deviceType) || this.accessory.addService(this.service.Thermostat, this.deviceName, this.deviceType); zoneService.getCharacteristic(this.Characteristic.CurrentHeatingCoolingState) .setProps({ maxValue: this.Characteristic.CurrentHeatingCoolingState.HEAT, }) .onGet(this.getCurrentHeatingCoolingState.bind(this)); zoneService.getCharacteristic(this.Characteristic.TargetHeatingCoolingState) .setProps({ maxValue: this.Characteristic.TargetHeatingCoolingState.HEAT, }) .onGet(this.getTargetHeatingCoolingState.bind(this)) .onSet(this.setTargetHeatingCoolingState.bind(this)); zoneService.getCharacteristic(this.Characteristic.CurrentTemperature) .setProps({ maxValue: settings_1.MAX_TEMP, minValue: settings_1.MIN_TEMP, minStep: 1, }) .onGet(this.getCurrentTemperature.bind(this)); zoneService.getCharacteristic(this.Characteristic.TargetTemperature) .setProps({ maxValue: settings_1.MAX_TARGET_TEMP, minValue: settings_1.MIN_TARGET_TEMP, minStep: 1, }) .onGet(this.getTargetTemperature.bind(this)) .onSet(this.setTargetTemperature.bind(this)); zoneService.getCharacteristic(this.Characteristic.TemperatureDisplayUnits) .setValue(this.Characteristic.TemperatureDisplayUnits.CELSIUS); this.services.push(zoneService); return zoneService; } createPoolSpaService() { this.log.debug('Creating %s service for controller', 'Pool Spa Switch'); const zoneService = this.accessory.getServiceById(this.service.Switch, this.deviceType) || this.accessory.addService(this.service.Switch, 'Spa On', this.deviceType); zoneService.getCharacteristic(this.Characteristic.On) .onSet(this.setSpaOnState.bind(this)) .onGet(this.getSpaOnState.bind(this)); this.services.push(zoneService); return zoneService; } createPoolTemperatureService() { this.log.debug('Creating %s service for controller', 'Pool Temperature'); const zoneService = this.accessory.getServiceById(this.service.TemperatureSensor, this.deviceType) || this.accessory.addService(this.service.TemperatureSensor, 'Pool Temperature', this.deviceType); zoneService.getCharacteristic(this.Characteristic.CurrentTemperature) .setProps({ maxValue: settings_1.MAX_TEMP, minValue: settings_1.MIN_TEMP, minStep: 1, }); zoneService.getCharacteristic(this.Characteristic.Name) .onGet(this.getPoolTemperatureName.bind(this)); this.services.push(zoneService); return zoneService; } /// ///////////////////// // GET AND SET CONFIG STATUS /// ///////////////////// getHeaterConfigStatus(status) { const currentStatus = status; const currentDevice = this.device; const currentConfig = currentDevice.data; if (currentStatus) { const heaterStatus = currentStatus.heaters.find(h => h.heater_number === currentConfig.heater_number); const configStatus = Object.assign({}, new status_1.HeaterStatus(currentConfig.heater_number, true, currentDevice.hasPoolSpaSelectionEnabled), { temperature: currentStatus.temperature, pool_spa_selection: currentStatus.pool_spa_selection, }, currentConfig, heaterStatus); if (configStatus.set_temperature < settings_1.MIN_TARGET_TEMP) { configStatus.set_temperature = settings_1.MIN_TARGET_TEMP; } if (configStatus.spa_set_temperature < settings_1.MIN_TARGET_TEMP) { configStatus.spa_set_temperature = settings_1.MIN_TARGET_TEMP; } if (configStatus.temperature < settings_1.MIN_TEMP) { configStatus.temperature = settings_1.MIN_TEMP; } this.debugLog('heaterConfigStatus', configStatus); return configStatus; } else { const configStatus = Object.assign({}, currentConfig, new status_1.HeaterStatus(currentConfig.heater_number)); this.debugLog('heaterConfigStatus', configStatus); return configStatus; } } setConfigStatus(status) { this.heaterConfigStatus = this.getHeaterConfigStatus(status); } async updateStatus(status) { await super.updateStatus(status); const currentHeatingCoolingState = this.getCurrentHeatingCoolingState(); this.services[0].getCharacteristic(this.Characteristic.CurrentHeatingCoolingState) .updateValue(currentHeatingCoolingState); this.services[0].getCharacteristic(this.Characteristic.TargetHeatingCoolingState) .updateValue(currentHeatingCoolingState); this.services[0].getCharacteristic(this.Characteristic.CurrentTemperature) .updateValue(this.getCurrentTemperature()); this.services[0].getCharacteristic(this.Characteristic.TargetTemperature) .updateValue(this.getTargetTemperature()); this.services[1].getCharacteristic(this.Characteristic.CurrentTemperature) .updateValue(this.getCurrentTemperature()); this.services[1].getCharacteristic(this.Characteristic.Name) .updateValue(this.getPoolTemperatureName()); if (this.device.hasPoolSpaSelectionEnabled) { this.services[2].getCharacteristic(this.Characteristic.On) .updateValue(this.getSpaOnState()); } } /// ///////////////////// // GET AND SET FUNCTIONS /// ///////////////////// getSpaOnState() { const heaterConfigStatus = this.heaterConfigStatus; if (heaterConfigStatus.hasStatus) { this.stateSpaOn = heaterConfigStatus.pool_spa_selection === settings_1.PoolSpaSelection.SPA; } this.debugLog('getSpaOnState:', this.stateSpaOn); return this.stateSpaOn; } setSpaOnState(value) { this.debugLog('setSpaOnState:', value, this.stateSpaOn); if (this.stateSpaOn === value) { return; } this.stateSpaOn = value; this.platform.setPoolAction(action_1.PoolAction.SetPoolSpaSelection, this.device.deviceTypeNumber, String(value ? settings_1.PoolSpaSelection.SPA : settings_1.PoolSpaSelection.POOL), true) .then((res) => { this.debugLog('setSpaOnState Result', res); }); } getCurrentTemperature() { const heaterConfigStatus = this.heaterConfigStatus; if (heaterConfigStatus.hasStatus) { this.currentTemperature = heaterConfigStatus.temperature; } this.debugLog('getCurrentTemperature:', this.currentTemperature); return this.currentTemperature; } getTargetTemperature() { const heaterConfigStatus = this.heaterConfigStatus; if (heaterConfigStatus.hasStatus) { const value = heaterConfigStatus.pool_spa_selection === settings_1.PoolSpaSelection.SPA ? heaterConfigStatus.spa_set_temperature : heaterConfigStatus.set_temperature; this.currentTargetTemperature = value; } this.debugLog('getTargetTemperature:', this.currentTargetTemperature); return this.currentTargetTemperature; } setTargetTemperature(value) { this.debugLog('%s setTargetTemperature:', value, this.currentTargetTemperature); if (this.currentTargetTemperature === value) { return; } this.currentTargetTemperature = value; this.platform.setPoolAction(action_1.PoolAction.SetHeaterSetTemperature, this.device.deviceTypeNumber, String(value), true).then((res) => { this.debugLog('setTargetTemperature Result', res); }); } getCurrentHeatingCoolingState() { const heaterConfigStatus = this.heaterConfigStatus; if (heaterConfigStatus.hasStatus) { this.stateCurrentHeatingCooling = heaterConfigStatus.mode; } this.debugLog('getCurrentHeatingCoolingState:', this.stateCurrentHeatingCooling); return this.stateCurrentHeatingCooling; } getTargetHeatingCoolingState() { const heaterConfigStatus = this.heaterConfigStatus; if (heaterConfigStatus.hasStatus) { const value = heaterConfigStatus.mode === settings_1.HeatersMode.OFF ? this.Characteristic.TargetHeatingCoolingState.OFF : this.Characteristic.TargetHeatingCoolingState.HEAT; this.stateTargetHeatingCooling = value; } this.debugLog('getTargetHeatingCoolingState:', this.stateTargetHeatingCooling); return this.stateTargetHeatingCooling; } setTargetHeatingCoolingState(value) { this.debugLog('setTargetHeatingCoolingState:', value, this.stateTargetHeatingCooling); if (this.stateTargetHeatingCooling === value) { return; } this.stateTargetHeatingCooling = value; this.platform.setPoolAction(action_1.PoolAction.SetHeaterMode, this.device.deviceTypeNumber, String(value), true).then((res) => { this.debugLog('stateTargetHeatingCooling Result', res); }); } getPoolTemperatureName() { const heaterConfigStatus = this.heaterConfigStatus; if (heaterConfigStatus.hasStatus) { const value = this.heaterConfigStatus.pool_spa_selection === settings_1.PoolSpaSelection.SPA ? 'Spa Temperature' : 'Pool Temperature'; this.statePoolTemperatureName = value; } this.debugLog('getPoolTemperatureName:', this.statePoolTemperatureName); return this.statePoolTemperatureName; } } exports.HeaterAccessory = HeaterAccessory; //# sourceMappingURL=heaterAccessory.js.map