Update thermostat-auto-off.groovy
[smartapps.git] / official / hub-ip-notifier.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Hub IP Notifier
14  *
15  *  Author: luke
16  *  Date: 2014-01-28
17  */
18 definition(
19     name: "Hub IP Notifier",
20     namespace: "smartthings",
21     author: "SmartThings",
22     description: "Listen for local IP changes when your hub registers.",
23     category: "SmartThings Internal",
24     iconUrl: "https://s3.amazonaws.com/smartapp-icons/MyApps/Cat-MyApps.png",
25     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/MyApps/Cat-MyApps@2x.png"
26 )
27
28 preferences {
29         page(name: "pageWithIp", title: "Hub IP Notifier", install: true)
30
31 }
32
33 def pageWithIp() {
34         def currentIp = state.localip ?: 'unknown'
35         def registerDate = state.lastRegister ?: null
36         dynamicPage(name: "pageWithIp", title: "Hub IP Notifier", install: true, uninstall: true) {
37                 section("When Hub Comes Online") {
38                         input "hub", "hub", title: "Select a hub"
39                 }
40                 section("Last Registration Details") {
41                         if(hub && registerDate) {
42                                    paragraph """Your hub last registered with IP:
43 $currentIp
44 on:
45 $registerDate"""
46                         } else if (hub && !registerDate) {
47                                 paragraph "Your hub has not (re)registered since you installed this app"
48                         } else {
49                                 paragraph "Check back here after installing to see the current IP of your hub"
50                         }
51                 }
52         }
53 }
54
55 def installed() {
56         log.debug "Installed with settings: ${settings}"
57
58         initialize()
59 }
60
61 def updated() {
62         log.debug "Updated with settings: ${settings}"
63
64         unsubscribe()
65         initialize()
66 }
67
68 def initialize() {
69         subscribe(hub, "hubInfo", registrationHandler, [filterEvents: false])
70 }
71
72 def registrationHandler(evt) {
73         def hubInfo = evt.description.split(',').inject([:]) { map, token ->
74                 token.split(':').with { map[it[0].trim()] = it[1] }
75                 map
76         }
77         state.localip = hubInfo.localip
78         state.lastRegister = new Date()
79         sendNotificationEvent("${hub.name} registered in prod with IP: ${hubInfo.localip}")
80 }