-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDynamic_Hue_Motion_Sensor_Controller.groovy
180 lines (153 loc) · 5.75 KB
/
Dynamic_Hue_Motion_Sensor_Controller.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
metadata {
definition (name: "Hue Motion Sensor Controller", namespace: "hueKeo", author: "Joe Rosiak") {
capability "Switch"
capability "Refresh"
command "setMotionSensorName", ["string"]
attribute "motionSensorState", "string" // Custom attribute to display the sensor's current state (enabled/disabled)
}
preferences {
input name: "hueIp", type: "string", title: "Hue Bridge IP", description: "Enter the IP of your Hue Bridge", required: true
input name: "motionSensor", type: "string", title: "Motion Sensor Name", description: "Enter the Motion Sensor name", required: true
input name: "apiKey", type: "password", title: "Hue API Key", description: "Enter your Hue Bridge API Key", required: true
}
}
def installed() {
log.debug "Installing Hue Motion Sensor Controller"
initialize()
}
def updated() {
log.debug "Updating Hue Motion Sensor Controller settings"
initialize()
}
def initialize() {
log.debug "Initializing Hue Motion Sensor Controller"
refresh() // Automatically fetch the current state on initialization
}
def on() {
log.debug "Enabling the motion sensor"
enableMotionSensor()
}
def off() {
log.debug "Disabling the motion sensor"
disableMotionSensor()
}
def refresh() {
log.debug "Refreshing Hue Motion Sensor Controller"
getMotionSensorState()
}
def setMotionSensorName(String newSensorName) {
log.debug "Setting new Motion Sensor Name: ${newSensorName}"
motionSensor = newSensorName
getMotionSensorState() // Fetch the current state when changing the sensor
}
def enableMotionSensor() {
if (!hueIp || !motionSensor || !apiKey) {
log.error "Hue IP, Motion Sensor, or API key is not set. Please configure the device."
return
}
def sensorId = getMotionSensorId(motionSensor)
if (!sensorId) {
log.error "Failed to find Hue motion sensor with name: ${motionSensor}"
return
}
def url = "http://${hueIp}/api/${apiKey}/sensors/${sensorId}/config"
def body = [on: true]
try {
def params = [
uri: url,
body: body,
contentType: "application/json"
]
httpPut(params) { resp ->
if (resp.status == 200) {
log.debug "Enabled motion sensor: ${motionSensor}"
sendEvent(name: "motionSensorState", value: "enabled") // Update the state to "enabled"
} else {
log.error "Failed to enable motion sensor: ${motionSensor}. Response: ${resp.status}"
}
}
} catch (Exception e) {
log.error "Error enabling motion sensor: ${e.message}"
}
}
def disableMotionSensor() {
if (!hueIp || !motionSensor || !apiKey) {
log.error "Hue IP, Motion Sensor, or API key is not set. Please configure the device."
return
}
def sensorId = getMotionSensorId(motionSensor)
if (!sensorId) {
log.error "Failed to find Hue motion sensor with name: ${motionSensor}"
return
}
def url = "http://${hueIp}/api/${apiKey}/sensors/${sensorId}/config"
def body = [on: false]
try {
def params = [
uri: url,
body: body,
contentType: "application/json"
]
httpPut(params) { resp ->
if (resp.status == 200) {
log.debug "Disabled motion sensor: ${motionSensor}"
sendEvent(name: "motionSensorState", value: "disabled") // Update the state to "disabled"
} else {
log.error "Failed to disable motion sensor: ${motionSensor}. Response: ${resp.status}"
}
}
} catch (Exception e) {
log.error "Error disabling motion sensor: ${e.message}"
}
}
def getMotionSensorId(sensorName) {
def url = "http://${hueIp}/api/${apiKey}/sensors"
def sensorId = null
try {
httpGet([uri: url, contentType: "application/json"]) { resp ->
if (resp.status == 200) {
def sensors = resp.data
sensors.each { id, sensor ->
if (sensor.name == sensorName && sensor.type == "ZLLPresence") {
sensorId = id
}
}
} else {
log.error "Failed to retrieve sensors. Response: ${resp.status}"
}
}
} catch (Exception e) {
log.error "Error retrieving sensors: ${e.message}"
}
return sensorId
}
def getMotionSensorState() {
if (!hueIp || !motionSensor || !apiKey) {
log.error "Hue IP, Motion Sensor, or API key is not set. Please configure the device."
return
}
def sensorId = getMotionSensorId(motionSensor)
if (!sensorId) {
log.error "Failed to find Hue motion sensor with name: ${motionSensor}"
return
}
def url = "http://${hueIp}/api/${apiKey}/sensors/${sensorId}"
try {
httpGet([uri: url, contentType: "application/json"]) { resp ->
if (resp.status == 200) {
def sensorData = resp.data
def isEnabled = sensorData.config.on
if (isEnabled) {
sendEvent(name: "motionSensorState", value: "enabled") // Update the state to "enabled"
} else {
sendEvent(name: "motionSensorState", value: "disabled") // Update the state to "disabled"
}
log.debug "Motion sensor ${motionSensor} is currently ${isEnabled ? 'enabled' : 'disabled'}"
} else {
log.error "Failed to get the motion sensor state. Response: ${resp.status}"
}
}
} catch (Exception e) {
log.error "Error retrieving motion sensor state: ${e.message}"
}
}