Sorry for the delay between posts, but I wanted to follow up incase someone is trying to do the same thing. Here is the device I ended up creating:
metadata {
definition(name: "Door Sensors", namespace: "Home Sensors", author: "Jason") {
capability "Switch"
capability "Sensor"
capability "Refresh"
attribute "panelStatus", "string"
command "stopSchedule"
command "runSchedule"
}
}
preferences {
section("Settings") {
input "endpointUrl", "text", title: "Endpoint URL", required: true
input "pullingSeconds", "number", title: "Number of seconds", required: true
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
}
}
def initialize() {
log.debug 'Initializing'
stopSchedule()
}
def stopSchedule(){
unschedule()
}
def runSchedule(){
log.info "Scheduling Refresh evey ${pullingSeconds} seconds"
schedule("0/${pullingSeconds} * * * * ? *", refresh)
}
def logsOff() {
log.warn "debug logging disabled..."
device.updateSetting("logEnable", [value: "false", type: "bool"])
}
def updated() {
log.info "updated..."
log.warn "debug logging is: ${logEnable == true}"
if (logEnable) runIn(1800, logsOff)
}
def getParams(){
def params = [uri: "${endpointUrl}",requestContentType: "application/json",timeout: 20]
}
def refresh() {
if(state.Refreshing) {
log.warn "Already Refreshing"
return
}
state.Refreshing = true
if (logEnable) log.info "Refreshing..."
asynchttpGet('getPanelStatusCallback', getParams());
}
def getPanelStatusCallback(resp, data) {
try {
def json = new groovy.json.JsonSlurper().parseText(resp.data)
sendEvent(name: 'panelStatus', value: json.panelDescription, data: json)
handleSensorValues(json.sensors)
sendEvent(name: 'ERROR', value: null)
}
catch(Exception e) {
log.warn "error occured: ${e}"
sendEvent(name: 'ERROR', value: "error occured calling security panel api", data: e)
}
if (logEnable) log.info "Refresh complete"
state.Refreshing = false
}
def handleSensorValues(sensors) {
if(!state.Sensors) { state.Sensors = [:]}
SensorCodes = [O: "Open", C: "Closed", OW: "Open", CW: "Closed"]
sensors.each { sensor ->
prevCode = state.Sensors[sensor.name]
newCode = SensorCodes.get(sensor.code,sensor.code)
if(prevCode && prevCode.code != sensor.code) {
oldCode = SensorCodes.get(prevCode.code,prevCode.code)
log.info "${sensor.name} is now ${newCode}. It was: ${oldCode}. ${sensor.text.trim()}"
//sendEvent(name: "sensor.${sensor.name}", value: newCode, data: sensor)
setChildDevice(sensor.name, newCode)
}
state.Sensors << [(sensor.name): sensor]
}
}
def setChildDevice(name, contactCode){
def childDevice = getChildDevices()?.find { it.deviceNetworkId.endsWith(name)}
if (!childDevice) {
try {
addChildDevice("hubitat", "Virtual Contact Sensor", "${device.deviceNetworkId}-Sensor${name}", [name: "${device.displayName} ${name} Sensor", isComponent: true])
childDevice = getChildDevices()?.find { it.deviceNetworkId.endsWith(name)}
} catch (e) {
log.error "Failed to add child device: ${name}"
}
}
if (childDevice) {
try {
childDevice.sendEvent(name: "contact", value: contactCode)
//log.debug "Senser ${name} is ${contactCode}"
} catch (e) {
log.error "Couldn't find child device, probably doesn't exist...? Error: ${e}"
}
}
}
private removeChildDevices() {
if (logEnable) log.debug "Removing Child Devices"
try {
getChildDevices()?.each {
try {
deleteChildDevice(it.deviceNetworkId)
} catch (e) {
log.debug "Error deleting ${it.deviceNetworkId}, probably locked into a SmartApp: ${e}"
}
}
} catch (err) {
log.debug "Either no children exist or error finding child devices for some reason: ${err}"
}
}
If you see anything that could be better let me know. Thanks for the help and advise!