设备功能接口(trait)
对于 Aqara 设备,endpoint(端点)、function(功能) 和 trait(功能点)是描述和操作智能设备功能的三个核心层级。
- 端点:代表设备的物理或逻辑端口,每个端点标识设备的一个子单元(如插座的某一个孔),用
endpointId唯一标识。 - 功能:每个端点可包含一个或多个功能,例如“开关控制”、“状态信息”等。功能以
functionCode区分。 - 功能点:每个功能进一步由多个功能点组成。功能点是可读、可写、可订阅的最小操作单元,对应具体的某个属性和操作,如“开关状态”、“当前功率值”等。功能点以
traitCode区分。
以双键开关 A 为例,示意上述关系:
双键开关 A
├─ Root (endpointId: 0)
│ └─ 基本信息 (functionCode: BasicInformation)
│ │ └─ 固件版本(traitCode:FirmwareRevision)
│ │ └─ ...
│ └─ ...
├─ Public (endpointId: 1)
│ └─ 电量控制 (functionCode: EnergyManagement)
│ │ └─ 电压值(traitCode:CurrentVoltage)
│ │ └─ ...
│ └─ ...
├─ Switch (endpointId: 2)
│ └─ 开关功能 (functionCode: Output)
│ │ └─ 开关状态(traitCode:OnOff)
└─ Switch (endpointId: 3)
└─ 开关功能 (functionCode: Output)
│ └─ 开关状态(traitCode:OnOff)
在实际业务开发时,您会经常在接口参数和返回中遇到 type、trait 等字段。关于这些字段涉及的设备类型和功能点编码,可参考以下文档:
根据 ID 查询 Matter 设备的功能
- 接口intent:spec.query.specdevice.config
- 接口描述:该接口通过 Matter 设备的 ID 查询其功能。Matter 设备,是指 model 前缀为
aqara.matter.的设备。
请求参数说明
| 名称 | 类型 | 是否必须 | 描述 |
|---|---|---|---|
| dids | Array<String> | 是 | 待查询的 matter 设备 ID 列表,数组长度上限为 10。 |
请求参数示例
{
"intent" : "spec.query.specdevice.config",
"data":{
"dids": ["matt.2990fb3413ed87c9b7370000"]
}
}
响应参数说明
result.data 是一个 Object 数组。下表为 result.data 的数据结构详情,表中的各字段均为 result.data 的子结构:
| 字段 | 类型 | 说明 |
|---|---|---|
| deviceId | String | 设备 ID。 |
| types | Array<String> | 设备支持的类型列表,字符串含义请参考 设备类型说明。 |
| endpoints | Array<Object> | 端点列表。 |
| endpoints[].endpointId | Number | 端点 ID。 |
| endpoints[].endpointTypes | Array<String> | 端点类型列表。 |
| endpoints[].functions | Array<Object> | 功能列表。 |
| endpoints[].functions[].functionId | Number | 功能 ID。 |
| endpoints[].functions[].functionCode | String | 功能代码。 |
| endpoints[].functions[].traits | Array<Object> | 功能点列表。 |
| endpoints[].functions[].traits[].traitId | Number | 功能点 ID。 |
| endpoints[].functions[].traits[].traitCode | String | 功能点代码,对应设备支持的具体功能。请参阅 功能点代码,了解该功能点详情包括含义、类型(如数值、布尔、枚举等)、是否可读/可写/可上报等。 |
| endpoints[].functions[].traits[].attribute | Object | 功能点属性。 |
| endpoints[].functions[].traits[].attribute.readable | Boolean | 是否可读。 |
| endpoints[].functions[].traits[].attribute.writable | Boolean | 是否可写。 |
| endpoints[].functions[].traits[].attribute.subscribable | Boolean | 是否可订阅。 |
| endpoints[].functions[].traits[].attribute.type | String | 功能点属性的类型(BOOLEAN、STRING、ENUM、NUMBER、OTHER 等)。 |
| endpoints[].functions[].traits[].attribute.format | String | 功能点属性的具体类型。 |
| endpoints[].functions[].traits[].attribute.unit | String | 单位。仅当 attribute.type 为 NUMBER 时,此字段有意义。 |
| endpoints[].functions[].traits[].attribute.defaultValue | String | 默认值。 |
| endpoints[].functions[].traits[].attribute.min/max/step | 数值 | 数值型的最小/最大/步进值。 |
| endpoints[].functions[].traits[].attribute.enums | Array<Object> | 枚举值列表。仅当 attribute.type 为 ENUM 和 BOOLEAN 时,此字段有意义。 |
| endpoints[].functions[].traits[].attribute.enums.key | string | 枚举键。 |
| endpoints[].functions[].traits[].attribute.enums.value | string | 枚举值。 |
| endpoints[].functions[].traits[].value | Any | 功能点当前值,类型视 format 而定。 |
| endpoints[].functions[].traits[].time | Number | 功能点值更新的 UTC 时间戳(毫秒级),可能无此字段。 |
| model | String | 设备的 model 名称。 |
响应数据示例
{
"code": 0,
"message": "Success",
"msgDetails": null,
"requestId": "",
"result": {
"data": [
{
"types": [
"ContactSensor"
],
"endpoints": [
{
"functions": [
{
"functionId": 128,
"traits": [
{
"traitId": 32901,
"attribute": {
"readable": true,
"format": "Bool",
"type": "BOOLEAN",
"subscribable": true,
"writable": false
},
"time": 1762399511357,
"value": "1",
"traitCode": "Reachable"
}
],
"functionCode": "BasicInformation"
},
{
...
}
],
"endpointId": 0,
"endpointTypes": [
"Root"
]
},
{
...
},
],
"model": "aqara.matter.4447_8194",
"deviceId": "matt.2990fb3413ed87c9b7370000"
}
]
}
}
根据 model 查询非 Matter 设备的功能
- 接口intent:spec.query.qlinkmodel.config
- 接口描述:该接口通过非 Matter 设备的 model 查询其所有功能。
请求参数说明
| 名称 | 类型 | 是否必须 | 描述 |
|---|---|---|---|
| models | Array of String | 是 | 待查询的 model 列表,数组长度上限为 5。 |
请求参数示例
{
"intent" : "spec.query.qlinkmodel.config",
"data":{
"models": ["lumi.switch.aus002"]
}
}
响应数据说明
result.data 是一个 Map<String, Object>,以设备的 model 作为 key,其 value 为该设备的配置对象。下表为 result.data 的数据结构详情,表中的各字段均为 result.data 的子结构:
| 字段 | 类型 | 说明 |
|---|---|---|
| {model} | Object | {model} 表示动态字段名,即以具体的设备 model 名称为 key,不同 model 对应不同的配置对象。 |
| {model}.model | String | 设备 model 名称。 |
| {model}.types | Array<String> | 设备支持的类型列表,字符串含义请参考 设备类型说明。 |
| {model}.endpoints | Array<Object> | 端点列表。 |
| {model}.endpoints[].endpointId | Number | 端点 ID。 |
| {model}.endpoints[].endpointTypes | Array<String> | 端点类型列表。 |
| {model}.endpoints[].functions | Array<Object> | 功能列表。 |
| {model}.endpoints[].functions[].functionId | Number | 功能 ID。 |
| {model}.endpoints[].functions[].functionCode | String | 功能代码。 |
| {model}.endpoints[].functions[].traits | Array<Object> | 功能点列表。 |
| {model}.endpoints[].functions[].traits[].traitId | Number | 功能点 ID。 |
| {model}.endpoints[].functions[].traits[].traitCode | String | 功能点代码,对应设备支持的具体功能。请参阅 功能点代码,了解该功能点详情包括含义、类型(如数值、布尔、枚举等)、是否可读/可写/可上报等。 |
| {model}.endpoints[].functions[].traits[].attribute | Object | 功能点属性。 |
| {model}.endpoints[].functions[].traits[].attribute.readable | Boolean | 是否可读。 |
| {model}.endpoints[].functions[].traits[].attribute.writable | Boolean | 是否可写。 |
| {model}.endpoints[].functions[].traits[].attribute.subscribable | Boolean | 是否可订阅。 |
| {model}.endpoints[].functions[].traits[].attribute.type | String | 功能点属性的类型(BOOLEAN、STRING、ENUM、NUMBER、OTHER 等)。 |
| {model}.endpoints[].functions[].traits[].attribute.format | String | 功能点属性的具体类型。 |
| {model}.endpoints[].functions[].traits[].attribute.unit | String | 单位。仅当 attribute.type 为 NUMBER 时,此字段有意义。 |
| {model}.endpoints[].functions[].traits[].attribute.defaultValue | String | 默认值。 |
| {model}.endpoints[].functions[].traits[].attribute.min/max/step | 数值 | 数值型的最小/最大/步进值。 |
| {model}.endpoints[].functions[].traits[].attribute.enums | Array<Object> | 枚举值列表。仅当 attribute.type 为 ENUM 和 BOOLEAN 时,此字段有意义。 |
| {model}.endpoints[].functions[].traits[].attribute.enums.key | string | 枚举键。 |
| {model}.endpoints[].functions[].traits[].attribute.enums.value | string | 枚举值。 |
响应数据示例
{
"code": 0,
"message": "Success",
"msgDetails": null,
"requestId": "",
"result": {
"data": {
"lumi.switch.aus002": {
"types": [
"Switch",
"Button",
"OccupancySensor"
],
"endpoints": [
{
"functions": [
{
"functionId": 128,
"traits": [
{
"traitId": 32901,
"attribute": {
"readable": true,
"enums": [
{
"value": "0",
"key": "Offline"
}
],
"format": "Bool",
"type": "BOOLEAN",
"subscribable": true,
"writable": false
},
"traitCode": "Reachable"
}
],
"functionCode": "BasicInformation"
},
{
...
}
],
"endpointId": 0,
"endpointTypes": [
"Root"
]
},
{
...
}
],
"model": "lumi.switch.aus002"
}
}
}
}
获取最新功能点值
- 接口intent:spec.query.trait
- 接口描述:本接口支持批量查询 trait 的当前值,单次最多支持 50 个 trait。如果传入的 trait 数量超过 50,仅返回前 50 个 trait 的最新值。
请求参数说明
| 参数 | 类型 | 是否必须 | 描述 |
|---|---|---|---|
| traits | Array<Object> | 是 | 待查询功能点的数组,最多支持 50 个。 |
| traits.deviceId | String | 是 | 设备唯一标识。 |
| traits.endpointId | Number | 是 | 端点 ID。 |
| traits.functionCode | String | 是 | 功能代码。 |
| traits.traitCode | String | 是 | 功能点代码,对应设备支持的具体功能。请参阅 功能点代码说明,了解该功能点详情包括含义、类型(如数值、布尔、枚举等)、是否可读/可写/可上报等。 |
请求参数示例
{
"intent": "spec.query.trait",
"data": {
"traits": [
{
"deviceId": "lumi1.54ef448a0b73",
"endpointId": 0,
"functionCode": "BasicInformation",
"traitCode": "Reachable"
},
{
"deviceId": "lumi1.54ef448a0b73",
"endpointId": 0,
"functionCode": "Descriptor",
"traitCode": "EndpointArrayDynamic"
},
{
...
}
]
}
}
响应参数说明
result.data 是一个 Object 数组,下表为 result.data 的数据结构详情,表中的各字段均为 result.data 的子结构:
| 字段名 | 类型 | 说明 |
|---|---|---|
| functionCode | String | 功能代码,标识功能点所属的功能类型,如 BasicInformation 等 |
| endpointId | Number | 端点 ID。 |
| time | Number/Null | 功能点值更新的 UTC 时间戳(毫秒)。当值为 null时,表示您不需要关注此功能点的更新时间。 |
| deviceId | String | 设备 ID。 |
| value | String | 功能点当前值,类型视 traitCode 所表示功能点而定 。 |
| traitCode | String | 功能点代码,对应设备支持的具体功能。请参阅 功能点代码说明,了解该功能点详情包括含义、类型(如数值、布尔、枚举等)、是否可读/可写/可上报等。 |
响应数据示例
{
"code": 0,
"message": "Success",
"msgDetails": null,
"requestId": "",
"result": {
"data": [
{
"functionCode": "BasicInformation",
"endpointId": 0,
"time": null,
"deviceId": "lumi1.54ef448a0b73",
"value": "1",
"traitCode": "Reachable"
},
{
"functionCode": "Descriptor",
"endpointId": 0,
"time": 1762417659743,
"deviceId": "lumi1.54ef448a0b73",
"value": "[0,1,2,3]",
"traitCode": "EndpointArrayDynamic"
},
{
...
}
]
}
}
控制设备
- 接口intent:spec.write.trait
- 接口描述:本接口用于通过指定功能点列表批量控制设备。单次最多可控制 10 台设备,对每台设备可同时下发任意数量的功能点。当设备数量超过 10 台时,仅处理前 50 个功能点的控制请求,超出部分将被忽略。
请求参数说明
| 字段 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| traits | Array<Object> | 是 | 待写入的功能点的数组。 数组长度说明:
|
| traits.deviceId | String | 是 | 设备唯一ID。 |
| traits.endpointId | Number | 是 | 端点ID。 |
| traits.functionCode | String | 是 | 功能代码。 |
| traits.traitCode | String | 是 | 功能点代码。 |
| traits.value | Any | 是 | 功能点目标值。 |
请求参数示例
{
"intent": "spec.write.trait",
"data": {
"traits": [
{
"deviceId": "lumi1.54ef448a0b73",
"endpointId": 3,
"functionCode": "Output",
"traitCode": "OnOff",
"value": "0"
}
]
}
}
响应参数说明
| 字段 | 类型 | 说明 |
|---|---|---|
| functionCode | String | 功能代码,对应被控制的功能,如 "Output"。 |
| endpointId | Number | 端点 ID,标识具体功能所在的端点。 |
| deviceId | String | 设备 ID,被控制的设备唯一标识。 |
| value | Any | 设置后的功能点值,类型与实际 trait 属性相关。 |
| traitCode | String | 功能点代码,对应设备支持的具体功能。请参阅 功能点代码说明,了解该功能点详情包括含义、类型(如数值、布尔、枚举等)、是否可读/可写/可上报等。 |
| code | Number | 设备调用返回码,0 表示调用成功,非 0 表示调用失败(仅代表接口请求本身,不代表设备实际控制成功与否)。 |
响应数据示例
result.data 是一个 Object 数组,下表为 result.data 的数据结构详情,表中的各字段均为 result.data 的子结构:
{
"code": 0,
"message": "Success",
"msgDetails": null,
"requestId": "",
"result": {
"data": [
{
"functionCode": "Output",
"endpointId": 3,
"deviceId": "lumi1.54ef448a0b73",
"value": "0",
"traitCode": "OnOff",
//设备调用是否成功,0:成功,非0:不成功,这只代表接口调用成功,不代表设备控制成功
"code": 0
}
]
}
}
订阅和取订设备功能值
如需订阅或取消订阅设备功能点值的推送,请参考消息推送接口(trait)文档中的相关说明: