Device Management

Last Updated on : 2026-02-03 02:33:46download

The SDK supports operations to manage devices after they are paired. For example, listen for changes in device status, rename devices, update device firmware, remove devices, restore default settings, and export device information.

Before performing operations, make sure you have obtained the list of devices and groups in a home.

Functional description

Classes for device management

Class name Description
TSmartDevice Device management
TSmartDeviceModel Device data model

ThingSmartDeviceModel data model

Property Type Description
devId string The unique ID of a device.
name string The device name.
iconUrl string The URL of a device icon.
isOnline boolean Indicates whether a device is online. The device is online when it is connected to a Wi-Fi network, a local area network (LAN), or a Bluetooth network.
isCloudOnline boolean Indicates whether a device is connected to a Wi-Fi network.
getIsLocalOnline boolean Indicates whether a device is connected to a LAN.
isShare boolean Indicates whether a device is a shared device.
dps DpsType The data points (DPs) of a device.
dpCodes Record<string, Object> The DPs in the code-value format.
schemaArray Array The schema array of DP rules.
productId string The product ID (PID) of a device.
capability number The product capability of a device.
supportGroup boolean Indicates whether groups can be created.
gwType string A value of v indicates a virtual device, and an empty value indicates a real device for pairing.
pv number The protocol version of a device. A Wi-Fi protocol version or a Bluetooth protocol version are supported.
lpv number The LAN protocol version of a device. By default, this parameter is empty. The value is specified after the device is connected to a LAN.
latitude string The latitude.
longitude string The longitude.
localKey string The key that a device uses for communication.
uuid string The universally unique identifier (UUID) of a device.
ownerId string The ID of the home that a device belongs to.
roomId number The ID of the room that a device belongs to.
timezoneId string The time zone of a device.
nodeId string The short URL of a device. This parameter is empty for non-sub-devices. Each sub-device of a gateway is assigned a unique short URL.
parentId string The ID of a parent device. This parameter is empty for non-sub-devices. Sub-devices search for the gateway ID based on this parameter. For Bluetooth mesh sub-devices, this parameter means the mesh ID or gateway ID.
devKey string The Bluetooth communication key for a standard Bluetooth mesh device.
standard boolean Indicates whether a device is a standard device. Standard DPs are available to standard devices.
standSchemaModel TSmartStandSchemaModel The schema array of standard DP rules.
activeTime number The time when a device is activated.
homeDisplayOrder number The serial number of a device. When a list of devices is returned for a home, the devices can be sorted by this parameter.
sharedTime number The time when a device is shared.
accessType number The method that is used to connect devices. Valid values:
  • 0: Tuya’s DP
  • 1: Matter
  • 2: TuyaLink
category string The code of the device category. For example, dj indicates a light. For more information, see the list of category codes.

API description

Register device service

This method must be called after successful login. Then, you can use functionalities such as LAN, MQTT, and Bluetooth online and offline operations.

API description

export class TSmartDevice {
	public static register(uid: string)
}

Parameters

Parameter Description Optional or not
uid The user ID. Required

Sample code

TSmartDevice.register(userModel.uid)

Initialize devices

Initializes the device control class by device ID.

  • You must use ThingSmartHome to initialize a home instance and call getHomeDetailWithSuccess:failure: to get home details. The device is initialized only after home details are synchronized.
  • An incorrect device ID might cause failed initialization. In this case, the device instance returns nil.

API description

export class TSmartDevice {
  public static create(devId: string): TSmartDevice | undefined
}

Parameters

Parameter Description Optional or not
devId The device ID. Required

Return value

TSmartDevice | undefined

Sample code

const device = TSmartDevice.create(devId)

Listen for device status

After you implement TSmartDeviceListener, you can process the callback to invoke when device status is changed and refresh the UI of the control panel on your app.

API description

export class TSmartDevice {
	public registerListener(listener: TSmartDeviceListener): void
}

Parameters

Parameter Description Optional or not
listener The callback to invoke when the device status is changed. Required
export interface TSmartDeviceListener {
  onDpsChanged: (devId: string, dps: DpsType, dpsTime: Record<string, Object>) => void;
  onOnlineChanged: (devId: string) => void;
  onDeviceInfoUpdated: (devId: string) => void;
  onDeviceRemoved: (devId: string) => void
  onWarningInfoUpdate: (devId: string, info: Record<string, Object>) => void
  onSignalQuery: (devId: string, signal: number) => void
}

Sample code

public device: TSmartDevice = TSmartDevice.create(devId)

new Action('Register device listener', async () => {
  	this.device.registerListener({
      	// Device data point(s) changed
        onDpsChanged: (devId: string, dps: DpsType, dpsTime: Record<string, Object>) => {
          console.log(`onDpsChanged devId=${devId}, dps=${dps}`)
        },
      	// Device online status changed
        onOnlineChanged: async (devId: string) => {
          const o = await this.deviceModel!.isOnline()
          console.log("Online status:" + (o ? "Online" : "Offline"))        },
      	// Device information updated
        onDeviceInfoUpdated: (devId: string) => {
          console.log(devId + "Device information updated")
        },
      	// Device removed
        onDeviceRemoved: (devId: string) => {
          console.log(devId + "Device removed")
        },
      	// Device received a warning message
        onWarningInfoUpdate: (devId: string, info: Record<string, Object>) => {
          console.log(devId + "Device received a warning message")
        },
        onSignalQuery: (devId: string, signal: number) => {
          console.log(devId + "Device signal query result:" + signal)
        }
      })
}),

Query device information

Query a single DP. After the query, the data will be returned via the onDpsChanged: (devId: string, dps: DpsType, dpsTime: Record<string, Object>) => void method of the TSmartDeviceListener.

This API method applies to DPs that do not initiate data transmission. For example, query countdown information. For regular DP queries, we recommend that you call TSmartDeviceModel.dps.

Example of sending an instruction:

async queryDP() {
    let dpResult: Record<string, null> = {}
    dpResult[this.swichId] = null
    await this.device?.publishDps(JSON.stringify(dpResult))
  }

Rename a device

API description

export class TSmartDevice {
	public async updateName(name: string)
}

Parameters

Parameter Description Optional or not
name The new name of the device. Required

Return value

void

Sample code

public device: TSmartDevice = TSmartDevice.create(devId)

new Action('Rename a device', () => {
	this.device.updateName("new name")
}),

Remove a device and restore factory defaults

After a device is removed, it enters the state ready for pairing. By default, the device enters the Wi-Fi Easy Connect (EZ) mode.

After default settings are restored for a device, it enters the state ready for pairing. By default, the Wi-Fi device enters the Wi-Fi EZ mode. In this case, device data is cleared.

API description

export class TSmartDevice {
	public remove(reset: boolean = false)
}

Parameters

Parameter Description Optional or not
reset Specifies whether to restore factory defaults. Optional. By default, factory settings will not be restored.

Return value

void

Sample code

public device: TSmartDevice = TSmartDevice.create(devId)

new Action('Remove a device', () => {
	this.device.remove()
}),

Query the Wi-Fi signal strength

Query the device’s Wi-Fi signal strength, and receive the data via a callback function.

API description

export class TSmartDevice {
	public queryWifiSignal(callback:(signal:number) => void)
}

Parameters

Parameter Description
callback The success callback.

Sample code

public device: TSmartDevice = TSmartDevice.create(devId)

new Action('Query device signal', () => {
	this.device!.queryWifiSignal(signal => {
      console.log("signal is =" + signal)
    })
}),

Query a list of sub-devices for a gateway

Returns a list of sub-devices for a specific gateway.

API description

export class TSmartDevice {
	public async getSubDeviceList(): Promise<TSmartDeviceModel[]>
}

Sample code

public device: TSmartDevice = TSmartDevice.create(devId)

async getSubDeviceList() {
    const subDeviceList = await this.device?.getSubDeviceList()
  }

Wake up a low power device

For some low power devices, it is usually necessary to wake up the device before controlling it. You can try waking up the device using the following interface.

Waking up low power devices takes some time (the exact time varies depending on the device, usually 3 to 5 seconds), so it is recommended to call this function in advance at an appropriate time.

API description

export class TSmartDevice {
	public async awakeDevice(): Promise<boolean | undefined>
}

Sample code

public device: TSmartDevice = TSmartDevice.create(devId)

async awakeDevice() {
    await this.device?.awakeDevice()
  }