Represents a Tuya device.
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 |
options.key String
|
encryption key of device |
options.productKey String
|
product key of device |
options.version Number
(default 3.1 )
|
protocol version |
options.persistentConnection Boolean
(default false )
|
whether or not to use a persistent socket with heartbeat packets |
const tuya = new TuyaDevice({id: 'xxxxxxxxxxxxxxxxxxxx',
key: 'xxxxxxxxxxxxxxxx',
persistentConnection: true})
(any)
Gets a device's current status.
Defaults to returning only the value of the first DPS index.
If returnAsEvent = true
, all options are ignored and
all data returned from device is emitted as event.
Promise<Object>
:
returns boolean if no options are provided, otherwise returns object of results
// get all properties and emit event with data
tuya.get({returnAsEvent: true});
// 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))
Sets a property on a device.
Promise<Boolean>
:
returns
true
if the command succeeded
// set default property
tuya.set({set: true}).then(() => console.log('device was changed'))
// set custom property
tuya.set({dps: 2, set: true}).then(() => console.log('device was changed'))
// set multiple properties
tuya.set({
multiple: true,
data: {
'1': true,
'2': 'white'
}}).then(() => console.log('device was changed'))
Events that TuyAPI emits.
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 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.