Google Action Smart Home Set-top Box sample

95 views Asked by At

I am trying to Integrate smart home devices (Set-top Box) with Google Assistant. So that I refer this link Smart Home Set-top Box Guide.

Here I am planning to create cloud function with basic intent like action.devices.traits.OnOff, action.devices.traits.Volume, etc.

But my sample code is not working. I tested in my mobile Google Home device shows offline

my sample code here:

const functions = require('firebase-functions');
const {smarthome} = require('actions-on-google');
const {google} = require('googleapis');
const util = require('util');
const admin = require('firebase-admin');


// Initialize Firebase
admin.initializeApp();
const firebaseRef = admin.database().ref('/');
// Initialize Homegraph
const auth = new google.auth.GoogleAuth({
  scopes: ['https://www.googleapis.com/auth/homegraph'],
});
const homegraph = google.homegraph({
  version: 'v1',
  auth: auth,
});
// Hardcoded user ID
const USER_ID = '123';


exports.login = functions.https.onRequest((request, response) => {
    if (request.method === 'GET') {
    functions.logger.log('Requesting login page');
    response.send(`
    <html>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <body>
        <form action="/login" method="post">
          <input type="hidden"
            name="responseurl" value="${request.query.responseurl}" />
          <button type="submit" style="font-size:14pt">
            Link this service to Google
          </button>
        </form>
      </body>
    </html>
  `);
  } else if (request.method === 'POST') {
    // Here, you should validate the user account.
    // In this sample, we do not do that.
    const responseurl = decodeURIComponent(request.body.responseurl);
    functions.logger.log(`Redirect to ${responseurl}`);
    return response.redirect(responseurl);
  } else {
    // Unsupported method
    response.send(405, 'Method Not Allowed');
  }
})

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);
  functions.logger.log(`Set redirect as ${responseurl}`);
  return response.redirect(
      `/login?responseurl=${encodeURIComponent(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;
  functions.logger.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);
});

const app = smarthome();


app.onSync((body) => {
  return {
    requestId: body.requestId,
    payload: {
      agentUserId: USER_ID,
      devices: [{
        id: 'settop',
        type: 'action.devices.types.SETTOP',
        traits: [
          'action.devices.traits.OnOff',
          'action.devices.traits.Volume',
        ],
        name: {
          defaultNames: ['AMX TV'],
          name: 'AMX TV',
          nicknames: ['AMX TV'],
        },
        deviceInfo: {
          manufacturer: 'ZTE',
          model: 'Claro',
          hwVersion: '1.0',
          swVersion: '1.0.1',
        },
        willReportState: true,
        attributes: {
          pausable: true,
        },
        // TODO: Add otherDeviceIds for local execution
      }],
    },
  };
});




exports.smarthome = functions.https.onRequest(app);



/*const {onRequest} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");


exports.helloWorld = onRequest((request, response) => {
  logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});
*/

Please suggest me some sample code for action.devices.types.SETTOP

1

There are 1 answers

1
Siddhant Priyadarshi On

Could you start building your setup using the following codelab and change the firebase implementation one by one from washer to set top device type. Let us know if the issue still persists and if so send across the complete SYNC and QUERY response.