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

Google Assistant集成action.devices.types.SETTOP代码故障求助及示例需求

智能机顶盒(SETTOP)Google Assistant集成问题排查及示例代码

离线状态排查要点

  • 确保Cloud Function的同步(SYNC)响应中设备的online字段设为true,且设备ID与后续查询(QUERY)、执行(EXECUTE)请求中的ID完全一致
  • 检查Cloud Function的访问权限,确保Google Assistant能正常调用(避免IP限制或未公开触发地址)
  • 确认QUERY请求返回的设备状态中online字段为true,Assistant依赖该字段判断设备在线状态
  • 验证Cloud Function的响应格式完全符合Smart Home API规范,避免语法错误或必填字段缺失

action.devices.types.SETTOP完整示例代码

以下是包含OnOff、Volume trait的Cloud Function示例,涵盖SYNC、QUERY、EXECUTE核心请求处理:

const functions = require('firebase-functions');

exports.smartHome = functions.https.onRequest((request, response) => {
  const { headers, body } = request;
  
  // 认证验证(根据实际OAuth2配置调整)
  if (!headers.authorization || !validateToken(headers.authorization)) {
    return response.status(401).send('Unauthorized');
  }

  switch (body.inputs[0].intent) {
    // 设备同步请求
    case 'action.devices.SYNC':
      response.json({
        requestId: body.requestId,
        payload: {
          agentUserId: 'user_123', // 替换为实际用户ID
          devices: [
            {
              id: 'stb_001', // 唯一设备ID
              type: 'action.devices.types.SETTOP',
              traits: [
                'action.devices.traits.OnOff',
                'action.devices.traits.Volume'
              ],
              name: {
                name: '客厅机顶盒',
                nicknames: ['机顶盒', '电视盒子']
              },
              willReportState: true, // 开启状态主动上报(可选)
              attributes: {
                volumeMax: 100,
                canMuteAndUnmute: true
              },
              deviceInfo: {
                manufacturer: '自定义厂商',
                model: 'STB-Pro',
                hwVersion: '1.0',
                swVersion: '2.3'
              },
              online: true // 同步时标记设备在线
            }
          ]
        }
      });
      break;

    // 设备状态查询请求
    case 'action.devices.QUERY':
      const queryDeviceIds = body.inputs[0].payload.devices.map(device => device.id);
      const queryResults = {};

      // 替换为真实设备状态查询逻辑
      queryDeviceIds.forEach(deviceId => {
        queryResults[deviceId] = {
          on: true,
          online: true,
          volumeLevel: 30,
          isMuted: false
        };
      });

      response.json({
        requestId: body.requestId,
        payload: {
          devices: queryResults
        }
      });
      break;

    // 设备命令执行请求
    case 'action.devices.EXECUTE':
      const executeCommands = body.inputs[0].payload.commands;
      const executeResults = {};

      executeCommands.forEach(command => {
        command.devices.forEach(device => {
          command.execution.forEach(execution => {
            switch (execution.command) {
              // 开关机命令
              case 'action.devices.commands.OnOff':
                const newOnState = execution.params.on;
                // 添加控制机顶盒开关机的实际逻辑
                executeResults[device.id] = {
                  status: 'SUCCESS',
                  states: {
                    on: newOnState,
                    online: true
                  }
                };
                break;
              
              // 设置音量命令
              case 'action.devices.commands.setVolume':
                const newVolume = execution.params.volumeLevel;
                // 添加设置音量的实际逻辑
                executeResults[device.id] = {
                  status: 'SUCCESS',
                  states: {
                    volumeLevel: newVolume,
                    online: true
                  }
                };
                break;
              
              // 静音/取消静音命令
              case 'action.devices.commands.mute':
                const muteState = execution.params.mute;
                // 添加静音控制的实际逻辑
                executeResults[device.id] = {
                  status: 'SUCCESS',
                  states: {
                    isMuted: muteState,
                    online: true
                  }
                };
                break;
            }
          });
        });
      });

      response.json({
        requestId: body.requestId,
        payload: {
          commands: [
            {
              ids: Object.keys(executeResults),
              status: 'SUCCESS',
              states: executeResults[Object.keys(executeResults)[0]].states
            }
          ]
        }
      });
      break;

    default:
      response.status(400).send('Unsupported intent');
  }
});

// 替换为真实Token认证逻辑
function validateToken(token) {
  return token.startsWith('valid_token_');
}

关键注意事项

  • 所有响应必须包含requestId,与请求中的ID完全一致
  • QUERY和EXECUTE响应中必须返回online: true,否则Google Home会标记设备离线
  • 若开启状态主动上报,需确保设备能主动向Google发送状态更新
  • 认证逻辑需根据实际OAuth2服务配置实现,避免硬编码验证

内容的提问来源于stack exchange,提问作者Ramesh S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 01:20:27