Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a backlight switch if supported & automatic switch count detection #386

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
23 changes: 22 additions & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"pluginAlias": "Tuya",
"pluginAlias": "TuyaLan",
"pluginType": "platform",
"singular": true,
"headerDisplay": "<p align='center'><img height='60px' src='https://user-images.githubusercontent.com/3979615/78354049-dc7ff780-75f6-11ea-8026-2f8bf81d8331.png'></p>\n\nBefore using the Tuya plugin you need to discover your devices by following [these instructions](https://github.com/AMoo-Miki/homebridge-tuya-lan/wiki/Setup-Instructions).\n\n",
Expand Down Expand Up @@ -107,6 +107,12 @@
"enum": [
"RGBTWOutlet"
]
},
{
"title": "Switch",
"enum": [
"Switch"
]
}
]
},
Expand Down Expand Up @@ -467,6 +473,21 @@
"condition": {
"functionBody": "return model.devices && model.devices[arrayIndices] && ['RGBTWOutlet'].includes(model.devices[arrayIndices].type);"
}
},
"SwitchCount": {
"type": "integer",
"description": "Manually overrides automatically detected switch count. Use only if it wasn't detected right.",
"condition": {
"functionBody": "return model.devices && model.devices[arrayIndices] && ['Switch'].includes(model.devices[arrayIndices].type);"
}
},
"BacklightId": {
"type": "integer",
"description": "Manually overrides backlightId. Default is 16, but some switches may have it different.",
"placeholder": 16,
"condition": {
"functionBody": "return model.devices && model.devices[arrayIndices] && ['Switch'].includes(model.devices[arrayIndices].type);"
}
}
}
}
Expand Down
41 changes: 37 additions & 4 deletions lib/SwitchAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SwitchAccessory extends BaseAccessory {

constructor(...props) {
super(...props);
({log: this.log} = this.platform);
}

_registerPlatformAccessory() {
Expand All @@ -18,12 +19,34 @@ class SwitchAccessory extends BaseAccessory {
}

_verifyCachedPlatformAccessory() {
//const signature = JSON.stringify(this.device.state);
const signature = this.device.state;
//console.log("sig: ", signature);

if (this._justRegistered) return;

const {Service} = this.hap;

const switchCount = parseInt(this.device.context.switchCount) || 1;
let switchCount = 0;
const backlightId = (this.device.context.backlightId || 16).toString();//default backlight id is 16, but it can be overrided in plugin settings
//console.log("sig keys: " + signature.keys);
//get switch count
for (let i in signature){
//console.log("value: " + parseInt(signature[i]))
if (parseInt(i) < 7){//max gangs is 6
//console.log("SW++")
switchCount = switchCount + 1;
//console.log("SWcount: " + switchCount);
}
}
//override switchcount if set in plugin settings
//const switchCount = parseInt(this.device.context.switchCount) || 1;
if (this.device.context.switchCount){
switchCount = parseInt(this.device.context.switchCount);
}
this.log(`[TUYA] SwitchCount: ${switchCount}, backlightId: ${backlightId}`);
const _validServices = [];
//add switches
for (let i = 0; i++ < switchCount;) {
let service = this.accessory.getServiceByUUIDAndSubType(Service.Switch, 'switch ' + i);
if (service) this._checkServiceName(service, this.device.context.name + ' ' + i);
Expand All @@ -32,10 +55,20 @@ class SwitchAccessory extends BaseAccessory {
_validServices.push(service);
}

//add backlight if available - default object 16
if (typeof(signature[backlightId]) != "undefined"){
//console.log("This switch has a backlight, we add it as a button");
let service = this.accessory.getServiceByUUIDAndSubType(Service.Switch, 'switch ' + backlightId);
if (service) this._checkServiceName(service, this.device.context.name + ' backlight');
else service = this.accessory.addService(Service.Switch, this.device.context.name + ' ' + "backlight", 'switch ' + backlightId);

_validServices.push(service);
}

this.accessory.services
.filter(service => service.UUID === Service.Switch.UUID && !_validServices.includes(service))
.forEach(service => {
console.log('Removing', service.displayName);
this.log('Removing', service.displayName);
this.accessory.removeService(service);
});
}
Expand Down Expand Up @@ -80,9 +113,9 @@ class SwitchAccessory extends BaseAccessory {
this._pendingPower.props = {...this._pendingPower.props, ...{[dp]: value}};
this._pendingPower.callbacks.push(callback);

this._pendingPower.timer = setTimeout(() => {
//this._pendingPower.timer = setTimeout(() => {
this.setPower();
}, 500);
//}, 500);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/TuyaAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class TuyaAccessory extends EventEmitter {
_incrementAttemptCounter() {
this._connectionAttempts++;
setTimeout(() => {
console.log(`[Tuya DEBUG] decrementing this._connectionAttempts, currently ${this._connectionAttempts}`);
//console.log(`[Tuya DEBUG] decrementing this._connectionAttempts, currently ${this._connectionAttempts}`);
this._connectionAttempts--;
}, 10000);
}
Expand Down