Update speaker-mood-music.groovy
[smartapps.git] / official / obything-music-connect.groovy
1 /**
2  *  ObyThing Music SmartApp
3  *
4  *  Copyright 2014 obycode
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "ObyThing Music (Connect)",
18     namespace: "com.obycode",
19     author: "obycode",
20     description: "Use this free SmartApp in conjunction with the ObyThing Music app for your Mac to control and automate music and more with iTunes and SmartThings.",
21     category: "SmartThings Labs",
22     iconUrl: "http://obycode.com/obything/ObyThingSTLogo.png",
23     iconX2Url: "http://obycode.com/obything/ObyThingSTLogo@2x.png",
24     singleInstance: true)
25
26
27 preferences {
28     section("Get the IP address and port for your Mac computer using the ObyThing App (http://obything.obycode.com) and set up the SmartApp below:") {
29         input "theAddr", "string", title: "IP:port (click icon in status bar)", multiple: false, required: true
30     }
31     section("on this hub...") {
32         input "theHub", "hub", multiple: false, required: true
33     }
34
35 }
36
37 def installed() {
38     log.debug "Installed ${app.label} with address '${settings.theAddr}' on hub '${settings.theHub.name}'"
39
40     initialize()
41 }
42
43 def updated() {
44     /*
45         log.debug "Updated ${app.label} with address '${settings.theAddr}' on hub '${settings.theHub.name}'"
46
47         def current = getChildDevices()
48         log.debug "children: $current"
49
50         if (app.label != current.label) {
51                 log.debug "CHANGING name from ${current.label} to ${app.label}"
52                 log.debug "label props: ${current.label.getProperties()}"
53                 current.label[0] = app.label
54         }
55         */
56 }
57
58 def initialize() {
59     def parts = theAddr.split(":")
60     def iphex = convertIPtoHex(parts[0])
61     def porthex = convertPortToHex(parts[1])
62     def dni = "$iphex:$porthex"
63     def hubNames = location.hubs*.name.findAll { it }
64     def d = addChildDevice("com.obycode", "ObyThing Music", dni, theHub.id, [label:"${app.label}", name:"ObyThing"])
65     log.trace "created ObyThing '${d.displayName}' with id $dni"
66 }
67
68 private String convertIPtoHex(ipAddress) {
69     String hex = ipAddress.tokenize( '.' ).collect {  String.format( '%02X', it.toInteger() ) }.join()
70     return hex
71
72 }
73
74 private String convertPortToHex(port) {
75     String hexport = port.toString().format( '%04X', port.toInteger() )
76     return hexport
77 }