hc-meross-smartplug/HCMeross.js
2021-12-22 10:03:00 +01:00

58 lines
1.5 KiB
JavaScript

const {
HCSwitch,
} = require("homecontrol-control-base");
const { MerossSmartPlug } = require("meross-local");
class HCMeross extends HCSwitch {
constructor(config) {
super(config);
if (!("address" in this._configuration)) {
throw new Error(`Required configuration field "address" is missing`);
}
if (!("key" in this._configuration)) {
throw new Error(`Required configuration field "key" is missing`);
}
this._channel = ("channel" in this._configuration) ? this._configuration.channel : 0;
this._plug = new MerossSmartPlug(this._configuration.address, this._configuration.key);
}
static get version() {
return require("./package.json").version;
}
turnOn() {
return this._plug.turnOn(this._channel).then(resp => {
if (resp.header.method == "SETACK") {
this._state.on = true;
this.emit("state change", this.state);
}
});
}
turnOff() {
return this._plug.turnOff(this._channel).then(resp => {
if (resp.header.method == "SETACK") {
this._state.on = false;
this.emit("state change", this.state);
}
});
}
pullState() {
return this._plug.getPower(this._channel).then(power => {
let hashBefore = this.state.hash;
this._state.on = power;
if (this.state.hash != hashBefore) {
this.emit("state change", this.state);
}
});
}
}
module.exports = HCMeross;