From 9678a21b34a5b7a3ebdeb6164bfdd4d63a436b7a Mon Sep 17 00:00:00 2001 From: SeydX Date: Tue, 5 Oct 2021 10:22:48 +0200 Subject: [PATCH] v1.1.2 --- CHANGELOG.md | 9 ++++ README.md | 3 +- accessories/AISyncFanAccessory.js | 80 +++++++++++++++++++++++-------- index.js | 21 +++++--- package-lock.json | 55 ++++++++++----------- package.json | 4 +- 6 files changed, 115 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 966dd68..5d9e407 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# Changelog + +## v1.1.2 - 2020-10-05 + +- Added new parameter to config: `debug` +- Added reconnect function if websocket connection fails +- Update dependencies +- Minor bugfixes + ## v1.1.1 - 2020-09-28 - Minor bugfixes diff --git a/README.md b/README.md index 7363d78..f3d840f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,8 @@ To configure, add this to your homebridge config.json file: "platform": "AISync", "name": "AISync Platform", "email": "your_email@email.com", - "password": "your_password" + "password": "your_password", + "debug": false } ] diff --git a/accessories/AISyncFanAccessory.js b/accessories/AISyncFanAccessory.js index da50d3a..fbd55c9 100644 --- a/accessories/AISyncFanAccessory.js +++ b/accessories/AISyncFanAccessory.js @@ -1,6 +1,7 @@ -function AISyncFanAccessory(api, log, accessory, device, status, session) { +function AISyncFanAccessory(api, log, accessory, device, status, session, debug) { this.api = api; this.log = log; + this.debug = debug; this.accessory = accessory; this.fan = device; @@ -51,15 +52,22 @@ function AISyncFanAccessory(api, log, accessory, device, status, session) { AISyncFanAccessory.prototype = { eventUpdate: function (data) { + if (this.debug) { + this.log(`[DEBUG] ${this.accessory.displayName}: Event Update`); + this.log(`[DEBUG] ${this.accessory.displayName}: ${JSON.stringify(data)}`); + } + if (data && data.data && data.data.changes && data.data.changes.status) { const status = data.data.changes.status; - this.service.getCharacteristic(this.api.hap.Characteristic.On).updateValue(status.H00 || false); + this.service.getCharacteristic(this.api.hap.Characteristic.On).updateValue(status.H00 === 1); this.service.getCharacteristic(this.api.hap.Characteristic.RotationSpeed).updateValue(status.H02 || 0); - this.lightService.getCharacteristic(this.api.hap.Characteristic.On).updateValue(status.H0B || false); + this.lightService.getCharacteristic(this.api.hap.Characteristic.On).updateValue(status.H0B === 1); } else { - this.log('Undefined status. Dumping data:'); - this.log(data); + if (this.debug) { + this.log(`[DEBUG] ${this.accessory.displayName}: Undefined status. Dumping data:`); + this.log(`[DEBUG] ${this.accessory.displayName}: ${JSON.stringify(data) || data}`); + } } }, @@ -67,10 +75,18 @@ AISyncFanAccessory.prototype = { const state = this.service.getCharacteristic(this.api.hap.Characteristic.On).value; this.aisync.deviceStatus(this.deviceId, (data) => { - if (data && data.data && data.data.status && data.data.status.H00 == 1) { - this.service.getCharacteristic(this.api.hap.Characteristic.On).updateValue(true); + if (this.debug) { + this.log(`[DEBUG] ${this.accessory.displayName}: Get Current State`); + this.log(`[DEBUG] ${this.accessory.displayName}: ${JSON.stringify(data)}`); + } + + if (data && data.data && data.data.status) { + this.service.getCharacteristic(this.api.hap.Characteristic.On).updateValue(data.data.status.H00 === 1); } else { - this.service.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false); + if (this.debug) { + this.log(`[DEBUG] ${this.accessory.displayName}: Undefined status. Dumping data:`); + this.log(`[DEBUG] ${this.accessory.displayName}: ${JSON.stringify(data) || data}`); + } } }); @@ -81,7 +97,9 @@ AISyncFanAccessory.prototype = { const val = targetState === true ? 1 : 0; // eslint-disable-next-line no-unused-vars - this.aisync.fanOnOff(this.deviceId, val, (data) => this.log(`State: ${targetState ? 'ON' : 'OFF'}`)); + this.aisync.fanOnOff(this.deviceId, val, (data) => + this.log(`${this.accessory.displayName}: State: ${targetState ? 'ON' : 'OFF'}`) + ); callback(null); }, @@ -89,16 +107,31 @@ AISyncFanAccessory.prototype = { _getSpeedState: function (callback) { const state = this.service.getCharacteristic(this.api.hap.Characteristic.RotationSpeed).value; - this.aisync.deviceStatus(this.deviceId, (data) => - this.service.getCharacteristic(this.api.hap.Characteristic.RotationSpeed).updateValue(data.data.status.H02) - ); + this.aisync.deviceStatus(this.deviceId, (data) => { + if (data && data.data && data.data.status) { + /*if(this.debug){ + this.log(`[DEBUG] ${this.accessory.displayName}: Get Speed State`); + this.log(`[DEBUG] ${this.accessory.displayName}: ${JSON.stringify(data, null, 2)}`); + }*/ + this.service + .getCharacteristic(this.api.hap.Characteristic.RotationSpeed) + .updateValue(data.data.status.H02 || 0); + } /*else { + if (this.debug) { + this.log(`[DEBUG] ${this.accessory.displayName}: Undefined status. Dumping data:`); + this.log(`[DEBUG] ${this.accessory.displayName}: ${JSON.stringify(data) || data}`); + } + }*/ + }); callback(null, state); }, _setSpeedState: function (targetValue, callback) { // eslint-disable-next-line no-unused-vars - this.aisync.fanSpeed(this.deviceId, targetValue, (data) => this.log(`Rotation Speed: ${targetValue}`)); + this.aisync.fanSpeed(this.deviceId, targetValue, (data) => + this.log(`${this.accessory.displayName}: Rotation Speed: ${targetValue}`) + ); callback(null); }, @@ -107,11 +140,18 @@ AISyncFanAccessory.prototype = { const state = this.lightService.getCharacteristic(this.api.hap.Characteristic.On).value; this.aisync.deviceStatus(this.deviceId, (data) => { - if (data && data.data && data.data.status && data.data.status.H0B == 1) { - this.lightService.getCharacteristic(this.api.hap.Characteristic.On).updateValue(true); - } else { - this.lightService.getCharacteristic(this.api.hap.Characteristic.On).updateValue(false); - } + if (data && data.data && data.data.status) { + /*if(this.debug){ + this.log(`[DEBUG] ${this.accessory.displayName}: Get Current Light State`); + this.log(`[DEBUG] ${this.accessory.displayName}: ${JSON.stringify(data, null, 2)}`); + }*/ + this.lightService.getCharacteristic(this.api.hap.Characteristic.On).updateValue(data.data.status.H0B === 1); + } /*else { + if (this.debug) { + this.log(`[DEBUG] ${this.accessory.displayName}: Undefined status. Dumping data:`); + this.log(`[DEBUG] ${this.accessory.displayName}: ${JSON.stringify(data) || data}`); + } + }*/ }); callback(null, state); @@ -121,7 +161,9 @@ AISyncFanAccessory.prototype = { var val = targetState === true ? 1 : 0; // eslint-disable-next-line no-unused-vars - this.aisync.lightOnOff(this.deviceId, val, (data) => this.log(`Light: ${targetState ? 'ON' : 'OFF'}`)); + this.aisync.lightOnOff(this.deviceId, val, (data) => + this.log(`${this.accessory.displayName}: Light: ${targetState ? 'ON' : 'OFF'}`) + ); callback(null); }, diff --git a/index.js b/index.js index 012ccbf..1f3c8b3 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -var AISyncApi = require('ai-sync-api').AISyncApi; +var AISyncApi = require('@seydx/ai-sync-api').AISyncApi; var Accessory, Service, UUIDGen; var AISyncFanAccessory = require('./accessories/AISyncFanAccessory'); @@ -21,11 +21,13 @@ function AISync(log, config, api) { this.api = api; this.log = log; + this.config = config; this.accessories = []; + this.debug = config.debug || false; + this.subscribed = false; this.aisync = null; - this.config = config; this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this)); } @@ -83,13 +85,18 @@ AISync.prototype = { if (accessory === undefined) { this.registerFanAccessory(device, deviceStatus); } else { + const acc = accessory instanceof AISyncFanAccessory ? accessory.accessory : accessory; + + this.log(`Initializing Device: ${acc.displayName}`); + this.accessories[uuid] = new AISyncFanAccessory( this.api, this.log, - accessory instanceof AISyncFanAccessory ? accessory.accessory : accessory, + acc, device, deviceStatus, - this.aisync + this.aisync, + this.debug ); } } @@ -101,17 +108,19 @@ AISync.prototype = { const name = device.properties.displayName == '' ? 'Fan' : device.properties.displayName; const acc = new Accessory(name, uuid); + this.log(`Registering new Device: ${name}`); + acc.addService(Service.Fan); acc.addService(Service.Lightbulb); - this.accessories[uuid] = new AISyncFanAccessory(this.log, acc, device, status, this.aisync); + this.accessories[uuid] = new AISyncFanAccessory(this.api, this.log, acc, device, status, this.aisync, this.debug); this.api.registerPlatformAccessories('homebridge-ai-sync-platform', 'AISync', [acc]); }, removeAccessory: function (accessory) { if (accessory) { - this.log('[' + accessory.name + '] Removed from HomeBridge.'); + this.log(`Removing Device: ${accessory.name}`); if (this.accessories[accessory.UUID]) { delete this.accessories[accessory.UUID]; } diff --git a/package-lock.json b/package-lock.json index 7164b46..dd9740f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@seydx/homebridge-ai-sync-platform", - "version": "1.1.1", + "version": "1.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -310,6 +310,18 @@ "integrity": "sha512-wdppn25U8z/2yiaT6YGquE6X8sSv7hNMWSXYSSU1jGv/yd6XqjXgTDJ8KP4NgjTXfJ3GbRjeeb8RTV7a/VpM+w==", "dev": true }, + "@seydx/ai-sync-api": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@seydx/ai-sync-api/-/ai-sync-api-1.0.1.tgz", + "integrity": "sha512-Msdc0Ei7oMjlQQ3vhFtgC7KJP7bXdt2BxoVeFhVhyd7I776PDV2xHmRulye++TATuIPWY7fnJ0MVWbK1cCfLcA==", + "requires": { + "debug": "^4.3.2", + "events": "^3.3.0", + "request": "^2.88.2", + "util": "^0.12.4", + "ws": "^7.5.5" + } + }, "@types/json5": { "version": "0.0.29", "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", @@ -328,24 +340,6 @@ "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", "dev": true }, - "ai-sync-api": { - "version": "0.0.2", - "resolved": "https://registry.npmjs.org/ai-sync-api/-/ai-sync-api-0.0.2.tgz", - "integrity": "sha512-cE5GDIDCeGaO9ILtD2qaW6e6C4vvUaMpWGSpT5+PAbnUTnDw6NjDbhGfryFrvaOZc4thfgQ3oIWkvFEm5NB12g==", - "requires": { - "events": "^3.0.0", - "request": "^2.88.0", - "util": "^0.12.1", - "ws": "^7.2.1" - }, - "dependencies": { - "ws": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.5.tgz", - "integrity": "sha512-BAkMFcAzl8as1G/hArkxOxq3G7pjUqQ3gzYbLL0/5zNkph70e+lCoxBGnm6AW1+/aiNeV4fnKqZ8m4GZewmH2w==" - } - } - }, "ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -601,7 +595,6 @@ "version": "4.3.2", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", "integrity": "sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==", - "dev": true, "requires": { "ms": "2.1.2" } @@ -1636,16 +1629,16 @@ } }, "mime-db": { - "version": "1.49.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.49.0.tgz", - "integrity": "sha512-CIc8j9URtOVApSFCQIF+VBkX1RwXp/oMMOrqdyXSBXq5RWNEsRfyj1kiRnQgmNXmHxPoFIxOroKA3zcU9P+nAA==" + "version": "1.50.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.50.0.tgz", + "integrity": "sha512-9tMZCDlYHqeERXEHO9f/hKfNXhre5dK2eE/krIvUjZbS2KPcqGDfNShIWS1uW9XOTKQKqK6qbeOci18rbfW77A==" }, "mime-types": { - "version": "2.1.32", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.32.tgz", - "integrity": "sha512-hJGaVS4G4c9TSMYh2n6SQAGrC4RnfU+daP8G7cSCmaqNjiOoUY0VHCMS42pxnQmVF1GWwFhbHWn3RIxCqTmZ9A==", + "version": "2.1.33", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.33.tgz", + "integrity": "sha512-plLElXp7pRDd0bNZHw+nMd52vRYjLwQjygaNg7ddJ2uJtTlmnTCjWuPKxVu6//AdaRuME84SvLW91sIkBqGT0g==", "requires": { - "mime-db": "1.49.0" + "mime-db": "1.50.0" } }, "minimatch": { @@ -1666,8 +1659,7 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "nanocolors": { "version": "0.1.12", @@ -2411,6 +2403,11 @@ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true }, + "ws": { + "version": "7.5.5", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.5.tgz", + "integrity": "sha512-BAkMFcAzl8as1G/hArkxOxq3G7pjUqQ3gzYbLL0/5zNkph70e+lCoxBGnm6AW1+/aiNeV4fnKqZ8m4GZewmH2w==" + }, "yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", diff --git a/package.json b/package.json index 4e8d74d..219ce40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@seydx/homebridge-ai-sync-platform", - "version": "1.1.1", + "version": "1.1.2", "description": "Homebridge plugin for ai-control fans", "main": "index.js", "scripts": { @@ -20,7 +20,7 @@ "homebridge": "^1.3.0" }, "dependencies": { - "ai-sync-api": "^0.0.2" + "@seydx/ai-sync-api": "^1.0.1" }, "devDependencies": { "@babel/core": "7.15.5",