Skip to content

Commit

Permalink
Adding first implementation of humidifier
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed May 10, 2017
1 parent 5e0907b commit a6d7fad
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 23 deletions.
3 changes: 2 additions & 1 deletion docs/devices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Mi Smart Home Outlet | - | Yes | ❓ Unknown | Aqa
Mi Smart Power Strip 1 | `switch` | Unknown | ✅ Basic | Setting power and mode is untested.
Mi Smart Power Strip 2 | `switch` | Unknown | ✅ Basic | Setting power and mode is untested.
Mi Rice Cooker | - | Unknown | ❓ Unknown |
Mi Humidifier | - | Unknown | ❓ Unknown |
Mi Humidifier | - | Unknown | ⚠️ Untested |
Mi Smart Fan | `generic` | Unknown | ⚠️ Generic |
Mi Air Quality Monitor (PM2.5)| `generic` | Unknown | ⚠️ Generic |
Yeelight Desk Lamp | `light` | No | ✅ Basic |
Expand All @@ -108,6 +108,7 @@ Id | Type | Capabilities
`zhimi.airpurifier.v4` | - | | Unknown | ⚠️ Generic | Testing needed to check compatibility with `air-purifier` type.
`zhimi.airpurifier.v5` | - | | Unknown | ⚠️ Generic | Testing needed to check compatibility with `air-purifier` type.
`zhimi.airpurifier.v6` | `air-purifier` | `power`, `sensor`, `temperature`, `humidity`, `aqi` | Yes | ✅ Basic |
`zhimi.humidifier.v1` | `humdifier` | `power`, `sensor`, `temperature`, `humidity` | Unknown | ⚠️ Untested |
`chuangmi.plug.m1` | `power-plug` | `power-channels` | Yes | ✅ Good |
`chuangmi.plug.v1` | `power-plug` | `power-channels` | Yes | ✅ Good |
`chuangmi.plug.v2` | `power-plug` | `power-channels` | Yes | ✅ Good |
Expand Down
49 changes: 28 additions & 21 deletions docs/devices/humidifier.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
# Humidifier

* `device.type`: `humidifier`
* **Models**: zhimi-humidifier-v1
* **Model identifiers**: `zhimi-humidifier-v1`
* **Models**: Mi Humidifier
* **Model identifiers**: `zhimi.humidifier.v1`

### Properties and sensor values
### Properties

* `power`
* `mode`
* `temp_dec`
* `humidity`
* `led_b`
* `buzzer`
* `child_lock`
* `limit_hum`
* `trans_level`
* `cola`

Example: return device.call('get_prop', ["cola","humidity","temp_dec","power","mode","led_b","buzzer","child_lock","limit_hum","trans_level"])
* `power`, boolean indicating the power state of the device
* `mode`, string indicating the current mode
* `temperature`, number indicating the temperature in Celsius
* `humidity`, number indicating the relative humidity in percent

### Power

* power is specified as on or off string
Example: return device.call('set_power', ["on"])
* `device.power`, read-only boolean indicating if the device is powered on
* `device.setPower(boolean)`, change the power state of the device, returns a promise

### Modes

* `silent`, lowest speed
The humidifiers have different modes that controls their speed.

* `device.mode`, read-only string indicating the current mode
* `device.modes`, read-only array indicating the modes supports by the device
* `device.setMode(string)`, set the current mode of the device, returns a promise

The modes are currently:

* `idle`, turn the device off
* `silent`, lower speed
* `medium`, medium speed
* `high`, high speed

Example: return device.call('set_mode', ["medium"])
### Sensor values

* `device.temperature` - number indicating the temperature in Celsius
* `device.humidity` - number indicating the relative humidity in percent
* `device.aqi` - number indicating the Air Quality Index (based on PM2.5)

### Settings

* `buzzer` - turn the buzzer on or off.
Example: return device.call('set_buzzer', ['on'])
* `device.buzzer` - boolean indicating if the buzzer (beep) is active
* `device.setBuzzer(boolean)` - switch the buzzer on or off
* `device.ledBrightness` - the LED brightness, `bright`, `dim` or `off`
* `device.setLedBrightness(string)` - set the brightness of the LED
134 changes: 134 additions & 0 deletions lib/devices/humidifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
'use strict';

