Skip to content

Instantly share code, notes, and snippets.

@lttlrck
Last active August 29, 2015 14:04
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 lttlrck/523c08153d2de62f8167 to your computer and use it in GitHub Desktop.
Save lttlrck/523c08153d2de62f8167 to your computer and use it in GitHub Desktop.
SmartThings Garage Controller Device Type
/**
* Garage Controller
*
* Copyright 2014 Stuart Allen
*
* 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.
*
*/
metadata {
definition (name: "Garage Controller", namespace: "lttlrck", author: "Stuart Allen") {
capability "Switch"
capability "Refresh"
capability "Polling"
attribute "leftDoor", "string"
attribute "rightDoor", "string"
command "closeLeftDoor"
command "closeRightDoor"
command "openLeftDoor"
command "openRightDoor"
}
simulator {
// TODO: define status and reply messages here
}
tiles {
standardTile("leftDoor", "device.leftDoor", width: 2, height: 2) {
state("open", label:'Open', icon:"st.doors.garage.garage-open", action: "closeLeftDoor", backgroundColor:"#b8b821", nextState: "closing")
state("closing", label:'Closing', icon:"st.doors.garage.garage-closing", /*action: "pushLeft",*/ backgroundColor:"#b8b821")
state("closed", label:'Closed', icon:"st.doors.garage.garage-closed", action: "openLeftDoor", backgroundColor:"#79b821", nextState: "opening")
state("opening", label:'Opening', icon:"st.doors.garage.garage-opening", /*action: "pushLeft",*/ backgroundColor:"#b8b821")
}
standardTile("rightDoor", "device.rightDoor", width: 2, height: 2) {
state("open", label:'Open', icon:"st.doors.garage.garage-open", action: "closeRightDoor", backgroundColor:"#b8b821", nextState: "closing")
state("closing", label:'Closing', icon:"st.doors.garage.garage-closing", /*action: "pushLeft",*/ backgroundColor:"#b8b821")
state("closed", label:'Closed', icon:"st.doors.garage.garage-closed", action: "openRightDoor", backgroundColor:"#79b821", nextState: "opening")
state("opening", label:'Opening', icon:"st.doors.garage.garage-opening", /*action: "pushLeft",*/ backgroundColor:"#b8b821")
}
main(["leftDoor","rightDoor"])
details(["leftDoor","rightDoor"])
}
}
def updateStatus()
{
log.debug "updateStatus"
}
mappings {
path("/GarageController/1/status") {
action: [
GET: "updateStatus"
]
}
}
private def parseEventMessage(String description) {
def event = [:]
def parts = description.split(',')
parts.each { part ->
part = part.trim()
if (part.startsWith('headers')) {
part -= "headers:"
def valueString = part.trim()
if (valueString) {
event.headers = valueString
}
}
else if (part.startsWith('body')) {
part -= "body:"
def valueString = part.trim()
if (valueString) {
event.body = valueString
}
}
}
event
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
def parsedEvent= parseEventMessage( description)
def headerString = new String(parsedEvent.headers.decodeBase64())
def bodyString = new String(parsedEvent.body.decodeBase64())
def json = new groovy.json.JsonSlurper().parseText( bodyString)
log.trace json
if( json.msg)
{
if( json.msg.startsWith("state"))
{
log.trace "Setting state"
sendEvent (name: json.name, value: json.state)
}
else if( json.msg.startsWith("status"))
{
log.trace "Setting state from status"
sendEvent (name: "leftDoor", value: json.leftDoor)
sendEvent (name: "rightDoor", value: json.rightDoor)
}
}
}
private getCallBackAddress()
{
device.hub.getDataValue("localIP") + ":" + device.hub.getDataValue("localSrvPortTCP")
}
private Integer convertHexToInt(hex) {
Integer.parseInt(hex,16)
}
private String convertHexToIP(hex) {
[convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".")
}
private getHostAddress() {
def parts = device.deviceNetworkId.split(":")
def ip = convertHexToIP(parts[0])
def port = convertHexToInt(parts[1])
return ip + ":" + port
}
def poll() {
refresh()
}
def refresh() {
log.debug "Executing 'refresh'"
def ip= device.hub.getDataValue("localIP")
def port= device.hub.getDataValue("localSrvPortTCP")
log.debug ip
log.debug port
//def path= "/GarageController/1/subscribe?ip=${ip}&port=${port}"
def path= "/GarageController/1/status"
log.debug path
def result = new physicalgraph.device.HubAction(
method: "GET",
path: path,
headers: [HOST:getHostAddress()]
)
}
def closeLeftDoor() {
log.debug "Executing 'closeLeftDoor'"
def path= "/GarageController/1/leftDoor/close"
def result = new physicalgraph.device.HubAction(
method: "GET",
path: path,
headers: [HOST:getHostAddress()]
)
}
def openLeftDoor() {
log.debug "Executing 'openLeftDoor'"
def path= "/GarageController/1/leftDoor/open"
def result = new physicalgraph.device.HubAction(
method: "GET",
path: path,
headers: [HOST:getHostAddress()]
)
}
def closeRightDoor() {
log.debug "Executing 'closeRightDoor'"
def path= "/GarageController/1/rightDoor/close"
def result = new physicalgraph.device.HubAction(
method: "GET",
path: path,
headers: [HOST:getHostAddress()]
)
}
def openRightDoor() {
log.debug "Executing 'openRightDoor'"
def path= "/GarageController/1/rightDoor/open"
def result = new physicalgraph.device.HubAction(
method: "GET",
path: path,
headers: [HOST:getHostAddress()]
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment