Commit a0708982 by Alex Rhodes

Added JavaDoc, Updated ReadMe

parent 3b9477cd
Thermostat for Echo
Copyright (C) 2016 Alex Rhodes
https://www.alexscottrhodes.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Thermostat for Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nest®, Nest Learning Thermostat®, Nest Leaf Logo® and the Works with Nest logos are trademarks licensed by Nest Labs, Inc.
Echo, Alexa are trademarks licensed to Amazon.com, Inc. or its affiliates © 1996-2016
# Amazon Echo/Nest Learning Thermostat Control #
This source code is for an Amazon Echo "Alexa" skill to control the Nest Learning Thermostat. It was previously under the Amazon Skills and Works With Nest stores. Nest has released native integration with the Amazon Echo. I am releasing this skill because it will no longer be supported. It offers more features than the Nest version. If you are into using custom skill/alexa you can use this skill for your Echo.
##Set Up
###Requirements
* A Nest application (free from nest developer site) that allows you to get API access to your nest.
* General knowledge of Amazon Skill dev process.
Without going into detail about skill development etc.. This skill can be hosted in a Lambda function on AWS as typical of any skill. It requires that you enter the Alexa Skill Application ID in the request handle, and your Nest API Key in the nest functions class. Then you need to add a default thermostat ID that you want to use when you DO NOT specify a thermostat name. If you specify a thermostat name, it will get the ID from your home structure. (See below for supported commands etc.)
##Skill description
Full description of supported features here:
http://alexsrhodes.webfactional.com/misc/therm_legacy/echo_help.html
##Contact##
Please email me with any questions, I'm not going to guide you through skill development or using the Nest application etc. Tutorials are readily available for that on Amazon and Nest's developer sites.
Email: alexscottrhodes@gmail.com
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--
Thermostat for Echo
Copyright (C) 2016 Alex Rhodes
https://www.alexscottrhodes.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Thermostat for Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nest®, Nest Learning Thermostat®, Nest Leaf Logo® and the Works with Nest logos are trademarks licensed by Nest Labs, Inc.
Echo, Alexa are trademarks licensed to Amazon.com, Inc. or its affiliates © 1996-2016
-->
<modelVersion>4.0.0</modelVersion>
<groupId>NestController</groupId>
<artifactId>Nest</artifactId>
<name>Nest for Echo </name>
<description>Control your Nest smart thermostat from your Amazon echo.</description>
<dependencies>
<!-- Nest Function and Echo Dependencies -->
......
/**
Thermostat for Echo
Copyright (C) 2016 Alex Rhodes
https://www.alexscottrhodes.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Thermostat for Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nest, Nest Learning Thermostat, Nest Leaf Logo and the Works with Nest logos are trademarks licensed by Nest Labs, Inc.
Echo, Alexa are trademarks licensed to Amazon.com, Inc. or its affiliates 1996-2016
*/
package com.alexscottrhodes.actions;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.alexscottrhodes.nest.ErrorHandler;
import com.amazon.speech.slu.Intent;
import com.amazon.speech.slu.Slot;
......@@ -22,6 +45,12 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* This class contains the main business logic for the Nest operations.
* @author Alex Rhodes
*
*/
public class NestFunctions {
private static String thermName = "undefined";
......@@ -32,8 +61,10 @@ public class NestFunctions {
public static SpeechletResponse InitiateResponse(Intent intent) throws SpeechletException{
thermId = "<Nest thermostat ID here>";
/**
* Your thermostat ID and nest api key need to be added to these fields.
*/
thermId = "<Defualt Nest thermostat ID to use without specifying name first here>";
apiKey = "<Nest API key here>";
HashMap<String,JsonObject> temp = queryDevice();
......@@ -97,7 +128,11 @@ public class NestFunctions {
}
/* interpret the type of temperature call */
/**
* Interprets the type of temperature call, whether it be range or single temperature
* @param intent an Intent indicating a temperature request
* @return a SpeechletResponse to the user
*/
public static SpeechletResponse tempCall(Intent intent) {
Slot lowSlot = intent.getSlot("LOWTEMP");
......@@ -145,7 +180,12 @@ public class NestFunctions {
}
}
/* Set temperature top or bottom */
/**
* The following method sets the tempearutre between the given values
* @param low an Integer indicating the low value for the temperature range in heat-cool mode
* @param high an Integer indicating the high value for the temperature range in heat-cool mode
* @return a SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse setTemp(int low, int high) {
String speechText ="";
......@@ -170,6 +210,12 @@ public class NestFunctions {
return SpeechletResponse.newTellResponse(speech);
}
/**
* Sets either the high or low temperature of the range in heat-cool mode
* @param high a Boolean indicating if the temperature to be changed is the high (true) or low (false)
* @param temp an Integer of the desired temperature for the range
* @return a SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse setTemp(boolean high, int temp) {
String speechText = "Okay, setting ";
if(high){
......@@ -206,7 +252,10 @@ public class NestFunctions {
return SpeechletResponse.newTellResponse(speech);
}
/* Query the temperatures*/
/**
* Returns an information response to the user including the current temperature and settings
* @return a SpeecheltResponse with the current temperature and setting information
*/
public static SpeechletResponse tempQuery() {
String speechText;
if(!thermName.equals("undefined")){
......@@ -241,7 +290,11 @@ public class NestFunctions {
}
/* Set a single temperature */
/**
* Sets a temperature when in heat or cool mode
* @param intent an Intent indicating a single temperature change
* @return SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse singleTemp(Intent intent) {
String speechText;
int temp;
......@@ -272,7 +325,11 @@ public class NestFunctions {
return SpeechletResponse.newTellResponse(speech);
}
/**
* A method to change between thermostat modes, either heat, cool, heat-cool or off
* @param intent an Intent indicating a mode change request
* @return a SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse modeChange(Intent intent){
String speechText;
String mode,modePlain;
......@@ -328,7 +385,11 @@ public class NestFunctions {
return SpeechletResponse.newTellResponse(speech);
}
/**
* Sets the fan on or off
* @param on a Boolean indicating if the fan is to be turned on or shut off
* @return a SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse fanSwitch(boolean on){
String speechText;
if(!thermName.equals("undefined")){
......@@ -358,7 +419,11 @@ public class NestFunctions {
}
/* quick filter */
/**
* A utility method to determine which direction a user desired the temperature to move in a "quick" 2 degree adjustment
* @param intent an Intent indicating a "quick" adjustment intent
* @return a SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse quickFilter(Intent intent){
Slot directionSlot = intent.getSlot("DIRECTION");
if(directionSlot.getValue()==null){
......@@ -373,7 +438,11 @@ public class NestFunctions {
}
}
/* quick adjust by fixed amount */
/**
* Method to quickly adjust temperature by two degrees with a simple "quick" request
* @param warmer a Boolean to indicate if the temperature should be increased or decreased
* @return a SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse quickAdjust(boolean warmer){
if(warmer){
return tempAdjuster(2,"higher");
......@@ -382,7 +451,11 @@ public class NestFunctions {
}
}
/* Specified adjustment direction */
/**
* Method to adjust the temperature a given direction and specified number of degrees. Will adjust either a single temperature or shift the range in heat-cool mode.
* @param intent an Intent indicating a directional temperature command
* @return a SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse specAdjustment(Intent intent){
Slot degreeSlot = intent.getSlot("DEGREES");
......@@ -405,6 +478,12 @@ public class NestFunctions {
return tempAdjuster(degrees,direction);
}
/**
* A method for adjusting the temperature the given amount in the given direction
* @param degrees an Integer representing the desired number of degrees
* @param direction the direction to move the temperature
* @returna SpeecheltResponse for the user with a message repeating back the desired range so that he or she may ensure it was accurately interpreted
*/
public static SpeechletResponse tempAdjuster(int degrees, String direction){
String speechText = "";
String currentMode = model.get("hvac_mode").getAsString();
......@@ -481,10 +560,12 @@ public class NestFunctions {
return SpeechletResponse.newTellResponse(speech);
}
/**
* A method for sending a failure response to the user
* @param message a String with the method-specific failure message to read to the user
* @param cardContent a String with content for a card to be passed with the response
* @return a SpeecheltResponse with an informative failure message to the user
*/
public static SpeechletResponse failedCard(String message,String cardContent){
SimpleCard card = new SimpleCard();
card.setTitle("Support");
......@@ -495,7 +576,10 @@ public class NestFunctions {
return SpeechletResponse.newTellResponse(speech,card);
}
/* query the device */
/**
* A method that builds a model of the device's current state
* @return a HashMap with data about the device keyed by the name of the respective field
*/
private static HashMap<String, JsonObject> queryDevice(){
HashMap<String, JsonObject> modelMap = new HashMap<String,JsonObject>();
......@@ -523,7 +607,11 @@ public class NestFunctions {
}
/* Send JSON communication against NEST api */
/**
* A method that sends data to the Nest via the Nest API
* @param JSON a JSON string of the fields to be updated
* @return a HashMap containing the response information from the request
*/
private static HashMap<String,String> nestCom(String JSON) {
HashMap<String,String> result = new HashMap<String,String>();
try {
......@@ -559,6 +647,11 @@ public class NestFunctions {
}
}
/**
* A utility for identifying the thermostat ID based on the name passed by the user
* @param thermNameRaw a String representing the thermostat's raw name
* @return a String of the thermostats ID within the Nest structure model
*/
@SuppressWarnings("rawtypes")
public static String nameToThermId(String thermNameRaw){
String result = "not-found";
......
/**
Thermostat for Echo
Copyright (C) 2016 Alex Rhodes
https://www.alexscottrhodes.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Thermostat for Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nest, Nest Learning Thermostat, Nest Leaf Logo and the Works with Nest logos are trademarks licensed by Nest Labs, Inc.
Echo, Alexa are trademarks licensed to Amazon.com, Inc. or its affiliates 1996-2016
*/
package com.alexscottrhodes.nest;
import com.amazon.speech.speechlet.SpeechletResponse;
import com.amazon.speech.ui.PlainTextOutputSpeech;
/**
* This class handles the Nest error responses as documented in the Nest API documentation
* @author Alex Rhodes
*
*/
public class ErrorHandler {
/**
* Sends a failure response with a given message
* @param message a String of the message to provide with the failure response
* @return a SpeechletResponse with the failure message
*/
public static SpeechletResponse failed(String message){
PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
speech.setText(message);
return SpeechletResponse.newTellResponse(speech);
}
/**
* Returns an informative message that handles the Nest error messages as described in the Nest API documentation
* @param message a String of the Nest failure response
* @return a SpeechletResponse with an informative message for the user to understand why a failure occured.
*/
public static SpeechletResponse nestFailureResponse(String message){
PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
......
/**
Thermostat for Echo
Copyright (C) 2016 Alex Rhodes
https://www.alexscottrhodes.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Thermostat for Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nest®, Nest Learning Thermostat®, Nest Leaf Logo® and the Works with Nest logos are trademarks licensed by Nest Labs, Inc.
Echo, Alexa are trademarks licensed to Amazon.com, Inc. or its affiliates © 1996-2016
*/
package com.alexscottrhodes.nest;
import java.util.HashSet;
import java.util.Set;
import com.amazon.speech.speechlet.lambda.SpeechletRequestStreamHandler;
/**
* Handles the Alexa Skill Service request
* @author Alex Rhodes
*
*/
public final class NestRequestHandler extends SpeechletRequestStreamHandler {
private static final Set<String> supportedApplicationIds = new HashSet<String>();
private static final Set<String> supportedApplicationIds = new HashSet<String>();
static {
//Actual:
/**
* Your Amazon application ID must be provided here
*/
supportedApplicationIds.add("<Amazon application ID here>");
}
/**
* Instantiates a nest speechlet with the given application IDs
*/
public NestRequestHandler() {
super(new NestSpeechlet(), supportedApplicationIds);
}
......
/**
Thermostat for Echo
Copyright (C) 2016 Alex Rhodes
https://www.alexscottrhodes.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Thermostat for Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nest, Nest Learning Thermostat, Nest Leaf Logo and the Works with Nest logos are trademarks licensed by Nest Labs, Inc.
Echo, Alexa are trademarks licensed to Amazon.com, Inc. or its affiliates 1996-2016
*/
package com.alexscottrhodes.nest;
import com.alexscottrhodes.actions.NestFunctions;
......@@ -10,11 +35,8 @@ import com.amazon.speech.speechlet.SessionStartedRequest;
import com.amazon.speech.speechlet.Speechlet;
import com.amazon.speech.speechlet.SpeechletException;
import com.amazon.speech.speechlet.SpeechletResponse;
import com.amazon.speech.speechlet.User;
import com.amazon.speech.ui.LinkAccountCard;
import com.amazon.speech.ui.PlainTextOutputSpeech;
import com.amazon.speech.ui.Reprompt;
import com.amazon.speech.ui.SimpleCard;
public class NestSpeechlet implements Speechlet{
......
Thermostat for Echo
Copyright (C) 2016 Alex Rhodes
https://www.alexscottrhodes.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Thermostat for Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Use Below:
{
"intents": [
{
......
Thermostat for Echo
Copyright (C) 2016 Alex Rhodes
https://www.alexscottrhodes.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
Thermostat for Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Use below:
NestSpecIntent {DIRECTION} {DEGREES} degrees
NestSpecIntent {DIRECTION} {DEGREES}
NestSpecIntent {DIRECTION} the temperature {DEGREES} degrees
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment