跳到主要内容

Room Management with CDF Entities

This guide shows how CDF entities map to room management tasks (like adding devices to rooms, creating scenes, etc.).


Rooms are Groups

In CDF, a room is represented by ESPCDFGroup. Think of it as a container for devices.

// Get all rooms in your home
const home = espCDF.groupStore.getGroupById("home-id");
const rooms = home?.subGroups || [];

// Access a specific room
const livingRoom = espCDF.groupStore.getGroupById("room-id");

Add/Remove Devices to a Room

ObjC example (from your reference):

[self.room addDeviceWithDeviceId:@"devId"];
[self.room removeDeviceWithDeviceId:@"devId"];

CDF example:

const room = espCDF.groupStore.getGroupById("room-id")!;

// Add device to room
await room.addNodes(["device-id"]);

// Remove device from room
await room.removeNodes(["device-id"]);

Update Room Name

ObjC example:

[self.room updateRoomName:@"new_room_name"];

CDF example:

const room = espCDF.groupStore.getGroupById("room-id")!;
await room.updateGroupInfo({ name: "new_room_name" });

Load Devices in a Room

ObjC example:

// ThingSmartRoom manages devices, but you need to load them
NSArray *devices = self.room.deviceList;

CDF example:

const room = espCDF.groupStore.getGroupById("room-id")!;

// Load all devices (nodes) in this room
const nodes = await room.getNodes();
console.log(nodes.length); // Number of devices

Control Devices in a Room

Once you have devices loaded in a room, control them using ESPCDFNode:

const room = espCDF.groupStore.getGroupById("room-id")!;
const nodes = await room.getNodes();

// Control the first device in the room
const firstDevice = nodes[0];
await firstDevice.setMultipleParams({
Light: { Power: true, Brightness: 80 },
});

Create Room Scenes

CDF example:

const room = espCDF.groupStore.getGroupById("room-id")!;

// Create a "Movie Night" scene for this room
const scene = await room.createScene({
name: "Movie Night",
nodes: ["device-id-1", "device-id-2"],
actions: {
"device-id-1": { Light: { Power: true, Brightness: 30 } },
"device-id-2": { Light: { Power: false } },
},
});

// Get all scenes in this room
const scenes = await room.getScenes();

Create Room Automations

CDF example:

const room = espCDF.groupStore.getGroupById("room-id")!;

// Create an automation: turn on lights at sunset
const automation = await room.createAutomation({
name: "Sunset Lights",
enabled: true,
eventType: "daylight",
events: [{ check: { name: "sunset" } }],
actions: [
{ nodeId: "device-id", action: { Light: { Power: true } } },
],
});

// Get all automations in this room
const automations = await room.getAutomations();

Entity Relationship

Home (ESPCDFGroup)
├── Room (ESPCDFGroup - subgroup)
│ ├── Device 1 (ESPCDFNode)
│ ├── Device 2 (ESPCDFNode)
│ ├── Scene (ESPCDFScene)
│ ├── Schedule (ESPCDFSchedule)
│ └── Automation (ESPCDFAutomation)
└── Room 2 (ESPCDFGroup - subgroup)
├── Device 3 (ESPCDFNode)
└── ...

Common Tasks

TaskEntityMethod
Get all roomsESPCDFGroupgroup.getSubGroups()
Add device to roomESPCDFGroupgroup.addNodes(["device-id"])
Remove device from roomESPCDFGroupgroup.removeNodes(["device-id"])
Rename roomESPCDFGroupgroup.updateGroupInfo({ name: "..." })
Get devices in roomESPCDFGroupgroup.getNodes()
Control deviceESPCDFNodenode.setMultipleParams({...})
Create sceneESPCDFGroupgroup.createScene({...})
Create automationESPCDFGroupgroup.createAutomation({...})

Next Steps

On this page