/* A simple driver that will run a command via telnet Useful with Rpi's Copyright 2019 Sha'ul ben Yeshua / Scott Grayban 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. Inspired by https://community.hubitat.com/t/telnet/18323 */ metadata { definition ( name: "Send Telnet Command", namespace: "sgrayban", author: "Scott Grayban", importUrl: "http://sgrayban.borgnet.xyz:8081/hubitat-public-repo/Send%20Telnet%20Command.groovy" ) { capability "Initialize" capability "Telnet" capability "Switch" command "RebootDevice" command "PowerOff" command "RestartNut" } attribute "Telnet", "String" preferences { input "ip", "text", title: "Target System IP", description: "", required: true, displayDuringSetup: true input "port", "text", title: "Target System Telnet Port", description: "", required: true, displayDuringSetup: true input "username", "text", title: "Username", description: "", required: true, displayDuringSetup: true input "password", "password", title: "Password", description: "", required: true, displayDuringSetup: true input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true } } def initialize(){ log.debug "Connecting to telnet - IP = ${ip}, Port = ${port.toInteger()}, Username = ${username}, Password = ${password}" telnetConnect([terminalType: 'VT100'], ip, port.toInteger(), username, password) } def installed(){ log.info "Installed" log.info "Please enter your login credentials." } def updated(){ log.info "Preferences updated" runIn(5,sendEvent(name: "switch", value: "off")) } def on() { initialize() if (logEnable) log.info "Send Reboot" sendEvent(name: "switch", value: "on") def msg = "sudo reboot" if (logEnable) log.info "Sending msg = ${msg}" sendEvent(name: "Command Sent", value: msg) sendHubCommand(new hubitat.device.HubAction("""$msg\r\n""", hubitat.device.Protocol.TELNET)) runIn(5,sendEvent(name: "switch", value: "off")) } def RebootDevice(){ on() } def RestartNut() { initialize() if (logEnable) log.info "Send Reboot" //sendEvent(name: "switch", value: "on") def msg = "sudo service nut-server restart" if (logEnable) log.info "Sending msg = ${msg}" sendEvent(name: "Command Sent", value: msg) sendHubCommand(new hubitat.device.HubAction("""$msg\r\n""", hubitat.device.Protocol.TELNET)) //runIn(5,sendEvent(name: "switch", value: "off")) } def PowerOff() { initialize() if (logEnable) log.info "Send PowerOff" sendEvent(name: "switch", value: "on") def msg = "sudo poweroff" if (logEnable) log.info "Sending msg = ${msg}" sendEvent(name: "Command Sent", value: msg) sendHubCommand(new hubitat.device.HubAction("""$msg\r\n""", hubitat.device.Protocol.TELNET)) runIn(5,sendEvent(name: "switch", value: "off")) } def parse(String msg) { if (logEnable) log.debug "Telnet Response = ${msg}" if (msg == "permitted by applicable law.") { sendEvent(name: "Telnet", value: "Connected"); } } def telnetStatus(String status){ if (logEnable) log.info "telnetStatus- error: ${status}" if (status == "receive error: Stream is closed"){ if (logEnable) log.error "Telnet connection dropped..." sendEvent(name: "Telnet", value: "Disconnected") initialize() } else { sendEvent(name: "Telnet", value: "Connected") } }