Skip to content

Commit

Permalink
v1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
seydx committed Oct 5, 2021
1 parent 7361465 commit 9678a21
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 57 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]

Expand Down
80 changes: 61 additions & 19 deletions accessories/AISyncFanAccessory.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -51,26 +52,41 @@ 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}`);
}
}
},

_getCurrentState: function (callback) {
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}`);
}
}
});

Expand All @@ -81,24 +97,41 @@ 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);
},

_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);
},
Expand All @@ -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);
Expand All @@ -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);
},
Expand Down
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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));
}
Expand Down Expand Up @@ -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
);
}
}
Expand All @@ -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];
}
Expand Down
55 changes: 26 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down

0 comments on commit 9678a21

Please sign in to comment.