From 3a16b0df972aa25686d2f20d10e5c4eda9752d1c Mon Sep 17 00:00:00 2001 From: Sei Kan Date: Mon, 22 May 2017 22:42:48 +0800 Subject: [PATCH] Fixed errors when device initializing. Added new configuration to show/hide temperature, humidity, and air quality sensors. --- README.md | 17 ++++-- example.config.json | 5 +- index.js | 139 +++++++++++++++++++++++++++++++------------- package.json | 2 +- 4 files changed, 116 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 8e8b560..520f352 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,13 @@ This is Xiaomi Mi Air Purifier plugin for [Homebridge](https://github.com/nfarin ​ -* Display humidity within the **Fan** device. +* Display temperature. -* Display air quality as a separated device **Air Quality Sensor**. +* Display humidity. + +* Display air quality. + + ​ ​ @@ -44,17 +48,22 @@ This is Xiaomi Mi Air Purifier plugin for [Homebridge](https://github.com/nfarin ​ -2. Add your Accessory to the `config.json`. +2. Add following accessory to the `config.json`. ``` "accessories": [ { "accessory": "MiAirPurifier", - "name": "Air Purifier" + "name": "Air Purifier", + "showTemperature": true, + "showHumidity": true, + "showAirQuality": true } ] ``` + ​**Notes:** Set value for `showTemperature`, `showHumidity`, `showAirQuality` to **true** or **false** to show or hide these sensors in Home app. + ​ 3. Restart Homebridge, and your Mi air purifier will be discovered automatically. diff --git a/example.config.json b/example.config.json index edd8c3d..48584e3 100644 --- a/example.config.json +++ b/example.config.json @@ -9,7 +9,10 @@ "accessories": [ { "accessory": "MiAirPurifier", - "name": "Air Purifier" + "name": "Air Purifier", + "showTemperature": true, + "showHumidity": true, + "showAirQuality": true } ] } diff --git a/index.js b/index.js index 3a4af1b..82a9396 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,11 @@ var miio = require('miio'); -var Accessory, Service, Characteristic, UUIDGen; +var Accessory, Service, Characteristic; var devices = []; module.exports = function(homebridge) { Accessory = homebridge.platformAccessory; Service = homebridge.hap.Service; Characteristic = homebridge.hap.Characteristic; - UUIDGen = homebridge.hap.uuid; homebridge.registerAccessory('homebridge-mi-air-purifier', 'MiAirPurifier', MiAirPurifier); } @@ -14,40 +13,69 @@ module.exports = function(homebridge) { function MiAirPurifier(log, config) { this.log = log; this.name = config.name || 'Air Purifier'; + this.showAirQuality = config.showAirQuality || false; + this.showTemperature = config.showTemperature || false; + this.showHumidity = config.showTemperature || false; + this.services = []; + + // Modes supported this.modes = [ [0, 'idle'], [60, 'auto'], [80, 'silent'], [100, 'favorite'] ]; - // Air purifier is not available in Homekit yet, use as fan for now + // Air purifier is not available in Homekit yet, register as Fan this.fanService = new Service.Fan(this.name); - // Register another service as air quality sensor - this.airQualitySensorService = new Service.AirQualitySensor('Air Quality Sensor'); + this.fanService + .getCharacteristic(Characteristic.On) + .on('get', this.getPowerState.bind(this)) + .on('set', this.setPowerState.bind(this)); + + this.fanService + .getCharacteristic(Characteristic.RotationSpeed) + .on('get', this.getRotationSpeed.bind(this)) + .on('set', this.setRotationSpeed.bind(this)); + + this.services.push(this.fanService); this.serviceInfo = new Service.AccessoryInformation(); this.serviceInfo .setCharacteristic(Characteristic.Manufacturer, 'Xiaomi') .setCharacteristic(Characteristic.Model, 'Air Purifier'); + + this.services.push(this.serviceInfo); - this.fanService - .getCharacteristic(Characteristic.On) - .on('get', this.getOn.bind(this)) - .on('set', this.setOn.bind(this)); + if(this.showAirQuality){ + this.airQualitySensorService = new Service.AirQualitySensor('Air Quality Sensor'); - this.fanService - .getCharacteristic(Characteristic.RotationSpeed) - .on('get', this.getRotationSpeed.bind(this)) - .on('set', this.setRotationSpeed.bind(this)); + this.airQualitySensorService + .getCharacteristic(Characteristic.AirQuality) + .on('get', this.getAirQuality.bind(this)); - this.fanService - .addCharacteristic(Characteristic.CurrentRelativeHumidity) - .on('get', this.getCurrentRelativeHumidity.bind(this)); + this.services.push(this.airQualitySensorService); + } + + if(this.showTemperature){ + this.temperatureSensorService = new Service.TemperatureSensor('Temperature'); + + this.temperatureSensorService + .getCharacteristic(Characteristic.CurrentTemperature) + .on('get', this.getCurrentTemperature.bind(this)); + + this.services.push(this.temperatureSensorService); + } - this.airQualitySensorService - .getCharacteristic(Characteristic.AirQuality) - .on('get', this.getAirQuality.bind(this)); + if(this.showHumidity){ + this.humiditySensorService = new Service.HumiditySensor('Humidity'); + + this.humiditySensorService + .getCharacteristic(Characteristic.CurrentRelativeHumidity) + .on('get', this.getCurrentRelativeHumidity.bind(this)); + + this.services.push(this.humiditySensorService); + } this.discover(); } @@ -93,39 +121,54 @@ MiAirPurifier.prototype = { }); }, - getOn: function(callback) { + getPowerState: function(callback) { + if(!this.device){ + callback(null, false); + return; + } + callback(null, this.device.power); }, - setOn: function(powerOn, callback) { + setPowerState: function(state, callback) { if(!this.device){ callback(new Error('No air purifier is discovered.')); return; } - var accessory = this; - - this.device.setPower(powerOn) - .then(function(state){ - callback(null, state); - }); - + this.device.setPower(state); callback(); }, getCurrentRelativeHumidity: function(callback) { + if(!this.device){ + callback(null, 0); + return; + } + callback(null, this.device.humidity); }, getRotationSpeed: function(callback) { + if(!this.device){ + callback(null, 0); + return; + } + for(var item of this.modes){ if(this.device.mode == item[1]){ callback(null, item[0]); + return; } } }, setRotationSpeed: function(speed, callback) { + if(!this.device){ + callback(new Error('No air purifier is discovered.')); + return; + } + for(var item of this.modes){ if(speed <= item[0]){ this.log.debug('Set mode: ' + item[1]); @@ -138,24 +181,38 @@ MiAirPurifier.prototype = { }, getAirQuality: function(callback) { - var airQuality = Characteristic.AirQuality.UNKNOWN; + if(!this.device){ + callback(null, Characteristic.AirQuality.UNKNOWN); + return; + } - if(this.device.aqi > 200) - airQuality = Characteristic.AirQuality.POOR; + var levels = [ + [200, Characteristic.AirQuality.POOR], + [150, Characteristic.AirQuality.INFERIOR], + [100, Characteristic.AirQuality.FAIR], + [50, Characteristic.AirQuality.GOOD], + [0, Characteristic.AirQuality.EXCELLENT], + ]; - else if(this.device.aqi > 150) - airQuality = Characteristic.AirQuality.INFERIOR; + var quality = Characteristic.AirQuality.UNKNOWN; - else if(this.device.aqi > 100) - airQuality = Characteristic.AirQuality.FAIR; + for(var item of levels){ + if(this.device.aqi > item[0]){ + quality = item[1]; + break; + } + } - else if(this.device.aqi > 50) - airQuality = Characteristic.AirQuality.GOOD; + callback(null, quality); + }, - else - airQuality = Characteristic.AirQuality.EXCELLENT; + getCurrentTemperature: function(callback) { + if(!this.device){ + callback(null, 0); + return; + } - callback(null, airQuality); + callback(null, this.device.temperature); }, identify: function(callback) { @@ -163,6 +220,6 @@ MiAirPurifier.prototype = { }, getServices: function() { - return [this.serviceInfo, this.fanService, this.airQualitySensorService]; + return this.services; } }; diff --git a/package.json b/package.json index df5cc5c..775933a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-mi-air-purifier", - "version": "1.0.2", + "version": "1.1.0", "description": "Xiaomi Mi air purifier plugin for Homebridge.", "license": "MIT", "keywords": [