In part four we’ve created an empty AWS lambda application to answer to the Google Smart Home requests, now it’s time to really implement it.

To fullfill the requests coming from the Google Assistant we need to implement 4 REST API in our AWS fullfillment .net Core Web API c# solution:

  • SYNC
  • QUERY
  • EXECUTE
  • DISCONNECT
  • REPORT STATE

There is an official library for .net (nuget), but since is bad documented we’ll implement a minimal set of the Google Smart Home SDK, just for the thermostats.

SYNC

The first call from Google is sync, which asks for the list of thermostats. The token for the thermostat backend is in the Authorization header, and the body contains a JSON with the SmartHomeRequest object:

public class SmartHomeRequest
    {
        public string requestId { get; set; }
        public List<RequestInput> inputs { get; set; }

        public SmartHomeRequest()
        {
            inputs = new List<RequestInput>();
        }
    }
public class RequestInput
    {
        public string intent { get; set; }
    }

With the authorization bearer token in the header (coming from the account linking phase) we can retrieve the list of thermostat, and pack it a SmartHomeResponse object:

With this c# models we can elaborate the answer needed by Google in the POST fullfillment call:

We have given Google the list of thermostats. If we test now in the smartphone Home app, when we do the account pairing with [test] Smart Fullfill we get the list of thermostats:

QUERY

Ask the fullfillment server the current real-time status of one or more devices.

NOTE: there are two ways to know the status of device:

  • QUERY call to fullfillment server
  • Report State: the thermostats are constantly connected to the Google Home Graph sending statuses

When the Google Assistant is asked for the status of a thermostat, it issues the fullfillment server a QUERY POST call, asking the current real-time status. We just need to put together a JSON like this with data coming from the IoT backend:

{
  "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
  "payload": {
    "devices": {
      "123": {
        "online": true,
        "thermostatMode": "cool",
        "thermostatTemperatureSetpoint": 23,
        "thermostatTemperatureAmbient": 25.1,
        "thermostatHumidityAmbient": 45.3
      }
    }
  }
}

EXECUTE

Send one or more commands to the fullfillment server for one or more thermostats.

DISCONNECT

Stop reporting to this user.

Report state

The Home Graph is a database of your house devices, and it will contains our thermostats. To show the temperature of this thermostats, especially in the visual-assistant devices, the database must be kept updated with the latest temperatures coming from the thermostats. Every now and then we should call the report-state-api updating the Home Graph. The best moments to call it are:

  • After the SYNC fullfillment request
  • After each QUERY fullfillment request
  • After every EXECUTE fullfillment request

To call report state API we need a private key. To see the code and how to call the report state API look at the JWT post.

Once the fullfillment API is ready and published on AWS, we insert its public address in the intents tab:

Now it’s time to test.

End of part 5 — Part 6