You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Alexa智能家居技能自动发现账号下所有Amazon智能插头?

问题解答

一、能否让发现请求识别账号下绑定的所有Amazon智能插头?

不能直接实现。Alexa智能家居技能的设备发现逻辑是:技能作为设备提供方,需要自行管理设备列表并返回给Alexa。亚马逊官方智能插头属于亚马逊自有设备生态,第三方技能无法调用开放接口获取用户账号下绑定的官方设备数据。

可行的替代方案:

  • 让用户在技能配套的页面/应用中手动录入智能插头的信息(如友好名称、序列号),将数据存储至数据库(如DynamoDB)
  • 后续通过数据库查询用户的设备列表,生成发现响应

二、当前特定插头未显示的排查方向

你的硬编码设备配置结构基本符合规范,但可以从以下几点排查问题:

  1. 区域一致性:确保Lambda函数部署的区域与你的Alexa设备所在区域匹配(如北美用us-east-1,欧洲用eu-west-1)
  2. 授权有效性:验证发现请求的Header中是否携带了有效的用户授权Token,若授权流程存在问题,Alexa会忽略响应
  3. JSON格式校验:检查代码中JSON的语法正确性(如括号、逗号配对),可使用在线JSON校验工具验证响应Payload
  4. 触发方式:在Alexa App中手动触发设备发现(路径:设备→添加设备→其他→发现设备),确保请求已发送到你的Lambda函数
  5. 日志排查:查看Lambda对应的CloudWatch日志,确认发现请求是否被正确处理,是否存在报错信息

三、修改代码实现批量设备发现(基于用户录入数据)

假设你已通过DynamoDB存储用户的智能插头信息,可修改Lambda代码如下:

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();

async function handleDiscoveryRequest(request) {
    // 从授权Token中解析用户ID(需根据你的授权流程调整)
    const userId = request.directive.payload.scope.token;
    
    // 查询当前用户的所有智能插头
    const dbParams = {
        TableName: 'SmartPlugDevices',
        KeyConditionExpression: 'userId = :uid',
        ExpressionAttributeValues: { ':uid': userId }
    };
    const dbResult = await dynamodb.query(dbParams).promise();
    
    // 生成设备列表Payload
    const endpoints = dbResult.Items.map(item => ({
        endpointId: `plug-${item.serialNumber}`, // 用序列号生成唯一设备ID
        manufacturerName: "Amazon",
        friendlyName: item.friendlyName,
        description: "Amazon Smart Plug",
        displayCategories: ["SMARTPLUG"],
        additionalAttributes: {
            manufacturer: "Amazon",
            serialNumber: item.serialNumber,
            softwareVersion: "300000003"
        },
        cookie: {}, // 可存储设备控制所需的额外信息
        capabilities: [
            {
                "interface": "Alexa.PowerController",
                "version": "3",
                "type": "AlexaInterface",
                "properties": {
                    "supported": [{ "name": "powerState" }],
                    "retrievable": true
                }
            },
            {
                "type": "AlexaInterface",
                "interface": "Alexa.EndpointHealth",
                "version": "3.2",
                "properties": {
                    "supported": [{ "name": "connectivity" }],
                    "retrievable": true
                }
            },
            {
                "type": "AlexaInterface",
                "interface": "Alexa",
                "version": "3"
            }
        ]
    }));
    
    // 返回标准发现响应
    return {
        event: {
            header: {
                namespace: 'Alexa.Discovery',
                name: 'Discover.Response',
                payloadVersion: '3',
                messageId: request.directive.header.messageId
            },
            payload: { endpoints }
        }
    };
}

内容的提问来源于stack exchange,提问作者katerina-92

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 03:22:21