Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manekinekko/046f550223e392e040fcbaf3fb9ec492 to your computer and use it in GitHub Desktop.
Save manekinekko/046f550223e392e040fcbaf3fb9ec492 to your computer and use it in GitHub Desktop.
'use strict';
const functions = require('firebase-functions');
const { smarthome } = require('actions-on-google');
const util = require('util');
const admin = require('firebase-admin');
// Initialize Firebase
admin.initializeApp();
exports.fakeauth = functions.https.onRequest((request, response) => {
const responseurl = util.format(
'%s?code=%s&state=%s',
decodeURIComponent(request.query.redirect_uri),
'xxxxxx',
request.query.state
);
console.log(responseurl);
return response.redirect(responseurl);
});
exports.faketoken = functions.https.onRequest((request, response) => {
const grantType = request.query.grant_type
? request.query.grant_type
: request.body.grant_type;
const secondsInDay = 86400; // 60 * 60 * 24
const HTTP_STATUS_OK = 200;
console.log(`Grant type ${grantType}`);
let obj;
if (grantType === 'authorization_code') {
obj = {
token_type: 'bearer',
access_token: '123access',
refresh_token: '123refresh',
expires_in: secondsInDay
};
} else if (grantType === 'refresh_token') {
obj = {
token_type: 'bearer',
access_token: '123access',
expires_in: secondsInDay
};
}
response.status(HTTP_STATUS_OK).json(obj);
});
let jwt;
try {
jwt = require('./key.json');
} catch (e) {
console.warn('Service account key is not found');
console.warn('Report state will be unavailable');
}
const app = smarthome({
debug: true,
key: '<api-key>',
jwt: jwt
});
app.onSync((body, headers) => {
return {
requestId: body.requestId,
payload: {
agentUserId: '1836.15267389',
devices: [
{
id: '123',
type: 'action.devices.types.WASHER',
traits: ['action.devices.traits.Modes'],
name: {
defaultNames: ['Flexy Desk'],
name: 'FlexyDesk',
nicknames: ['Standing Desk']
},
willReportState: false,
attributes: {
pausable: true,
availableModes: [
{
name: 'level',
name_values: [
{
name_synonym: ['one', 'two', 'three'],
lang: 'en'
}
],
settings: [
{
setting_name: 'one',
setting_values: [
{
setting_synonym: ['one', '1'],
lang: 'en'
}
]
},
{
setting_name: 'two',
setting_values: [
{
setting_synonym: ['two', '2'],
lang: 'en'
}
]
},
{
setting_name: 'three',
setting_values: [
{
setting_synonym: ['three', '3'],
lang: 'en'
}
]
}
],
ordered: true
}
],
availableToggles: [
{
name: 'level1',
name_values: [
{
name_synonym: ['level one'],
lang: 'en'
}
]
},
{
name: 'level2',
name_values: [
{
name_synonym: ['level two'],
lang: 'en'
}
]
},
{
name: 'level3',
name_values: [
{
name_synonym: ['level three'],
lang: 'en'
}
]
}
]
},
deviceInfo: {
manufacturer: 'Wassim Chegham',
model: '492134',
hwVersion: '3.2',
swVersion: '11.4'
}
}
]
}
};
});
app.onQuery((body, headers) => {
return {
requestId: body.requestId,
payload: {
devices: {
123: {
on: true,
online: true,
currentModeSettings: {
level: 'one'
}
}
}
}
};
});
app.onExecute((body, headers) => {
return {
requestId: body.requestId,
payload: {
commands: [
{
ids: ['123'],
status: 'SUCCESS',
states: {
currentModeSettings: {
level: 'two'
}
}
}
]
}
};
});
exports.smarthome = functions.https.onRequest(app);
exports.requestsync = functions.https.onRequest((request, response) => {
console.info('Request SYNC for user 123');
app
.requestSync('123')
.then(res => {
console.log('Request sync completed');
response.json(data);
})
.catch(err => {
console.error(err);
});
});
/**
* Send a REPORT STATE call to the homegraph when data for any device id
* has been changed.
*/
exports.reportstate = functions.database.ref('{deviceId}').onWrite(event => {
console.info('Firebase write event triggered this cloud function');
if (!app.jwt) {
console.warn('Service account key is not configured');
console.warn('Report state is unavailable');
return;
}
const snapshotVal = event.after.val();
const postData = {
requestId: 'ff36a3cc' /* Any unique ID */,
agentUserId: '123' /* Hardcoded user ID */,
payload: {
devices: {
states: {
/* Report the current state of our washer */
[event.params.deviceId]: {
currentModeSettings: {
level: snapshotVal.level
}
}
}
}
}
};
return app.reportState(postData).then(data => {
console.log('Report state came back');
console.info(data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment