homebridge-eq3ble
Version:
Homebridge plugin to control EQ3 bluetooth thermostats
203 lines (170 loc) • 7.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _eq3ble = require('eq3ble');
var _eq3ble2 = _interopRequireDefault(_eq3ble);
var _pTimeout = require('p-timeout');
var _pTimeout2 = _interopRequireDefault(_pTimeout);
var _events = require('events');
var _events2 = _interopRequireDefault(_events);
var _isMac = require('is-mac');
var _isMac2 = _interopRequireDefault(_isMac);
var _constants = require('./constants');
var _parseInfo = require('./parseInfo');
var _parseInfo2 = _interopRequireDefault(_parseInfo);
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 _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Thermostat = function (_EventEmitter) {
_inherits(Thermostat, _EventEmitter);
function Thermostat(_ref) {
var address = _ref.address,
discoverTimeout = _ref.discoverTimeout,
connectionTimeout = _ref.connectionTimeout;
_classCallCheck(this, Thermostat);
var _this = _possibleConstructorReturn(this, (Thermostat.__proto__ || Object.getPrototypeOf(Thermostat)).call(this));
_this.address = address.toLowerCase();
_this.discoverTimeout = discoverTimeout;
_this.connectionTimeout = connectionTimeout;
if (!(0, _isMac2.default)(_this.address)) {
throw new Error('invalid address "' + _this.address + '"');
}
return _this;
}
_createClass(Thermostat, [{
key: 'discover',
value: function discover() {
var _this2 = this;
return (0, _pTimeout2.default)(new Promise(function (resolve, reject) {
_this2.emit('discovering');
_eq3ble2.default.discoverByAddress(_this2.address, function (device) {
_this2.device = device;
_this2.emit('discovered', device);
resolve();
}, function (err) {
_this2.discoverPromise = null;
_this2.emit('undiscoverable', err);
reject(err);
});
}), this.discoverTimeout);
}
}, {
key: 'connect',
value: function connect() {
var _this3 = this;
this.discoverPromise = this.discoverPromise || this.discover();
return this.discoverPromise.then(function () {
_this3.connectionPromise = _this3.connectionPromise || (0, _pTimeout2.default)(new Promise(function (resolve, reject) {
clearTimeout(_this3.timeout);
if (_this3.isConnected) {
_this3.connectionPromise = null;
resolve();
return;
}
_this3.emit('connecting');
_this3.device.connectAndSetup().then(function () {
_this3.emit('connected', _this3.device);
_this3.connectionPromise = null;
_this3.isConnected = true;
_this3.startDisconnectTimeout();
resolve();
}, function (err) {
_this3.emit('connectionError', err);
_this3.connectionPromise = null;
_this3.isConnected = false;
_this3.discoverPromise = null;
reject(err);
});
}), _this3.connectionTimeout);
return _this3.connectionPromise;
});
}
}, {
key: 'disconnect',
value: function disconnect() {
if (this.device) this.device.disconnect();
this.isConnected = false;
this.connectionPromise = null;
this.emit('disconnected');
}
}, {
key: 'startDisconnectTimeout',
value: function startDisconnectTimeout() {
clearTimeout(this.timeout);
this.timeout = setTimeout(this.disconnect.bind(this), this.connectionTimeout);
}
}, {
key: 'getInfo',
value: function getInfo() {
var _this4 = this;
this.emit('gettingInfo');
return this.connect().then(function () {
return _this4.device.getInfo();
}).then(function (raw) {
var info = (0, _parseInfo2.default)(raw);
_this4.emit('info', info);
return info;
});
}
}, {
key: 'getCachedInfo',
value: function getCachedInfo() {
var _this5 = this;
return new Promise(function (resolve, reject) {
if (_this5.info) {
resolve(_this5.info);
return;
}
_this5.infoPromise = _this5.infoPromise || _this5.getInfo();
_this5.infoPromise.then(function (info) {
_this5.info = info;
_this5.infoPromise = null;
resolve(_this5.info);
setTimeout(function () {
_this5.info = null;
}, 250);
}, reject);
});
}
}, {
key: 'setBoost',
value: function setBoost(boost) {
var _this6 = this;
return this.connect().then(function () {
return _this6.device.setBoost(boost);
});
}
}, {
key: 'setTargetTemperature',
value: function setTargetTemperature(temperature) {
var _this7 = this;
return this.connect().then(function () {
return _this7.device.setTemperature(temperature);
});
}
}, {
key: 'setTargetHeatingCoolingState',
value: function setTargetHeatingCoolingState(state) {
var _this8 = this;
return this.connect().then(function () {
switch (state) {
case _constants.TargetHeatingCoolingState.OFF:
return _this8.device.turnOff();
case _constants.TargetHeatingCoolingState.HEAT:
return _this8.device.turnOn();
case _constants.TargetHeatingCoolingState.AUTO:
return _this8.device.automaticMode();
case _constants.TargetHeatingCoolingState.COOL:
return _this8.device.manualMode();
default:
throw new Error('Unsupported mode');
}
});
}
}]);
return Thermostat;
}(_events2.default);
exports.default = Thermostat;