需求
通過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
-
重新關聯到Google Assistant
-
可以在 Firebase Database 看到washer的信息
- 如果需要控制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。