Update speaker-mood-music.groovy
[smartapps.git] / official / smartblock-linker.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  SmartBlock Linker
14  *
15  *  Author: Steve Vlaminck
16  *
17  *  Date: 2013-12-26
18  */
19
20 definition(
21     name: "SmartBlock Linker",
22     namespace: "vlaminck/Minecraft",
23     author: "SmartThings",
24     description: "A SmartApp that links SmartBlocks to switches",
25     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
26     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
27 )
28
29 preferences {
30
31         page(name: "linkerPage")
32 }
33
34 def linkerPage(params) {
35
36         log.debug "linkerPage params: ${params}"
37
38         dynamicPage(name: "linkerPage", title: "Link your SmartBlock to a physical device", install: true, uninstall: false) {
39
40                 section {
41                         input(
42                                 name: "linkedSmartBlock",
43                                 type: "capability.switch",
44 //                              type: "device.SmartBlock",
45                                 title: "Linked SmartBlock",
46                                 required: true,
47                                 multiple: false
48                         )
49                         input(
50                                 name: "switchUpdatesBlock",
51                                 type: "bool",
52                                 title: "Update this SmartBlock when the switch below changes state",
53                                 description: "",
54                                 defaultValue: "false"
55                         )
56                 }
57                 section {
58                         input(
59                                 name: "linkedSwitch",
60                                 type: "capability.switch",
61                                 title: "Linked Switch",
62                                 required: true,
63                                 multiple: false
64                         )
65                         input(
66                                 name: "blockUpdatesSwitch",
67                                 type: "bool",
68                                 title: "Update this switch when the SmartBlock above changes state",
69                                 description: "",
70                                 defaultValue: "true"
71                         )
72                 }
73
74                 section {
75                         label(
76                                 title: "Label this Link",
77                                 required: false
78                         )
79                         mode(
80                                 title: "Only link these devices when in one of these modes",
81                                 description: "All modes"
82                         )
83                 }
84
85                 section("When \"Update this SmartBlock...\" is on") {
86                         paragraph "If you place a Redstone Lamp next to your SmartBlock, it will turn on/off when \"Linked Switch\" turns on/off"
87                 }
88
89                 section("When \"Update this switch...\" is on") {
90                         paragraph "If you place a lever on your Minecraft SmartBlock, it will control \"Linked Switch\""
91                 }
92
93                 section("Why turning both on can be bad") {
94                         paragraph "Because there can be latency."
95                         paragraph "Flipping the lever will send a signal from Minecraft to SmartThings. SmartThings will then send the signal back when the light has turned on."
96                         paragraph "If you flip the lever again before that round trip is complete, you can get into an infinite loop of signals being sent back and forth."
97                         paragraph "You've been warned ;)"
98                 }
99         }
100 }
101
102 def installed() {
103         log.debug "Installed with settings: ${settings}"
104
105         initialize()
106 }
107
108 def updated() {
109         log.debug "Updated with settings: ${settings}"
110
111         unsubscribe()
112         initialize()
113 }
114
115 def initialize() {
116
117         if (blockUpdatesSwitch)
118         {
119                 subscribe(linkedSmartBlock, "level", updateSwitchLevel)
120                 subscribe(linkedSmartBlock, "switch", updateSwitchState)
121         }
122
123         if (switchUpdatesBlock)
124         {
125                 subscribe(linkedSwitch, "level", updateBlockLevel)
126                 subscribe(linkedSwitch, "switch", updateBlockState)
127         }
128
129 }
130
131 def updateSwitchLevel(evt) {
132         int level = evt.value as int
133         log.debug "matching level: ${level}"
134         linkedSwitch.setLevel(level)
135 }
136
137 def updateBlockLevel(evt) {
138         int level = evt.value as int
139         log.debug "matching level: ${level}"
140         linkedSmartBlock.setLevel(level)
141 }
142
143 def updateSwitchState(evt) {
144         log.debug "setting linkedSwitch to ${evt.value}"
145         linkedSwitch."${evt.value}"()
146 }
147
148 def updateBlockState(evt) {
149         log.debug "setting linkedSmartBlock to ${evt.value}"
150         linkedSmartBlock."${evt.value}"()
151 }
152
153 def getBlockId() {
154         return linkedSmartBlock.id
155 }
156
157 def getLinkerDescription() {
158
159         def left = linkedSmartBlock ? "${linkedSmartBlock.label ?: linkedSmartBlock.name}" : ""
160         def right = linkedSwitch ? "${linkedSwitch.label ?: linkedSwitch.name}" : ""
161
162         log.debug "left: ${left}, right: ${right}"
163
164         def leftLink = switchUpdatesBlock ? "<" : ""
165         def rightLink = blockUpdatesSwitch ? ">" : ""
166
167         log.debug "leftLink: ${leftLink}, rightLink: ${rightLink}"
168
169         log.debug "switchUpdatesBlock: ${switchUpdatesBlock}"
170         log.debug "blockUpdatesSwitch: ${blockUpdatesSwitch}"
171
172         if (leftLink == "" && rightLink == "")
173         {
174                 return null
175         }
176
177         "${left} ${leftLink}--${rightLink} ${right}"
178 }