Last active
March 8, 2016 03:24
-
-
Save jimmyjames/e1e3483be7c3ddea9c06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Collections Fun | |
* | |
* Copyright 2016 james anderson | |
* | |
* 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. | |
* | |
*/ | |
definition( | |
name: "Collections Fun", | |
namespace: "jimmyjames", | |
author: "james anderson", | |
description: "showing some of the fun way to use collections in a SmartApp, using the settings (map) object.", | |
category: "", | |
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", | |
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png", | |
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png") | |
preferences { | |
section("Title") { | |
input "switches", "capability.switch", multiple: true, title: "select some switches" | |
input "motions", "capability.motionSensor", multiple: true, title: "select some motion sensors" | |
input "colors", "capability.colorControl", multiple: true, title: "select some color bulbs" | |
} | |
} | |
def installed() { | |
log.debug "Installed with settings: ${settings}" | |
initialize() | |
} | |
def updated() { | |
log.debug "Updated with settings: ${settings}" | |
unsubscribe() | |
initialize() | |
} | |
def initialize() { | |
// settings is a map of all inputs. | |
// Because "multiple: true", each entry's value is a list. | |
// For this SmartApp using three inputs with "multiple: true", it would look | |
// something like this: | |
// [switches: [SwitchObject1, SwitchObject2], motions: [MotionObject1], colors: [ColorObject1, ColorObject2]] | |
// get a list of all installed devices using collect | |
def devices = settings.collect {k, device -> device} | |
log.debug "devices: $devices" | |
// But that has nested lists, because of "input: multiple" (each input | |
// element is a list). Ick. Use flatten to collapse all lists | |
// (repeating the above just to show how this can be a nice one-liner): | |
def flattenedDevices = settings.collect {k, device -> device}.flatten() | |
log.debug "devices list, flattened: $flattenedDevices" | |
log.debug "there are ${flattenedDevices.size()} devices configured for this SmartApp" | |
// get all the IDs of the devices: | |
def deviceIds = settings.collect {k, device -> device.id}.flatten() | |
log.debug "there are ${deviceIds.size()} total Device IDs" | |
log.debug "deviceIds: $deviceIds" | |
// what if the user configured the same device in the "switches" and "colorControl" inputs? | |
// use unique() to filter out duplicates | |
def uniqueIds = settings.collect {k, device -> device.id}.flatten().unique() | |
log.debug "there are ${uniqueIds.size()} unique Device IDs" | |
log.debug "all unique device IDs: $uniqueIds" | |
// Let's get crazy! | |
// Find the number of total configured devices that support the Switch capability | |
def switchDevices = settings.collect {k, device -> device}.flatten()*.hasCapability("Switch").findAll { | |
it == true | |
} | |
log.debug "number of configured devices (non-unique) that support the Switch capability: ${switchDevices.size()}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment