homebridge-xiaomi-fan
Version:
Homebridge plugin for Xiaomi Smartmi Fans
144 lines (106 loc) • 3.78 kB
JavaScript
const MiotFan = require('./MiotFan.js');
const FanCapabilities = require('../../FanCapabilities.js');
class MiotGenericFan extends MiotFan {
constructor(miioDevice, model, deviceId, name, log) {
super(miioDevice, model, deviceId, name, log);
}
// based on some zhimi.fan.za5 props
// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:zhimi-za5:2
/*----------========== INIT ==========----------*/
initFanCapabilities() {
this.addCapability(FanCapabilities.POWER_CONTROL, true);
this.addCapability(FanCapabilities.FAN_SPEED_CONTROL, true);
this.addCapability(FanCapabilities.FAN_LEVEL_CONTROL, true);
this.addCapability(FanCapabilities.NUMBER_OF_FAN_LEVELS, 4);
this.addCapability(FanCapabilities.OSCILLATION_CONTROL, true);
this.addCapability(FanCapabilities.NATURAL_MODE, true);
this.addCapability(FanCapabilities.CHILD_LOCK, true);
this.addCapability(FanCapabilities.POWER_OFF_TIMER, true);
this.addCapability(FanCapabilities.POWER_OFF_TIMER_UNIT, 'seconds');
this.addCapability(FanCapabilities.BUZZER_CONTROL, true);
this.addCapability(FanCapabilities.LED_CONTROL, true);
}
/*----------========== SETUP ==========----------*/
addFanProperties() {
// define general fan properties
this.defineProperty('power', 2, 1);
this.defineProperty('fan_level', 2, 2);
this.defineProperty('child_lock', 3, 1);
this.defineProperty('swing_mode', 2, 3);
this.defineProperty('swing_mode_angle', 2, 5);
this.defineProperty('mode', 2, 7);
this.defineProperty('power_off_time', 2, 10);
this.defineProperty('light', 4, 3);
this.defineProperty('buzzer', 5, 1);
}
/*----------========== STATUS ==========----------*/
isPowerOn() {
return this.getFanProperties().power === true;
}
getRotationSpeed() {
return this.getSafePropertyValue(this.getFanProperties().fan_speed, 0);
}
getFanLevel() {
return this.getFanProperties().fan_level;
}
isChildLockActive() {
return this.getFanProperties().child_lock === true;
}
isSwingModeEnabled() {
return this.getFanProperties().swing_mode === true;
}
getAngle() {
return this.getFanProperties().swing_mode_angle;
}
isNaturalModeEnabled() {
return this.getFanProperties().mode === 0;
}
isBuzzerEnabled() {
return this.getFanProperties().buzzer === true;
}
isLedEnabled() {
return this.getFanProperties().light === true;
}
getShutdownTimer() {
return Math.ceil(this.getFanProperties().power_off_time / 60); // return in minutes, rounded up
}
isShutdownTimerEnabled() {
return this.getShutdownTimer() > 0;
}
/*----------========== COMMANDS ==========----------*/
async setPowerOn(power) {
return this.setProperty('power', power);
}
async setRotationSpeed(speed) {
return this.setProperty('fan_speed', speed);
}
async setFanLevel(level) {
return this.setProperty('fan_level', level);
}
async setChildLock(active) {
return this.setProperty('child_lock', active);
}
async setSwingModeEnabled(enabled) {
return this.setProperty('swing_mode', enabled);
}
async setAngle(angle) {
if (angle > 120) angle = 120;
if (angle < 0) angle = 0;
return this.setProperty('swing_mode_angle', angle);
}
async setNaturalModeEnabled(enabled) {
let mode = enabled ? 0 : 1;
return this.setProperty('mode', mode);
}
async setBuzzerEnabled(enabled) {
return this.setProperty('buzzer', enabled);
}
async setLedEnabled(enabled) {
return this.setProperty('light', enabled);
}
async setShutdownTimer(minutes) {
let seconds = minutes * 60;
return this.setProperty('power_off_time', seconds);
}
}
module.exports = MiotGenericFan;