TuyaDevice

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.

new TuyaDevice(options: Object)

Extends EventEmitter

Parameters
options (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.
Example
const tuya = new TuyaDevice({id: 'xxxxxxxxxxxxxxxxxxxx',
                             key: 'xxxxxxxxxxxxxxxx'})
Instance Members
get(options = {})
refresh(options = {})
set(options)
connect()
disconnect()
isConnected()
resolveId(options)
find(options = {})
toggle(property)

Events

Events that TuyAPI emits.

TuyaDevice#error

Emitted on socket error, usually a result of a connection timeout. Also emitted on parsing errors.

TuyaDevice#error
Properties
error (Error) : error event

TuyaDevice#connected

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.

TuyaDevice#connected

TuyaDevice#heartbeat

Emitted when a heartbeat ping is returned.

TuyaDevice#heartbeat

TuyaDevice#dp-refresh

Emitted when dp_refresh data is proactive returned from device, omitting dps 1 Only changed dps are returned.

TuyaDevice#dp-refresh
Properties
data (Object) : received data
commandByte (Number) : commandByte of result( 8=proactive update from device)
sequenceN (Number) : the packet sequence number

TuyaDevice#data

Emitted when data is returned from device.

TuyaDevice#data
Properties
data (Object) : received data
commandByte (Number) : commandByte of result (e.g. 7=requested response, 8=proactive update from device)
sequenceN (Number) : the packet sequence number

TuyaDevice#disconnected

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.

TuyaDevice#disconnected

Packet

A complete packet.

Packet

Type: Object

Properties
payload ((Buffer | Object | String)) : Buffer if hasn't been decoded, object or string if it has been
leftover (Buffer) : bytes adjacent to the parsed packet
commandByte (Number)
sequenceN (Number)

MessageParser

Low-level class for parsing packets.

new MessageParser(options: Object)
Parameters
options (Object = {})
Name Description
options.key String localKey of cipher
options.version Number (default 3.1) protocol version
Example
const parser = new MessageParser({key: 'xxxxxxxxxxxxxxxx', version: 3.1})
Instance Members
parsePacket(buffer)
getPayload(data)
parse(buffer)
encode(options)

TuyaCipher

Low-level class for encrypting and decrypting payloads.

new TuyaCipher(options: Object)
Parameters
options (Object)
Name Description
options.key String localKey of cipher
options.version Number protocol version
Example
const cipher = new TuyaCipher({key: 'xxxxxxxxxxxxxxxx', version: 3.1})
Instance Members
encrypt(options)
decrypt(data)
md5(data)

crc32

Computes a Tuya flavored CRC32

crc32(bytes: Iterable): Number
Parameters
bytes (Iterable)
Returns
Number: Tuya CRC32