7.2.0
Represents a Tuya device.
You must pass either an IP or an ID. If you're experiencing problems when only passing one, try passing both if possible.
Extends EventEmitter
(Object
= {}
)
Name | Description |
---|---|
options.ip String?
|
IP of device |
options.port Number
(default 6668 )
|
port of device |
options.id String?
|
ID of device (also called
devId
)
|
options.gwID String
(default '' )
|
gateway ID (not needed for most devices),
if omitted assumed to be the same as
options.id
|
options.key String
|
encryption key of device (also called
localKey
)
|
options.productKey String?
|
product key of device (currently unused) |
options.version Number
(default 3.1 )
|
protocol version |
options.nullPayloadOnJSONError Boolean
(default false )
|
if true, emits a data event containing a payload of null values for on-device JSON parsing errors |
options.issueGetOnConnect Boolean
(default true )
|
if true, sends GET request after
connection is established. This should probably be
false
in synchronous usage.
|
options.issueRefreshOnConnect Boolean
(default false )
|
if true, sends DP_REFRESH request after
connection is established. This should probably be
false
in synchronous usage.
|
options.issueRefreshOnPing Boolean
(default false )
|
if true, sends DP_REFRESH and GET request after
every ping. This should probably be
false
in synchronous usage.
|
const tuya = new TuyaDevice({id: 'xxxxxxxxxxxxxxxxxxxx',
key: 'xxxxxxxxxxxxxxxx'})
Gets a device's current status. Defaults to returning only the value of the first DPS index.
Promise<(Boolean | Object)>
:
returns boolean if single property is requested, otherwise returns object of results
// get first, default property from device
tuya.get().then(status => console.log(status))
// get second property from device
tuya.get({dps: 2}).then(status => console.log(status))
// get all available data from device
tuya.get({schema: true}).then(data => console.log(data))
Refresh a device's current status. Defaults to returning all values.
(Object?
= {}
)
Name | Description |
---|---|
options.schema Boolean?
|
true to return entire list of properties from device |
options.dps Number
(default 1 )
|
DPS index to return |
options.cid String?
|
if specified, use device id of zigbee gateway and cid of subdevice to refresh its status |
options.requestedDPS Array.Number
(default [4,5,6,18,19,20] )
|
only set this if you know what you're doing |
Promise<Object>
:
returns object of results
// get first, default property from device
tuya.refresh().then(status => console.log(status))
// get second property from device
tuya.refresh({dps: 2}).then(status => console.log(status))
// get all available data from device
tuya.refresh({schema: true}).then(data => console.log(data))
Sets a property on a device.
(Object)
Name | Description |
---|---|
options.dps Number
(default 1 )
|
DPS index to set |
options.set any?
|
value to set |
options.cid String?
|
if specified, use device id of zigbee gateway and cid of subdevice to set its property |
options.multiple Boolean
(default false )
|
Whether or not multiple properties should be set with options.data |
options.data Object
(default {} )
|
Multiple properties to set at once. See above. |
options.shouldWaitForResponse Boolean
(default true )
|
see #420 and #421 for details |
Promise<Object>
:
returns response from device
// set default property
tuya.set({set: true}).then(() => console.log('device was turned on'))
// set custom property
tuya.set({dps: 2, set: false}).then(() => console.log('device was turned off'))
// set multiple properties
tuya.set({
multiple: true,
data: {
'1': true,
'2': 'white'
}}).then(() => console.log('device was changed'))
// set custom property for a specific (virtual) deviceId
tuya.set({
dps: 2,
set: false,
devId: '04314116cc50e346566e'
}).then(() => console.log('device was turned off'))
Disconnects from the device, use to close the socket and exit gracefully.
(any)
Events that TuyAPI emits.
Emitted on socket error, usually a result of a connection timeout. Also emitted on parsing errors.
(Error)
: error event
Emitted when socket is connected to device. This event may be emitted multiple times within the same script, so don't use this as a trigger for your initialization code.
Emitted when a heartbeat ping is returned.
Emitted when dp_refresh data is proactive returned from device, omitting dps 1 Only changed dps are returned.
Emitted when data is returned from device.
Emitted when a socket is disconnected
from device. Not an exclusive event:
error
and disconnected
may be emitted
at the same time if, for example, the device
goes off the network.
A complete packet.
Type: Object
Low-level class for parsing packets.
const parser = new MessageParser({key: 'xxxxxxxxxxxxxxxx', version: 3.1})
Low-level class for encrypting and decrypting payloads.
const cipher = new TuyaCipher({key: 'xxxxxxxxxxxxxxxx', version: 3.1})
Computes a Tuya flavored CRC32
(Iterable)
Number
:
Tuya CRC32