UNPKG

homebridge-connect-my-pool-home-automation

Version:
188 lines 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SolarSystemAccessory = void 0; const sunrise_sunset_js_1 = require("sunrise-sunset-js"); const action_1 = require("../action"); const settings_1 = require("../settings"); const status_1 = require("../status"); const accessory_1 = require("./accessory"); /** * Solar System 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 SolarSystemAccessory 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.stateCurrentHeatingCooling = this.Characteristic.CurrentHeatingCoolingState.OFF; this.stateTargetHeatingCooling = this.Characteristic.TargetHeatingCoolingState.OFF; this.solarSystemConfigStatus = this.getSolarSystemConfigStatus(status); //looks like bug in library??? this.sunset = (0, sunrise_sunset_js_1.getSunrise)(this.platform.config.latitude, this.platform.config.longitude).valueOf(); this.sunrise = (0, sunrise_sunset_js_1.getSunset)(this.platform.config.latitude, this.platform.config.longitude).valueOf(); } setUpServices() { super.setUpServices(); this.createSolarSystemService(); this.services[0].setPrimaryService(true); this.createPoolTemperatureService(); } createSolarSystemService() { 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; } 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, }); this.services.push(zoneService); return zoneService; } isDayTime() { const d = new Date(); const todayDate = new Date(d.setMinutes(d.getMinutes() - d.getTimezoneOffset())).valueOf(); const result = (todayDate >= this.sunrise) && (todayDate <= this.sunset); this.debugLog('isDayTime', result, todayDate, this.sunrise, this.sunset); return result; } /// ///////////////////// // GET AND SET CONFIG STATUS /// ///////////////////// getSolarSystemConfigStatus(status) { const currentStatus = status; const currentDevice = this.device; const currentConfig = currentDevice.data; if (currentStatus) { const solarSystemStatus = currentStatus.solar_systems.find(s => s.solar_number === currentConfig.solar_number); const solarSystemConfigStatus = Object.assign({}, new status_1.SolarSystemStatus(currentConfig.solar_number, true), { temperature: currentStatus.temperature }, currentConfig, solarSystemStatus); if (solarSystemConfigStatus.set_temperature < settings_1.MIN_TARGET_TEMP) { solarSystemConfigStatus.set_temperature = settings_1.MIN_TARGET_TEMP; } if (solarSystemConfigStatus.temperature < settings_1.MIN_TEMP) { solarSystemConfigStatus.temperature = settings_1.MIN_TEMP; } this.debugLog('solarSystemConfigStatus', solarSystemConfigStatus); return solarSystemConfigStatus; } else { const solarSystemConfigStatus = Object.assign({}, currentConfig, new status_1.SolarSystemStatus(currentConfig.solar_number)); this.debugLog('solarSystemConfigStatus', solarSystemConfigStatus); return solarSystemConfigStatus; } } setConfigStatus(status) { this.solarSystemConfigStatus = this.getSolarSystemConfigStatus(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); //const solarSystemConfigStatus = this.getSolarSystemConfigStatus(); 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()); } /// ///////////////////// // GET AND SET FUNCTIONS /// ///////////////////// getCurrentTemperature() { const solarSystemConfigStatus = this.solarSystemConfigStatus; if (solarSystemConfigStatus && solarSystemConfigStatus.hasStatus) { this.currentTemperature = solarSystemConfigStatus.temperature; } this.debugLog('getCurrentTemperature:', this.currentTemperature); return this.currentTemperature; } getTargetTemperature() { const solarSystemConfigStatus = this.solarSystemConfigStatus; if (solarSystemConfigStatus && solarSystemConfigStatus.hasStatus) { this.currentTargetTemperature = solarSystemConfigStatus.set_temperature; } this.debugLog('getTargetTemperature:', this.currentTargetTemperature); return this.currentTargetTemperature; } setTargetTemperature(value) { this.debugLog('setTargetTemperature:', value); } getCurrentHeatingCoolingState() { const solarSystemConfigStatus = this.solarSystemConfigStatus; if (solarSystemConfigStatus && solarSystemConfigStatus.hasStatus) { const value = solarSystemConfigStatus.mode === settings_1.SolarSystemMode.OFF ? this.Characteristic.CurrentHeatingCoolingState.OFF : (solarSystemConfigStatus.temperature < solarSystemConfigStatus.set_temperature) && this.isDayTime() ? this.Characteristic.CurrentHeatingCoolingState.HEAT : this.Characteristic.CurrentHeatingCoolingState.OFF; this.stateCurrentHeatingCooling = value; } this.debugLog('getCurrentHeatingCoolingState:', this.stateCurrentHeatingCooling); return this.stateCurrentHeatingCooling; } getTargetHeatingCoolingState() { const solarSystemConfigStatus = this.solarSystemConfigStatus; if (solarSystemConfigStatus && solarSystemConfigStatus.hasStatus) { const value = solarSystemConfigStatus.mode === settings_1.SolarSystemMode.OFF ? this.Characteristic.TargetHeatingCoolingState.OFF : (solarSystemConfigStatus.temperature < solarSystemConfigStatus.set_temperature) && this.isDayTime() ? this.Characteristic.TargetHeatingCoolingState.HEAT : this.Characteristic.TargetHeatingCoolingState.OFF; 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.SetSolarMode, this.device.deviceTypeNumber, String(value), true).then((res) => { this.debugLog('stateTargetHeatingCooling Result', res); }); } } exports.SolarSystemAccessory = SolarSystemAccessory; //# sourceMappingURL=solarSystemAccessory.js.map