EchoNestPy - Alexa meet Nest via Python

TL;DR

EchoNestPy is a python based API server running on flask. It allows the Amazon Echo to talk to the server and then the server will talk to your Nest allowing you to control it. This version is setup to allow multiple Amazon Echo users to share the same server to control their own Nest without interfering with the other users Nests. This means that each Amazon Echo UserID is tied to their own Nest Token and it remembers which Echo UserID is tied to each Nest Token. It uses pickle to write the datastore to a file on the disk so that when the server is restarted it does not require each user to re-authenticate the nest again.

Right now it will control multiple Nests in the same house however when setting the temperature to a set value, it will set all Nests to the same temperature. But when changing the temperature by warmer or cooler it will +/- 2 degrees f to each Nest, even if they are different values.

Sample Interactions:
  • Alexa, Talk to Nest
    • What can I say?
  • Alexa, Tell Nest to set the temperature to 76 degrees.
  • Alexa, Tell Nest that I am too warm.
  • Alexa, Tell Nest to turn the temperature up.

From the video you can see that there are sometime problems when you give it a few commands with out some time between them - For example it should have only turned it down 2 degrees.. Not 4 degrees.. But if you were to wait 30 seconds between the commands it would not have this issue..


Since Amazon opened up the Alexa Skill Kit (ASK), I have been looking at the documentation and trying to figure out how I wanted to approach this problem...

I knew that I didn't want to use Amazon Lambda or AWS.. I wanted something that I could host.. That I fully managed.. So I decided that I was going to write my own service to host a Echo API.. And since I have been so focused in Python lately I decided that I would take that path..

Things to keep in mind..

Rules set in place by Amazon:

  • Must be publicly available using https
  • Must use port 443 My Rules:
  • Must know how to handle multiple users
    • Each user must only be able to control their own devices
    • Must remember users and user settings
  • Must use Nest Oauth2
    • AKA: Must not use Nest Username and Password (Lots of Python Nest APIs use this approach)

The Story

With those Ideas in place I started to work on my first ASK App.. Hello World.. Amazon has lots of sample code and documentation on their developer website on how to do simple apps.. But all of these use Java or Node.js on Amazon Lambda.. So most of that was useless to me..

After digging around some I found samples of the requests that Amazon will send to your service and what it expects back as a response.. Once I found that things started coming together..

curl -v -k -H "Content-Type: application/json" -X POST http://localhost:5000/EchoPyAPI --data-binary  '{              
  "version": "1.0",
  "session": {
    "new": true,
    "sessionId": "session1234",
    "application": {
      "applicationId": "amzn1.echo-sdk-ams.app.1234"
    },               
    "attributes": {},
    "user": {
      "userId": null
    }
  },          
  "request": {
    "type": "LaunchRequest",
    "requestId": "request5678",
    "timestamp": "2015-05-13T12:34:56Z"
  }
}'

{
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "text": "Hello world"
    },
    "card": {
      "type": "Simple",
      "title": "HelloWorld - Title",
      "content": "HelloWorld - Content"
    }
  }

So I decided to use flask as a simple web server.. That part was easy... I only had to really handle a single url for requests...

Form there it was just a matter of interrupting the data and forming a response.. So in no time I has a simple Hello World.. One that was already modular.. (I will post about this soon.. Also with the source code) So now it was time to start working on EchoNestPy..

First off it was a matter of building a new Python API for Nest.. Again start with flask as a base so I could do oath requests.. I decided that I should build out a class of a DataStore so that I could have multiple users and have a way to import and export data when you needed to restart the service.. So each Nest auth token was tied to a UserID.. This UserID would eventually be the Amazon Echo ID that was provided by the Echo with each request.. Allowing me to have multiple users on the same service.. So in order to complete the Nest auth you needed to know your Amazon Echo ID.. So I decided that the best way of do this was to have the Echo give it to you in the Echo App on your phone if you try to talk to the nest and you don't have an oauth token... (I will also post about this soon.. Also with the source code)

Now that I had two Python apps that were built in the same way.. Making them play together wasn't too bad..

Right now it will control multiple Nests in the same house however when setting the temperature to a set value, it will set all Nests to the same temperature. But when changing the temperature by warmer or cooler it will +/- 2 degrees f to each Nest, even if they are different values.

Stuff I still have to do:

  • Add chnage mode to Nest and Alexa for Away / Home
  • Add better support for multi-Nest Households.
  • Add check in time of inbound requests for security.
  • Improve sample utterances
  • Add better help.

Download

As always my code is available on my GitHub

Go Top
comments powered by Disqus