Code part 2/2:
private parseweatherResponse(resp,childDevice) {
logDebug( "Executing parseweatherResponse: "+resp.data)
logDebug( "Output status: "+resp.status)
def temperatureUnit = state.tempunit
logDebug( "Temperature Unit is ${temperatureUnit}")
if(resp.status == 200) {
logDebug( "Executing parseResponse.successTrue")
def outsidetemperature
if (temperatureUnit == "C") {
outsidetemperature = resp.data.outsideTemperature.celsius
}
else if(temperatureUnit == "F"){
outsidetemperature = resp.data.outsideTemperature.fahrenheit
}
logDebug( "Read outside temperature: " + outsidetemperature)
childDevice?.sendEvent(name: 'outsidetemperature', value: outsidetemperature, unit: temperatureUnit)
logDebug( "Send Outside Temperature Event Fired")
return result
}else if(resp.status == 201){
logDebug( "Something was created/updated")
}
}
def getidCommand(){
logDebug( "Executing 'sendCommand.getidCommand'" )
sendCommand("getid",null,[])
}
def getTempUnitCommand(){
logDebug( "Executing 'sendCommand.getidCommand'" )
sendCommand("gettempunit",null,[])
}
def getZonesCommand(){
logDebug( "Executing 'sendCommand.getzones'" )
sendCommand("getzones",null,[])
}
def getMobileDevicesCommand(){
logDebug( "Executing 'sendCommand.getMobileDevices'" )
sendCommand("getMobileDevices",null,[])
}
def weatherStatusCommand(childDevice){
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
logDebug( "Executing 'sendCommand.weatherStatusCommand'" )
def result = sendCommand("weatherStatus",childDevice,[deviceId])
}
def getCapabilitiesCommand(childDevice, deviceDNI){
logDebug("childDevice is: " + childDevice.inspect())
logDebug("deviceDNI is: " + deviceDNI.inspect())
def item = deviceDNI.tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
logDebug("Executing 'sendCommand.getcapabilities'")
sendCommand("getcapabilities",childDevice,[deviceId])
}
private removeChildDevices(delete) {
try {
delete.each {
deleteChildDevice(it.deviceNetworkId)
log.info "Successfully Removed Child Device: ${it.displayName} (${it.deviceNetworkId})"
}
}
catch (e) { log.error "There was an error (${e}) when trying to delete the child device" }
}
def parseCapabilityData(Map results){
logDebug("in parseCapabilityData")
def result
results.each { name, value ->
if (name == "value")
{
logDebug("Map Name Returned, ${name} and Value is ${value}")
result = value.toString()
logDebug("Result is ${result}")
//return result
}
}
return result
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Device Commands Below Here
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def autoCommand(childDevice){
logDebug("Executing 'sendCommand.autoCommand' on device ${childDevice.device.name}")
def terminationmode = settings.manualmode
def traperror
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
if (deviceType == "AIR_CONDITIONING")
{
def capabilitySupportsAuto = parseCapabilityData(childDevice.getCapabilitySupportsAuto())
def capabilitySupportsAutoSwing = parseCapabilityData(childDevice.getCapabilitySupportsAutoSwing())
def capabilitysupported = capabilitySupportsAuto
if (capabilitysupported == "true"){
logDebug("Executing 'sendCommand.autoCommand' on device ${childDevice.device.name}")
def jsonbody
if (capabilitySupportsAutoSwing == "true")
{
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"AUTO", power:"ON", swing:"OFF", type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else
{
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"AUTO", power:"ON", type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
sendCommand("temperature",dchildDevice,[deviceId,jsonbody])
statusCommand(device)
} else {
logDebug("Sorry Auto Capability not supported on device ${childDevice.device.name}")
}
}
if(deviceType == "HEATING")
{
def initialsetpointtemp
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug("Existing Setpoint is not set")
traperror = 0
}
if(traperror == 0){
initialsetpointtemp = settings.defHeatingTemp
} else {
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
def jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[celsius:initialsetpointtemp], type:"HEATING"], termination:[type:terminationmode]])
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
}
if (deviceType == "HOT_WATER")
{
logDebug("Executing 'sendCommand.autoCommand'")
def initialsetpointtemp
def jsonbody
def capabilitySupportsWaterTempControl = parseCapabilityData(childDevice.getCapabilitySupportsWaterTempControl())
if(capabilitySupportsWaterTempControl == "true"){
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug("Existing Setpoint is not set")
traperror = 0
}
if(traperror == 0){
initialsetpointtemp = settings.defHeatingTemp
} else {
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[celsius:initialsetpointtemp], type:"HOT_WATER"], termination:[type:terminationmode]])
} else {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", type:"HOT_WATER"], termination:[type:terminationmode]])
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
}
}
def dryCommand(childDevice){
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def capabilitySupportsDry = parseCapabilityData(childDevice.getCapabilitySupportsDry())
def capabilitySupportsDrySwing = parseCapabilityData(childDevice.getCapabilitySupportsDrySwing())
def capabilitysupported = capabilitySupportsDry
if (capabilitysupported == "true"){
def terminationmode = settings.manualmode
logDebug("Executing 'sendCommand.dryCommand' on device ${childDevice.device.name}")
def jsonbody
if (capabilitySupportsDrySwing == "true")
{
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"DRY", power:"ON", swing:"OFF", type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else
{
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"DRY", power:"ON", type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
} else {
logDebug("Sorry Dry Capability not supported on device ${childDevice.device.name}")
}
}
def fanAuto(childDevice){
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def capabilitySupportsFan = parseCapabilityData(childDevice.getCapabilitySupportsFan())
def capabilitySupportsFanSwing = parseCapabilityData(childDevice.getCapabilitySupportsFanSwing())
def capabilitysupported = capabilitySupportsFan
if (capabilitysupported == "true"){
def terminationmode = settings.manualmode
logDebug("Executing 'sendCommand.fanAutoCommand' on device ${childDevice.device.name}")
def jsonbody
if (capabilitySupportsFanSwing == "true")
{
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"FAN", power:"ON", swing:"OFF", type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else
{
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"FAN", power:"ON", type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
} else {
logDebug("Sorry Fan Capability not supported by your HVAC Device")
}
}
def endManualControl(childDevice){
logDebug("Executing 'sendCommand.endManualControl' on device ${childDevice.device.name}")
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
sendCommand("deleteEntry",childDevice,[deviceId])
statusCommand(childDevice)
}
def cmdFanSpeedAuto(childDevice){
def supportedfanspeed
def terminationmode = settings.manualmode
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def jsonbody
def capabilitySupportsCool = parseCapabilityData(childDevice.getCapabilitySupportsCool())
def capabilitysupported = capabilitySupportsCool
def capabilitySupportsCoolAutoFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsCoolAutoFanSpeed())
def fancapabilitysupported = capabilitySupportsCoolAutoFanSpeed
if (fancapabilitysupported == "true"){
supportedfanspeed = "AUTO"
} else {
supportedfanspeed = "HIGH"
}
def curSetTemp = (childDevice.device.currentValue("thermostatSetpoint"))
def curMode = ((childDevice.device.currentValue("thermostatMode")).toUpperCase())
if (curMode == "COOL" || curMode == "HEAT"){
if (capabilitySupportsCoolSwing == "true" || capabilitySupportsHeatSwing == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", swing:"OFF", temperature:[celsius:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", swing:"OFF", temperature:[fahrenheit:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", temperature:[celsius:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", temperature:[fahrenheit:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
logDebug("Executing 'sendCommand.fanSpeedAuto' to ${supportedfanspeed}")
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
}
}
def cmdFanSpeedHigh(childDevice){
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def jsonbody
def supportedfanspeed = "HIGH"
def terminationmode = settings.manualmode
def curSetTemp = (childDevice.device.currentValue("thermostatSetpoint"))
def curMode = ((childDevice.device.currentValue("thermostatMode")).toUpperCase())
if (curMode == "COOL" || curMode == "HEAT"){
if (capabilitySupportsCoolSwing == "true" || capabilitySupportsHeatSwing == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", swing:"OFF", temperature:[celsius:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", swing:"OFF", temperature:[fahrenheit:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", temperature:[celsius:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", temperature:[fahrenheit:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
logDebug("Executing 'sendCommand.fanSpeedAuto' to ${supportedfanspeed}")
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
}
}
def cmdFanSpeedMid(childDevice){
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def supportedfanspeed = "MIDDLE"
def terminationmode = settings.manualmode
def jsonbody
def curSetTemp = (childDevice.device.currentValue("thermostatSetpoint"))
def curMode = ((childDevice.device.currentValue("thermostatMode")).toUpperCase())
if (curMode == "COOL" || curMode == "HEAT"){
if (capabilitySupportsCoolSwing == "true" || capabilitySupportsHeatSwing == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", swing:"OFF", temperature:[celsius:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", swing:"OFF", temperature:[fahrenheit:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", temperature:[celsius:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", temperature:[fahrenheit:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
logDebug("Executing 'sendCommand.fanSpeedMid' to ${supportedfanspeed}")
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
}
}
def cmdFanSpeedLow(childDevice){
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def capabilitySupportsCoolSwing = parseCapabilityData(childDevice.getCapabilitySupportsCoolSwing())
def capabilitySupportsHeatSwing = parseCapabilityData(childDevice.getCapabilitySupportsHeatSwing())
def supportedfanspeed = "LOW"
def terminationmode = settings.manualmode
def jsonbody
def curSetTemp = (childDevice.device.currentValue("thermostatSetpoint"))
def curMode = ((childDevice.device.currentValue("thermostatMode")).toUpperCase())
if (curMode == "COOL" || curMode == "HEAT"){
if (capabilitySupportsCoolSwing == "true" || capabilitySupportsHeatSwing == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", swing:"OFF", temperature:[celsius:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", swing:"OFF", temperature:[fahrenheit:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", temperature:[celsius:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:curMode, power:"ON", temperature:[fahrenheit:curSetTemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
logDebug("Executing 'sendCommand.fanSpeedLow' to ${supportedfanspeed}")
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
}
}
def setCoolingTempCommand(childDevice,targetTemperature){
def terminationmode = settings.manualmode
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def supportedfanspeed
def capabilitySupportsCool = parseCapabilityData(childDevice.getCapabilitySupportsCool())
def capabilitySupportsCoolSwing = parseCapabilityData(childDevice.getCapabilitySupportsCoolSwing())
def capabilitysupported = capabilitySupportsCool
def capabilitySupportsCoolFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsCoolFanSpeed())
def capabilitySupportsCoolAutoFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsCoolAutoFanSpeed())
def fancapabilitysupported = capabilitySupportsCoolAutoFanSpeed
def jsonbody
if (fancapabilitysupported == "true"){
supportedfanspeed = "AUTO"
} else {
supportedfanspeed = "HIGH"
}
if (capabilitySupportsCoolSwing == "true" && capabilitySupportsCoolFanSpeed == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"COOL", power:"ON", swing:"OFF", temperature:[celsius:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"COOL", power:"ON", swing:"OFF", temperature:[fahrenheit:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsCoolSwing == "true" && capabilitySupportsCoolFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"COOL", power:"ON", swing:"OFF", temperature:[celsius:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"COOL", power:"ON", swing:"OFF", temperature:[fahrenheit:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsCoolSwing == "false" && capabilitySupportsCoolFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"COOL", power:"ON", temperature:[celsius:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"COOL", power:"ON", temperature:[fahrenheit:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"COOL", power:"ON", temperature:[celsius:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"COOL", power:"ON", temperature:[fahrenheit:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
logDebug("Executing 'sendCommand.setCoolingTempCommand' to ${targetTemperature} on device ${childDevice.device.name}")
sendCommand("temperature",childDevice,[deviceId,jsonbody])
}
def setHeatingTempCommand(childDevice,targetTemperature){
def terminationmode = settings.manualmode
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
if(deviceType == "AIR_CONDITIONING")
{
def capabilitySupportsHeat = parseCapabilityData(childDevice.getCapabilitySupportsHeat())
def capabilitysupported = capabilitySupportsHeat
def capabilitySupportsHeatSwing = parseCapabilityData(childDevice.getCapabilitySupportsHeatSwing())
def capabilitySupportsHeatAutoFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsHeatAutoFanSpeed())
def capabilitySupportsHeatFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsHeatFanSpeed())
def fancapabilitysupported = capabilitySupportsHeatAutoFanSpeed
def supportedfanspeed
def jsonbody
if (fancapabilitysupported == "true")
{
supportedfanspeed = "AUTO"
}
else
{
supportedfanspeed = "HIGH"
}
if (capabilitySupportsHeatSwing == "true" && capabilitySupportsHeatFanSpeed == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", swing:"OFF", temperature:[celsius:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", swing:"OFF", temperature:[fahrenheit:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsHeatSwing == "true" && capabilitySupportsHeatFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", swing:"OFF", temperature:[celsius:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", swing:"OFF", temperature:[fahrenheit:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsHeatSwing == "false" && capabilitySupportsHeatFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", temperature:[celsius:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", temperature:[fahrenheit:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", temperature:[celsius:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", temperature:[fahrenheit:targetTemperature], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
logDebug("Executing 'sendCommand.setHeatingTempCommand' to ${targetTemperature} on device ${childDevice.device.name}")
sendCommand("temperature",childDevice,[deviceId,jsonbody])
}
if(deviceType == "HEATING")
{
def jsonbody
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[celsius:targetTemperature], type:"HEATING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[fahrenheit:targetTemperature], type:"HEATING"], termination:[type:terminationmode]])
}
logDebug("Executing 'sendCommand.setHeatingTempCommand' to ${targetTemperature} on device ${childDevice.device.name}")
sendCommand("temperature",childDevice,[deviceId,jsonbody])
}
if(deviceType == "HOT_WATER")
{
def jsonbody
def capabilitySupportsWaterTempControl = parseCapabilityData(childDevice.getCapabilitySupportsWaterTempControl())
if(capabilitySupportsWaterTempControl == "true"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[celsius:targetTemperature], type:"HOT_WATER"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[fahrenheit:targetTemperature], type:"HOT_WATER"], termination:[type:terminationmode]])
}
logDebug("Executing 'sendCommand.setHeatingTempCommand' to ${targetTemperature} on device ${childDevice.device.name}")
sendCommand("temperature",[jsonbody])
} else {
logDebug( "Hot Water Temperature Capability Not Supported on device ${childDevice.device.name}" )
}
}
}
def offCommand(childDevice){
logDebug("Executing 'sendCommand.offCommand' on device ${childDevice.device.name}")
def terminationmode = settings.manualmode
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def jsonbody = new groovy.json.JsonOutput().toJson([setting:[type:deviceType, power:"OFF"], termination:[type:terminationmode]])
sendCommand("temperature",childDevice,[deviceId,jsonbody])
}
def onCommand(childDevice){
logDebug("Executing 'sendCommand.onCommand'")
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
if(deviceType == "AIR_CONDITIONING")
{
coolCommand(childDevice)
}
if(deviceType == "HEATING" || deviceType == "HOT_WATER")
{
heatCommand(childDevice)
}
}
def coolCommand(childDevice){
logDebug("Executing 'sendCommand.coolCommand'")
def terminationmode = settings.manualmode
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
def initialsetpointtemp
def supportedfanspeed
def capabilitySupportsCool = parseCapabilityData(childDevice.getCapabilitySupportsCool())
def capabilitySupportsCoolSwing = parseCapabilityData(childDevice.getCapabilitySupportsCoolSwing())
def capabilitysupported = capabilitySupportsCool
def capabilitySupportsCoolAutoFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsCoolAutoFanSpeed())
def capabilitySupportsCoolFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsCoolFanSpeed())
def fancapabilitysupported = capabilitySupportsCoolAutoFanSpeed
def traperror
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug("Existing Setpoint is not set")
traperror = 0
}
if (fancapabilitysupported == "true"){
supportedfanspeed = "AUTO"
} else {
supportedfanspeed = "HIGH"
}
if(traperror == 0){
initialsetpointtemp = settings.defCoolingTemp
} else {
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
def jsonbody
if (capabilitySupportsCoolSwing == "true" && capabilitySupportsCoolFanSpeed == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"COOL", power:"ON", swing:"OFF", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"COOL", power:"ON", swing:"OFF", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsCoolSwing == "true" && capabilitySupportsCoolFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"COOL", power:"ON", swing:"OFF", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"COOL", power:"ON", swing:"OFF", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsCoolSwing == "false" && capabilitySupportsCoolFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"COOL", power:"ON", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"COOL", power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"COOL", power:"ON", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"COOL", power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
}
def heatCommand(childDevice){
logDebug("Executing 'sendCommand.heatCommand' on device ${childDevice.device.name}")
def terminationmode = settings.manualmode
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
if(deviceType == "AIR_CONDITIONING")
{
def initialsetpointtemp
def supportedfanspeed
def traperror
def capabilitySupportsHeat = parseCapabilityData(childDevice.getCapabilitySupportsHeat())
def capabilitySupportsHeatSwing = parseCapabilityData(childDevice.getCapabilitySupportsHeatSwing())
def capabilitysupported = capabilitySupportsHeat
def capabilitySupportsHeatAutoFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsHeatAutoFanSpeed())
def capabilitySupportsHeatFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsHeatFanSpeed())
def fancapabilitysupported = capabilitySupportsHeatAutoFanSpeed
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug( "Existing Setpoint is not set" )
traperror = 0
}
if (fancapabilitysupported == "true")
{
supportedfanspeed = "AUTO"
}
else
{
supportedfanspeed = "HIGH"
}
if(traperror == 0)
{
initialsetpointtemp = settings.defHeatingTemp
}
else
{
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
def jsonbody
if (capabilitySupportsHeatSwing == "true" && capabilitySupportsHeatFanSpeed == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", swing:"OFF", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", swing:"OFF", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsHeatSwing == "true" && capabilitySupportsHeatFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", swing:"OFF", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", swing:"OFF", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsHeatSwing == "false" && capabilitySupportsHeatFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
}
if(deviceType == "HEATING")
{
def initialsetpointtemp
def traperror
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug("Existing Setpoint is not set")
traperror = 0
}
if(traperror == 0)
{
initialsetpointtemp = settings.defHeatingTemp
}
else
{
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
def jsonbody
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[celsius:initialsetpointtemp], type:"HEATING"], termination:[type:terminationmode]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"HEATING"], termination:[type:terminationmode]])
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
}
if(deviceType == "HOT_WATER")
{
def jsonbody
def initialsetpointtemp
def traperror
def capabilitySupportsWaterTempControl = parseCapabilityData(childDevice.getCapabilitySupportsWaterTempControl())
if(capabilitySupportsWaterTempControl == "true"){
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug("Existing Setpoint is not set")
traperror = 0
}
if(traperror == 0){
initialsetpointtemp = settings.defHeatingTemp
} else {
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[celsius:initialsetpointtemp], type:"HOT_WATER"], termination:[type:terminationmode]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"HOT_WATER"], termination:[type:terminationmode]])
}
} else {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", type:"HOT_WATER"], termination:[type:terminationmode]])
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
}
}
def emergencyHeat(childDevice){
logDebug("Executing 'sendCommand.heatCommand' on device ${childDevice.device.name}")
def traperror
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
if(deviceType == "AIR_CONDITIONING")
{
def capabilitySupportsHeat = parseCapabilityData(childDevice.getCapabilitySupportsHeat())
def capabilitysupported = capabilitySupportsHeat
def capabilitySupportsHeatSwing = parseCapabilityData(childDevice.getCapabilitySupportsHeatSwing())
def capabilitySupportsHeatAutoFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsHeatAutoFanSpeed())
def capabilitySupportsHeatFanSpeed = parseCapabilityData(childDevice.getCapabilitySupportsHeatFanSpeed())
def fancapabilitysupported = capabilitySupportsHeatAutoFanSpeed
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug("Existing Setpoint is not set")
traperror = 0
}
if (capabilitysupported == "true")
{
def initialsetpointtemp
def supportedfanspeed
if (fancapabilitysupported == "true")
{
supportedfanspeed = "AUTO"
}
else
{
supportedfanspeed = "HIGH"
}
if(traperror == 0)
{
initialsetpointtemp = settings.defHeatingTemp
}
else
{
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
def jsonbody
if (capabilitySupportsHeatSwing == "true" && capabilitySupportsHeatFanSpeed == "true")
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", swing:"OFF", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", swing:"OFF", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
}
else if(capabilitySupportsHeatSwing == "true" && capabilitySupportsHeatFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", swing:"OFF", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", swing:"OFF", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else if(capabilitySupportsHeatSwing == "false" && capabilitySupportsHeatFanSpeed == "false"){
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
else if(state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[mode:"HEAT", power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[type:terminationmode]])
}
}
else
{
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", temperature:[celsius:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[fanSpeed:supportedfanspeed, mode:"HEAT", power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"AIR_CONDITIONING"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(device)
}
else
{
logDebug("Sorry Heat Capability not supported on device ${childDevice.device.name}")
}
}
if(deviceType == "HEATING")
{
def initialsetpointtemp
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug( "Existing Setpoint is not set" )
traperror = 0
}
if(traperror == 0)
{
initialsetpointtemp = settings.defHeatingTemp
}
else
{
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
def jsonbody
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[celsius:initialsetpointtemp], type:"HEATING"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"HEATING"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
}
(deviceType == "HOT_WATER")
{
def initialsetpointtemp
def jsonbody
def capabilitySupportsWaterTempControl = parseCapabilityData(childDevice.getCapabilitySupportsWaterTempControl())
if(capabilitySupportsWaterTempControl == "true"){
if(childDevice.device.currentValue("thermostatSetpoint"))
{
traperror = ((childDevice.device.currentValue("thermostatSetpoint")).intValue())
}
else
{
logDebug("Existing Setpoint is not set")
traperror = 0
}
if(traperror == 0)
{
initialsetpointtemp = settings.defHeatingTemp
}
else
{
initialsetpointtemp = childDevice.device.currentValue("thermostatSetpoint")
}
if (state.tempunit == "C") {
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[celsius:initialsetpointtemp], type:"HOT_WATER"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
else if (state.tempunit == "F"){
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", temperature:[fahrenheit:initialsetpointtemp], type:"HOT_WATER"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
}
else
{
jsonbody = new groovy.json.JsonOutput().toJson([setting:[power:"ON", type:"HOT_WATER"], termination:[durationInSeconds:"3600", type:"TIMER"]])
}
sendCommand("temperature",childDevice,[deviceId,jsonbody])
statusCommand(childDevice)
}
}
def statusCommand(childDevice){
def item = (childDevice.device.deviceNetworkId).tokenize('|')
def deviceId = item[0]
def deviceType = item[1]
def deviceToken = item[2]
logDebug("Executing 'sendCommand.statusCommand'")
sendCommand("status",childDevice,[deviceId])
}
def userStatusCommand(childDevice){
try{
logDebug("Executing 'sendCommand.userStatusCommand'")
sendCommand("userStatus",childDevice,[])
} catch(Exception e) { log.error("Failed in setting userStatusCommand: " + e)
}
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Log methods
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
def logDebug(message) {
if (isDebugLogActive()){
log.debug message }
}
def openForcedDebugLog(seconds) {
if (seconds == 0){ seconds = getDefaultLogForceSecs() }
def forceLogRequestedUntil = now() + (seconds * 1000)
if (state.debugLogForcedUntil < forceLogRequestedUntil) {
state.debugLogForcedUntil = forceLogRequestedUntil }
}
def isDebugLogActive() {
if (now() < state.debugLogForcedUntil || getDebugLogStatus() == "On" ) {
return true }
else {
return false }
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Password Grant methods
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
def initiateDeviceCodeFlow() {
def result = false
logDebug("Initiating device code flow")
def params = [
uri: getTadoLoginUri(),
path: getDeviceCodePath(),
body: [
client_id: getClientId(),
scope: "offline_access"
]
]
try {
logDebug("Parameters for initiating device code flow:" + " " + params)
httpPost(params)
{ resp ->
logDebug("Response data:" + " " + resp.data)
logDebug("Respones succes:" + " " + resp.success)
if (resp && resp.data && resp.success) {
logDebug("Device Code Received")
state.deviceCode = resp.data.device_code
state.userCode = resp.data.user_code
state.deviceCodeExpiresIn = resp.data.expires_in
state.deviceCodeExpires = (now() + (resp.data.expires_in * 1000))
state.deviceCodeCheckInterval = resp.data.interval
state.deviceCodeVerificationUri = resp.data.verification_uri
state.deviceCodeVerificationUriComplete = resp.data.verification_uri_complete
logDebug("User Code:" + " " + resp.data.user_code)
logDebug("Device Code Verification URL:" + " " + state.deviceCodeVerificationUriComplete)
logDebug("Device code expires:" + " " + state.deviceCodeExpires)
result = true
} else { log.error "Failed to get Device Code" }
}
}
catch (e) {
log.error "Device Code Retreival Error: ${e}"
}
return result
}
def getDeviceAccessToken() {
def result = false
logDebug( "Initiating get Device Access Token" )
def params = [
uri: getTadoLoginUri(),
path: getDeviceTokenPath(),
body: [
client_id: getClientId(),
device_code: state.deviceCode,
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
]
]
try {
logDebug( "Parameters for initiating get Device Access Token:" + " " + params )
httpPost(params)
{ resp ->
logDebug( "Response data:" + " " + resp.data )
logDebug( "Respones succes:" + " " + resp.success )
if (resp && resp.data && resp.success) {
logDebug( "Access Token Received" )
state.accessToken = resp.data.access_token
state.accessTokenRefreshed = now()
state.accessTokenExpires = (now() + (resp.data.expires_in * 1000))
state.nextAccessTokenRefresh = now() + (resp.data.expires_in * 1000) - getTokenExpOffset()
state.accessTokenTimeToLive = resp.data.expires_in
state.refreshToken = resp.data.refresh_token
state.tokenScope = resp.data.scope
state.tokenType = resp.data.token_type
state.userId = resp.data.userId
logDebug( "Access Token:" + " " + resp.data.access_token )
logDebug( "Refresh Token:" + " " + resp.data.refresh_token )
logDebug( "User ID:" + " " + state.userId )
logDebug( "Token expires in:" + " " + resp.data.expires_in )
logDebug( "Token expires:" + " " + state.accessTokenExpires )
result = true
} else { log.error "Failed to get access code"
log.error( "Parameters for get access code:" + " " + params )
if(resp){ log.error( "Response:" + " " + resp.data ) }
if(resp.data){ log.error( "Response data:" + " " + resp.data ) }
if(resp.success){ log.error( "Respones succes:" + " " + resp.success ) }
state.accessToken = null
state.refreshToken = null }
}
}
catch (e) {
log.error "Device Access Code Retreival Error: ${e}"
if(resp){ log.error( "Response:" + " " + resp.data )
if(resp.data){ log.error( "Response data:" + " " + resp.data ) }
if(resp.success){ log.error( "Respones succes:" + " " + resp.success ) } }
state.accessToken = null
state.refreshToken = null
}
return result
}
def refreshAccessToken() {
if (!state.accessTokenExpires || state.nextAccessTokenRefresh <= now()) { return true } else { return false }
}
def isAccessTokenExpired() {
if (!state.accessTokenExpires || state.accessTokenExpires <= now()) { return true } else { return false }
}
def refreshToken() {
def result = false
logDebug( "Refreshing Access token" )
//Open debug log if access token is cleared previously
if (isAccessTokenExpired() && settings.debugLog != "On" ) {
def debugTurnedOn = true
settings.debugLog = "On" }
try {
def params = [
uri: getAccessTokenUri(),
path: getAccessTokenPath(),
body: [
client_id: getClientId(),
grant_type: "refresh_token",
refresh_token: getRefreshToken()
] ]
logDebug( "Parameters for token refresh:" + " " + params )
httpPost(params) { resp ->
logDebug( "Response data:" + " " + resp.data )
logDebug( "Respones succes:" + " " + resp.success )
if (resp && resp.data && resp.success) {
logDebug( "Token refreshed successfully" )
state.accessToken = resp.data.access_token
state.accessTokenRefreshed = now()
state.accessTokenExpires = now() + (resp.data.expires_in * 1000)
state.nextAccessTokenRefresh = now() + (resp.data.expires_in * 1000) - getTokenExpOffset()
state.accessTokenTimeToLive = resp.data.expires_in
state.refreshToken = resp.data.refresh_token
logDebug( "Access Token:" + " " + resp.data.access_token )
logDebug( "Access Token Saved:" + " " + state.accessToken )
logDebug( "Refresh Token:" + " " + resp.data.refresh_token )
logDebug( "Refresh Token Saved:" + " " + state.refreshToken )
logDebug( "User ID:" + " " + state.userId )
logDebug( "Token expires in:" + " " + resp.data.expires_in )
logDebug( "Token expires:" + " " + state.accessTokenExpires )
result = true
}
else {
log.error( "Failed to refresh access token using refresh token" + " " + state.refreshToken )
log.error( "Parameters for token refresh:" + " " + params )
if(resp){ log.error( "Response:" + " " + resp.data )
if(resp.data){ log.error( "Response data:" + " " + resp.data ) }
if(resp.success){ log.error( "Respones succes:" + " " + resp.success ) } }
result = false
}
}
}
catch (e) {
log.error "Failed to refresh access token (HTTP error): ${e}"
if(resp){ log.error( "Response:" + " " + resp.data )
if(resp.data){ log.error( "Response data:" + " " + resp.data ) }
if(resp.success){ log.error( "Respones succes:" + " " + resp.success ) } }
result = false
}
if (debugTurnedOn) {
def debugTurnedOn = false
settings.debugLog = "Off" }
return result
}