Closed CZ1979 closed 6 years ago
I have a similar situation with a device with 5 switches - I handled this by returning the entire dps result to the caller (in Python).
See https://github.com/clach04/python-tuya/blob/master/pytuya/__init__.py#L222
E.g.:
data = d.status()
print('Dictionary %r' % data)
switch_state = data['dps']['1']
print('switch_state %r' % switch_state)
@CZ1979 @clach04 please check out v2.0, it should work for devices with multiple switches/properties/dps
s.
Hi ! Thanks for that grat project. Makes my new wall switch works together with my smarthome. I bought a Xenon / Jinvoo (Tuya based) Wall switch with two buttons. Like this one: https://de.aliexpress.com/item/Manufacturer-Xenon-Wall-Switch-110-240V-Smart-Wi-Fi-Switch-button-Glass-Panel-3-gang-Ivory/32774600013.html
I first tried to flash it with tasmota FW, without success. So after hours of research, i found this project. After some small code changes , i successfully integrate the switch into my smarthome environment (iobroker mainly).
Only problem with the tuyapi was, that it only supports one datapoint (Means one button). So i add that functionality to the getStatus and setStatus method. I like to share my changes here, maybe someone also has that requirement. I just tested it with my wall switch! Works fine for me.
` /**
@param {number} [dpId=1] - DataPoint for multi switch devices */ TuyaDevice.prototype.getStatus = function (callback, dpId) { var dpId = dpId || 1;
// Add data to command if ('gwId' in requests[this.type].status.command) { requests[this.type].status.command.gwId = this.id; } if ('devId' in requests[this.type].status.command) { requests[this.type].status.command.devId = this.id; }
// Create byte buffer from hex data const thisData = Buffer.from(JSON.stringify(requests[this.type].status.command)); const buffer = this._constructBuffer(thisData, 'status');
this._send(buffer).then(data => { // Extract returned JSON try { data = data.toString(); data = data.slice(data.indexOf('{'), data.lastIndexOf('}') + 1); data = JSON.parse(data); return callback(null, data.dps[dpId]); } catch (err) { return callback(err, null); } }); };
/**
true
for on,false
for offtrue
if the command succeeded@param {number} [dpId=1] - DataPoint for multi switch devices */ TuyaDevice.prototype.setStatus = function (on, callback, dpId) { const thisRequest = requests[this.type][on ? 'on' : 'off']; var dpId = dpId || 1;
// Add data to command const now = new Date(); if ('gwId' in thisRequest.command) { thisRequest.command.gwId = this.id; } if ('devId' in thisRequest.command) { thisRequest.command.devId = this.id; } if ('uid' in thisRequest.command) { thisRequest.command.uid = this.uid; }
if ('dps' in thisRequest.command) { thisRequest.command.dps = JSON.parse("{\""+dpId+"\": "+on+"}"); } if ('t' in thisRequest.command) { thisRequest.command.t = (parseInt(now.getTime() / 1000, 10)).toString(); }
// Encrypt data this.cipher.start({iv: ''}); this.cipher.update(forge.util.createBuffer(JSON.stringify(thisRequest.command), 'utf8')); this.cipher.finish();
// Encode binary data to Base64 const data = forge.util.encode64(this.cipher.output.data);
// Create MD5 signature const preMd5String = 'data=' + data + '||lpv=' + this.version + '||' + this.key; const md5hash = forge.md.md5.create().update(preMd5String).digest().toHex(); const md5 = md5hash.toString().toLowerCase().substr(8, 16);
// Create byte buffer from hex data const thisData = Buffer.from(this.version + md5 + data); const buffer = this._constructBuffer(thisData, [on ? 'on' : 'off']);
// Send request to change status this._send(buffer).then(data => { return callback(null, true); }).catch(err => { return callback(err, null); }); }; `