Here is the full original code. I've been able to get it to load, but now I'm stuck trying to get it to actually register any button presses. Device log shows them. App log doesn't show anything, app doesn't do anything.
definition(
name: "MagicCube",
namespace: "magicCube1",
author: "roguetech",
description: "Control MagicCube",
category: "Convenience",
iconUrl: "https://raw.githubusercontent.com/ClassicGOD/SmartThingsPublic/master/devicetypes/classicgod/xiaomi-magic-cube-controller.src/images/mi_face_s.png",
iconX2Url: "https://raw.githubusercontent.com/ClassicGOD/SmartThingsPublic/master/devicetypes/classicgod/xiaomi-magic-cube-controller.src/images/mi_face.png"
)
preferences {
page(name: "selectButton")
for (def i=1; i<=8; i++) {
page(name: "configureButton$i")
}
}
def selectButton() {
dynamicPage(name: "selectButton", title: "Select the controller.", nextPage: "configureButton1", uninstall: configured()) {
section {
input "buttonDevice", "capability.pushableButton", title: "Button", multiple: false, required: true
}
}
}
def createPage(pageNum, title) {
if ((state.numButton == pageNum) || (pageNum == 8))
state.installCondition = true
dynamicPage(name: "configureButton$pageNum", title: title,
nextPage: "configureButton${pageNum+1}", install: state.installCondition, uninstall: configured(), getButtonSections(pageNum))
}
def configureButton1() {
state.numButton = buttonDevice.currentState("numberOfButtons")?.longValue ?: 4
log.debug "state variable numButton: ${state.numButton}"
state.installCondition = false
def title="Set the first light for 90° flip and rotate."
createPage(1,title)
}
def configureButton2() {
def title="Set the second light for 90° flip and rotate."
createPage(2,title)
}
def configureButton3() {
def title="Set the third light for 90° flip and rotate."
createPage(3,title)
}
def configureButton4() {
def title="Set the light for 180° flip."
createPage(4,title)
}
def configureButton5() {
def title="Unused."
createPage(5,title)
}
def configureButton6() {
def title="Unused."
createPage(6,title)
}
def configureButton7() {
def title="Unused."
createPage(7,title)
}
def configureButton8() {
def title="Unused."
createPage(8,title)
}
def getButtonSections(buttonNumber) {
return {
section("Lights") {
input "lights_${buttonNumber}_pushed", "capability.switch", title: "Pushed", multiple: true, required: false
}
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
subscribe(buttonDevice, "button", buttonEvent)
}
def configured() {
return buttonDevice || buttonConfigured(1) || buttonConfigured(2) || buttonConfigured(3) || buttonConfigured(4)
}
def buttonConfigured(idx) {
return settings["lights_$idx_pushed"]
}
def buttonEvent(evt){
def buttonNumber = evt.data
def value = evt.value
// log.debug "buttonEvent: $evt.name = $evt.value ($evt.data)"
log.debug "button: $buttonNumber, value: $value"
// def recentEvents = buttonDevice.eventsSince(new Date(now() - 3000)).findAll{it.value == evt.value && it.data == evt.data}
// log.debug "Found ${recentEvents.size()?:0} events in past 3 seconds"
// Shake; turn on in sequence
if(buttonNumber.startsWith("{\"buttonNumber\":1,")){
if (lights_2_pushed != null){
if (lights_1_pushed.currentValue('switch').contains('off') && lights_2_pushed.currentValue('switch').contains('off')) {
lights_1_pushed.on()
log.info "Turned on $lights_1_pushed."
}else if(lights_1_pushed.currentValue('switch').contains('on') && lights_2_pushed.currentValue('switch').contains('off')) {
lights_2_pushed.on()
log.info "Turned on $lights_2_pushed."
}else if(lights_1_pushed.currentValue('switch').contains('on') && lights_2_pushed.currentValue('switch').contains('on')) {
lights_1_pushed.off()
log.info "Turned off $lights_1_pushed."
}else if(lights_1_pushed.currentValue('switch').contains('off') && lights_2_pushed.currentValue('switch').contains('on')) {
lights_2_pushed.off()
log.info "Turned off $lights_2_pushed."
}
}
// 90 flip; turn off
} else if(buttonNumber.startsWith("{\"buttonNumber\":2,")){ //90 flip
if (lights_2_pushed == null){
if (lights_1_pushed.currentValue('switch').contains('on')){
lights_1_pushed.off()
log.info "Turned off $lights_1_pushed."
}else{
lights_1_pushed.on()
log.info "Turned on $lights_1_pushed."
}
} else {
if (lights_1_pushed.currentValue('switch').contains('on') && lights_2_pushed.currentValue('switch').contains('off')) {
lights_1_pushed.off()
lights_3_pushed.off()
log.info "Turned off $lights_1_pushed and $lights_3_pushed."
}else if(lights_2_pushed.currentValue('switch').contains('on') && lights_2_pushed.currentValue('switch').contains('on')) {
lights_2_pushed.off()
log.info "Turned off $lights_2_pushed."
}
}
// 180 flip; turn off each other's
} else if(buttonNumber.startsWith("{\"buttonNumber\":3,")){
if (lights_4_pushed.currentValue('switch').contains('on')){
lights_4_pushed.off()
log.info "Turned off $lights_4_pushed."
} else if (lights_4_pushed.currentValue('switch').contains('off')){
lights_4_pushed.on()
log.info "Turned on $lights_4_pushed."
}
// Rotate right; increase level
} else if(buttonNumber.startsWith("{\"buttonNumber\":6,")){
def $lvl
if (lights_1_pushed.currentValue('switch').contains('on')){
$lvl = lights_1_pushed.currentValue("level")
} else {
if (lights_2_pushed != null){
if(lights_2_pushed.currentValue('switch').contains('on')){
$lvl = lights_2_pushed.currentValue("level")
} else if(lights_3_pushed.currentValue('switch').contains('on')){
$lvl = lights_3_pushed.currentValue("level")
}
}
}
$lvl[0] = Math.min(Math.round($lvl[0]*1.33+1),100)
if(lights_1_pushed.currentValue('switch').contains('on')) {
lights_1_pushed.setLevel($lvl[0])
log.info "Set $lights_1_pushed to " + $lvl[0] + "%."
}
if (lights_2_pushed != null){
if(lights_2_pushed.currentValue('switch').contains('on')) {
lights_2_pushed.setLevel($lvl[0])
log.info "Set $lights_2_pushed to " + $lvl[0] + "%."
}
if(lights_3_pushed.currentValue('switch').contains('on')) {
lights_3_pushed.setLevel($lvl[0])
log.info "Set $lights_3_pushed to " + $lvl[0] + "%."
}
}
// Rotate left; decrease level
} else if(buttonNumber.startsWith("{\"buttonNumber\":7,")){
def $lvl
if (lights_1_pushed.currentValue('switch').contains('on')){
$lvl = lights_1_pushed.currentValue("level")
} else {
if (lights_2_pushed != null){
if(lights_1_pushed.currentValue('switch').contains('on')){
$lvl = lights_2_pushed.currentValue("level")
} else if(lights_3_pushed.currentValue('switch').contains('on')){
$lvl = lights_3_pushed.currentValue("level")
}
}
}
$lvl[0] = Math.max(Math.round($lvl[0]*0.66),1)
if(lights_1_pushed.currentValue('switch').contains('on')) {
lights_1_pushed.setLevel($lvl[0])
log.info "Set $lights_1_pushed to " + $lvl[0] + "%."
}
if (lights_2_pushed != null){
if(lights_2_pushed.currentValue('switch').contains('on')) {
lights_2_pushed.setLevel($lvl[0])
log.info "Set $lights_2_pushed to " + $lvl[0] + "%."
}
if(lights_3_pushed.currentValue('switch').contains('on')) {
lights_3_pushed.setLevel($lvl[0])
log.info "Set $lights_3_pushed to " + $lvl[0] + "%."
}
}
}
}
Here is the code that loads, but doesn't actually do anything:
definition(
name: "MagicCube",
namespace: "magicCube1",
author: "roguetech",
description: "Control MagicCube",
category: "Convenience",
iconUrl: "https://raw.githubusercontent.com/ClassicGOD/SmartThingsPublic/master/devicetypes/classicgod/xiaomi-magic-cube-controller.src/images/mi_face_s.png",
iconX2Url: "https://raw.githubusercontent.com/ClassicGOD/SmartThingsPublic/master/devicetypes/classicgod/xiaomi-magic-cube-controller.src/images/mi_face.png"
)
preferences {
page(name: "selectButton")
page(name: "configureButton1")
page(name: "configureButton2")
page(name: "configureButton3")
page(name: "configureButton4")
}
def selectButton() {
dynamicPage(name: "selectButton", title: "Select the Cube.", nextPage: "configureButton1", uninstall: true) {
section {
input "buttonDevice", "capability.pushableButton", title: "Button", multiple: false, required: true
}
}
}
def configureButton1() {
dynamicPage(name: "configureButton1", title: "Set the first light for 90° flip and/or rotate.", nextPage: "configureButton2", install: false){
section("Lights") {
input "lights_1_pushed", "capability.switch", title: "Pushed", multiple: true, required: false
}
}
}
def configureButton2() {
dynamicPage(name: "configureButton2", title: "Set the second light for 90° flip and/or rotate.", nextPage: "configureButton3", install: false){
section("Lights") {
input "lights_2_pushed", "capability.switch", title: "Pushed", multiple: true, required: false
}
}
}
def configureButton3() {
dynamicPage(name: "configureButton3", title: "Set the third light for 90° flip and/or rotate.", nextPage: "configureButton4", install: false){
section("Lights") {
input "lights_3_pushed", "capability.switch", title: "Pushed", multiple: true, required: false
}
}
}
def configureButton4() {
dynamicPage(name: "configureButton3", title: "Set the light for 180° flip.", install: true){
section("Lights") {
input "lights_4_pushed", "capability.switch", title: "Pushed", multiple: true, required: false
}
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
log.debug "INITIALIZED"
subscribe(buttonDevice, "pushed", buttonEvent)
}
def configured() {
return buttonDevice || buttonConfigured(1) || buttonConfigured(2) || buttonConfigured(3) || buttonConfigured(4)
}
def buttonConfigured(idx) {
return settings["lights_$idx_pushed"]
}
def buttonEvent(evt){
def buttonNumber = evt.value
// def value = evt.value
// log.debug "buttonEvent: $evt.name = $evt.value ($evt.data)"
log.debug "button: $buttonNumber, value: $value"
def recentEvents = buttonDevice.eventsSince(new Date(now() - 3000)).findAll{it.value == evt.value && it.data == evt.data}
log.debug "Found ${recentEvents.size()?:0} events in past 3 seconds"
// Shake; turn on in sequence [fixed #]
if(buttonNumber.startsWith("{\"buttonNumber\":1,")){
if (lights_2_pushed != null){
if (lights_1_pushed.currentValue('switch').contains('off') && lights_2_pushed.currentValue('switch').contains('off')) {
lights_1_pushed.on()
log.info "Turned on $lights_1_pushed."
}else if(lights_1_pushed.currentValue('switch').contains('on') && lights_2_pushed.currentValue('switch').contains('off')) {
lights_2_pushed.on()
log.info "Turned on $lights_2_pushed."
}else if(lights_1_pushed.currentValue('switch').contains('on') && lights_2_pushed.currentValue('switch').contains('on')) {
lights_1_pushed.off()
log.info "Turned off $lights_1_pushed."
}else if(lights_1_pushed.currentValue('switch').contains('off') && lights_2_pushed.currentValue('switch').contains('on')) {
lights_2_pushed.off()
log.info "Turned off $lights_2_pushed."
}
}
// 90 flip; turn off
} else if(buttonNumber.startsWith("{\"buttonNumber\":2,")){ //90 flip
if (lights_2_pushed == null){
if (lights_1_pushed.currentValue('switch').contains('on')){
lights_1_pushed.off()
log.info "Turned off $lights_1_pushed."
}else{
lights_1_pushed.on()
log.info "Turned on $lights_1_pushed."
}
} else {
if (lights_1_pushed.currentValue('switch').contains('on') && lights_2_pushed.currentValue('switch').contains('off')) {
lights_1_pushed.off()
lights_3_pushed.off()
log.info "Turned off $lights_1_pushed and $lights_3_pushed."
}else if(lights_2_pushed.currentValue('switch').contains('on') && lights_2_pushed.currentValue('switch').contains('on')) {
lights_2_pushed.off()
log.info "Turned off $lights_2_pushed."
}
}
// 180 flip; turn off each other's
} else if(buttonNumber.startsWith("{\"buttonNumber\":3,")){
if (lights_4_pushed.currentValue('switch').contains('on')){
lights_4_pushed.off()
log.info "Turned off $lights_4_pushed."
} else if (lights_4_pushed.currentValue('switch').contains('off')){
lights_4_pushed.on()
log.info "Turned on $lights_4_pushed."
}
// Rotate right; increase level
} else if(buttonNumber.startsWith("{\"buttonNumber\":6,")){
def $lvl
if (lights_1_pushed.currentValue('switch').contains('on')){
$lvl = lights_1_pushed.currentValue("level")
} else {
if (lights_2_pushed != null){
if(lights_2_pushed.currentValue('switch').contains('on')){
$lvl = lights_2_pushed.currentValue("level")
} else if(lights_3_pushed.currentValue('switch').contains('on')){
$lvl = lights_3_pushed.currentValue("level")
}
}
}
$lvl[0] = Math.min(Math.round($lvl[0]*1.33+1),100)
if(lights_1_pushed.currentValue('switch').contains('on')) {
lights_1_pushed.setLevel($lvl[0])
log.info "Set $lights_1_pushed to " + $lvl[0] + "%."
}
if (lights_2_pushed != null){
if(lights_2_pushed.currentValue('switch').contains('on')) {
lights_2_pushed.setLevel($lvl[0])
log.info "Set $lights_2_pushed to " + $lvl[0] + "%."
}
if(lights_3_pushed.currentValue('switch').contains('on')) {
lights_3_pushed.setLevel($lvl[0])
log.info "Set $lights_3_pushed to " + $lvl[0] + "%."
}
}
// Rotate left; decrease level
} else if(buttonNumber.startsWith("{\"buttonNumber\":7,")){
def $lvl
if (lights_1_pushed.currentValue('switch').contains('on')){
$lvl = lights_1_pushed.currentValue("level")
} else {
if (lights_2_pushed != null){
if(lights_1_pushed.currentValue('switch').contains('on')){
$lvl = lights_2_pushed.currentValue("level")
} else if(lights_3_pushed.currentValue('switch').contains('on')){
$lvl = lights_3_pushed.currentValue("level")
}
}
}
$lvl[0] = Math.max(Math.round($lvl[0]*0.66),1)
if(lights_1_pushed.currentValue('switch').contains('on')) {
lights_1_pushed.setLevel($lvl[0])
log.info "Set $lights_1_pushed to " + $lvl[0] + "%."
}
if (lights_2_pushed != null){
if(lights_2_pushed.currentValue('switch').contains('on')) {
lights_2_pushed.setLevel($lvl[0])
log.info "Set $lights_2_pushed to " + $lvl[0] + "%."
}
if(lights_3_pushed.currentValue('switch').contains('on')) {
lights_3_pushed.setLevel($lvl[0])
log.info "Set $lights_3_pushed to " + $lvl[0] + "%."
}
}
}
}