---
name: "saveDeviceLinkageScene"
mode: "kit"
versionRequirements:
  - { name: "@tuya-miniapp/cloud-api", version: "1.1.0" }
---

## saveDeviceLinkageScene

> [VERSION] @tuya-miniapp/cloud-api >= 1.1.0

> 💡 接口依赖云能力，需在 [小程序开发者平台](https://platform.tuya.com/miniapp/)开发设置-云能力 进行授权配置。
> 注意：该方法联动条件和动作仅支持设备 DP 类型，且设备需要在当前家庭下。@tuya-miniapp/cloud-api 1.4.0 版本起，通过该方法保存的场景会显示在 app 场景 tab 中，并可通过 getSceneAndAuto、getSceneListByHomeID 等接口查询。

### 描述

保存或编辑场景(仅支持设备联动设备)

### 参数

`Params`

| 参数 | 类型 | 必填 | 默认值 | 描述 |
| --- | --- | --- | --- | --- |
| `params` | `SaveDeviceLinkageSceneParams` | 是 | - | 请求体 |

### 引用对象

##### `interface` SaveDeviceLinkageSceneParams

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `gid` | `string` | 家庭ID |
| `name` | `string` | 场景名称 |
| `coverIcon` | `string` | 图标 |
| `actions` | `SceneActionInput[]` | 动作列表 |
| `conditions` | `SceneCondition[]` | 条件列表，新建更新自动化时必传，新建更新一键执行时不传 |
| `enabled` | `boolean` | 是否启用 |
| `matchType` | `number` | 匹配类型 1:当满足任一条件时执行 2:当满足所有条件时执行 |
| `displayColor` | `string` | 背景色 |
| `id` | `string` | 场景ID 更新时必传，新增时不需要传 |

##### `type` SceneActionInput

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `id` | `string` | 动作ID 新增时不需要传，更新时必传 |
| `devDelMark` | `boolean` | 关联设备是否被移除 |
| `entityId` | `string` | 设备ID |
| `executorProperty` | `Record<string, string \| number \| false \| true>` | 执行属性 |
| `extraProperty` | `Record<string, unknown>` | 额外属性 |

##### `type` SceneCondition

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `id` | `string` | 条件ID 新增时不需要传，更新时必传 |
| `entitySubIds` | `string` | 条件关联的dpId |
| `iconUrl` | `string` | 产品图标URL |
| `entityId` | `string` | 条件关联的设备ID |
| `expr` | `string \| number[][]` | 条件表达式 |
| `devDelMark` | `boolean` | 关联设备是否被移除 |
| `entityType` | `number` | 条件类型 1: 小于、等于、大于 41: 区间内、区间外 |
| `extraInfo` | `Record<string, unknown>` | 额外信息 |


### 示例代码

#### 请求示例

```typescript
import { saveDeviceLinkageScene } from '@tuya-miniapp/cloud-api';

// 新增场景
saveDeviceLinkageScene({
  gid: '123456789',
  name: '回家模式',
  coverIcon: 'https://example.com/icon.png',
  actions: [
    {
      devDelMark: false,
      entityId: 'device_001',
      executorProperty: {
        '1': true,
        '2': 50,
      },
      extraProperty: {},
    },
  ],
  conditions: [
    {
      entitySubIds: '1',
      iconUrl: 'https://example.com/icon.png',
      entityId: 'device_002',
      expr: [['1', '==', 'true']],
      devDelMark: false,
      entityType: 1,
      extraInfo: {},
    },
  ],
  enabled: true,
  matchType: 1,
  displayColor: '#FF5722',
})
  .then(response => {
    console.log('Scene saved:', response);
  })
  .catch(error => {
    console.error('Failed to save scene:', error);
  });

// 更新场景
saveDeviceLinkageScene({
  gid: '123456789',
  id: 'scene_001',
  name: '回家模式（已更新）',
  coverIcon: 'https://example.com/icon.png',
  actions: [
    {
      id: 'action_001',
      devDelMark: false,
      entityId: 'device_001',
      executorProperty: {
        '1': true,
        '2': 80,
      },
      extraProperty: {},
    },
  ],
  conditions: [],
  enabled: true,
  matchType: 1,
  displayColor: '#FF5722',
})
  .then(response => {
    console.log('Scene updated:', response);
  })
  .catch(error => {
    console.error('Failed to update scene:', error);
  });
```

#### 返回示例

```json
{
  "name": "回家模式",
  "ruleId": "scene_001",
  "ruleGenre": 1
}
```
