/* * UPB Connector for DIMable devices * * Assembles UPB commands and transmits via GlobalCache Flex Link to PIM-R via HTTP or to the PulseWorx Gateway * * Intial release: 11-Sep-22 */ metadata { definition(name: "UPB Dim Object", namespace: "UPB", author: "UPB Voice Bridge", importUrl: "") { capability "Switch" capability "Switch Level" } } preferences { section("UPB Network Connection") { input name: "UPBId", type: "text", title: "UPB unit id", required: true input name: "Channel", type: "text", title: "Channel", required: true input name: "ConnectedAt", type: "text", title: "Last configuration update" input name: "OnControlByScene", type: "bool", title: "Control ON by scene", defaultValue: false input name: "OnSceneId", type: "text", title: "Scene id for ON control" input name: "OnDirectCmdAlso", type: "bool", title: "Follow ON scene control with ON command direct to device", defaultValue: false input name: "OffControlByScene", type: "bool", title: "Control OFF by scene", defaultValue: false input name: "OffSceneId", type: "text", title: "Scene id for OFF control" input name: "OffDirectCmdAlso", type: "bool", title: "Follow OFF scene control with OFF command direct to device", defaultValue: false input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: false } } // //----------------------------------------------------------------------------- // Standard stuff //----------------------------------------------------------------------------- // void installed() { if (logEnable) log.debug ("In Dim driver Installed"); } 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 parse(String description) { // Shouldn't be used log.debug(description); } // //----------------------------------------------------------------------------- // Used by the app //----------------------------------------------------------------------------- // def UpdateUPBId(Integer id) { if (logEnable) log.debug ("In UpdateUPBId " + id); device.updateSetting("UPBId", [value: id, type: "Integer"]); } def UpdateConnectedAt(String text) { if (logEnable) log.debug ("In UpdateConnectedAt " + text); device.updateSetting("ConnectedAt", [value: text, type: "text"]); } def UpdateOnSceneControl (Integer controlByScene, Integer sceneId, Integer directCmdAlso) { if (logEnable) log.debug ("In UpdateOnSceneControl"); if (controlByScene == 0) device.updateSetting("OnControlByScene", [value: "false", type: "bool"]); else device.updateSetting("OnControlByScene", [value: "true", type: "bool"]); device.updateSetting("OnSceneId", [value: sceneId, type: "Integer"]); if (directCmdAlso == 0) device.updateSetting("OnDirectCmdAlso", [value: "false", type: "bool"]); else device.updateSetting("OnDirectCmdAlso", [value: "true", type: "bool"]); } def UpdateOffSceneControl (Integer controlByScene, Integer sceneId, Integer directCmdAlso) { if (logEnable) log.debug ("In UpdateOffSceneControl"); if (controlByScene == 0) device.updateSetting("OffControlByScene", [value: "false", type: "bool"]); else device.updateSetting("OffControlByScene", [value: "true", type: "bool"]); device.updateSetting("OffSceneId", [value: sceneId, type: "Integer"]); if (directCmdAlso == 0) device.updateSetting("OffDirectCmdAlso", [value: "false", type: "bool"]); else device.updateSetting("OffDirectCmdAlso", [value: "true", type: "bool"]); } def UpdateDeviceLabel(String text) { device.setLabel(text) } def UpdateDeviceChannel(Integer channel) { device.updateSetting("Channel", [value: channel, type: "Integer"]); } def GetConnectedAt() { if (logEnable) log.debug ("In GetConnectedAt"); return settings.ConnectedAt; } // //----------------------------------------------------------------------------- // Operations //----------------------------------------------------------------------------- // def on() { if (logEnable) log.debug ("Sending ON to device [${settings.UPBId}]"); try { worksWith = parent.GetWorksWith(); if (worksWith == 0) { PIMIP = parent.GetPIMIP(); String url = "http://" + PIMIP + "/api/v1/serialports/1/sendserial"; if (settings.OnControlByScene) { String cmd = parent.BuildSceneActivateCommand(Integer.parseInt(settings.OnSceneId), Integer.parseInt(settings.UPBId)); if (logEnable) log.debug ("UPB Command activate [${cmd}]"); def postMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(postMap) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: true); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } if (settings.OnDirectCmdAlso) { cmd = parent.BuildGotoCommand(Integer.parseInt(settings.UPBId), 100, Integer.parseInt(settings.Channel)); if (logEnable) log.debug ("UPB Command Goto [${cmd}]"); def directPostMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(directPostMap) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: true); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } } } else { String cmd = parent.BuildGotoCommand(Integer.parseInt(settings.UPBId), 100, Integer.parseInt(settings.Channel)); if (logEnable) log.debug ("UPB Command Goto [${cmd}]"); def postMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(postMap) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: true); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } } } else { gatewayIP = parent.GetGatewayIP(); if (settings.OnControlByScene) { String s = "http://" + gatewayIP + "/api/v1/ActivateLink?id=" + settings.OnSceneId; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: true); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } if (settings.OnDirectCmdAlso) { s = "http://" + gatewayIP + "/api/v1/Goto?id=" + settings.UPBId + "&level=100&channel=" + settings.Channel; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: false); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } } } else { String s = "http://" + gatewayIP + "/api/v1/Goto?id=" + settings.UPBId + "&level=100&channel=" + settings.Channel; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: true); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } } } } catch (Exception e) { log.warn ("Call to on failed: ${e.message}"); } } def off() { if (logEnable) log.debug ("Sending OFF to device [${settings.UPBId}]"); try { worksWith = parent.GetWorksWith(); if (worksWith == 0) { PIMIP = parent.GetPIMIP(); String url = "http://" + PIMIP + "/api/v1/serialports/1/sendserial"; if (settings.OffControlByScene) { String cmd = parent.BuildSceneDeactivateCommand(Integer.parseInt(settings.OffSceneId), Integer.parseInt(settings.UPBId)); if (logEnable) log.debug ("UPB Command Deactivate [${cmd}]"); def postMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(postMap) { resp -> if (resp.success) { sendEvent(name: "switch", value: "off", isStateChange: true); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } if (settings.OffDirectCmdAlso) { cmd = parent.BuildGotoCommand(Integer.parseInt(settings.UPBId), 0, Integer.parseInt(settings.Channel)); if (logEnable) log.debug ("UPB Command Goto [${cmd}]"); def directPostMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(directPostMap) { resp -> if (resp.success) { sendEvent(name: "switch", value: "off", isStateChange: false); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } } } else { String cmd = parent.BuildGotoCommand(Integer.parseInt(settings.UPBId), 0, Integer.parseInt(settings.Channel)); if (logEnable) log.debug ("UPB Command Goto [${cmd}]"); def postMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(postMap) { resp -> if (resp.success) { sendEvent(name: "switch", value: "off", isStateChange: true); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } } } else { gatewayIP = parent.GetGatewayIP(); if (settings.OffControlByScene) { String s = "http://" + gatewayIP + "/api/v1/DeactivateLink?id=" + settings.OffSceneId; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { sendEvent(name: "switch", value: "off", isStateChange: true); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } if (settings.OffDirectCmdAlso) { s = "http://" + gatewayIP + "/api/v1/Goto?id=" + settings.UPBId + "&level=0&channel=" + settings.Channel; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { sendEvent(name: "switch", value: "on", isStateChange: false); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } } } else { String s = "http://" + gatewayIP + "/api/v1/Goto?id=" + settings.UPBId + "&level=0&channel=" + settings.Channel; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { sendEvent(name: "switch", value: "off", isStateChange: true); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } } } } catch (Exception e) { log.warn ("Call to off failed: ${e.message}"); } } def setLevel(value, duration=null) { if (logEnable) log.debug ("Sending Set-Percent to device [${settings.UPBId}]"); try { worksWith = parent.GetWorksWith(); if (worksWith == 0) { PIMIP = parent.GetPIMIP(); String url = "http://" + PIMIP + "/api/v1/serialports/1/sendserial"; String cmd = parent.BuildGotoCommand(Integer.parseInt(settings.UPBId), value.intValue(), Integer.parseInt(settings.Channel)); if (logEnable) log.debug ("UPB Command Goto level [${cmd}]"); def postMap = [ uri: url, contentType: "text/plain", requestContentType: "text/plain", body: cmd ] httpPost(postMap) { resp -> if (resp.success) { if (value > 0) { sendEvent(name: "switch", value: "on", isStateChange: false); } else { sendEvent(name: "switch", value: "off", isStateChange: false); } sendEvent(name: "level", value: value, isStateChange: true); } if (logEnable) { if (resp.data) log.debug ("${resp.data}"); } } } else { gatewayIP = parent.GetGatewayIP(); String s = "http://" + gatewayIP + "/api/v1/Goto?id=" + settings.UPBId + "&level=" + value + "&channel=" + settings.Channel; if (logEnable) log.debug "sending [${s}]"; httpGet(s) { resp -> if (resp.success) { if (value > 0) { sendEvent(name: "switch", value: "on", isStateChange: false); } else { sendEvent(name: "switch", value: "off", isStateChange: false); } sendEvent(name: "level", value: value, isStateChange: true); } if (logEnable) { if (resp.data) log.debug "${resp.data}"; } } } } catch (Exception e) { log.warn ("Call to setLevel failed: ${e.message}"); } }