I just revisted the Device Handler after I had to reset my ST. I added the port to be added as hex in the options. Hope it helps someone. I use webshutdown by Renato Silvan my PCs. No Pistons or anything else required.
Copy and paste the below into a Device handler.
/**
WOL Switch
Copyright 2016 Sumit Garg
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: âWOL Switchâ, namespace: âsumitkgargâ, author: âSumit Gargâ) {
// capability âActuatorâ
// capability âSensorâ
capability âSwitchâ
capability âRefreshâ
}
preferences {
input âmacAddressâ, âstringâ, title: âMAC Addressâ, description: âEnter the MAC address of the computerâ, required: true, displayDuringSetup: true
input âipAddressâ, âstringâ, title: âIP Addressâ, description: âEnter the IP address of the computerâ, required: true, displayDuringSetup: true
input âHEXPortâ, âstringâ, title: âHEXPortâ, description: âEnter the Port for status in HEXâ, required: true, displayDuringSetup: true
input âPortâ, âstringâ, title: âPortâ, description: âEnter the Port for statusâ, required: true, displayDuringSetup: true
}
simulator {
}
tiles(scale: 2) {
standardTile(âswitchâ, âdevice.switchâ, decoration: âflatâ, width: 6, height: 4, canChangeIcon: true) {
state âonâ, label:â${currentValue}â, icon:âst.switches.switch.onâ, backgroundColor:"#79b821"
state âoffâ, label:â${currentValue}â, icon:âst.switches.switch.offâ, backgroundColor:"#ffffff"
state âscanningâ, label:âSCANNINGâ, icon:âst.switches.switch.offâ, backgroundColor:"#ffa81e"
}
standardTile("turnon", "device.switch", decoration: "flat", width: 2, height: 2) {
state "on", label:'On', action:"switch.on", icon:"st.switches.switch.on", backgroundColor: "#aaefad" //"#79b821" //"#dff0d8"
state "off", label:'On', action:"switch.on", icon:"st.switches.switch.on", backgroundColor:"#aaefad"
}
standardTile("turnoff", "device.switch", decoration: "flat", width: 2, height: 2) {
state "on", label:'Off', action:"switch.off", icon: "st.switches.switch.off", backgroundColor: "#efaaaa" //"#f2dede"
state "off", label:'Off', action:"switch.off", icon:"st.switches.switch.off", backgroundColor:"#efaaaa"
}
standardTile("refresh", "device.switch", decoration: "flat", width: 2, height: 2) {
state "default", action:"refresh.refresh", icon:"st.secondary.refresh" //, backgroundColor: "#efd2aa"
}
main "switch"
details (["switch", "turnon", "turnoff", "refresh"])
}
}
def parse(description) {
def msg = parseLanMessage(description)
def ok = msg.status == 200
def switchResponse = msg.port == â${HEXPort}â
// log.debug â${description}â
if (switchResponse) {
setSwitchValue(ok ? âonâ : âoffâ)
}
}
// Get the state of the machine
def machineGET() {
sendHubCommand(new physicalgraph.device.HubAction([
method: âGETâ,
path: â/â,
headers: [
HOST: â${ipAddress}:${Port}â,
]]))
log.debug âSent HTTP request to PC - ${ipAddress}:${Port}â
}
// Using a third party app on the windows server
// to make the computer go to sleep by making a GET request
def machineSleepGET() {
sendHubCommand(new physicalgraph.device.HubAction([
method: âGETâ,
path: â/shutdown?auth=Shutd0wNâ,
headers: [
HOST: â${ipAddress}:${Port}â,
]]))
log.debug âSent HTTP SLEEP request to PC - http://${ipAddress}:${Port}/shutdown?auth=Shutd0wNâ
}
def sendWOL() {
sendHubCommand(new physicalgraph.device.HubAction(
âwake on lan ${macAddress.replaceAll(â:â,ââ).replaceAll(â â,ââ).toLowerCase()}â,
physicalgraph.device.Protocol.LAN,
null,
[:]
))
log.debug âSent WOL request to PC - ${ipAddress}â
}
def on() {
initializeCron()
log.debug âExecuting âonââ
setScanningMode()
sendWOL()
machineGET() // in case it is already on - for more immediate feedback
runIn(7, machineGET) // in case it is still waking up
runIn(12, timeout)
}
def off() {
initializeCron()
log.debug âExecuting âoffââ
// setScanningMode()
machineSleepGET()
setSwitchValue(âoffâ)
}
def refresh() {
initializeCron()
if (getSwitchValue() != âscanningâ) {
log.debug âExecuting ârefreshââ
setScanningMode()
machineGET()
runIn(5, timeout)
}
}
def initializeCron() {
if (state.initialized != âtrueâ) {
state.initialized = âtrueâ
// refresh every 10 minutes.
def cronExpression = â0 0/10 * 1/1 * ? *â
log.debug âInitializing refresh schedule based on cron expression - â${cronExpression}ââ
schedule(cronExpression, refresh)
}
}
def timeout() {
if (getSwitchValue() == âscanningâ) {
log.debug âExecuting âtimeoutâ for switchâ
setSwitchValue(âoffâ)
}
}
def getSwitchValue() {
return state.switch
}
def setScanningMode(){
setSwitchValue(âscanningâ)
}
def setSwitchValue(val) {
if (state.switch != val) {
state.switch = val
sendEvent(name: âswitchâ, value: val)
log.debug âSet Switch value to ${val}â
}
}
def setDeviceNetworkId(dni) {
log.debug âChanging DNI to ${dni}â
device.setDeviceNetworkId(dni)
}
def getDeviceNetworkId(port) {
return â${convertIPtoHex(ipAddress)}:${convertPortToHex(port)}â
}
private String convertIPtoHex(ipAddress) {
return ipAddress.tokenize(â.â).collect{String.format(â%02xâ, it.toInteger())}.join()
}
private String convertPortToHex(port) {
return port.toString().format(â%04xâ, port.toInteger())
}
// c0a80010:0050 - 192.168.0.16:80
// c0a80010:7e90 - 192.168.0.16:32400
// c0a80010:1f40 - 192.168.0.16:8000
// C0A85634:0050 - 192.168.86.52:80
// change the dni in My Devices for this device if IP address changs