2 * Copyright 2015 SmartThings
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 * Author: Steve Vlaminck
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"
31 page(name: "linkerPage")
34 def linkerPage(params) {
36 log.debug "linkerPage params: ${params}"
38 dynamicPage(name: "linkerPage", title: "Link your SmartBlock to a physical device", install: true, uninstall: false) {
42 name: "linkedSmartBlock",
43 type: "capability.switch",
44 // type: "device.SmartBlock",
45 title: "Linked SmartBlock",
50 name: "switchUpdatesBlock",
52 title: "Update this SmartBlock when the switch below changes state",
60 type: "capability.switch",
61 title: "Linked Switch",
66 name: "blockUpdatesSwitch",
68 title: "Update this switch when the SmartBlock above changes state",
76 title: "Label this Link",
80 title: "Only link these devices when in one of these modes",
81 description: "All modes"
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"
89 section("When \"Update this switch...\" is on") {
90 paragraph "If you place a lever on your Minecraft SmartBlock, it will control \"Linked Switch\""
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 ;)"
103 log.debug "Installed with settings: ${settings}"
109 log.debug "Updated with settings: ${settings}"
117 if (blockUpdatesSwitch)
119 subscribe(linkedSmartBlock, "level", updateSwitchLevel)
120 subscribe(linkedSmartBlock, "switch", updateSwitchState)
123 if (switchUpdatesBlock)
125 subscribe(linkedSwitch, "level", updateBlockLevel)
126 subscribe(linkedSwitch, "switch", updateBlockState)
131 def updateSwitchLevel(evt) {
132 int level = evt.value as int
133 log.debug "matching level: ${level}"
134 linkedSwitch.setLevel(level)
137 def updateBlockLevel(evt) {
138 int level = evt.value as int
139 log.debug "matching level: ${level}"
140 linkedSmartBlock.setLevel(level)
143 def updateSwitchState(evt) {
144 log.debug "setting linkedSwitch to ${evt.value}"
145 linkedSwitch."${evt.value}"()
148 def updateBlockState(evt) {
149 log.debug "setting linkedSmartBlock to ${evt.value}"
150 linkedSmartBlock."${evt.value}"()
154 return linkedSmartBlock.id
157 def getLinkerDescription() {
159 def left = linkedSmartBlock ? "${linkedSmartBlock.label ?: linkedSmartBlock.name}" : ""
160 def right = linkedSwitch ? "${linkedSwitch.label ?: linkedSwitch.name}" : ""
162 log.debug "left: ${left}, right: ${right}"
164 def leftLink = switchUpdatesBlock ? "<" : ""
165 def rightLink = blockUpdatesSwitch ? ">" : ""
167 log.debug "leftLink: ${leftLink}, rightLink: ${rightLink}"
169 log.debug "switchUpdatesBlock: ${switchUpdatesBlock}"
170 log.debug "blockUpdatesSwitch: ${blockUpdatesSwitch}"
172 if (leftLink == "" && rightLink == "")
177 "${left} ${leftLink}--${rightLink} ${right}"