const Device = require('../device');
const Power = require('./capabilities/power');
const Sensor = require('./capabilities/sensor');

/**
* Abstraction over a Mi Humidifier.
*/
class Humidifier extends Device {
static get TYPE() { return 'humidifier' }

constructor(options) {
super(options);

this.type = Humidifier.TYPE;

this.defineProperty('power', v => v === 'on');
Power.extend(this, {
set: (power) => this.call('set_power', [ power ? 'on' : 'off' ], {
refresh: [ 'power', 'mode' ],
refreshDelay: 200
})
});

this.capabilities.push('mode');
this.defineProperty('mode');

// Sensor values reported by the device
this.defineProperty('temp_dec', {
name: 'temperature',
mapper: v => v / 10.0
});
Sensor.extend(this, { name : 'temperature' });

this.defineProperty('humidity');
Sensor.extend(this, { name : 'humidity' });

// Buzzer and beeping
this.defineProperty('buzzer', {
mapper: v => v == 'on'
});

this.defineProperty('led_b', {
name: 'ledBrightness',
mapper: v => {
switch(v) {
case 0:
return 'bright';
case 1:
return 'dim';
case 2:
return 'off';
default:
return 'unknown';
}
}
});
}

/**
* Get the mode that the device is in.
*/
get mode() {
return this.property('mode');
}

/**
* Get the modes that are available to set.
*/
get modes() {
return [ 'idle', 'silent', 'medium', 'high' ];
}

/**
* Set the mode of this device.
*/
setMode(mode) {
return this.call('set_mode', [ mode ], {
refresh: [ 'power', 'mode' ],
refreshDelay: 200
})
.then(Device.checkOk)
.catch(err => {
throw err.code == -5001 ? new Error('Mode `' + mode + '` not supported') : err
});
}

/**
* Get the current LED brightness, either `bright`, `dim` or `off`.
*/
get ledBrightness() {
return this.property('ledBrightness');
}

/**
* Set the LED brightness to either `bright`, `dim` or `off`.
*/
setLedBrightness(level) {
switch(level) {
case 'bright':
level = 0;
break;
case 'dim':
level = 1;
break;
case 'off':
level = 2;
break;
default:
return Promise.reject(new Error('Invalid LED brigthness: ' + level));
}
return this.call('set_led_b', [ level ], { refresh: true })
.then(() => null);
}

/**
* Get if the buzzer on the device is active, where it will beep whenever
* a command is performed.
*/
get buzzer() {
return this.property('buzzer');
}

/**
* Set if the device should beep whenever a command is performed.
*/
setBuzzer(active) {
return this.call('set_buzzer', [ active ? 'on' : 'off' ], { refresh: true })
.then(() => null);
}
}

module.exports = Humidifier;
7 changes: 6 additions & 1 deletion lib/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
* Mapping from models into high-level devices.
*/
const AirPurifier = require('./devices/air-purifier');
const Vacuum = require('./devices/vacuum');
const Gateway = require('./devices/gateway');

const Vacuum = require('./devices/vacuum');

const PowerPlug = require('./devices/power-plug');
const PowerStrip = require('./devices/power-strip');

const Humidifier = require('./devices/humidifier');

module.exports = {
'zhimi.airpurifier.m1': AirPurifier,
'zhimi.airpurifier.v1': AirPurifier,
'zhimi.airpurifier.v2': AirPurifier,
'zhimi.airpurifier.v3': AirPurifier,
'zhimi.airpurifier.v6': AirPurifier,

'zhimi.humidifier.v1': Humidifier,

'chuangmi.plug.m1': PowerPlug,
'chuangmi.plug.v1': require('./devices/chuangmi.plug.v1'),
'chuangmi.plug.v2': PowerPlug,
Expand Down

0 comments on commit a6d7fad

Please sign in to comment.