Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Fixed errors when device initializing.
Browse files Browse the repository at this point in the history
Added new configuration to show/hide temperature, humidity, and air quality sensors.
  • Loading branch information
seikan committed May 22, 2017
1 parent e35bfec commit 3a16b0d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 47 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion example.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"accessories": [
{
"accessory": "MiAirPurifier",
"name": "Air Purifier"
"name": "Air Purifier",
"showTemperature": true,
"showHumidity": true,
"showAirQuality": true
}
]
}
139 changes: 98 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,81 @@
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);
}

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();
}
Expand Down Expand Up @@ -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]);
Expand All @@ -138,31 +181,45 @@ 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) {
callback();
},

getServices: function() {
return [this.serviceInfo, this.fanService, this.airQualitySensorService];
return this.services;
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 3a16b0d

Please sign in to comment.