UNPKG

homebridge-eq3ble

Version:

Homebridge plugin to control EQ3 bluetooth thermostats

155 lines (122 loc) 6.62 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _Thermostat = require('./Thermostat'); var _Thermostat2 = _interopRequireDefault(_Thermostat); var _MQTTClient = require('./MQTTClient'); var _MQTTClient2 = _interopRequireDefault(_MQTTClient); var _callbackify = require('./callbackify'); var _callbackify2 = _interopRequireDefault(_callbackify); var _constants = require('./constants'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function thermostatFactory(_ref) { var Service = _ref.Service, Characteristic = _ref.Characteristic; return function () { function EQ3Thermostat(log, config) { var _this = this; _classCallCheck(this, EQ3Thermostat); this.log = log; this.name = config.name; this.address = config.address.toLowerCase(); this.discoverTimeout = config.discoverTimeout || 60 * 1000; // 1 minute this.connectionTimeout = config.connectionTimeout || 10 * 1000; // 10 seconds this.disableBoostSwitch = false; this.temperatureDisplayUnits = Characteristic.TemperatureDisplayUnits.CELSIUS; this.thermostat = new _Thermostat2.default({ address: this.address, discoverTimeout: this.discoverTimeout, connectionTimeout: this.connectionTimeout }); this.thermostatService = new Service.Thermostat(this.name); this.informationService = new Service.AccessoryInformation(); this.boostService = new Service.Switch(this.name + ' boost mode'); this.currentTemperature = null; if (config.currentTemperature) { var mqttTemperature = new _MQTTClient2.default(config.currentTemperature); mqttTemperature.on('change', function (temperature) { _this.thermostatService.setCharacteristic(Characteristic.CurrentTemperature, temperature); _this.currentTemperature = temperature; }); } this.setupServices(); } _createClass(EQ3Thermostat, [{ key: 'getServices', value: function getServices() { return [this.informationService, this.thermostatService, !this.disableBoostSwitch && this.boostService]; } }, { key: 'setupServices', value: function setupServices() { var _this2 = this; this.boostService.setCharacteristic(Characteristic.Name, 'Boost Mode'); this.informationService.setCharacteristic(Characteristic.Manufacturer, 'eq-3').setCharacteristic(Characteristic.Model, 'CC-RT-BLE'); var boostOn = this.boostService.getCharacteristic(Characteristic.On); var currentHeatingCoolingState = this.thermostatService.getCharacteristic(Characteristic.CurrentHeatingCoolingState); var targetHeatingCoolingState = this.thermostatService.getCharacteristic(Characteristic.TargetHeatingCoolingState); var currentTemperature = this.thermostatService.getCharacteristic(Characteristic.CurrentTemperature).setProps({ minValue: -100, maxValue: 100 }); var targetTemperature = this.thermostatService.getCharacteristic(Characteristic.TargetTemperature).setProps({ minValue: 4.5, maxValue: 30 }); var heatingThresholdTemperature = this.thermostatService.getCharacteristic(Characteristic.HeatingThresholdTemperature); boostOn.on('get', (0, _callbackify2.default)(function () { return _this2.getInfoValue('boost'); })).on('set', (0, _callbackify2.default)(function (boost) { return _this2.thermostat.setBoost(boost); })); currentHeatingCoolingState.on('get', (0, _callbackify2.default)(function () { return _this2.getInfoValue('currentHeatingCoolingState'); })); targetHeatingCoolingState.on('get', (0, _callbackify2.default)(function () { return _this2.getInfoValue('targetHeatingCoolingState'); })).on('set', (0, _callbackify2.default)(function (state) { return _this2.thermostat.setTargetHeatingCoolingState(state); })); currentTemperature.on('get', this.currentTemperature == null ? (0, _callbackify2.default)(function () { return _this2.getInfoValue('targetTemperature'); }) : function (callback) { return callback(null, _this2.currentTemperature); }); targetTemperature.on('get', (0, _callbackify2.default)(function () { return _this2.getInfoValue('targetTemperature'); })).on('set', (0, _callbackify2.default)(function (temperature) { return _this2.thermostat.setTargetTemperature(temperature); })); heatingThresholdTemperature.on('get', (0, _callbackify2.default)(function () { return _this2.getInfoValue('targetTemperature'); })); this.thermostat.on('info', function (info) { boostOn.setValue(info.boost); currentHeatingCoolingState.setValue((0, _constants.map)(Characteristic, info.currentHeatingCoolingState)); targetHeatingCoolingState.setValue((0, _constants.map)(Characteristic, info.targetHeatingCoolingState)); targetTemperature.setValue(info.targetTemperature); heatingThresholdTemperature.setValue(info.targetTemperature); if (_this2.currentTemperature == null) { currentTemperature.setValue(info.targetTemperature); } }); } }, { key: 'getInfoValue', value: function getInfoValue(what) { return this.thermostat.getCachedInfo().then(function (info) { return info[what]; }); } }]); return EQ3Thermostat; }(); } module.exports = function register(homebridge) { var Service = homebridge.hap.Service; var Characteristic = homebridge.hap.Characteristic; homebridge.registerAccessory('homebridge-eq3ble', 'EQ3-Thermostat', thermostatFactory({ Service: Service, Characteristic: Characteristic })); };