@Someguy, I made a few adjustments to the groovy code you linked to above. The driver was generating unnecessary logs (that included the tokens in plain text). It also generated http exceptions for every on/off PUT request. I adjusted the httpPut requests to look for the correct contentType and the errors are now gone. I also added a Debug Log toggle so the excessive logging could be enabled/disabled through the driver preferences.
See my updated code below.
/**
*
* Ring Spotlight Cam Light Control
*
* Needs the username/password and the device ID of the device.
* This needs to be sourced by an API call to
* https://api.ring.com/clients_api/ring_devices?api_version=9&auth_token={{auth_token}}
*
* I used https://github.com/davglass/doorbot as a guide to build the requests.
* Author: Philip
*
*
* edited for hubitat by Stephan Hackett
* 3/16/19
* - added debug logging toggle
* - changed PUT requests for on() and off() to use 'application/html' instead of Json - prevents the exception errors that were occurring
*
*/
metadata {
definition (name: "Ring Spotlight", namespace: "stephack", author: "Philip") {
capability "Actuator"
capability "Switch"
capability "Sensor"
}
preferences {
input "username", "email", title: "Ring Username", description: "Email used to login to Ring.com", displayDuringSetup: true, required: true
input "password", "password", title: "Ring Password", description: "Password you login to Ring.com", displayDuringSetup: true, required: true
//Not sure there is a better way to do this than ask?
input "deviceid", "text", title: "Device ID", description: "The numeric value that identifies the device", displayDuringSetup: true, required: true
input "logEnable", "bool", title: "Enable Debug Logging?"
}
}
def parse(String description) {
}
def authenticate()
{
def s = "${username}:${password}"
String encodedUandP = s.bytes.encodeBase64()
def token = "EMPTY"
def params = [
uri: "https://oauth.ring.com",
path: "/oauth/token",
headers: [
"User-Agent": "iOS"
],
requestContentType: "application/json",
body: "{\"client_id\": \"ring_official_ios\",\"grant_type\": \"password\",\"password\": \"${password}\",\"scope\": \"client\",\"username\": \"${username}\"}"
]
try {
httpPost(params) { resp ->
if(logEnable) log.debug "POST response code: ${resp.status}"
if(logEnable) log.debug "response data: ${resp.data}"
token = resp.data.access_token
}
} catch (e) {
log.error "HTTP Exception Received on POST: $e"
log.error "response data: ${resp.data}"
return
}
params = [
uri: "https://api.ring.com",
path: "/clients_api/session",
headers: [
Authorization: "Bearer ${token}",
"User-Agent": "iOS"
],
requestContentType: "application/x-www-form-urlencoded",
body: "device%5Bos%5D=ios&device%5Bhardware_id%5D=a565187537a28e5cc26819e594e28213&api_version=9"
]
try {
httpPost(params) { resp ->
if(logEnable) log.debug "POST response code: ${resp.status}"
if(logEnable) log.debug "response data: ${resp.data}"
token = resp.data.profile.authentication_token
}
} catch (e) {
log.error "HTTP Exception Received on POST: $e"
log.error "response data: ${resp.data}"
return
}
if(logEnable) log.debug "Authenticated, Token Found."
return token
}
def on() {
if(logEnable) log.debug "Attempting to Switch On."
def token = authenticate()
//Send Command to Turn On
def paramsforPut = [
uri: "https://api.ring.com",
path: "/clients_api/doorbots/${deviceid}/floodlight_light_on",
query: [
api_version: "9",
"auth_token": token
],
contentType:'application/html'
]
try {
httpPut(paramsforPut) { resp ->
if(logEnable) log.debug "PUT(on) response code: ${resp.status}"
}
} catch (e) {
//ALWAYS seems to throw an exception?
//Platform bug maybe?
log.error "HTTP Exception Received on PUT: $e"
}
sendEvent(name: "switch", value: "on")
log.info "${device.label} was turned on"
}
def off() {
if(logEnable) log.debug "Attempting to Switch Off"
def token = authenticate()
//Send Command to Turn Off
def paramsforPut = [
uri: "https://api.ring.com",
path: "/clients_api/doorbots/${deviceid}/floodlight_light_off",
query: [
api_version: "9",
"auth_token": token
],
contentType:'application/html'
]
try {
httpPut(paramsforPut) { resp ->
if(logEnable) log.debug "PUT(off) response code: ${resp.status}"
}
} catch (e) {
//ALWAYS seems to throw an exception?
//Platform bug maybe?
log.error "HTTP Exception Received on PUT: $e"
}
sendEvent(name: "switch", value: "off")
log.info "${device.label} was turned off"
}