mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-14 05:11:50 +00:00
Compare commits
1 Commits
MSA-2158-1
...
MSA-2161-1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63800a7097 |
@@ -1,466 +0,0 @@
|
||||
/**
|
||||
* Copyright 2015 SmartThings
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
metadata {
|
||||
definition (name: "Hue Motion Sensor", namespace: "digitalgecko", author: "digitalgecko") {
|
||||
|
||||
|
||||
capability "Motion Sensor"
|
||||
capability "Configuration"
|
||||
capability "Battery"
|
||||
capability "Refresh"
|
||||
capability "Temperature Measurement"
|
||||
capability "Sensor"
|
||||
capability "Illuminance Measurement" //0x0400
|
||||
|
||||
fingerprint profileId: "0104", inClusters: "0000,0001,0003,0406,0400,0402", outClusters: "0019", manufacturer: "Philips", model: "SML001", deviceJoinName: "Hue Motion Sensor"
|
||||
}
|
||||
|
||||
preferences {
|
||||
section {
|
||||
input title: "Temperature Offset", description: "This feature allows you to correct any temperature variations by selecting an offset. Ex: If your sensor consistently reports a temp that's 5 degrees too warm, you'd enter '-5'. If 3 degrees too cold, enter '+3'.", displayDuringSetup: false, type: "paragraph", element: "paragraph"
|
||||
input "tempOffset", "number", title: "Degrees", description: "Adjust temperature by this many degrees", range: "*..*", displayDuringSetup: false
|
||||
}
|
||||
section {
|
||||
input title: "Luminance Offset", description: "This feature allows you to correct the luminance reading by selecting an offset. Enter a value such as 20 or -20 to adjust the luminance reading.", displayDuringSetup: false, type: "paragraph", element: "paragraph"
|
||||
input "luxOffset", "number", title: "Lux", description: "Adjust luminance by this amount", range: "*..*", displayDuringSetup: false
|
||||
}
|
||||
}
|
||||
|
||||
tiles(scale: 2) {
|
||||
multiAttributeTile(name:"motion", type: "generic", width: 6, height: 4){
|
||||
tileAttribute ("device.motion", key: "PRIMARY_CONTROL") {
|
||||
attributeState "active", label:'motion', icon:"st.motion.motion.active", backgroundColor:"#53a7c0"
|
||||
attributeState "inactive", label:'no motion', icon:"st.motion.motion.inactive", backgroundColor:"#ffffff"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
valueTile("temperature", "device.temperature", width: 2, height: 2) {
|
||||
state("temperature", label:'${currentValue}°',
|
||||
backgroundColors:[
|
||||
[value: 31, color: "#153591"],
|
||||
[value: 44, color: "#1e9cbb"],
|
||||
[value: 59, color: "#90d2a7"],
|
||||
[value: 74, color: "#44b621"],
|
||||
[value: 84, color: "#f1d801"],
|
||||
[value: 95, color: "#d04e00"],
|
||||
[value: 96, color: "#bc2323"]
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
valueTile("battery", "device.battery", decoration: "flat", inactiveLabel: false, width: 2, height: 2) {
|
||||
state "battery", label:'${currentValue}% battery', unit:""
|
||||
}
|
||||
valueTile("illuminance", "device.illuminance", width: 2, height: 2) {
|
||||
state("illuminance", label:'${currentValue}', unit:"lux",
|
||||
backgroundColors:[
|
||||
[value: 9, color: "#767676"],
|
||||
[value: 315, color: "#ffa81e"],
|
||||
[value: 1000, color: "#fbd41b"]
|
||||
]
|
||||
)
|
||||
}
|
||||
standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
|
||||
state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
|
||||
}
|
||||
standardTile("configure", "device.configure", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
|
||||
state "default", label:"configure", action:"configure"
|
||||
}
|
||||
main "motion"
|
||||
details(["motion","temperature","battery", "refresh","illuminance",'configure'])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Parse incoming device messages to generate events
|
||||
def parse(String description) {
|
||||
def msg = zigbee.parse(description)
|
||||
|
||||
//log.warn "--"
|
||||
//log.trace description
|
||||
//log.debug msg
|
||||
//def x = zigbee.parseDescriptionAsMap( description )
|
||||
//log.error x
|
||||
|
||||
Map map = [:]
|
||||
if (description?.startsWith('catchall:')) {
|
||||
map = parseCatchAllMessage(description)
|
||||
}
|
||||
else if (description?.startsWith('temperature: ')) {
|
||||
map = parseCustomMessage(description)
|
||||
}
|
||||
else if (description?.startsWith('illuminance: ')) {
|
||||
map = parseCustomMessage(description)
|
||||
}
|
||||
// else if (description?.startsWith('zone status')) {
|
||||
// //map = parseIasMessage(description)
|
||||
// log.trace "zone status"
|
||||
// }
|
||||
|
||||
def result = map ? createEvent(map) : null
|
||||
|
||||
if (description?.startsWith('enroll request')) {
|
||||
List cmds = enrollResponse()
|
||||
result = cmds?.collect { new physicalgraph.device.HubAction(it) }
|
||||
}
|
||||
else if (description?.startsWith('read attr -')) {
|
||||
result = parseReportAttributeMessage(description).each { createEvent(it) }
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/*
|
||||
Refresh Function
|
||||
*/
|
||||
def refresh() {
|
||||
log.debug "Refreshing Values"
|
||||
|
||||
def refreshCmds = []
|
||||
refreshCmds +=zigbee.readAttribute(0x0001, 0x0020) // Read battery?
|
||||
refreshCmds += zigbee.readAttribute(0x0402, 0x0000) // Read temp?
|
||||
refreshCmds += zigbee.readAttribute(0x0400, 0x0000) // Read luminance?
|
||||
refreshCmds += zigbee.readAttribute(0x0406, 0x0000) // Read motion?
|
||||
|
||||
return refreshCmds + enrollResponse()
|
||||
|
||||
}
|
||||
/*
|
||||
Configure Function
|
||||
*/
|
||||
def configure() {
|
||||
|
||||
// TODO : device watch?
|
||||
|
||||
String zigbeeId = swapEndianHex(device.hub.zigbeeId)
|
||||
log.debug "Confuguring Reporting and Bindings."
|
||||
|
||||
|
||||
def configCmds = []
|
||||
configCmds += zigbee.batteryConfig()
|
||||
configCmds += zigbee.temperatureConfig(60, 600) // Set temp reporting times // Confirmed
|
||||
|
||||
configCmds += zigbee.configureReporting(0x406,0x0000, 0x18, 30, 600, null) // motion // confirmed
|
||||
|
||||
|
||||
// Data type is not 0x20 = 0x8D invalid data type Unsigned 8-bit integer
|
||||
|
||||
configCmds += zigbee.configureReporting(0x400,0x0000, 0x21, 60, 600, 0x20) // Set luminance reporting times?? maybe
|
||||
return refresh() + configCmds
|
||||
}
|
||||
|
||||
/*
|
||||
getMotionResult
|
||||
*/
|
||||
|
||||
private Map getMotionResult(value) {
|
||||
//log.trace "Motion : " + value
|
||||
|
||||
def descriptionText = value == "01" ? '{{ device.displayName }} detected motion':
|
||||
'{{ device.displayName }} stopped detecting motion'
|
||||
|
||||
return [
|
||||
name: 'motion',
|
||||
value: value == "01" ? "active" : "inactive",
|
||||
descriptionText: descriptionText,
|
||||
translatable: true,
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
getTemperatureResult
|
||||
*/
|
||||
private Map getTemperatureResult(value) {
|
||||
|
||||
//log.trace "Temperature : " + value
|
||||
if (tempOffset) {
|
||||
def offset = tempOffset as int
|
||||
def v = value as int
|
||||
value = v + offset
|
||||
}
|
||||
def descriptionText = temperatureScale == 'C' ? '{{ device.displayName }} was {{ value }}°C':
|
||||
'{{ device.displayName }} was {{ value }}°F'
|
||||
|
||||
return [
|
||||
name: 'temperature',
|
||||
value: value,
|
||||
descriptionText: descriptionText,
|
||||
translatable: true,
|
||||
unit: temperatureScale
|
||||
]
|
||||
}
|
||||
|
||||
def getTemperature(value) {
|
||||
def celsius = Integer.parseInt(value, 16).shortValue() / 100
|
||||
if(getTemperatureScale() == "C"){
|
||||
return Math.round(celsius)
|
||||
} else {
|
||||
return Math.round(celsiusToFahrenheit(celsius))
|
||||
}
|
||||
}
|
||||
|
||||
private Map getLuminanceResult(rawValue) {
|
||||
log.debug "Luminance rawValue = ${rawValue}"
|
||||
|
||||
if (luxOffset) {
|
||||
def offset = luxOffset as int
|
||||
def v = rawValue as int
|
||||
rawValue = v + offset
|
||||
}
|
||||
|
||||
def result = [
|
||||
name: 'illuminance',
|
||||
value: '--',
|
||||
translatable: true,
|
||||
unit: 'lux'
|
||||
]
|
||||
|
||||
result.value = rawValue as Integer
|
||||
return result
|
||||
}
|
||||
|
||||
/*
|
||||
getBatteryResult
|
||||
*/
|
||||
//TODO: needs calibration
|
||||
private Map getBatteryResult(rawValue) {
|
||||
//log.debug "Battery rawValue = ${rawValue}"
|
||||
|
||||
def result = [
|
||||
name: 'battery',
|
||||
value: '--',
|
||||
translatable: true
|
||||
]
|
||||
|
||||
def volts = rawValue / 10
|
||||
|
||||
if (rawValue == 0 || rawValue == 255) {}
|
||||
else {
|
||||
if (volts > 3.5) {
|
||||
result.descriptionText = "{{ device.displayName }} battery has too much power: (> 3.5) volts."
|
||||
}
|
||||
else {
|
||||
if (device.getDataValue("manufacturer") == "SmartThings") {
|
||||
volts = rawValue // For the batteryMap to work the key needs to be an int
|
||||
def batteryMap = [28:100, 27:100, 26:100, 25:90, 24:90, 23:70,
|
||||
22:70, 21:50, 20:50, 19:30, 18:30, 17:15, 16:1, 15:0]
|
||||
def minVolts = 15
|
||||
def maxVolts = 28
|
||||
|
||||
if (volts < minVolts)
|
||||
volts = minVolts
|
||||
else if (volts > maxVolts)
|
||||
volts = maxVolts
|
||||
def pct = batteryMap[volts]
|
||||
if (pct != null) {
|
||||
result.value = pct
|
||||
result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
|
||||
}
|
||||
}
|
||||
else {
|
||||
def minVolts = 2.1
|
||||
def maxVolts = 3.0
|
||||
def pct = (volts - minVolts) / (maxVolts - minVolts)
|
||||
def roundedPct = Math.round(pct * 100)
|
||||
if (roundedPct <= 0)
|
||||
roundedPct = 1
|
||||
result.value = Math.min(100, roundedPct)
|
||||
result.descriptionText = "{{ device.displayName }} battery was {{ value }}%"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
/*
|
||||
parseCustomMessage
|
||||
*/
|
||||
private Map parseCustomMessage(String description) {
|
||||
Map resultMap = [:]
|
||||
if (description?.startsWith('temperature: ')) {
|
||||
def value = zigbee.parseHATemperatureValue(description, "temperature: ", getTemperatureScale())
|
||||
resultMap = getTemperatureResult(value)
|
||||
}
|
||||
|
||||
if (description?.startsWith('illuminance: ')) {
|
||||
log.warn "value: " + description.split(": ")[1]
|
||||
log.warn "proc: " + value
|
||||
|
||||
def value = zigbee.lux( description.split(": ")[1] as Integer ) //zigbee.parseHAIlluminanceValue(description, "illuminance: ", getTemperatureScale())
|
||||
resultMap = getLuminanceResult(value)
|
||||
}
|
||||
return resultMap
|
||||
}
|
||||
|
||||
/*
|
||||
parseReportAttributeMessage
|
||||
*/
|
||||
private List parseReportAttributeMessage(String description) {
|
||||
Map descMap = (description - "read attr - ").split(",").inject([:]) { map, param ->
|
||||
def nameAndValue = param.split(":")
|
||||
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
|
||||
}
|
||||
|
||||
List result = []
|
||||
|
||||
// Temperature
|
||||
if (descMap.cluster == "0402" && descMap.attrId == "0000") {
|
||||
def value = getTemperature(descMap.value)
|
||||
result << getTemperatureResult(value)
|
||||
}
|
||||
|
||||
// Motion
|
||||
else if (descMap.cluster == "0406" && descMap.attrId == "0000") {
|
||||
result << getMotionResult(descMap.value)
|
||||
}
|
||||
|
||||
// Battery
|
||||
else if (descMap.cluster == "0001" && descMap.attrId == "0020") {
|
||||
result << getBatteryResult(Integer.parseInt(descMap.value, 16))
|
||||
}
|
||||
|
||||
// Luminance
|
||||
else if (descMap.cluster == "0402" ) { //&& descMap.attrId == "0020") {
|
||||
log.error "Luminance Response " + description
|
||||
//result << getBatteryResult(Integer.parseInt(descMap.value, 16))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
parseCatchAllMessage
|
||||
*/
|
||||
private Map parseCatchAllMessage(String description) {
|
||||
Map resultMap = [:]
|
||||
def cluster = zigbee.parse(description)
|
||||
// log.debug cluster
|
||||
if (shouldProcessMessage(cluster)) {
|
||||
switch(cluster.clusterId) {
|
||||
case 0x0001:
|
||||
// 0x07 - configure reporting
|
||||
if (cluster.command != 0x07) {
|
||||
resultMap = getBatteryResult(cluster.data.last())
|
||||
}
|
||||
break
|
||||
|
||||
case 0x0400:
|
||||
if (cluster.command == 0x07) { // Ignore Configure Reporting Response
|
||||
if(cluster.data[0] == 0x00) {
|
||||
log.trace "Luminance Reporting Configured"
|
||||
sendEvent(name: "checkInterval", value: 60 * 12, displayed: false, data: [protocol: "zigbee", hubHardwareId: device.hub.hardwareID])
|
||||
}
|
||||
else {
|
||||
log.warn "Luminance REPORTING CONFIG FAILED- error code:${cluster.data[0]}"
|
||||
}
|
||||
}
|
||||
else {
|
||||
log.debug "catchall : luminance" + cluster
|
||||
resultMap = getLuminanceResult(cluster.data.last());
|
||||
}
|
||||
|
||||
break
|
||||
|
||||
|
||||
|
||||
case 0x0402:
|
||||
if (cluster.command == 0x07) {
|
||||
if(cluster.data[0] == 0x00) {
|
||||
log.trace "Temperature Reporting Configured"
|
||||
sendEvent(name: "checkInterval", value: 60 * 12, displayed: false, data: [protocol: "zigbee", hubHardwareId: device.hub.hardwareID])
|
||||
}
|
||||
else {
|
||||
log.warn "TEMP REPORTING CONFIG FAILED- error code:${cluster.data[0]}"
|
||||
}
|
||||
}
|
||||
else {
|
||||
// temp is last 2 data values. reverse to swap endian
|
||||
String temp = cluster.data[-2..-1].reverse().collect { cluster.hex1(it) }.join()
|
||||
def value = getTemperature(temp)
|
||||
resultMap = getTemperatureResult(value)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return resultMap
|
||||
}
|
||||
|
||||
private boolean shouldProcessMessage(cluster) {
|
||||
// 0x0B is default response indicating message got through
|
||||
boolean ignoredMessage = cluster.profileId != 0x0104 ||
|
||||
cluster.command == 0x0B ||
|
||||
(cluster.data.size() > 0 && cluster.data.first() == 0x3e)
|
||||
return !ignoredMessage
|
||||
}
|
||||
|
||||
|
||||
// This seems to be IAS Specific and not needed we are not really a motion sensor
|
||||
def enrollResponse() {
|
||||
// log.debug "Sending enroll response"
|
||||
// String zigbeeEui = swapEndianHex(device.hub.zigbeeEui)
|
||||
// [
|
||||
// //Resending the CIE in case the enroll request is sent before CIE is written
|
||||
// "zcl global write 0x500 0x10 0xf0 {${zigbeeEui}}", "delay 200",
|
||||
// "send 0x${device.deviceNetworkId} 1 ${endpointId}", "delay 500",
|
||||
// //Enroll Response
|
||||
// "raw 0x500 {01 23 00 00 00}", "delay 200",
|
||||
// "send 0x${device.deviceNetworkId} 1 1", "delay 200"
|
||||
// ]
|
||||
}
|
||||
|
||||
def configureHealthCheck() {
|
||||
Integer hcIntervalMinutes = 12
|
||||
refresh()
|
||||
sendEvent(name: "checkInterval", value: hcIntervalMinutes * 60, displayed: false, data: [protocol: "zigbee", hubHardwareId: device.hub.hardwareID])
|
||||
}
|
||||
|
||||
def updated() {
|
||||
log.debug "in updated()"
|
||||
configureHealthCheck()
|
||||
}
|
||||
|
||||
def ping() {
|
||||
return zigbee.onOffRefresh()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private getEndpointId() {
|
||||
new BigInteger(device.endpointId, 16).toString()
|
||||
}
|
||||
|
||||
private String swapEndianHex(String hex) {
|
||||
reverseArray(hex.decodeHex()).encodeHex()
|
||||
}
|
||||
|
||||
private byte[] reverseArray(byte[] array) {
|
||||
int i = 0;
|
||||
int j = array.length - 1;
|
||||
byte tmp;
|
||||
while (j > i) {
|
||||
tmp = array[j];
|
||||
array[j] = array[i];
|
||||
array[i] = tmp;
|
||||
j--;
|
||||
i++;
|
||||
}
|
||||
return array
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
/**
|
||||
* iHomeSmartPlug iSP5
|
||||
*
|
||||
* Copyright 2016 EVRYTHNG LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
* Last reviewed:20.07.2017
|
||||
* - Added capabilities: Health Check, Outlet, Light
|
||||
* - Added ocfDeviceType
|
||||
* - Changed background colour of tiles
|
||||
* - Added lifecycle functions
|
||||
* - Added ping method
|
||||
*/
|
||||
import groovy.json.JsonOutput
|
||||
|
||||
metadata {
|
||||
definition (name: "iHomeSmartPlug-iSP5", namespace: "ihome_devices", author: "iHome", ocfDeviceType: "oic.d.smartplug") {
|
||||
capability "Actuator" //The device is an actuator (provides actions)
|
||||
capability "Sensor" //The device s a sensor (provides properties)
|
||||
capability "Refresh" //Enable the refresh by the user
|
||||
|
||||
capability "Switch" //Device complies to the SmartThings switch capability
|
||||
|
||||
capability "Health Check"
|
||||
capability "Outlet" //Needed for Google Home
|
||||
capability "Light" //Needed for Google Home
|
||||
|
||||
attribute "firmware","string" //Mapping the custom property firmware
|
||||
attribute "model","string" //Mapping the custom property model (model of the plug)
|
||||
attribute "status", "string" //Mapping the status of the last call to the cloud in a message to the user
|
||||
}
|
||||
|
||||
tiles(scale: 2){
|
||||
|
||||
multiAttributeTile(name:"control", type:"generic", width:6, height:4) {
|
||||
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
|
||||
attributeState( "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff", nextState: "off")
|
||||
attributeState( "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#00A0DC", nextState: "on")
|
||||
}
|
||||
tileAttribute ("device.status", key: "SECONDARY_CONTROL") {
|
||||
attributeState "status", label:'${currentValue}'
|
||||
}
|
||||
}
|
||||
|
||||
standardTile("refresh", "device.refresh", width:2, height:2, inactiveLabel: false, decoration: "flat") {
|
||||
state ("default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh")
|
||||
}
|
||||
valueTile("firmware", "device.firmware", width:2, height:2, decoration: "flat") {
|
||||
state "firmware", label:'Firmware v${currentValue}'
|
||||
}
|
||||
valueTile("model", "device.model", width:2, height:2, decoration: "flat") {
|
||||
state "model", label:'${currentValue}'
|
||||
}
|
||||
main (["control"])
|
||||
details (["control","refresh","firmware","model"])
|
||||
}
|
||||
}
|
||||
|
||||
def initialize() {
|
||||
sendEvent(name: "DeviceWatch-Enroll", value: JsonOutput.toJson([protocol: "cloud", scheme:"untracked"]), displayed: false)
|
||||
}
|
||||
|
||||
def installed() {
|
||||
log.debug "installed()"
|
||||
initialize()
|
||||
}
|
||||
|
||||
def updated() {
|
||||
log.debug "updated()"
|
||||
initialize()
|
||||
}
|
||||
|
||||
/*
|
||||
* This method creates the internal SmartThings events to handle changes in the properties of the plug
|
||||
*/
|
||||
def updateProperties(Map properties) {
|
||||
|
||||
log.debug "Updating plug's properties: ${properties}"
|
||||
|
||||
def connected = (properties["~connected"]?.value == true)
|
||||
if (connected == true){ //only update if plug is connected
|
||||
|
||||
//update status message
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
log.info "Updating ${device.displayName}: property status set to: ${parent.getConnectedMessage()}"
|
||||
|
||||
//update currentpowerstate1
|
||||
def currentpowerstate1 = properties["currentpowerstate1"].value
|
||||
if (currentpowerstate1 != null){
|
||||
log.info "Updating ${device.displayName}: property currentpowerstate1 set to value: ${currentpowerstate1}"
|
||||
currentpowerstate1 = "${currentpowerstate1}"
|
||||
if (currentpowerstate1 == "1") {
|
||||
sendEvent(name: "switch", value: "on")
|
||||
}
|
||||
else if (currentpowerstate1 == "0") {
|
||||
sendEvent(name: "switch", value: "off")
|
||||
}
|
||||
}
|
||||
|
||||
//update firmware version
|
||||
def appfwversion = properties["appfwversion"].value
|
||||
if (appfwversion != null){
|
||||
log.info "Updating ${device.displayName}: property appfwversion set to value: ${appfwversion}"
|
||||
appfwversion = "${appfwversion}"
|
||||
sendEvent(name: "firmware", value: appfwversion)
|
||||
}
|
||||
|
||||
//update model
|
||||
log.info "Updating ${device.displayName}: property model set to value: iSP5"
|
||||
sendEvent(name:"model", value:"iSP5")
|
||||
|
||||
} else { //the plug is not connected
|
||||
|
||||
//update status message
|
||||
sendEvent(name: "status", value: parent.getPlugNotConnectedMessage())
|
||||
log.info "Updating ${device.displayName}: property status set to: ${parent.getPlugNotConnectedMessage()}"
|
||||
}
|
||||
}
|
||||
|
||||
// Process the polling error, changing the status message
|
||||
def pollError(){
|
||||
log.info "Error retrieving info from the cloud"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the switch.on function by updating the corresponding property in the cloud
|
||||
*/
|
||||
def on() {
|
||||
|
||||
//update the status of the plug before attempting to change it
|
||||
refresh()
|
||||
|
||||
if (device.currentState("status")?.value == parent.getConnectedMessage()) {//only update if the plug is connected
|
||||
//Turn on if the device is off
|
||||
if (device.currentState("switch")?.value.toLowerCase().startsWith("off")){
|
||||
log.info "Updating ${device.displayName} in the cloud: property targetpowerstate1 set to value: 1"
|
||||
|
||||
def propertyUpdateJSON = "[{\"key\":\"targetpowerstate1\", \"value\":\"1\"}]"
|
||||
def success = parent.propertyUpdate(device.deviceNetworkId, propertyUpdateJSON)
|
||||
|
||||
if(success){
|
||||
log.info "Updating ${device.displayName}: sending switch.on command"
|
||||
sendEvent(name: "switch", value: "on")
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
} else {
|
||||
log.info "Cloud property update error, skipping event"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the switch.off function by updating the corresponding property in the cloud
|
||||
*/
|
||||
def off() {
|
||||
//update the status of the plug before attempting to change it
|
||||
refresh()
|
||||
|
||||
if (device.currentState("status")?.value == parent.getConnectedMessage()) {//only update if the plug is connected
|
||||
//Turn off if the device is on
|
||||
if (device.currentState("switch")?.value.toLowerCase().startsWith("on")){
|
||||
log.info "Updating ${device.displayName} in the cloud: property targetpowerstate1 set to value: 0"
|
||||
def propertyUpdateJSON = "[{\"key\":\"targetpowerstate1\", \"value\":\"0\"}]"
|
||||
def success = parent.propertyUpdate(device.deviceNetworkId, propertyUpdateJSON)
|
||||
|
||||
if (success){
|
||||
log.info "Updating ${device.displayName}: sending switch.off command"
|
||||
sendEvent(name: "switch", value: "off")
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
} else {
|
||||
log.info "Cloud property update error, skipping event"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the refresh capability
|
||||
*/
|
||||
def refresh() {
|
||||
parent.pollChildren(device.deviceNetworkId)
|
||||
}
|
||||
|
||||
def ping() {
|
||||
refresh()
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* iHomeSmartPlug iSP6
|
||||
*
|
||||
* Copyright 2016 EVRYTHNG LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
* Last reviewed:20.07.2017
|
||||
* - Added capabilities: Health Check, Outlet, Light
|
||||
* - Added ocfDeviceType
|
||||
* - Changed background colour of tiles
|
||||
* - Added lifecycle functions
|
||||
* - Added ping method
|
||||
*/
|
||||
import groovy.json.JsonOutput
|
||||
|
||||
metadata {
|
||||
definition (name: "iHomeSmartPlug-iSP6", namespace: "ihome_devices", author: "iHome", ocfDeviceType: "oic.d.smartplug") {
|
||||
capability "Actuator" //The device is an actuator (provides actions)
|
||||
capability "Sensor" //The device s a sensor (provides properties)
|
||||
capability "Refresh" //Enable the refresh by the user
|
||||
|
||||
capability "Switch" //Device complies to the SmartThings switch capability
|
||||
|
||||
capability "Health Check"
|
||||
capability "Outlet" //Needed for Google Home
|
||||
capability "Light" //Needed for Google Home
|
||||
|
||||
attribute "firmware","string" //Mapping the custom property firmware
|
||||
attribute "model","string" //Mapping the custom property model (model of the plug)
|
||||
attribute "status", "string" //Mapping the status of the last call to the cloud
|
||||
|
||||
}
|
||||
|
||||
tiles(scale: 2){
|
||||
|
||||
multiAttributeTile(name:"control", type:"generic", width:6, height:4) {
|
||||
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
|
||||
attributeState( "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff", nextState: "off")
|
||||
attributeState( "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#00A0DC", nextState: "on")
|
||||
}
|
||||
tileAttribute ("device.status", key: "SECONDARY_CONTROL") {
|
||||
attributeState "status", label:'${currentValue}'
|
||||
}
|
||||
}
|
||||
|
||||
standardTile("refresh", "device.refresh", width:2, height:2, inactiveLabel: false, decoration: "flat") {
|
||||
state ("default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh")
|
||||
}
|
||||
valueTile("firmware", "device.firmware", width:2, height:2, decoration: "flat") {
|
||||
state "firmware", label:'Firmware v${currentValue}'
|
||||
}
|
||||
valueTile("model", "device.model", width:2, height:2, decoration: "flat") {
|
||||
state "model", label:'${currentValue}'
|
||||
}
|
||||
main (["control"])
|
||||
details (["control","refresh","firmware","model"])
|
||||
}
|
||||
}
|
||||
|
||||
def initialize() {
|
||||
sendEvent(name: "DeviceWatch-Enroll", value: JsonOutput.toJson([protocol: "cloud", scheme:"untracked"]), displayed: false)
|
||||
}
|
||||
|
||||
def installed() {
|
||||
log.debug "installed()"
|
||||
initialize()
|
||||
}
|
||||
|
||||
def updated() {
|
||||
log.debug "updated()"
|
||||
initialize()
|
||||
}
|
||||
|
||||
/*
|
||||
* This method creates the internal SmartThings events to handle changes in the properties of the plug
|
||||
*/
|
||||
def updateProperties(Map properties) {
|
||||
|
||||
log.debug "Updating plug's properties: ${properties}"
|
||||
|
||||
def connected = (properties["~connected"]?.value == true)
|
||||
|
||||
if (connected == true){ //only update if plug is connected
|
||||
|
||||
//update status message
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
log.info "Updating ${device.displayName}: property status set to: ${parent.getConnectedMessage()}"
|
||||
|
||||
//update currentpowerstate1
|
||||
def currentpowerstate1 = properties["currentpowerstate1"].value
|
||||
if (currentpowerstate1 != null){
|
||||
log.info "Updating ${device.displayName}: property currentpowerstate1 set to value: ${currentpowerstate1}"
|
||||
currentpowerstate1 = "${currentpowerstate1}"
|
||||
if (currentpowerstate1 == "1") {
|
||||
sendEvent(name: "switch", value: "on")
|
||||
}
|
||||
else if (currentpowerstate1 == "0") {
|
||||
sendEvent(name: "switch", value: "off")
|
||||
}
|
||||
}
|
||||
|
||||
//update firmware version
|
||||
def appfwversion = properties["appfwversion"].value
|
||||
if (appfwversion != null){
|
||||
log.info "Updating ${device.displayName}: property appfwversion set to value: ${appfwversion}"
|
||||
appfwversion = "${appfwversion}"
|
||||
sendEvent(name: "firmware", value: appfwversion)
|
||||
}
|
||||
|
||||
//update model
|
||||
log.info "Updating ${device.displayName}: property model set to value: iSP6"
|
||||
sendEvent(name:"model", value:"iSP6")
|
||||
|
||||
} else { //the plug is not connected
|
||||
//update status message
|
||||
sendEvent(name: "status", value: parent.getPlugNotConnectedMessage())
|
||||
log.info "Updating ${device.displayName}: property status set to: ${parent.getPlugNotConnectedMessage()}"
|
||||
}
|
||||
}
|
||||
|
||||
// Process the polling error, changing the status message
|
||||
def pollError(){
|
||||
log.info "Error retrieving info from the cloud"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the switch.on function by updating the corresponding property in the cloud
|
||||
*/
|
||||
def on() {
|
||||
|
||||
//update the status of the plug before attempting to change it
|
||||
refresh()
|
||||
|
||||
if (device.currentState("status")?.value == parent.getConnectedMessage()) {//only update if the plug is connected
|
||||
//Turn on if the device is off
|
||||
if (device.currentState("switch")?.value.toLowerCase().startsWith("off")){
|
||||
log.info "Updating ${device.displayName} in the cloud: sending action _turnOn"
|
||||
|
||||
def actionJSON = "{\"thng\":\"${device.deviceNetworkId}\", \"type\":\"_turnOn\"}"
|
||||
def success = parent.sendAction("_turnOn", actionJSON)
|
||||
|
||||
if(success){
|
||||
log.info "Updating ${device.displayName}: sending switch.on command"
|
||||
sendEvent(name: "switch", value: "on")
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
} else {
|
||||
log.info "Cloud property update error, skipping event"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the switch.off function by updating the corresponding property in the cloud
|
||||
*/
|
||||
def off() {
|
||||
|
||||
//update the status of the plug before attempting to change it
|
||||
refresh()
|
||||
|
||||
if (device.currentState("status")?.value == parent.getConnectedMessage()) {//only update if the plug is connected
|
||||
|
||||
//Turn off only if the device is on
|
||||
if (device.currentState("switch")?.value.toLowerCase().startsWith("on")){
|
||||
log.info "Updating ${device.displayName} in the cloud: sending action _turnOff"
|
||||
|
||||
def actionJSON = "{\"thng\":\"${device.deviceNetworkId}\", \"type\":\"_turnOff\"}"
|
||||
def success = parent.sendAction("_turnOff", actionJSON)
|
||||
|
||||
if (success) {
|
||||
log.info "Updating ${device.displayName}: sending switch.off command"
|
||||
sendEvent(name: "switch", value: "off")
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
} else {
|
||||
log.info "Cloud property update error, skipping event"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the refresh capability
|
||||
*/
|
||||
def refresh() {
|
||||
parent.pollChildren(device.deviceNetworkId)
|
||||
}
|
||||
|
||||
def ping() {
|
||||
refresh()
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* iHomeSmartPlug iSP6X
|
||||
*
|
||||
* Copyright 2016 EVRYTHNG LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
* Last reviewed:20.07.2017
|
||||
* - Added capabilities: Health Check, Outlet, Light
|
||||
* - Added ocfDeviceType
|
||||
* - Changed background colour of tiles
|
||||
* - Added lifecycle functions
|
||||
* - Added ping method
|
||||
*/
|
||||
import groovy.json.JsonOutput
|
||||
|
||||
metadata {
|
||||
definition (name: "iHomeSmartPlug-iSP6X", namespace: "ihome_devices", author: "iHome", ocfDeviceType: "oic.d.smartplug") {
|
||||
capability "Actuator" //The device is an actuator (provides actions)
|
||||
capability "Sensor" //The device s a sensor (provides properties)
|
||||
capability "Refresh" //Enable the refresh by the user
|
||||
|
||||
capability "Switch" //Device complies to the SmartThings switch capability
|
||||
|
||||
capability "Health Check"
|
||||
capability "Outlet" //Needed for Google Home
|
||||
capability "Light" //Needed for Google Home
|
||||
|
||||
attribute "firmware","string" //Mapping the custom property firmware
|
||||
attribute "model","string" //Mapping the custom property model (model of the plug)
|
||||
attribute "status", "string" //Mapping the status of the last call to the cloud
|
||||
|
||||
}
|
||||
|
||||
tiles(scale: 2){
|
||||
|
||||
multiAttributeTile(name:"control", type:"generic", width:6, height:4) {
|
||||
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
|
||||
attributeState( "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff", nextState: "off")
|
||||
attributeState( "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#00A0DC", nextState: "on")
|
||||
}
|
||||
tileAttribute ("device.status", key: "SECONDARY_CONTROL") {
|
||||
attributeState "status", label:'${currentValue}'
|
||||
}
|
||||
}
|
||||
|
||||
standardTile("refresh", "device.refresh", width:2, height:2, inactiveLabel: false, decoration: "flat") {
|
||||
state ("default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh")
|
||||
}
|
||||
valueTile("firmware", "device.firmware", width:2, height:2, decoration: "flat") {
|
||||
state "firmware", label:'Firmware v${currentValue}'
|
||||
}
|
||||
valueTile("model", "device.model", width:2, height:2, decoration: "flat") {
|
||||
state "model", label:'${currentValue}'
|
||||
}
|
||||
main (["control"])
|
||||
details (["control","refresh","firmware","model"])
|
||||
}
|
||||
}
|
||||
|
||||
def initialize() {
|
||||
sendEvent(name: "DeviceWatch-Enroll", value: JsonOutput.toJson([protocol: "cloud", scheme:"untracked"]), displayed: false)
|
||||
}
|
||||
|
||||
def installed() {
|
||||
log.debug "installed()"
|
||||
initialize()
|
||||
}
|
||||
|
||||
def updated() {
|
||||
log.debug "updated()"
|
||||
initialize()
|
||||
}
|
||||
|
||||
/*
|
||||
* This method creates the internal SmartThings events to handle changes in the properties of the plug
|
||||
*/
|
||||
def updateProperties(Map properties) {
|
||||
|
||||
log.debug "Updating plug's properties: ${properties}"
|
||||
|
||||
def connected = (properties["~connected"]?.value == true)
|
||||
|
||||
if (connected == true){ //only update if plug is connected
|
||||
|
||||
//update status message
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
log.info "Updating ${device.displayName}: property status set to: ${parent.getConnectedMessage()}"
|
||||
|
||||
//update currentpowerstate1
|
||||
def currentpowerstate1 = properties["currentpowerstate1"].value
|
||||
if (currentpowerstate1 != null){
|
||||
log.info "Updating ${device.displayName}: property currentpowerstate1 set to value: ${currentpowerstate1}"
|
||||
currentpowerstate1 = "${currentpowerstate1}"
|
||||
if (currentpowerstate1 == "1") {
|
||||
sendEvent(name: "switch", value: "on")
|
||||
}
|
||||
else if (currentpowerstate1 == "0") {
|
||||
sendEvent(name: "switch", value: "off")
|
||||
}
|
||||
}
|
||||
|
||||
//update firmware version
|
||||
def appfwversion = properties["appfwversion"].value
|
||||
if (appfwversion != null){
|
||||
log.info "Updating ${device.displayName}: property appfwversion set to value: ${appfwversion}"
|
||||
appfwversion = "${appfwversion}"
|
||||
sendEvent(name: "firmware", value: appfwversion)
|
||||
}
|
||||
|
||||
//update model
|
||||
log.info "Updating ${device.displayName}: property model set to value: iSP6X"
|
||||
sendEvent(name:"model", value:"iSP6X")
|
||||
|
||||
} else { //the plug is not connected
|
||||
//update status message
|
||||
sendEvent(name: "status", value: parent.getPlugNotConnectedMessage())
|
||||
log.info "Updating ${device.displayName}: property status set to: ${parent.getPlugNotConnectedMessage()}"
|
||||
}
|
||||
}
|
||||
|
||||
// Process the polling error, changing the status message
|
||||
def pollError(){
|
||||
log.info "Error retrieving info from the cloud"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the switch.on function by updating the corresponding property in the cloud
|
||||
*/
|
||||
def on() {
|
||||
|
||||
//update the status of the plug before attempting to change it
|
||||
refresh()
|
||||
|
||||
if (device.currentState("status")?.value == parent.getConnectedMessage()) {//only update if the plug is connected
|
||||
//Turn on if the device is off
|
||||
if (device.currentState("switch")?.value.toLowerCase().startsWith("off")){
|
||||
log.info "Updating ${device.displayName} in the cloud: sending action _turnOn"
|
||||
|
||||
def actionJSON = "{\"thng\":\"${device.deviceNetworkId}\", \"type\":\"_turnOn\"}"
|
||||
def success = parent.sendAction("_turnOn", actionJSON)
|
||||
|
||||
if(success){
|
||||
log.info "Updating ${device.displayName}: sending switch.on command"
|
||||
sendEvent(name: "switch", value: "on")
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
} else {
|
||||
log.info "Cloud property update error, skipping event"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the switch.off function by updating the corresponding property in the cloud
|
||||
*/
|
||||
def off() {
|
||||
|
||||
//update the status of the plug before attempting to change it
|
||||
refresh()
|
||||
|
||||
if (device.currentState("status")?.value == parent.getConnectedMessage()) {//only update if the plug is connected
|
||||
|
||||
//Turn off only if the device is on
|
||||
if (device.currentState("switch")?.value.toLowerCase().startsWith("on")){
|
||||
log.info "Updating ${device.displayName} in the cloud: sending action _turnOff"
|
||||
|
||||
def actionJSON = "{\"thng\":\"${device.deviceNetworkId}\", \"type\":\"_turnOff\"}"
|
||||
def success = parent.sendAction("_turnOff", actionJSON)
|
||||
|
||||
if (success) {
|
||||
log.info "Updating ${device.displayName}: sending switch.off command"
|
||||
sendEvent(name: "switch", value: "off")
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
} else {
|
||||
log.info "Cloud property update error, skipping event"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the refresh capability
|
||||
*/
|
||||
def refresh() {
|
||||
parent.pollChildren(device.deviceNetworkId)
|
||||
}
|
||||
|
||||
def ping() {
|
||||
refresh()
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* iHomeSmartPlug iSP8
|
||||
*
|
||||
* Copyright 2016 EVRYTHNG LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
* Last reviewed:20.07.2017
|
||||
* - Added capabilities: Health Check, Outlet, Light
|
||||
* - Added ocfDeviceType
|
||||
* - Changed background colour of tiles
|
||||
* - Added lifecycle functions
|
||||
* - Added ping method
|
||||
*/
|
||||
import groovy.json.JsonOutput
|
||||
|
||||
metadata {
|
||||
definition (name: "iHomeSmartPlug-iSP8", namespace: "ihome_devices", author: "iHome", ocfDeviceType: "oic.d.smartplug") {
|
||||
capability "Actuator" //The device is an actuator (provides actions)
|
||||
capability "Sensor" //The device s a sensor (provides properties)
|
||||
capability "Refresh" //Enable the refresh by the user
|
||||
|
||||
capability "Switch" //Device complies to the SmartThings switch capability
|
||||
|
||||
capability "Health Check"
|
||||
capability "Outlet" //Needed for Google Home
|
||||
capability "Light" //Needed for Google Home
|
||||
|
||||
attribute "firmware","string" //Mapping the custom property firmware
|
||||
attribute "model","string" //Mapping the custom property model (model of the plug)
|
||||
attribute "status", "string" //Mapping the status of the last call to the cloud
|
||||
|
||||
}
|
||||
|
||||
tiles(scale: 2){
|
||||
|
||||
multiAttributeTile(name:"control", type:"generic", width:6, height:4) {
|
||||
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
|
||||
attributeState( "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff", nextState: "off")
|
||||
attributeState( "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#00A0DC", nextState: "on")
|
||||
}
|
||||
tileAttribute ("device.status", key: "SECONDARY_CONTROL") {
|
||||
attributeState "status", label:'${currentValue}'
|
||||
}
|
||||
}
|
||||
|
||||
standardTile("refresh", "device.refresh", width:2, height:2, inactiveLabel: false, decoration: "flat") {
|
||||
state ("default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh")
|
||||
}
|
||||
valueTile("firmware", "device.firmware", width:2, height:2, decoration: "flat") {
|
||||
state "firmware", label:'Firmware v${currentValue}'
|
||||
}
|
||||
valueTile("model", "device.model", width:2, height:2, decoration: "flat") {
|
||||
state "model", label:'${currentValue}'
|
||||
}
|
||||
main (["control"])
|
||||
details (["control","refresh","firmware","model"])
|
||||
}
|
||||
}
|
||||
|
||||
def initialize() {
|
||||
sendEvent(name: "DeviceWatch-Enroll", value: JsonOutput.toJson([protocol: "cloud", scheme:"untracked"]), displayed: false)
|
||||
}
|
||||
|
||||
def installed() {
|
||||
log.debug "installed()"
|
||||
initialize()
|
||||
}
|
||||
|
||||
def updated() {
|
||||
log.debug "updated()"
|
||||
initialize()
|
||||
}
|
||||
|
||||
/*
|
||||
* This method creates the internal SmartThings events to handle changes in the properties of the plug
|
||||
*/
|
||||
def updateProperties(Map properties) {
|
||||
|
||||
log.debug "Updating plug's properties: ${properties}"
|
||||
|
||||
def connected = (properties["~connected"]?.value == true)
|
||||
|
||||
if (connected == true){ //only update if plug is connected
|
||||
|
||||
//update status message
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
log.info "Updating ${device.displayName}: property status set to: ${parent.getConnectedMessage()}"
|
||||
|
||||
//update currentpowerstate1
|
||||
def currentpowerstate1 = properties["currentpowerstate1"].value
|
||||
if (currentpowerstate1 != null){
|
||||
log.info "Updating ${device.displayName}: property currentpowerstate1 set to value: ${currentpowerstate1}"
|
||||
currentpowerstate1 = "${currentpowerstate1}"
|
||||
if (currentpowerstate1 == "1") {
|
||||
sendEvent(name: "switch", value: "on")
|
||||
}
|
||||
else if (currentpowerstate1 == "0") {
|
||||
sendEvent(name: "switch", value: "off")
|
||||
}
|
||||
}
|
||||
|
||||
//update firmware version
|
||||
def appfwversion = properties["appfwversion"].value
|
||||
if (appfwversion != null){
|
||||
log.info "Updating ${device.displayName}: property appfwversion set to value: ${appfwversion}"
|
||||
appfwversion = "${appfwversion}"
|
||||
sendEvent(name: "firmware", value: appfwversion)
|
||||
}
|
||||
|
||||
//update model
|
||||
log.info "Updating ${device.displayName}: property model set to value: iSP8"
|
||||
sendEvent(name:"model", value:"iSP8")
|
||||
|
||||
} else { //the plug is not connected
|
||||
//update status message
|
||||
sendEvent(name: "status", value: parent.getPlugNotConnectedMessage())
|
||||
log.info "Updating ${device.displayName}: property status set to: ${parent.getPlugNotConnectedMessage()}"
|
||||
}
|
||||
}
|
||||
|
||||
// Process the polling error, changing the status message
|
||||
def pollError(){
|
||||
log.info "Error retrieving info from the cloud"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the switch.on function by updating the corresponding property in the cloud
|
||||
*/
|
||||
def on() {
|
||||
|
||||
//update the status of the plug before attempting to change it
|
||||
refresh()
|
||||
|
||||
if (device.currentState("status")?.value == parent.getConnectedMessage()) {//only update if the plug is connected
|
||||
//Turn on if the device is off
|
||||
if (device.currentState("switch")?.value.toLowerCase().startsWith("off")){
|
||||
log.info "Updating ${device.displayName} in the cloud: sending action _turnOn"
|
||||
|
||||
def actionJSON = "{\"thng\":\"${device.deviceNetworkId}\", \"type\":\"_turnOn\"}"
|
||||
def success = parent.sendAction("_turnOn", actionJSON)
|
||||
|
||||
if(success){
|
||||
log.info "Updating ${device.displayName}: sending switch.on command"
|
||||
sendEvent(name: "switch", value: "on")
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
} else {
|
||||
log.info "Cloud property update error, skipping event"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the switch.off function by updating the corresponding property in the cloud
|
||||
*/
|
||||
def off() {
|
||||
|
||||
//update the status of the plug before attempting to change it
|
||||
refresh()
|
||||
|
||||
if (device.currentState("status")?.value == parent.getConnectedMessage()) {//only update if the plug is connected
|
||||
|
||||
//Turn off only if the device is on
|
||||
if (device.currentState("switch")?.value.toLowerCase().startsWith("on")){
|
||||
log.info "Updating ${device.displayName} in the cloud: sending action _turnOff"
|
||||
|
||||
def actionJSON = "{\"thng\":\"${device.deviceNetworkId}\", \"type\":\"_turnOff\"}"
|
||||
def success = parent.sendAction("_turnOff", actionJSON)
|
||||
|
||||
if (success) {
|
||||
log.info "Updating ${device.displayName}: sending switch.off command"
|
||||
sendEvent(name: "switch", value: "off")
|
||||
sendEvent(name: "status", value: parent.getConnectedMessage())
|
||||
} else {
|
||||
log.info "Cloud property update error, skipping event"
|
||||
sendEvent(name: "status", value: parent.getConnectionErrorMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method handles the refresh capability
|
||||
*/
|
||||
def refresh() {
|
||||
parent.pollChildren(device.deviceNetworkId)
|
||||
}
|
||||
|
||||
def ping() {
|
||||
refresh()
|
||||
}
|
||||
@@ -0,0 +1,707 @@
|
||||
/**
|
||||
* iHome (Connect)
|
||||
*
|
||||
* Copyright 2016 EVRYTHNG LTD.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
* Last reviewed: 11.08.2017
|
||||
* - Use of variable serverUrl in the URLs.
|
||||
* Reviewed:20.07.2017
|
||||
* - Merged content with the old version modified by SmartThings
|
||||
* - Removed selection of plugs, all available plugs are imported by default
|
||||
* - Added location selection
|
||||
* - Added DeviceWatch-DeviceStatus event
|
||||
* - Added unschedule call on initialising
|
||||
* - Changed from schedule to runEvery5Minutes
|
||||
* - Updated refreshThngs method to support add/delete plugs automatically when they are added/removed in iHome app
|
||||
* Reviewed: 04.07.2017
|
||||
* - Added support for iSP6X
|
||||
* - Reimplemented the import with filtering using the new tag "Active" (removed serial and use thngId)
|
||||
* Review: 20.04.2017
|
||||
* - Added filter by deactive property
|
||||
* - Removed duplicates by creation date
|
||||
*
|
||||
*/
|
||||
include 'localization'
|
||||
|
||||
definition(
|
||||
name: "iHome Control (Connect)",
|
||||
namespace: "ihome_control",
|
||||
author: "iHome",
|
||||
description: "Control your iHome Control devices within the SmartThings app!",
|
||||
category: "Convenience",
|
||||
iconUrl: "https://www.ihomeaudio.com/media/uploads/product/logos/iH_iHomeControlicon.png",
|
||||
iconX2Url: "https://www.ihomeaudio.com/media/uploads/product/logos/iH_iHomeControlicon.png",
|
||||
iconX3Url: "https://www.ihomeaudio.com/media/uploads/product/logos/iH_iHomeControlicon.png",
|
||||
singleInstance: true
|
||||
)
|
||||
{
|
||||
appSetting "clientId" //Client Id of the SmartThings App in the iHome System
|
||||
appSetting "clientSecret" //Client Secret of the SmartThings app in the iHome System
|
||||
appSetting "iHomeServer" //URL of the iHome API
|
||||
appSetting "serverUrl" //Base URL of the server hosting the redirection URI
|
||||
appSetting "evrythngServer" //URL of the EVRYTHNG API (cloud control)
|
||||
}
|
||||
|
||||
preferences {
|
||||
page(name: "iHomeAuth", content:"authenticationPage", install: false)
|
||||
page(name: "iHomeConnectDevices", title: "Import your iHome devices", content:"connectPage", install:false)
|
||||
}
|
||||
|
||||
private getVendorName() { "iHome" }
|
||||
|
||||
/**********************************************************************************************
|
||||
*
|
||||
* AUTHENTICATION
|
||||
*
|
||||
* This block contains all the functions needed to carry out the OAuth Authentication
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
/*
|
||||
* Authentication endpoints (needed for OAuth)
|
||||
*/
|
||||
mappings {
|
||||
path("/oauth/initialize") {action: [GET: "oauthInitUrl"]}
|
||||
path("/oauth/callback") {action: [GET: "callback"]}
|
||||
}
|
||||
|
||||
/*
|
||||
* Authentication Page
|
||||
* Implements OAuth authentication with the Authorization Code Grant flow
|
||||
*/
|
||||
def authenticationPage()
|
||||
{
|
||||
log.debug "Checking authorisation..."
|
||||
|
||||
//Check first if the authorisation was already done before
|
||||
if(state.iHomeAccessToken == null)
|
||||
{
|
||||
log.debug "iHome token not found, starting authorisation request"
|
||||
|
||||
//Check if the internal OAuth tokens have been created already
|
||||
if (!state.accessToken){
|
||||
log.debug "Creating access token for the callback"
|
||||
createAccessToken()
|
||||
}
|
||||
|
||||
//Create the OAuth URL of the authorisation server
|
||||
def redirectUrl = "${appSettings.serverUrl}/oauth/initialize?appId=${app.id}&access_token=${state.accessToken}&apiServerUrl=${getApiServerUrl()}"
|
||||
log.debug "Redirecting to OAuth URL initializer: ${redirectUrl}"
|
||||
|
||||
//Display the connect your account section, it will redirect to the OAuth Authentication Server
|
||||
return dynamicPage(name: "iHomeAuth", title:"iHome Control", install:false) {
|
||||
section ("") {
|
||||
|
||||
paragraph "Welcome! In order to connect SmartThings to your ${vendorName} devices, you need to have already set up your devices using the ${vendorName} app."
|
||||
href (url:redirectUrl,
|
||||
style:"embedded",
|
||||
required:true,
|
||||
image:"https://www.ihomeaudio.com/media/uploads/product/logos/iH_iHomeControl_icon.png",
|
||||
title:"Connect your iHome Account",
|
||||
description:""
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log.debug "iHome token found. Loading connect page"
|
||||
loadThngs()
|
||||
return connectPage()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Authentication OAuth URL
|
||||
* Creates the OAuth compliant URL to the Authorisation Server
|
||||
*/
|
||||
def oauthInitUrl() {
|
||||
|
||||
log.debug "Creating OAuth URL..."
|
||||
|
||||
// Generate a random ID to use as a our state value. This value will be used to verify the response we get back from the 3rd party service.
|
||||
state.oauthState = UUID.randomUUID().toString()
|
||||
|
||||
def oauthParams = [
|
||||
response_type: "code",
|
||||
client_id: appSettings.clientId,
|
||||
state: state.oauthState,
|
||||
redirect_uri: "${appSettings.serverUrl}/oauth/callback"
|
||||
]
|
||||
|
||||
redirect(location: "${appSettings.iHomeServer}/oauth/authorize?${toQueryString(oauthParams)}")
|
||||
}
|
||||
|
||||
/*
|
||||
* Helper class to provide feedback to the user about the authentication process
|
||||
*
|
||||
*/
|
||||
def connectionStatus(message, redirectUrl = null) {
|
||||
def redirectHtml = ""
|
||||
if (redirectUrl) {
|
||||
redirectHtml = """
|
||||
<meta http-equiv="refresh" content="3; url=${redirectUrl}" />
|
||||
"""
|
||||
}
|
||||
|
||||
def html = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
|
||||
<title>SmartThings Connection</title>
|
||||
<style type="text/css">
|
||||
@font-face {
|
||||
font-family: 'Swiss 721 W01 Thin';
|
||||
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.eot');
|
||||
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.eot?#iefix') format('embedded-opentype'),
|
||||
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.woff') format('woff'),
|
||||
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.ttf') format('truetype'),
|
||||
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.svg#swis721_th_btthin') format('svg');
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
}
|
||||
@font-face {
|
||||
font-family: 'Swiss 721 W01 Light';
|
||||
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.eot');
|
||||
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.eot?#iefix') format('embedded-opentype'),
|
||||
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.woff') format('woff'),
|
||||
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.ttf') format('truetype'),
|
||||
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.svg#swis721_lt_btlight') format('svg');
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
}
|
||||
.container {
|
||||
/* width: 440px;
|
||||
padding: 40px;
|
||||
/!*background: #eee;*!/*/
|
||||
text-align: center;
|
||||
}
|
||||
img {
|
||||
vertical-align: middle;
|
||||
}
|
||||
img:nth-child(2) {
|
||||
margin: 0 30px;
|
||||
}
|
||||
p {
|
||||
font-size: 2em;
|
||||
font-family: 'Swiss 721 W01 Thin';
|
||||
text-align: center;
|
||||
color: #666666;
|
||||
padding: 0 40px;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
/*
|
||||
p:last-child {
|
||||
margin-top: 0px;
|
||||
}
|
||||
*/
|
||||
span {
|
||||
font-family: 'Swiss 721 W01 Light';
|
||||
}
|
||||
.image{
|
||||
width: 20%;
|
||||
/*height: 70px;*/
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<img class="image" src="https://www.ihomeaudio.com/media/uploads/product/logos/iH_iHomeControlicon.png" alt="iHome icon" />
|
||||
<img src="https://s3.amazonaws.com/smartapp-icons/Partner/support/connected-device-icn%402x.png" alt="connected device icon" />
|
||||
<img class="image" src="https://s3.amazonaws.com/smartapp-icons/Partner/support/st-logo%402x.png" alt="SmartThings logo" />
|
||||
${message}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
render contentType: 'text/html', data: html
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Handler of the OAuth redirection URI
|
||||
*
|
||||
*/
|
||||
def callback() {
|
||||
|
||||
log.debug "OAuth callback received..."
|
||||
|
||||
//OAuth server returns a code in the URL as a parameter
|
||||
state.iHomeAccessCode = params.code
|
||||
log.debug "Received authorization code ${state.iHomeAccessCode}"
|
||||
|
||||
def oauthState = params.state
|
||||
|
||||
def successMessage = """
|
||||
<p>Your iHome Account is now connected to SmartThings!</p>
|
||||
<p>Click 'Done' in the top corner to complete the setup.</p>
|
||||
"""
|
||||
|
||||
def errorMessage = """
|
||||
<p>Your iHome Account couldn't be connected to SmartThings!</p>
|
||||
<p>Click 'Done' in the top corner and try again.</p>
|
||||
"""
|
||||
|
||||
// Validate the response from the 3rd party by making sure oauthState == state.oauthInitState as expected
|
||||
if (oauthState == state.oauthState){
|
||||
|
||||
if (state.iHomeAccessCode == null) {
|
||||
log.debug "OAuth error: Access code is not present"
|
||||
connectionStatus(errorMessage)
|
||||
}
|
||||
else {
|
||||
getAccessToken();
|
||||
if (state.iHomeAccessToken){
|
||||
getEVTApiKey();
|
||||
if(state.evtApiKey){
|
||||
connectionStatus(successMessage)
|
||||
}
|
||||
else{
|
||||
log.debug "OAuth error: EVT API KEY could not be retrieved"
|
||||
connectionStatus(errorMessage)
|
||||
}
|
||||
}
|
||||
else {
|
||||
log.debug "OAuth error: Access Token could not be retrieved"
|
||||
connectionStatus(errorMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
log.debug "OAuth error: initial state does not match"
|
||||
connectionStatus(errorMessage)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exchanges the authorization code for an access token
|
||||
*/
|
||||
def getAccessToken(){
|
||||
log.debug "Getting iHome access token..."
|
||||
|
||||
def tokenParams = [
|
||||
grant_type: "authorization_code",
|
||||
code: state.iHomeAccessCode,
|
||||
client_id: appSettings.clientId,
|
||||
client_secret: appSettings.clientSecret,
|
||||
redirect_uri: "${appSettings.serverUrl}/oauth/callback"
|
||||
]
|
||||
def tokenUrl = "${appSettings.iHomeServer}/oauth/token/?" + toQueryString(tokenParams)
|
||||
|
||||
log.debug "Invoking token URL: ${tokenUrl}"
|
||||
|
||||
try{
|
||||
def jsonMap
|
||||
httpPost(uri:tokenUrl) { resp ->
|
||||
if(resp.status == 200)
|
||||
{
|
||||
jsonMap = resp.data
|
||||
if (resp.data) {
|
||||
state.iHomeRefreshToken = resp?.data?.refresh_token
|
||||
state.iHomeAccessToken = resp?.data?.access_token
|
||||
log.debug "Access token received ${state.iHomeAccessToken}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch (groovyx.net.http.HttpResponseException e) {
|
||||
log.warn "Error! Status Code was: ${e}"
|
||||
} catch (java.net.SocketTimeoutException e) {
|
||||
log.warn "Connection timed out, not much we can do here."
|
||||
}
|
||||
}
|
||||
|
||||
def getEVTApiKey() {
|
||||
|
||||
log.debug "Getting api key from the cloud"
|
||||
|
||||
def apiKeyParams = [
|
||||
uri: "${appSettings.iHomeServer}/v3/evrythng/",
|
||||
headers: [
|
||||
"Accept": "application/json",
|
||||
"Authorization": "Bearer ${state.iHomeAccessToken}"]
|
||||
]
|
||||
|
||||
try {
|
||||
def jsonMap
|
||||
httpGet(apiKeyParams)
|
||||
{ resp ->
|
||||
if(resp.status == 200)
|
||||
{
|
||||
jsonMap = resp.data
|
||||
if (resp.data)
|
||||
{
|
||||
state.evtUserId = resp?.data?.evrythng_user_id
|
||||
state.evtApiKey = resp?.data?.evrythng_api_key
|
||||
log.debug "Api key received: ${state.evtUserId}/${state.evtApiKey}"
|
||||
|
||||
//Preload thngs after getting the api key
|
||||
loadThngs()
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch (groovyx.net.http.HttpResponseException e) {
|
||||
log.warn "Error! Status Code was: ${e.statusCode}"
|
||||
} catch (java.net.SocketTimeoutException e) {
|
||||
log.warn "Connection timed out, not much we can do here"
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Maps the map to query parameters for the URL
|
||||
*
|
||||
*/
|
||||
def toQueryString(Map m)
|
||||
{
|
||||
return m.collect { k, v -> "${k}=${URLEncoder.encode(v.toString())}" }.sort().join("&")
|
||||
}
|
||||
|
||||
/**********************************************************************************************
|
||||
* IMPORT
|
||||
* This block contains all the functions needed to import the plugs from the cloud into SmartThings
|
||||
**********************************************************************************************/
|
||||
def loadThngs()
|
||||
{
|
||||
//Products in production account
|
||||
state.product = [:]
|
||||
state.product["UCfXBRHnse5Rpw7PySPYNq7b"] = "iHomeSmartPlug-iSP5"
|
||||
state.product["Ugrtyq8pAFVEqGSAAptgtqkc"] = "iHomeSmartPlug-iSP6"
|
||||
state.product["UXNtyNexVyRrWpAQeNHq9xad"] = "iHomeSmartPlug-iSP8"
|
||||
state.product["UF4NsmAEM3PhY6wwRgehdg5n"] = "iHomeSmartPlug-iSP6X"
|
||||
|
||||
//Save the all the plugs in the state for later use
|
||||
state.thngs = [:]
|
||||
|
||||
log.debug "Loading available devices..."
|
||||
def thngs = getThngs()
|
||||
|
||||
thngs.each { thng ->
|
||||
//Check that the plug is compatible with a Device Type
|
||||
log.debug "Checking if ${thng.id} is a compatible Device Type"
|
||||
if (state.product[thng.product])
|
||||
{
|
||||
thng.st_devicetype = state.product[thng.product]
|
||||
state.thngs["${thng.id}"] = thng
|
||||
log.info "Found compatible device ${state.thngs["${thng.id}"].name}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Import thngs page
|
||||
* Loads the thngs available from the user, checks that they have a DeviceType associated
|
||||
* and presents a list to the user
|
||||
*
|
||||
*/
|
||||
def connectPage()
|
||||
{
|
||||
return dynamicPage(name: "iHomeConnectDevices", uninstall: true, install:true) {
|
||||
section(""){
|
||||
input "selectedLocationId", "enum", required:false, title:"", multiple:false, options:["Default Location"], defaultValue: "Default Location", submitOnChange: true
|
||||
paragraph "Devices will be added automatically from your ${vendorName} account. To add or delete devices please use the Official ${vendorName} App."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Gets the thngs from the cloud
|
||||
* This is used as the discovery process
|
||||
*/
|
||||
def getThngs(){
|
||||
|
||||
log.debug "Getting available devices..."
|
||||
|
||||
def url = "${appSettings.evrythngServer}/thngs?filter=tags=Active"
|
||||
|
||||
try {
|
||||
|
||||
httpGet(uri: url, headers: ["Accept": "application/json", "Authorization": state.evtApiKey]) {response ->
|
||||
if (response.status == 200) {
|
||||
log.debug "GET on /thngs was succesful"
|
||||
log.debug "Response to GET /thngs ${response.data}"
|
||||
return response.data
|
||||
}
|
||||
}
|
||||
} catch (groovyx.net.http.HttpResponseException e) {
|
||||
log.warn "Error! Status Code was: ${e.statusCode}"
|
||||
} catch (java.net.SocketTimeoutException e) {
|
||||
log.warn "Connection timed out, not much we can do here"
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Gets a thng by id from EVRYTHNG
|
||||
* Used for updates
|
||||
*/
|
||||
def getThng(thngId){
|
||||
|
||||
log.trace "Getting device information..."
|
||||
|
||||
def url = "${appSettings.evrythngServer}/thngs/" + thngId
|
||||
|
||||
try {
|
||||
httpGet(uri: url, headers: ["Accept": "application/json", "Authorization": state.evtApiKey]) {response ->
|
||||
if (response.status == 200) {
|
||||
log.debug "GET on /thngs was succesful: ${response.data}"
|
||||
|
||||
def isAlive = response.data.properties["~connected"]
|
||||
def d = getChildDevice(thngId)
|
||||
d?.sendEvent(name: "DeviceWatch-DeviceStatus", value: isAlive? "online":"offline", displayed: false, isStateChange: true)
|
||||
|
||||
return response.data
|
||||
}
|
||||
else{
|
||||
log.warn "Error! Status Code was: ${response.status}"
|
||||
}
|
||||
}
|
||||
} catch (groovyx.net.http.HttpResponseException e) {
|
||||
log.warn "Error! Status Code was: ${e.statusCode}"
|
||||
} catch (java.net.SocketTimeoutException e) {
|
||||
log.warn "Connection timed out, not much we can do here"
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Adds all the available devices to SmartThings
|
||||
* Invoked by the lifecycle initialise
|
||||
*/
|
||||
def importThngs() {
|
||||
|
||||
def thngsToImport = []
|
||||
|
||||
state.thngs.each { thng ->
|
||||
thngsToImport.add(thng.key)
|
||||
}
|
||||
log.debug "Adding all available plugs...${thngsToImport}"
|
||||
|
||||
//Remove unselected plugs
|
||||
log.debug "Checking to delete ${state.imported}"
|
||||
state.imported.each{ id ->
|
||||
if(thngsToImport){
|
||||
if (thngsToImport.contains(id)){
|
||||
log.debug "${id} is already imported"
|
||||
} else{
|
||||
log.debug "Removing device not longer available: ${id}"
|
||||
// Error can occur if device has already been deleted or is in-use by SmartApps. Should it be force-deleted? + deleteChildDevice(thng)
|
||||
try {
|
||||
deleteChildDevice(id)
|
||||
} catch (Exception e) {
|
||||
log.error "Error deleting device with DNI $thng: $e"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.trace "Removing unselected device with id: ${id}"
|
||||
try {
|
||||
deleteChildDevice(id)
|
||||
}
|
||||
catch(Exception error){
|
||||
log.error "Error deleting device with id -> ${id}: $error"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state.imported = [];
|
||||
|
||||
thngsToImport.each { id ->
|
||||
log.debug "Importing plug with id: ${id} and serial: ${state.thngs["${id}"].identifiers.serial_num}"
|
||||
def smartThing = getChildDevice(id)
|
||||
|
||||
if(!smartThing) {
|
||||
def newSmartThing = state.thngs.find { it.key == id }
|
||||
log.debug "Creating SmartThing: ${newSmartThing}"
|
||||
|
||||
smartThing = addChildDevice("ihome_devices",
|
||||
newSmartThing.value.st_devicetype,
|
||||
newSmartThing.value.id,
|
||||
null,
|
||||
[label:"${newSmartThing.value.name}"])
|
||||
|
||||
log.info "Created ${smartThing.displayName} with id ${smartThing.deviceNetworkId}"
|
||||
}
|
||||
else {
|
||||
log.trace "${smartThing.displayName} with id ${id} already exists, skipping creation"
|
||||
}
|
||||
|
||||
//save plug in state
|
||||
state.imported.add(id);
|
||||
|
||||
//We need to get the current status of the plug
|
||||
pollChildren(smartThing.deviceNetworkId)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************************************
|
||||
*
|
||||
* LIFECYCLE
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
def installed() {
|
||||
log.debug "Application installed..."
|
||||
initialize()
|
||||
}
|
||||
|
||||
def updated() {
|
||||
log.debug "Application updated..."
|
||||
unsubscribe()
|
||||
initialize()
|
||||
}
|
||||
|
||||
def initialize() {
|
||||
log.debug "Application initialising..."
|
||||
importThngs()
|
||||
unschedule()
|
||||
//Refresh every five minutes for external changes in the thngs
|
||||
runEvery5Minutes("refreshThngs")
|
||||
}
|
||||
|
||||
def uninstalled() {
|
||||
log.debug "Removing installed plugs..."
|
||||
getChildDevices().each {
|
||||
log.debug "Deleting ${it.deviceNetworkId}"
|
||||
try {
|
||||
deleteChildDevice(it.deviceNetworkId)
|
||||
}
|
||||
catch (e) {
|
||||
log.warn "Error deleting device, ignoring ${e}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**********************************************************************************************
|
||||
* Properties and Actions UPDATES
|
||||
* This block contains the functionality to update property values and send actions in EVRYTHNG cloud
|
||||
* This methods are generic based on the EVRYTHNG API specification and are invoked from
|
||||
* the specific Device Type that handles the properties and action types
|
||||
**********************************************************************************************/
|
||||
|
||||
/*
|
||||
* Updates a property in EVRYTHNG
|
||||
*/
|
||||
def propertyUpdate(thngId, propertyUpdate){
|
||||
|
||||
def url = "${appSettings.evrythngServer}/thngs/${thngId}/properties"
|
||||
|
||||
def params = [
|
||||
uri: url,
|
||||
headers: [
|
||||
"Authorization": state.evtApiKey
|
||||
],
|
||||
body: propertyUpdate
|
||||
]
|
||||
|
||||
log.debug "Sending property update to the cloud: ${params}"
|
||||
|
||||
try {
|
||||
httpPutJson(params) { resp ->
|
||||
if (resp.status == 200) {
|
||||
log.debug "Response from the cloud: ${resp}"
|
||||
return true
|
||||
}
|
||||
else {
|
||||
log.debug "Response status from the cloud not valid: ${resp}"
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
log.debug "Something went wrong with the property update: ${e}"
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Sends an action to EVRYTHNG
|
||||
*/
|
||||
def sendAction(actionType, actionPayload){
|
||||
|
||||
def url = "${appSettings.evrythngServer}/actions/${actionType}"
|
||||
|
||||
def params = [
|
||||
uri: url,
|
||||
headers: [
|
||||
"Authorization": state.evtApiKey
|
||||
],
|
||||
body: actionPayload
|
||||
]
|
||||
|
||||
log.debug "Sending action to the cloud: ${params}"
|
||||
|
||||
try {
|
||||
httpPostJson(params) { resp ->
|
||||
if (resp.status == 201) {
|
||||
log.debug "Response from the cloud: ${resp}"
|
||||
return true
|
||||
}
|
||||
else {
|
||||
log.debug "Response status from the cloud not valid: ${resp}"
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
log.debug "Something went wrong with sending the action: ${e}"
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler of the refreshing of all the imported things
|
||||
*/
|
||||
def refreshThngs(){
|
||||
log.debug "Refreshing thngs"
|
||||
|
||||
//loading thngs to get plugs recently added or removed
|
||||
loadThngs()
|
||||
|
||||
//import the plugs into SmartThings
|
||||
importThngs()
|
||||
}
|
||||
|
||||
/*
|
||||
* Utility function to poll for a Thng and update its properties
|
||||
*/
|
||||
def pollChildren(thngId){
|
||||
//get plug device
|
||||
def smartThing = getChildDevice(thngId)
|
||||
|
||||
if (smartThing){
|
||||
//Get plug's latest state from the cloud
|
||||
log.debug "Getting updates for ${thngId}"
|
||||
def plug = getThng(thngId)
|
||||
|
||||
if (plug == null){
|
||||
smartThing.pollError()
|
||||
}
|
||||
else
|
||||
{
|
||||
//Update name
|
||||
smartThing.label = plug.name
|
||||
smartThing.name = plug.name
|
||||
|
||||
//Update properties
|
||||
smartThing.updateProperties(plug.properties)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Status messages for all types of plugs */
|
||||
def getConnectedMessage(){
|
||||
return "Connected"
|
||||
}
|
||||
def getConnectionErrorMessage(){
|
||||
return "Connection error. Please try again."
|
||||
}
|
||||
def getPlugNotConnectedMessage(){
|
||||
return "Your plug seems to be disconnected."
|
||||
}
|
||||
Reference in New Issue
Block a user