跳到主要内容

ESPCDFDevice

ESPCDFDevice is one logical device on a node (for example Light, Fan, Switch). Use it when you render a device card or refresh params for a single device slice of the node tree.

Devices are nested under ESPCDFNode.devices. They are created by adaptors during node transformation — not instantiated in UI code.

getParams is the main operation; param read/write usually goes through ESPCDFDeviceParam.


Properties

PropertyTypeDescription
namestringDevice name in node config
typeoptionale.g. esp.device.lightbulb
displayNameoptionalUI label
paramsESPCDFDeviceParam[]Observable param entities
attributesoptionalStatic device attributes
operationsESPCDFDeviceOperationSDK delegates
_rawanyOriginal SDK device
eventsESPCDFOperationEventEmitterOperation bus

Common Workflows

Walk the device tree on a node

import { initCDF } from "@espressif/rainmaker-base-cdf";

const espCDF = await initCDF({ sdkAdaptorRegistry });
const node = espCDF.nodeStore.getNodeById("node-id");

for (const device of node?.devices ?? []) {
console.log(device.name, device.type, device.displayName);
for (const param of device.params) {
console.log(" ", param.name, param.value);
}
}

Refresh params

const device = node!.devices!.find((d) => d.name === "Light")!;
const params = await device.getParams();

Toggle a param

Prefer ESPCDFDeviceParam.setValue for single-param updates:

const power = device.params.find((p) => p.name === "Power");
await power?.setValue(true);

Or set multiple params on the parent node:

await node!.setMultipleParams({ Light: { Power: true, Brightness: 80 } });

Error Handling

try {
await device.getParams();
} catch (e) {
showErrorBanner("Failed to refresh device state");
}


On this page