通过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
// 如果不是V**网络,会出现授权失败的问题
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
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
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,
};
});
"Turn on my washer"
"Pause my washer"
"Stop my washer"
按照文档中的示例,熟悉了通过Google Assistant 控制 washer。