Update turn-on-before-sunset.groovy
[smartapps.git] / official / tweet-to-hue.groovy
1 /**
2  *  Tweet to Hue
3  *
4  *  Copyright 2015 Andrew Mager & Kris Schaller
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: "Tweet to Hue",
18     namespace: "com.smartthings.dev",
19     author: "Andrew Mager & Kris Schaller",
20     description: "Update a Hue bulb's color based on a tweet.",
21     category: "Fun & Social",
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 which functions will fire when you hit certain endpoints. */
36
37 mappings {
38   path("/hue") {
39     action: [
40         PUT: "postHue"
41     ]
42   }
43 }
44
45 def installed() {
46     log.debug "Installed with settings: ${settings}"
47
48     initialize()
49 }
50
51 def updated() {
52     log.debug "Updated with settings: ${settings}"
53
54     unsubscribe()
55     initialize()
56 }
57
58 def initialize() {
59
60 }
61
62 /**
63 * Body: { color=[yourcolor] } to change color
64 * Example:
65 *     {
66 *          "value" : " #smartthings is so color=blue"
67 *     }
68 */
69
70 def postHue() {
71     def tweetText = request.JSON.text
72     log.info "POST: $tweetText"
73     
74     try {
75         def tweetColor = (tweetText =~ /color=(\w+)/)[0][1].toLowerCase()
76         log.debug (tweetText =~ /color=(\w+)/)
77         setHueColor(tweetColor)     
78     }
79     catch (any) {
80         log.trace "POST: Check Body (e.g: @RT: #smartthings color=red)"
81      }    
82 }
83
84 private setHueColor(color) {
85
86     def hueColor = 0
87     def saturation = 100
88
89     switch(color) {
90         case "white":
91             hueColor = 52
92             saturation = 19
93             break;
94         case "blue":
95             hueColor = 70
96             break;
97         case "green":
98             hueColor = 39
99             break;
100         case "yellow":
101             hueColor = 25
102             break;
103         case "orange":
104             hueColor = 10
105             break;
106         case "purple":
107             hueColor = 75
108             break;
109         case "pink":
110             hueColor = 83
111             break;
112         case "red":
113             hueColor = 100
114             break;
115     }
116
117     state.previous = [:]
118
119     hues.each {
120         state.previous[it.id] = [
121             "switch": it.currentValue("switch"),
122             "level" : it.currentValue("level"),
123             "hue": it.currentValue("hue"),
124             "saturation": it.currentValue("saturation")
125         ]
126     }
127
128     log.debug "current values = $state.previous"
129
130     def newValue = [hue: hueColor, saturation: saturation, level: 100]
131     log.debug "new value = $newValue"
132
133     hues*.setColor(newValue)
134 }