From 86a8a83de5d5ce9953520b0beac2b3c89b1f8d2e Mon Sep 17 00:00:00 2001 From: Jan Scheiper Date: Wed, 22 Dec 2021 10:03:00 +0100 Subject: [PATCH] initial comit with all the functionality --- .gitignore | 1 + HCMeross.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 55 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 19 ++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 HCMeross.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/HCMeross.js b/HCMeross.js new file mode 100644 index 0000000..477e83d --- /dev/null +++ b/HCMeross.js @@ -0,0 +1,58 @@ +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; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..284ce4f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,55 @@ +{ + "name": "hc-meross-smartplug", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "axios": { + "version": "0.24.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz", + "integrity": "sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==", + "requires": { + "follow-redirects": "^1.14.4" + } + }, + "follow-redirects": { + "version": "1.14.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.6.tgz", + "integrity": "sha512-fhUl5EwSJbbl8AR+uYL2KQDxLkdSjZGR36xy46AO7cOMTrCMON6Sa28FmAnC2tRTDbd/Uuzz3aJBv7EBN7JH8A==" + }, + "homecontrol-control-base": { + "version": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git#37eb940fbda9c423834ca3b0f6895753f446ed88", + "from": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git", + "requires": { + "merge-options": "^1.0.1", + "node-object-hash": "^1.4.1" + } + }, + "is-plain-obj": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz", + "integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4=" + }, + "merge-options": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-options/-/merge-options-1.0.1.tgz", + "integrity": "sha512-iuPV41VWKWBIOpBsjoxjDZw8/GbSfZ2mk7N1453bwMrfzdrIk7EzBd+8UVR6rkw67th7xnk9Dytl3J+lHPdxvg==", + "requires": { + "is-plain-obj": "^1.1" + } + }, + "meross-local": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/meross-local/-/meross-local-1.0.0.tgz", + "integrity": "sha512-KIyT1lwv2azclTm2Hmzk1bjbIZzcK3jllCR36vkJH8h5lZt7DLpP0HkW7Yn6ZieOfzIqlJkl4ky3O331/YBoFA==", + "requires": { + "axios": "^0.24.0" + } + }, + "node-object-hash": { + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/node-object-hash/-/node-object-hash-1.4.2.tgz", + "integrity": "sha512-UdS4swXs85fCGWWf6t6DMGgpN/vnlKeSGEQ7hJcrs7PBFoxoKLmibc3QRb7fwiYsjdL7PX8iI/TMSlZ90dgHhQ==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..6e2cf28 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "hc-meross-smartplug", + "version": "1.0.0", + "description": "Meross Plugin for Homecontrol", + "main": "HCMeross.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@git.literalchaos.de:jan/hc-meross-smartplug.git" + }, + "author": "Jan Scheiper", + "license": "UNLICENSED", + "dependencies": { + "homecontrol-control-base": "git+https://git.literalchaos.de/jan/homecontrol-control-base.git", + "meross-local": "^1.0.0" + } +}