From d59a2706021ef652da18bd71f42ef20bb635efdd Mon Sep 17 00:00:00 2001 From: Nick Betcher Date: Tue, 27 Apr 2021 02:05:26 -0700 Subject: [PATCH 1/2] Support scenes as a virtual device in order to allow calling it from remote services like Google Home/Assistant and more. Simply tap, "Create Child Devices" again (even if they already exist) and scenes will be automagically added! --- switchbotScene | 39 +++++++++++++++++++++++++++++++++++++++ switchbotSystem | 14 +++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 switchbotScene diff --git a/switchbotScene b/switchbotScene new file mode 100644 index 0000000..93778a0 --- /dev/null +++ b/switchbotScene @@ -0,0 +1,39 @@ +/* + +Copyright 2021 - Nick Betcher (@nbetcher) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +------------------------------------------- + +Change history: +0.1.0 - @nbetcher - Initial release. + +*/ + +metadata +{ + definition(name: "SwitchBot Scene", namespace: "tomw", author: "tomw", importUrl: "") + { + capability "Momentary" + } +} + +def push() { + def id = getParent()?.getId(device.getDeviceNetworkId()) + if(id) + { + def respData = getParent()?.executeScene(id) + runIn(2, refresh) + } +} \ No newline at end of file diff --git a/switchbotSystem b/switchbotSystem index 2c5b931..583c882 100644 --- a/switchbotSystem +++ b/switchbotSystem @@ -17,6 +17,7 @@ limitations under the License. ------------------------------------------- Change history: +0.9.7 - @nbetcher - Added Scene device support as a virtual driver. 0.9.6 - @tomw - Added Humidifier support. 0.9.3 - @tomw - Modified child device creation to reduce confusion around grouped Curtain devices. 0.9.0 - @tomw - Initial release. @@ -123,12 +124,23 @@ def checkCommStatus() def createChildDevices() { def devices = getDevices() + def scenes = getScenes() + // TODO: Handle Scenes still if Devices is null. if(!devices) { return } + if (scenes) { + for (it in scenes) { + logDebug("Scene = ${it}") + if((it.sceneName && it.sceneId) && !findChildDevice(it.sceneId, 'Scene')) { + createChildDevice(it.sceneName, it.sceneId, 'Scene') + } + } + } + for(it in devices?.deviceList) { logDebug("device = ${it}") @@ -205,7 +217,7 @@ def createChildDevice(name, id, deviceType) def child try { - if(!["Bot", "Curtain", "Meter", "IR Device", "Humidifier"].contains(deviceType.toString())) + if(!["Bot", "Curtain", "Meter", "IR Device", "Humidifier", "Scene"].contains(deviceType.toString())) { logDebug("createChildDevice: deviceType not supported") throw new Exception("deviceType not supported") From c775f01bd0bc3954158cb67353d7685512716977 Mon Sep 17 00:00:00 2001 From: Nick Betcher Date: Tue, 27 Apr 2021 02:11:59 -0700 Subject: [PATCH 2/2] Reformat some if() lines to follow tomw's orinal bracket formatting. Also, handle if scenes are found, but no devices. --- switchbotScene | 2 +- switchbotSystem | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/switchbotScene b/switchbotScene index 93778a0..c7aa45c 100644 --- a/switchbotScene +++ b/switchbotScene @@ -36,4 +36,4 @@ def push() { def respData = getParent()?.executeScene(id) runIn(2, refresh) } -} \ No newline at end of file +} diff --git a/switchbotSystem b/switchbotSystem index 583c882..5c69732 100644 --- a/switchbotSystem +++ b/switchbotSystem @@ -126,21 +126,23 @@ def createChildDevices() def devices = getDevices() def scenes = getScenes() - // TODO: Handle Scenes still if Devices is null. - if(!devices) + if (scenes) { - return - } - - if (scenes) { - for (it in scenes) { + for (it in scenes) + { logDebug("Scene = ${it}") - if((it.sceneName && it.sceneId) && !findChildDevice(it.sceneId, 'Scene')) { + if((it.sceneName && it.sceneId) && !findChildDevice(it.sceneId, 'Scene')) + { createChildDevice(it.sceneName, it.sceneId, 'Scene') } } } + if(!devices) + { + return + } + for(it in devices?.deviceList) { logDebug("device = ${it}")