Advertisement
Guest User

Untitled

a guest
May 21st, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 7.55 KB | None | 0 0
  1. /**
  2.  *
  3.  *  Rebooter
  4.  *
  5.  *  Copyright 2019-2020 Dominick Meglio
  6.  *
  7.  *  If you find this useful, donations are always appreciated
  8.  *  https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7LBRPJRLJSDDN&source=url
  9.  *
  10.  * Revision History
  11.  * v 2020.03.30 - Added an option to restart the Hubitat process instead of rebooting the hub
  12.  */
  13.  
  14. definition(
  15.     name: "Rebooter",
  16.     namespace: "dcm.rebooter",
  17.     author: "Dominick Meglio",
  18.     description: "Restart/Shutdown your hub on a schedule",
  19.     category: "My Apps",
  20.     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
  21.     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
  22.     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
  23.     documentationLink: "https://github.com/dcmeglio/hubitat-rebooter/blob/master/README.md")
  24.  
  25. preferences {
  26.     page(name: "prefMain")
  27.  
  28. }
  29.  
  30. def installed() {
  31.     initialize()
  32. }
  33.  
  34. def updated() {
  35.     unschedule()
  36.     unsubscribe()
  37.     initialize()
  38. }
  39.  
  40. def initialize() {
  41.    
  42.     if(actionType != null){
  43.         // Handle Reboot
  44.         if(actionType.contains("Reboot")){
  45.             if (rebootType == "Time" || rebootType == null) {
  46.                 def time = timeToday(rebootTime)
  47.                 def days = ""
  48.                 for (def i = 0; i < rebootDays.size(); i++) {
  49.                     days += rebootDays[i].substring(0,3)
  50.                     days += ","
  51.                 }
  52.                 days = days.substring(0,days.size()-1)
  53.                 schedule("00 ${time.minutes} ${time.hours} ? * ${days}", executeReboot)
  54.             }
  55.             else {
  56.                 subscribe(rebootButton, "pushed.1", rebootButtonPushed)
  57.             }
  58.         }
  59.        
  60.         // Handle Shutdown
  61.         if(actionType.contains("Shutdown")){
  62.             if (shutdownType == "Time" || shutdownType == null) {
  63.                 def time = timeToday(shutdownTime)
  64.                 def days = ""
  65.                 for (def i = 0; i < shutdownDays.size(); i++) {
  66.                     days += shutdownDays[i].substring(0,3)
  67.                     days += ","
  68.                 }
  69.                 days = days.substring(0,days.size()-1)
  70.                 schedule("00 ${time.minutes} ${time.hours} ? * ${days}", executeShutdown)
  71.             }
  72.             else {
  73.                 subscribe(shutdownButton, "pushed.1", shutdownButtonPushed)
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. def rebootButtonPushed(evt) {
  80.     executeReboot()
  81. }
  82.  
  83. def shutdownButtonPushed(evt) {
  84.     executeShutdown()
  85. }
  86.  
  87. def uninstalled() {
  88.     logDebug "uninstalling app"
  89.     unschedule()
  90.     unsubscribe()
  91. }
  92.  
  93. def prefMain() {
  94.     return dynamicPage(name: "prefMain", title: "Reboot/Shutdown Configuration", install: true, uninstall: true) {
  95.         section("") {
  96.            
  97.             // Handle Security
  98.             input "Security", "bool", title: "Is Hub Security enabled?", submitOnChange: true
  99.             if (Security)
  100.             {
  101.                 input "Username", "string", title: "Hub Security username", required: true
  102.                 input "Password", "password", title: "Hub Security password", required: true
  103.             }
  104.            
  105.             input "actionType", "enum", title: "Would you like to schedule a reboot or shutdown?", required: true, multiple: true, options:["Reboot","Shutdown"], submitOnChange: true
  106.            
  107.             if(actionType != null){
  108.                 // Handle Reboot
  109.                 if(actionType.contains("Reboot"))
  110.                 {
  111.                     input "rebootType", "enum", title: "What would you like to use to trigger a reboot?", options: ["Time","Button"], defaultValue: "Time", submitOnChange: true
  112.                     if (rebootType == "Time" || rebootType == null) {
  113.                         input "rebootTime", "time", title: "Time of day to reboot", required: true
  114.                         input "rebootDays", "enum", title: "Which days should the hub be rebooted?", required: true, multiple: true, options:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
  115.                     }
  116.                     else if (rebootType == "Button") {
  117.                         input "rebootButton", "capability.pushableButton", title: "Button that, when pressed, will trigger a reboot/restart", required: true
  118.                     }
  119.                     input "restartInsteadOfReboot", "bool", title: "Restart the Hubitat process instead of rebooting the hub", defaultValue: true
  120.                 }
  121.  
  122.                 // Handle Shutdown
  123.                 if(actionType.contains("Shutdown"))
  124.                 {
  125.                     input "shutdownType", "enum", title: "What would you like to use to trigger a shutdown?", options: ["Time","Button"], defaultValue: "Time", submitOnChange: true
  126.                     if (shutdownType == "Time" || shutdownType == null) {
  127.                         input "shutdownTime", "time", title: "Time of day to shutdown", required: true
  128.                         input "shutdownDays", "enum", title: "Which days should the hub be shutdown?", required: true, multiple: true, options:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
  129.                     }
  130.                     else if (shutdownType == "Button") {
  131.                         input "shutdownButton", "capability.pushableButton", title: "Button that, when pressed, will trigger a shutdown", required: true
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.         displayFooter()
  137.     }
  138. }
  139.  
  140. def executeReboot()
  141. {
  142.     def cookie = ""
  143.     if (restartInsteadOfReboot)
  144.         log.info "Restarting hub"
  145.     else
  146.         log.info "Rebooting hub"
  147.     if (Security)
  148.     {
  149.         httpPost(
  150.             [
  151.                 uri: "http://127.0.0.1:8080",
  152.                 path: "/login",
  153.                 query:
  154.                 [
  155.                     loginRedirect: "/"
  156.                 ],
  157.                 body:
  158.                 [
  159.                     username: Username,
  160.                     password: Password,
  161.                     submit: "Login"
  162.                 ]
  163.             ]
  164.         )
  165.         { resp ->
  166.             cookie = resp?.headers?.'Set-Cookie'?.split(';')?.getAt(0)
  167.         }
  168.     }
  169.    
  170.     def rebootPath = "/hub/reboot"
  171.     if (restartInsteadOfReboot)
  172.         rebootPath = "/hub/restart"
  173.     httpPost(
  174.         [
  175.             uri: "http://127.0.0.1:8080",
  176.             path: rebootPath,
  177.             headers:
  178.             [
  179.                 "Cookie": cookie
  180.             ]
  181.         ]
  182.     )
  183.     {
  184.         resp ->
  185.     }      
  186. }
  187.  
  188. def executeShutdown()
  189. {
  190.     def cookie = ""
  191.     log.info "Shutting down hub"
  192.     if (Security)
  193.     {
  194.         httpPost(
  195.             [
  196.                 uri: "http://127.0.0.1:8080",
  197.                 path: "/login",
  198.                 query:
  199.                 [
  200.                     loginRedirect: "/"
  201.                 ],
  202.                 body:
  203.                 [
  204.                     username: Username,
  205.                     password: Password,
  206.                     submit: "Login"
  207.                 ]
  208.             ]
  209.         )
  210.         { resp ->
  211.             cookie = resp?.headers?.'Set-Cookie'?.split(';')?.getAt(0)
  212.         }
  213.     }
  214.    
  215.     def shutdownPath = "/hub/shutdown"
  216.     httpPost(
  217.         [
  218.             uri: "http://127.0.0.1:8080",
  219.             path: shutdownPath,
  220.             headers:
  221.             [
  222.                 "Cookie": cookie
  223.             ]
  224.         ]
  225.     )
  226.     {
  227.         resp ->
  228.     }      
  229. }
  230.  
  231. def displayFooter(){
  232.     section() {
  233.         paragraph getFormat("line")
  234.         paragraph "<div style='color:#1A77C9;text-align:center'>Hub Rebooter<br><a href='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7LBRPJRLJSDDN&source=url' target='_blank'><img src='https://www.paypalobjects.com/webstatic/mktg/logo/pp_cc_mark_37x23.jpg' border='0' alt='PayPal Logo'></a><br><br>Please consider donating. This app took a lot of work to make.<br>If you find it valuable, I'd certainly appreciate it!</div>"
  235.     }      
  236. }
  237.  
  238. def getFormat(type, myText=""){         // Modified from @Stephack Code  
  239.     if(type == "line") return "<hr style='background-color:#1A77C9; height: 1px; border: 0;'>"
  240.     if(type == "title") return "<h2 style='color:#1A77C9;font-weight: bold'>${myText}</h2>"
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement