mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-16 17:54:36 +00:00
@@ -47,7 +47,7 @@ Color for lightbulb:
|
|||||||
tuya/lightbulb/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/color // Color as Payload as hexColor
|
tuya/lightbulb/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/color // Color as Payload as hexColor
|
||||||
|
|
||||||
Read data from device:
|
Read data from device:
|
||||||
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/dps // returns JSON.stringify(dps) values
|
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/dps // returns JSON.stringify(dps) values, use with care, does not always contain all dps values
|
||||||
|
|
||||||
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/dps/<tuya-dps-id> // return single dps data value
|
tuya/<tuyaAPI-type>/<tuyaAPI-id>/<tuyaAPI-key>/<tuyaAPI-ip>/dps/<tuya-dps-id> // return single dps data value
|
||||||
```
|
```
|
||||||
|
|||||||
44
tuya-mqtt.js
44
tuya-mqtt.js
@@ -2,12 +2,18 @@ const mqtt = require('mqtt');
|
|||||||
const TuyaDevice = require('./tuya-device');
|
const TuyaDevice = require('./tuya-device');
|
||||||
const debug = require('debug')('tuya-mqtt');
|
const debug = require('debug')('tuya-mqtt');
|
||||||
const debugMqtt = require('debug')('mqtt');
|
const debugMqtt = require('debug')('mqtt');
|
||||||
|
const debugTuya = require('debug')('tuyAPI-Events');
|
||||||
|
const debugError = require('debug')('error');
|
||||||
var cleanup = require('./cleanup').Cleanup(onExit);
|
var cleanup = require('./cleanup').Cleanup(onExit);
|
||||||
|
|
||||||
function bmap(istate) {
|
function bmap(istate) {
|
||||||
return istate ? 'ON' : "OFF";
|
return istate ? 'ON' : "OFF";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function boolToString(istate) {
|
||||||
|
return istate ? 'true' : "false";
|
||||||
|
}
|
||||||
|
|
||||||
var connected = undefined;
|
var connected = undefined;
|
||||||
|
|
||||||
const CONFIG = require("./config");
|
const CONFIG = require("./config");
|
||||||
@@ -69,7 +75,7 @@ mqtt_client.on('message', function (topic, message) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e);
|
debugError(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -92,12 +98,12 @@ function publishStatus(device, status) {
|
|||||||
retain: true,
|
retain: true,
|
||||||
qos: 2
|
qos: 2
|
||||||
});
|
});
|
||||||
debug("mqtt status updated to:" + topic + " -> " + status);
|
debugTuya("mqtt status updated to:" + topic + " -> " + status);
|
||||||
} else {
|
} else {
|
||||||
debug("mqtt status not updated");
|
debugTuya("mqtt status not updated");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e);
|
debugError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,23 +123,27 @@ function publishDPS(device, dps) {
|
|||||||
|
|
||||||
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
if (tuyaID != undefined && tuyaKey != undefined && tuyaIP != undefined) {
|
||||||
var topic = CONFIG.topic + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/dps";
|
var topic = CONFIG.topic + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/dps";
|
||||||
mqtt_client.publish(topic, JSON.stringify(dps), {
|
var data = JSON.stringify(dps);
|
||||||
|
debugTuya("mqtt dps updated to:" + topic + " -> ", data);
|
||||||
|
mqtt_client.publish(topic, data, {
|
||||||
retain: true,
|
retain: true,
|
||||||
qos: 2
|
qos: 2
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.keys(dps).forEach(function (key) {
|
Object.keys(dps).forEach(function (key) {
|
||||||
var topic = CONFIG.topic + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/dps/" + key;
|
var topic = CONFIG.topic + type + "/" + tuyaID + "/" + tuyaKey + "/" + tuyaIP + "/dps/" + key;
|
||||||
mqtt_client.publish(topic, dps[key], {
|
var data = JSON.stringify(dps[key]);
|
||||||
|
debugTuya("mqtt dps updated to:" + topic + " -> dps[" + key + "]", data);
|
||||||
|
mqtt_client.publish(topic, data, {
|
||||||
retain: true,
|
retain: true,
|
||||||
qos: 2
|
qos: 2
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
debug("mqtt dps updated to:" + topic + " -> " + dps);
|
|
||||||
} else {
|
} else {
|
||||||
debug("mqtt dps not updated");
|
debugTuya("mqtt dps not updated");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e);
|
debugError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -143,13 +153,17 @@ function publishDPS(device, dps) {
|
|||||||
* @see TuyAPI (https://github.com/codetheweb/tuyapi)
|
* @see TuyAPI (https://github.com/codetheweb/tuyapi)
|
||||||
*/
|
*/
|
||||||
TuyaDevice.onAll('data', function (data) {
|
TuyaDevice.onAll('data', function (data) {
|
||||||
debug('Data from device ' + this.type + ' :', data);
|
try {
|
||||||
var status = data.dps['1'];
|
debugTuya('Data from device ' + this.type + ' :', data);
|
||||||
if (this.type == "lightbulb" && status == undefined) {
|
var status = data.dps['1'];
|
||||||
status = true;
|
if (this.type == "lightbulb" && status == undefined) {
|
||||||
|
status = true;
|
||||||
|
}
|
||||||
|
publishStatus(this, bmap(status));
|
||||||
|
publishDPS(this, data.dps);
|
||||||
|
} catch (e) {
|
||||||
|
debugError(e);
|
||||||
}
|
}
|
||||||
publishStatus(this, bmap(status));
|
|
||||||
publishDPS(this, data.dps);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user