Last active
August 29, 2015 14:13
-
-
Save mbmccormick/1cf4590329f097db3807 to your computer and use it in GitHub Desktop.
Dropcam (Switch) device type for SmartThings, enabling on/off based on Hi, Home! actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
metadata { | |
definition (name: "Dropcam (Switch)", namespace: "mbmccormick", author: "Matt McCormick") { | |
capability "Actuator" | |
capability "Image Capture" | |
capability "Switch" | |
capability "Refresh" | |
capability "Polling" | |
} | |
simulator { | |
status "image": "raw:C45F5708D89A4F3CB1A7EEEE2E0C73D900, image:C45F5708D89A4F3CB1A7EEEE2E0C73D9, result:00" | |
reply "take C45F5708D89A4F3CB1A7EEEE2E0C73D9": "raw:C45F5708D89A4F3CB1A7EEEE2E0C73D900, image:C45F5708D89A4F3CB1A7EEEE2E0C73D9, result:00" | |
} | |
tiles { | |
standardTile("camera", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) { | |
state "default", label: "", action: "", icon: "st.camera.dropcam-centered", backgroundColor: "#FFFFFF" | |
} | |
carouselTile("cameraDetails", "device.image", width: 3, height: 2) { } | |
standardTile("take", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) { | |
state "take", label: "Take", action: "Image Capture.take", icon: "st.camera.dropcam", backgroundColor: "#FFFFFF", nextState:"taking" | |
state "taking", label:'Taking', action: "", icon: "st.camera.dropcam", backgroundColor: "#53a7c0" | |
state "image", label: "Take", action: "Image Capture.take", icon: "st.camera.dropcam", backgroundColor: "#FFFFFF", nextState:"taking" | |
} | |
standardTile("switch", "device.switch", canChangeIcon: true) { | |
state "on", label: 'On', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821" | |
state "off", label: 'Off', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff" | |
} | |
standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") { | |
state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh" | |
} | |
main "camera" | |
details(["cameraDetails", "take", "switch", "refresh"]) | |
} | |
} | |
def updated() { | |
log.debug "Updating Dropcam" | |
refresh() | |
} | |
def parse(String description) { | |
log.debug "Parsing: $description" | |
} | |
def take() { | |
log.debug "Taking picture from Dropcam" | |
if (isStreamingEnabled() == false) { | |
log.debug "Unable to take picture, Dropcam is off" | |
sendEvent(name: "take", value: "take", isStateChange: false) | |
return | |
} | |
def dni = device.deviceNetworkId | |
def imageBytes = parent.takePicture(dni, null) | |
if(imageBytes) { | |
storeImage(getPictureName(), imageBytes) | |
} | |
} | |
def on() { | |
log.debug "Turning Dropcam on" | |
setProperties("streaming.enabled", true) | |
sendEvent(name: "switch", value: "on", isStateChange: true) | |
} | |
def off() { | |
log.debug "Turning Dropcam off" | |
setProperties("streaming.enabled", false) | |
sendEvent(name: "switch", value: "off", isStateChange: true) | |
} | |
def poll() { | |
refresh() | |
} | |
def refresh() { | |
log.debug "Refreshing Dropcam" | |
def items = getProperties() | |
log.debug items | |
def streamingEnabled = isStreamingEnabled() | |
if (streamingEnabled != null) { | |
if (streamingEnabled) { | |
sendEvent(name: "switch", value: "on") | |
} else { | |
sendEvent(name: "switch", value: "off") | |
} | |
log.debug "Switch is now $streamingEnabled" | |
} | |
} | |
private getPictureName() { | |
def pictureUuid = java.util.UUID.randomUUID().toString().replaceAll('-', '') | |
getCameraUuid() + "_$pictureUuid" + ".jpg" | |
} | |
private getCookieValue() { | |
parent.state.cookie | |
} | |
private getCameraUuid() { | |
device.deviceNetworkId.split(/\./)?.last() | |
} | |
private validUserAgent() { | |
parent.validUserAgent() | |
} | |
private getProperties() { | |
def content | |
httpGet( | |
[ | |
uri: "https://www.dropcam.com", | |
path: "/api/v1/dropcams.get_properties", | |
query: [uuid: getCameraUuid()], | |
headers: [Cookie: getCookieValue(), 'User-Agent': validUserAgent()], | |
requestContentType: "application/x-www-form-urlencoded" | |
], | |
{ response -> content = response.data } | |
) | |
def props = content?.items | |
if (props) { | |
return props[0] | |
} | |
return [:] | |
} | |
private setProperties(key, value) { | |
def success = false | |
def uuid = getCameraUuid() | |
httpPost( | |
[ | |
uri: "https://www.dropcam.com", | |
path: "/api/v1/dropcams.set_property", | |
headers: [Cookie: getCookieValue(), 'User-Agent': validUserAgent()], | |
requestContentType: "application/x-www-form-urlencoded", | |
body: "uuid=$uuid&key=$key&value=$value" | |
], | |
{ response -> success = (response.status == 200) } | |
) | |
return success | |
} | |
private isStreamingEnabled() | |
{ | |
def items = getProperties() | |
def streamingEnabled = items?.'streaming.enabled' | |
if (streamingEnabled == null) { | |
return false | |
} | |
if (streamingEnabled) { | |
return true | |
} else { | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment