Google Assistant SmartHome 入門指南

需求

通過Google Assistant 控制一些從設備(Light, Washer等設備), Youtube上一個簡短的視頻介紹了Google Assistant控制SmartHome的流程。Integrating Smart Home Devices with the Google Assistant

Google Assistant控制第三方從設備,必須通過創建一個Action

官方文檔中Smart Home Washer提供了一個通過Google Assistant控制Washer功能。

Actions console創建了一個簡單的Demo.

1. Create a smart home Action

需要填寫項目的名稱
在這裏插入圖片描述
選擇 Smart Home App

在這裏插入圖片描述

2. Install the Firebase Command Line Interface

Firebase命令行界面(CLI)允許您在本地提供Web應用程序,並將您的Web應用程序部署到Firebase託管。

// 安裝firebase-tools
npm -g install firebase-tools
firebase --version
// 如果不是VPN網絡,會出現授權失敗的問題
firebase login

3. 下載smarthome-washer源碼

//源碼下載
git clone https://github.com/googlecodelabs/smarthome-washer.git
//
cd washer-start
//
firebase use --add
//
npm --prefix functions/ install
// 部署
firebase deploy --only functions:smarthome
//部署成功之後會提示
✔  Deploy complete!

Please note that it can take up to 30 seconds for your updated functions to propagate.
Project Console: https://console.firebase.google.com/project/supple-tracker-237900/overview

4. Configure your project in the Actions on Google console

在這裏插入圖片描述

https://us-central1-<project-id>.cloudfunctions.net/smarthome
其中<project-id>就是 supple-tracker-237900
  • 鏈接賬戶
    在這裏插入圖片描述
  • 配置相關信息
    在這裏插入圖片描述
Client ID Client secret Authorization URL Token URL
ABC123 DEF456 https://us-central1-.cloudfunctions.net/fakeauth https://us-central1-.cloudfunctions.net/faketoken

5. Link to the Google Assistant

  • 找到 Google Assistant > Settings > Home Control
  • 會發現帶有 [test] 前綴的 test app
    在這裏插入圖片描述

6. Creating a washer

修改 washer-start/functions/index.js中的代碼

app.onSync(body => {
  return {
    requestId: body.requestId,
    payload: {
      agentUserId: '123', 
      devices: [{
        id: 'washer',
        type: 'action.devices.types.WASHER',
        traits: [
          'action.devices.traits.OnOff',
          'action.devices.traits.StartStop',
          'action.devices.traits.RunCycle'
        ],
        name: {
          defaultNames: ['My Washer'],
          name: 'Washer',
          nicknames: ['Washer']
        },
        deviceInfo: {
          manufacturer: 'Acme Co',
          model: 'acme-washer',
          hwVersion: '1.0',
          swVersion: '1.0.1'
        },
        attributes: {
          pausable: true
        }
     }]
    }
  };
});
  • 重新進行部署
firebase deploy

在這裏插入圖片描述

  • 如果需要控制washer,需要修改washer-start/functions/index.js中
app.onExecute((body) => {
  const {requestId} = body;
  const payload = {
    commands: [{
      ids: [],
      status: 'SUCCESS',
      states: {
        online: true,
      },
    }],
  };
  for (const input of body.inputs) {
    for (const command of input.payload.commands) {
      for (const device of command.devices) {
        const deviceId = device.id;
        payload.commands[0].ids.push(deviceId);
        for (const execution of command.execution) {
          const execCommand = execution.command;
          const {params} = execution;
          switch (execCommand) {
            case 'action.devices.commands.OnOff':
              firebaseRef.child(deviceId).child('OnOff').update({
                on: params.on,
              });
              payload.commands[0].states.on = params.on;
              break;
            case 'action.devices.commands.StartStop':
              firebaseRef.child(deviceId).child('StartStop').update({
                isRunning: params.start,
              });
              payload.commands[0].states.isRunning = params.start;
              break;
            case 'action.devices.commands.PauseUnpause':
              firebaseRef.child(deviceId).child('StartStop').update({
                isPaused: params.pause,
              });
              payload.commands[0].states.isPaused = params.pause;
              break;
          }
        }
      }
    }
  }
  return {
    requestId: requestId,
    payload: payload,
  };
});
  • 重新進行部署firebase deploy,然後就可以通過Google Assistant 控制了
"Turn on my washer"

"Pause my washer"

"Stop my washer"
總結

按照文檔中的示例,熟悉了通過Google Assistant 控制 washer。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章