Update Hue-Party-Mode.groovy
[smartapps.git] / official / 03-sms-to-hue.groovy
1 /**
2  *  [Workshop Demo] SMS to Hue
3  *
4  *  Copyright 2015 Andrew Mager
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "[Workshop Demo] SMS to Hue",
18     namespace: "com.smartthings.dev",
19     author: "Andrew Mager",
20     description: "Change the color of Hue bulbs from an SMS.",
21     category: "My Apps",
22     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
24     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
25     oauth: true)
26
27
28 preferences {
29   section("Control these hue bulbs...") {
30     input "hues", "capability.colorControl", title: "Which Hue Bulbs?", required:false, multiple:true
31   }
32 }
33  
34  
35 // This block defines an endpoint, and which functions will fire depending on which type
36 // of HTTP request you send
37 mappings {
38     // The path is appended to the endpoint to make request
39     path("/hue") {
40         action: [
41             PUT: "postHue" 
42         ]
43     }
44 }
45
46
47 def installed() {}
48 def updated() {}
49
50
51
52 /*
53   This function receives a JSON payload and parses out the color from a tweet. 
54   For example, someone tweets, "@SmartThingsDev #IoTWorld2015 color=blue". Then it sends the 
55   correct color as a string to setHueColor().
56 */
57 def postHue() {
58     /*
59       "request.JSON?" checks to make sure that the object exists. And ".text" is the
60       key for the value that we're looking for. It's the body of the tweet.
61     */
62     def color = (request.JSON?.value).toLowerCase()
63     
64     try {
65         // Finds the text "color=[colorname]" and parses out the color name
66         setHueColor(color)     
67     }
68     catch (any) {
69         log.trace "Something went wrong."
70     }    
71 }
72
73
74 // This function takes a String of text and associates it with an Integer value for the color.
75 private setHueColor(color) {
76
77     // Initaliaze values for hue, saturation, and level
78     def hueColor = 0
79     def saturation = 100
80     def level = 100
81
82     switch(color) {
83         case "white":
84             hueColor = 52
85             saturation = 19
86             break;
87         case "blue":
88             hueColor = 70
89             break;
90         case "green":
91             hueColor = 39
92             break;
93         case "yellow":
94             hueColor = 25
95             break;
96         case "orange":
97             hueColor = 10
98             break;
99         case "purple":
100             hueColor = 75
101             break;
102         case "pink":
103             hueColor = 83
104             break;
105         case "red":
106             hueColor = 100
107             break;
108     }
109
110     // Set the new value of hue, saturation, and level
111     def newValue = [hue: hueColor, saturation: saturation, level: level]
112
113     // Update each Hue bulb with the new values
114     hues*.setColor(newValue)
115 }