Skip to content

Instantly share code, notes, and snippets.

@nayelyzarazua-bluetrail
Created December 30, 2020 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayelyzarazua-bluetrail/1dfb23156e3c30be5cf07dfbe4bacd30 to your computer and use it in GitHub Desktop.
Save nayelyzarazua-bluetrail/1dfb23156e3c30be5cf07dfbe4bacd30 to your computer and use it in GitHub Desktop.
SmartApp_roomsList
/**
* roomsList
*
* Copyright 2020 Smart Things
*
* 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: "roomsList",
namespace: "nayelyz",
author: "Smart Things",
description: "SmartApp to allow user select the rooms from a list",
category: "My Apps",
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 {
page(name: "configRooms")
}
def configRooms() {
dynamicPage(name: "configRooms", title: "config rooms", install: true, uninstall: true) {
section {
input(name: "room1", type: "enum", title: "room 1", options: allRooms(), submitOnChange: true)
}
if (room1) {
section {
input(name: "room2", type: "enum", title: "room 2", options: roomsLeft(), submitOnChange: true)
}
}
}
}
def allRooms(){
def allRooms= ["room 1","room 2","room 3","room 4"]
log.debug "options ${allRooms}"
return allRooms
}
def roomsLeft(){
log.debug "options already selected ${room1}"
def otherRooms = ["room 1","room 2","room 3","room 4"]
otherRooms.removeElement(room1)
log.debug "options left ${otherRooms}"
return otherRooms
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
sendPush("installed");
}
def updated() {
initialize()
}
def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
}
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const SmartApp = require('@smartthings/smartapp');
require('dotenv').config();
const server = module.exports = express();
server.use(bodyParser.json());
const app = new SmartApp()
/* Handles lifecycle events from SmartThings */
server.post('/', async (req, res) => {
app.handleHttpCallback(req, res);
});
/* Defines the SmartApp .enableEventLogging() */
app.enableEventLogging()
.appId("select-rooms")
.permissions([
"r:locations:*",
"r:devices:*",
"x:devices:*"
])
.page('mainPage', (context, page, configData) => {
page.section('rooms', section => {
section.enumSetting('room1').options(allRooms())
.name("Room 1").description("Select Room 1")
.required(true).multiple(true).submitOnChange(true);
if(context.config.room1){
section.enumSetting('room2').options(leftRooms(context.config.room1))
.name("Room 2").description("Select Room 2")
.required(true).multiple(true);
}
});
})
.updated(async (context, updateData) => {
/*You can get the configurated values as follows:
context.config.room1
*/
//Get the Rooms list to match the Room name selected with its ID
context.api.rooms.list(context.locationId).then(rooms => {
/* You'll get an Array with this structure
{
roomId: 'xxxx-xxxx-xxxx',
locationId: 'xxxx-xxxx-xxxx',
name: 'Room1',
backgroundImage: null,
created: 1602192174148,
lastModified: 1602192174148
}
*/
/*Get the devices that belong to a room, you must use the RoomId.
You'll get an Array with the same structure as the [Devices](https://smartthings.developer.samsung.com/docs/api-ref/st-api.html#operation/getDevices) endpoint.
*/
return context.api.rooms.listDevices(roomId)
}).then(devices => {
console.log("devices in the room ",devices)
})
});
function allRooms(){
let allRooms=['room1','room2','room3','room4']
return allRooms
}
function leftRooms(selectedRoom){
let selected=selectedRoom[0].stringConfig.value;
let leftRooms=['room1','room2','room3','room4']
for( var i = 0; i < leftRooms.length; i++){ if ( leftRooms[i] === selected) { leftRooms.splice(i, 1); i--; }}
return leftRooms
}
/* Starts the server */
let port = process.env.PORT;
server.listen(port);
console.log(`Open: http://127.0.0.1:${port}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment