Room Management

Last Updated on : 2022-02-17 06:45:25download

Feature overview

TuyaCommunityRoom is used to describe all information about a room. One or more rooms can be created in each house. You can call its API methods to get room details, query a list of devices or groups from a room, and sort the devices and groups displayed in a room.

You can call TuyaCommunityRoomModel to get information about a room.

Initialize a room

Creates an instance of TuyaCommunityRoom by using the house ID and room ID.

API description

+ (instancetype)roomWithRoomId:(long long)roomId
                       houseId:(long long)houseId;

- (instancetype)initWithRoomId:(long long)roomId
                       houseId:(long long)houseId;

Parameters

Parameter Description
roomId The room ID.
houseId The house ID.

Example

long long roomId;
long long houseId;
TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomId houseId:houseId];

Add a room

API description

- (void)addHouseRoomWithName:(NSString *)name
                     success:(void(^)(TuyaCommunityRoomModel *result))success
                     failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
name The name of the room.
success The success callback.
failure The failure callback.

Example

long long houseId;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
[house addHouseRoomWithName:@"xxx" success:^(TuyaCommunityRoomModel * _Nonnull result) {
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

Delete a room

API description

- (void)removeHouseRoomWithRoomId:(long long)roomId
                          success:(void(^)(void))success
                          failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
roomId The room ID.
success The success callback.
failure The failure callback.

Example

long long houseId;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
[house removeHouseRoomWithRoomId:0 success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

Sort a list of rooms

API description

- (void)sortHouseRoomWithRoomList:(NSArray<TuyaCommunityRoomModel *>*)roomList
                          success:(void(^)(void))success
                          failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
roomList A list of rooms.
success The success callback.
failure The failure callback.

Example

long long houseId;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
[house sortHouseRoomWithRoomList:@[] success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

Update a room name

API description

- (void)updateRoomNameWithRoomName:(NSString *)roomName
                           success:(void(^)(void))success
                           failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
roomName The name of a room.
success The success callback.
failure The failure callback.

Example

TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomId houseId:houseId];
[room updateRoomNameWithRoomName:@"xxx" success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

Add a device to a room

Adds a device to a room by using the device ID. You can call TuyaSmartDeviceModel to get the device information.

API description

- (void)addDeviceWithDeviceId:(NSString *)deviceId
                      success:(void(^)(void))success
                      failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
deviceId The device ID.
success The success callback.
failure The failure callback.

Example

long long houseId;
TuyaCommunityRoomModel *roomModel;
TuyaSmartDeviceModel *deviceModel;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
if (house.deviceList.count && house.houseModel.rooms.count) {
    // Simulates to return the device model.
    deviceModel = house.deviceList.firstObject;
    // Simulates to return the room model.
    roomModel = house.houseModel.rooms.firstObject;
}

TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomModel.roomId houseId:houseId];
[room addDeviceWithDeviceId:deviceModel.devId success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

Remove a device from a room

Removes a device from a room.

- (void)removeDeviceWithDeviceId:(NSString *)deviceId
                         success:(void(^)(void))success
                         failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
deviceId The device ID.
success The success callback.
failure The failure callback.

Example

long long houseId;
TuyaCommunityRoomModel *roomModel;
TuyaSmartDeviceModel *deviceModel;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
if (house.roomList.count) roomModel = house.roomList.firstObject;
TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomModel.roomId houseId:houseId];
if (room.deviceList.count) deviceModel = room.deviceList.firstObject;

[room removeDeviceWithDeviceId:deviceModel.devId success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

Add a group to a room

Adds a group to a room by using the group ID. You can call TuyaSmartGroupModel to get the device information.

- (void)addGroupWithGroupId:(NSString *)groupId
                    success:(void(^)(void))success
                    failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
groupId The group ID.
success The success callback.
failure The failure callback.

Example

long long houseId;
TuyaCommunityRoomModel *roomModel;
TuyaSmartGroupModel *groupModel;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
// Simulates a room.
if (house.roomList.count) roomModel = house.roomList.firstObject;
// Simulates a group.
if (house.groupList.count) groupModel = house.groupList.firstObject;

TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomModel.roomId houseId:houseId];
// Adds a group.
[room addGroupWithGroupId:groupModel.groupId success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
   // failure handler
}];

Remove a group from a room

Removes a group from a room.

- (void)removeGroupWithGroupId:(NSString *)groupId
                       success:(void(^)(void))success
                       failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
groupId The group ID.
success The success callback.
failure The failure callback.

Example

long long houseId;
TuyaCommunityRoomModel *roomModel;
TuyaSmartGroupModel *groupModel;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
// Simulates a room.
if (house.roomList.count) roomModel = house.roomList.firstObject;
TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomModel.roomId houseId:houseId];
// Simulates a group.
if (room.groupList.count) groupModel = room.groupList.firstObject;
// Removes a group.
[room removeGroupWithGroupId:groupModel.groupId success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

Modify mappings between rooms and groups in bulk

Transfers groups in to or out of a room in bulk.

- (void)batchModifyRoomRelationWithDeviceGroupList:(NSArray<NSString *>*)deviceGroupList
                                           success:(void(^)(void))success
                                           failure:(void(^)(NSError *error))failure;

Parameters

Parameter Description
deviceGroupList The list of device ID and group ID.
success The success callback.
failure The failure callback.

Example

TuyaCommunityRoom *room;
[room batchModifyRoomRelationWithDeviceGroupList:@[@"xxx", @"xxx"] success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

Object description

TuyaCommunityRoom

TuyaCommunityRoom is used to describe all information about a room. One or more rooms can be created in each house.

Property Type Description
roomModel TuyaCommunityRoomModel The room information.
deviceList NSArray<TuyaSmartDeviceModel> A list of devices.
groupList NSArray<TuyaSmartGroupModel> A list of groups.

TuyaCommunityRoomModel

You can call the API methods of TuyaCommunityRoom to get room details, query a list of devices or groups from a room, and sort the devices and groups displayed in a room.

Property Type Description
roomId NSString The room ID.
name NSString The name of the room.
displayOrder NSInteger The sorting order.