Compare commits

..

1 Commits

Author SHA1 Message Date
Kiril Maslenkov
c09c530f81 MSA-1182: Control your Air Conditioner remotely. 2016-04-14 03:11:54 -05:00
4 changed files with 378 additions and 189 deletions

View File

@@ -0,0 +1,351 @@
/**
* Melissa Climate
*
* Copyright 2016 Kiril Maslenkov
*
* 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.
*
*/
preferences {
input "email", "text", title: "Email", description: "Your Email", required: true
input "password", "password", title: "Password", description: "Your Melissa Password", required: true
input "mac", "text", title: "Melissa MAC Address", description: "Melissa Mac Address", required: true
}
metadata {
definition (name: "Melissa Climate", namespace: "Melissa", author: "Melissa Climate") {
capability "Thermostat"
command temperatureUp
command temperatureDown
command sendCommand
command switchMode
command switchFanMode
command switchingState
command refreshApp
}
simulator { }
tiles(scale: 2) {
multiAttributeTile(name:"status", type: "thermostat", width: 6, height: 4){
tileAttribute("device.temperature", key:"PRIMARY_CONTROL"){
attributeState("default", label:'${currentValue}°', unit: "df", backgroundColor: '#4b8df8')
}
tileAttribute("device.humidity", key: "SECONDARY_CONTROL") {
attributeState("default", label:'${currentValue}%', unit:"%")
}
tileAttribute("device.temperature", key: "VALUE_CONTROL") {
attributeState("VALUE_UP", action: "temperatureUp")
attributeState("VALUE_DOWN", action: "temperatureDown")
}
}
standardTile("mode", "device.mode", decoration: "flat", width: 3, height: 2) {
state "auto", action:"switchMode", label: '${name}', nextState: "cool", icon: "http://server.seemelissa.com/smartthings/icons/modes/auto-2.png"
state "cool", action:"switchMode", label: '${name}', nextState: "heat", icon: "http://server.seemelissa.com/smartthings/icons/modes/cool.png"
state "heat", action:"switchMode", label: '${name}', nextState: "dry", icon: "http://server.seemelissa.com/smartthings/icons/modes/heat.png"
state "dry", action:"switchMode", label: '${name}', nextState: "auto", icon: "http://server.seemelissa.com/smartthings/icons/modes/dry.png"
}
standardTile("fan", "device.fan", decoration: "flat", width: 3, height: 2, canChangeIcon: true, canChangeBackground: true) {
state "auto", action:"switchFanMode", label: '${name}', nextState: "high", icon: "http://server.seemelissa.com/smartthings/icons/modes/auto-2.png"
state "high", action:"switchFanMode", label: '${name}', nextState: "medium", icon: "http://server.seemelissa.com/smartthings/icons/fan/fan1.png"
state "medium", action:"switchFanMode", label: '${name}', nextState: "low", icon: "http://server.seemelissa.com/smartthings/icons/fan/fan2.png"
state "low", action:"switchFanMode", label: '${name}', nextState: "auto", icon: "http://server.seemelissa.com/smartthings/icons/fan/fan3.png"
}
standardTile("switchState", "device.switchState", width: 3, height: 2, decoration: "flat", canChangeIcon: true, canChangeBackground: true) {
state "on", label: '${name}', action: "switchingState", nextState: "off", icon: "http://server.seemelissa.com/smartthings/icons/on_off/turn-on-off-white.png", backgroundColor: "#79b821"
state "off", label: '${name}', action: "switchingState", nextState: "on", icon: "http://server.seemelissa.com/smartthings/icons/on_off/turn-on-off.png", backgroundColor: "#ffffff"
//state "on", label: '${name}', action: "switchState.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
//state "off", label: '${name}', action: "switchState.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
}
standardTile("send", "device.send", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", action:"sendCommand", label: 'Send Command'
}
standardTile("refresh", "device.send2", inactiveLabel: false, decoration: "flat", width: 3, height: 2) {
state "default", action:"refreshApp", icon: "st.secondary.refresh"
}
standardTile("username", "device.username", inactiveLabel: false, decoration: "flat", width: 6, height: 2) {
state "default", label:'${currentValue}'
}
main(["status", "mode", "fan", "send"])
//details(["status", "mode", "fan", "refresh", "switchState", "username"])
details(["status", "mode", "fan", "refresh", "switchState"])
}
}
def switchingState() {
if (state.ac_state == "on") {
state.ac_state = "off"
} else {
state.ac_state = "on"
}
sendEvent(name: "switchState", value: state.ac_state);
sendCommand()
}
/* SWITCHING MODES Auto, Cool, Heat, Dry*/
def switchMode() {
switch (state.mode) {
case "auto":
state.mode = "cool";
break;
case "cool":
state.mode = "heat";
break;
case "heat":
state.mode = "dry";
break;
case "dry":
state.mode = "auto";
break;
}
//sendEvent(name: "username", value: state.mode)
sendCommand()
// log.debug state.mode
}
/* SWITCHING FAN MODES*/
def switchFanMode() {
switch (state.fan) {
case "auto":
state.fan = "high";
break;
case "high":
state.fan = "medium";
break;
case "medium":
state.fan = "low";
break;
case "low":
state.fan = "auto";
break;
}
sendEvent(name: "username", value: state.fan)
sendCommand()
//log.debug state.fan
}
def refreshApp() {
def params = [
uri: 'http://api2.seemelissa.com/user/login',
body: [
email: "k.maslenkov@sabev.at",
password: "xxxxx"
//email: settings.email,
//password: settings.password
]
]
try {
httpPost(params) {resp ->
/*
log.debug "resp data: ${resp.data}"
log.debug resp.data
log.debug resp
*/
def _try = resp.data
def slurper = new groovy.json.JsonSlurper()
def results = slurper.parseText("${resp.data}")
state.token = results.Data
settings.mac = "OIEQ321TGR6"
def getParams = [
uri: "http://api2.seemelissa.com",
path: "/testusers/getMelissaData/${settings.mac}"
//path: "/testusers/getMelissaData/OIEQ321TGR6"
]
try {
httpGet(getParams) {response->
def getResult = slurper.parseText("${response.data}")
state.temp = getResult.temp as int
state.humidity = getResult.humidity
state.codeset = getResult.codeset_id as int
switch (getResult.mode) {
case "0":
state.mode = "auto";
break;
case "1":
state.mode = "fan";
break;
case "2":
state.mode = "heat";
break;
case "3":
state.mode = "cool";
break;
case "4":
state.mode = "dry";
break;
}
switch (getResult.fan) {
case "0":
state.fan = "auto";
break;
case "1":
state.fan = "low";
break;
case "2":
state.fan = "medium";
break;
case "3":
state.fan = "high";
break;
}
if (getResult.state == "0") {
state.ac_state = "off"
} else {
state.ac_state = "on"
}
sendEvent(name: "mode", value: state.mode)
sendEvent(name: "fan", value: state.fan)
sendEvent(name: "temperature", value: state.temp)
sendEvent(name: "humidity", value: state.humidity)
sendEvent(name: "switchState", value: state.ac_state)
}
}
catch (e) {
log.error "error: $e"
}
}
} catch (e) {
log.error "error: $e"
}
}
def sendCommand () {
log.error state
def params = [
uri: 'http://api2.seemelissa.com/testusers/sendCommand',
body: [
mac: "OIEQ321TGR6",
//mac: settings.mac,
temp: state.temp,
token: state.token,
fan: state.fan,
mode: state.mode,
ac_state: state.ac_state,
codeset: state.codeset
]
]
try {
httpPost(params) {resp ->
log.debug "Send command done"
log.debug "resp data: ${resp.data}"
}
} catch (e) {
log.error "error: $e"
}
}
def updated() {
//log.debug "Updated !!!"
login()
}
def temperatureDown() {
if (state.temp != 18) {
state.temp = state.temp - 1
}
sendEvent(name: "temperature", value: state.temp)
sendCommand()
}
def temperatureUp() {
if (state.temp != 30) {
state.temp = state.temp + 1
}
sendEvent(name: "temperature", value: state.temp)
sendCommand()
}
def login() {
refreshApp()
/*
def params = [
uri: 'http://api2.seemelissa.com/user/login',
body: [
email: settings.email,
password: settings.password
]
]
try {
httpPost(params) {resp ->
log.debug "resp data: ${resp.data}"
log.debug "Response Received: Status [$resp.status]"
def _try = resp.data
def slurper = new groovy.json.JsonSlurper()
def results = slurper.parseText("${resp.data}")
state.token = results.Data
state.temp = 18
state.humidity = 40
sendEvent(name: "temperature", value: state.temp)
sendEvent(name: "humidity", value: state.humidity)
sendEvent(name: "username", value: state.token)
}
} catch (e) {
log.error "error: $e"
}
*/
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
}

View File

@@ -33,7 +33,7 @@ metadata {
state "power", label: '${currentValue} W'
}
htmlTile(name: "powerContent", attribute: "powerContent", type: "HTML", whitelist: ["www.wattvision.com"] , url: '${currentValue}', width: 3, height: 2)
htmlTile(name: "powerContent", attribute: "powerContent", type: "HTML", whitelist: "www.wattvision.com" , url: '${currentValue}', width: 3, height: 2)
standardTile("refresh", "device.power", inactiveLabel: false, decoration: "flat") {
state "default", label: '', action: "refresh.refresh", icon: "st.secondary.refresh"

View File

@@ -1,56 +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.
*
* Turn It On For 5 Minutes
* Turn on a switch when a contact sensor opens and then turn it back off 5 minutes later.
*
* Author: SmartThings
*/
definition(
name: "5분간켜줘",
namespace: "smartthings",
author: "SmartThings",
description: "When a SmartSense Multi is opened, a switch will be turned on, and then turned off after 5 minutes.",
category: "Safety & Security",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet@2x.png"
)
preferences {
section("When it opens..."){
input "contact1", "capability.contactSensor"
}
section("Turn on a switch for 5 minutes..."){
input "switch1", "capability.switch"
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
subscribe(contact1, "contact.open", contactOpenHandler)
}
def updated(settings) {
log.debug "Updated with settings: ${settings}"
unsubscribe()
subscribe(contact1, "contact.open", contactOpenHandler)
}
def contactOpenHandler(evt) {
switch1.on()
def fiveMinuteDelay = 60 * 5
runIn(fiveMinuteDelay, turnOffSwitch)
}
def turnOffSwitch() {
switch1.off()
}

View File

@@ -60,7 +60,7 @@ def bridgeDiscovery(params=[:])
app.updateSetting("selectedHue", "")
}
ssdpSubscribe()
subscribe(location, null, locationHandler, [filterEvents:false])
//bridge discovery request every 15 //25 seconds
if((bridgeRefreshCount % 5) == 0) {
@@ -152,10 +152,6 @@ private discoverBridges() {
sendHubCommand(new physicalgraph.device.HubAction("lan discovery urn:schemas-upnp-org:device:basic:1", physicalgraph.device.Protocol.LAN))
}
void ssdpSubscribe() {
subscribe(location, "ssdpTerm.urn:schemas-upnp-org:device:basic:1", ssdpBridgeHandler)
}
private sendDeveloperReq() {
def token = app.id
def host = getBridgeIP()
@@ -165,7 +161,7 @@ private sendDeveloperReq() {
headers: [
HOST: host
],
body: [devicetype: "$token-0"]], "${selectedHue}", [callback: "usernameHandler"]))
body: [devicetype: "$token-0"]], "${selectedHue}"))
}
private discoverHueBulbs() {
@@ -175,7 +171,7 @@ private discoverHueBulbs() {
path: "/api/${state.username}/lights",
headers: [
HOST: host
]], "${selectedHue}", [callback: "lightsHandler"]))
]], "${selectedHue}"))
}
private verifyHueBridge(String deviceNetworkId, String host) {
@@ -185,7 +181,7 @@ private verifyHueBridge(String deviceNetworkId, String host) {
path: "/description.xml",
headers: [
HOST: host
]], deviceNetworkId, [callback: "bridgeDescriptionHandler"]))
]], deviceNetworkId))
}
private verifyHueBridges() {
@@ -297,9 +293,8 @@ def bulbListHandler(hub, data = "") {
}
}
def bridge = null
if (selectedHue) {
bridge = getChildDevice(selectedHue)
}
if (selectedHue)
bridge = getChildDevice(selectedHue)
bridge.sendEvent(name: "bulbList", value: hub, data: bulbs, isStateChange: true, displayed: false)
msg = "${bulbs.size()} bulbs found. ${bulbs}"
return msg
@@ -387,9 +382,8 @@ def addBridge() {
def oldDNI = it.deviceNetworkId
log.debug "updating dni for device ${it} with $newDNI - previous DNI = ${it.deviceNetworkId}"
it.setDeviceNetworkId("${newDNI}")
if (oldDNI == selectedHue) {
app.updateSetting("selectedHue", newDNI)
}
if (oldDNI == selectedHue)
app.updateSetting("selectedHue", newDNI)
newbridge = false
}
}
@@ -418,111 +412,6 @@ def addBridge() {
}
}
def ssdpBridgeHandler(evt) {
def description = evt.description
log.trace "Location: $description"
def hub = evt?.hubId
def parsedEvent = parseLanMessage(description)
parsedEvent << ["hub":hub]
def bridges = getHueBridges()
log.trace bridges.toString()
if (!(bridges."${parsedEvent.ssdpUSN.toString()}")) {
//bridge does not exist
log.trace "Adding bridge ${parsedEvent.ssdpUSN}"
bridges << ["${parsedEvent.ssdpUSN.toString()}":parsedEvent]
} else {
// update the values
def ip = convertHexToIP(parsedEvent.networkAddress)
def port = convertHexToInt(parsedEvent.deviceAddress)
def host = ip + ":" + port
log.debug "Device ($parsedEvent.mac) was already found in state with ip = $host."
def dstate = bridges."${parsedEvent.ssdpUSN.toString()}"
def dni = "${parsedEvent.mac}"
def d = getChildDevice(dni)
def networkAddress = null
if (!d) {
childDevices.each {
if (it.getDeviceDataByName("mac")) {
def newDNI = "${it.getDeviceDataByName("mac")}"
if (newDNI != it.deviceNetworkId) {
def oldDNI = it.deviceNetworkId
log.debug "updating dni for device ${it} with $newDNI - previous DNI = ${it.deviceNetworkId}"
it.setDeviceNetworkId("${newDNI}")
if (oldDNI == selectedHue) {
app.updateSetting("selectedHue", newDNI)
}
doDeviceSync()
}
}
}
} else {
if (d.getDeviceDataByName("networkAddress")) {
networkAddress = d.getDeviceDataByName("networkAddress")
} else {
networkAddress = d.latestState('networkAddress').stringValue
}
log.trace "Host: $host - $networkAddress"
if (host != networkAddress) {
log.debug "Device's port or ip changed for device $d..."
dstate.ip = ip
dstate.port = port
dstate.name = "Philips hue ($ip)"
d.sendEvent(name:"networkAddress", value: host)
d.updateDataValue("networkAddress", host)
}
}
}
}
void bridgeDescriptionHandler(physicalgraph.device.HubResponse hubResponse) {
log.trace "description.xml response (application/xml)"
def body = hubResponse.xml
if (body?.device?.modelName?.text().startsWith("Philips hue bridge")) {
def bridges = getHueBridges()
def bridge = bridges.find {it?.key?.contains(body?.device?.UDN?.text())}
if (bridge) {
bridge.value << [name:body?.device?.friendlyName?.text(), serialNumber:body?.device?.serialNumber?.text(), verified: true]
} else {
log.error "/description.xml returned a bridge that didn't exist"
}
}
}
void lightsHandler(physicalgraph.device.HubResponse hubResponse) {
if (isValidSource(hubResponse.mac)) {
def body = hubResponse.json
if (!body?.state?.on) { //check if first time poll made it here by mistake
def bulbs = getHueBulbs()
log.debug "Adding bulbs to state!"
body.each { k, v ->
bulbs[k] = [id: k, name: v.name, type: v.type, modelid: v.modelid, hub: hubResponse.hubId]
}
}
}
}
void usernameHandler(physicalgraph.device.HubResponse hubResponse) {
if (isValidSource(hubResponse.mac)) {
def body = hubResponse.json
if (body.success != null) {
if (body.success[0] != null) {
if (body.success[0].username)
state.username = body.success[0].username
}
} else if (body.error != null) {
//TODO: handle retries...
log.error "ERROR: application/json ${body.error}"
}
}
}
/**
* @deprecated This has been replaced by the combination of {@link #ssdpBridgeHandler()}, {@link #bridgeDescriptionHandler()},
* {@link #lightsHandler()}, and {@link #usernameHandler()}. After a pending event subscription migration, it can be removed.
*/
@Deprecated
def locationHandler(evt) {
def description = evt.description
log.trace "Location: $description"
@@ -558,19 +447,17 @@ def locationHandler(evt) {
def oldDNI = it.deviceNetworkId
log.debug "updating dni for device ${it} with $newDNI - previous DNI = ${it.deviceNetworkId}"
it.setDeviceNetworkId("${newDNI}")
if (oldDNI == selectedHue) {
app.updateSetting("selectedHue", newDNI)
}
if (oldDNI == selectedHue)
app.updateSetting("selectedHue", newDNI)
doDeviceSync()
}
}
}
} else {
if (d.getDeviceDataByName("networkAddress")) {
networkAddress = d.getDeviceDataByName("networkAddress")
} else {
networkAddress = d.latestState('networkAddress').stringValue
}
if (d.getDeviceDataByName("networkAddress"))
networkAddress = d.getDeviceDataByName("networkAddress")
else
networkAddress = d.latestState('networkAddress').stringValue
log.trace "Host: $host - $networkAddress"
if(host != networkAddress) {
log.debug "Device's port or ip changed for device $d..."
@@ -603,9 +490,8 @@ def locationHandler(evt) {
def body = new groovy.json.JsonSlurper().parseText(parsedEvent.body)
if (body.success != null) {
if (body.success[0] != null) {
if (body.success[0].username) {
if (body.success[0].username)
state.username = body.success[0].username
}
}
} else if (body.error != null) {
//TODO: handle retries...
@@ -630,7 +516,11 @@ def doDeviceSync(){
log.trace "Doing Hue Device Sync!"
convertBulbListToMap()
poll()
ssdpSubscribe()
try {
subscribe(location, null, locationHandler, [filterEvents:false])
} catch (all) {
log.trace "Subscription already exist"
}
discoverBridges()
}
@@ -857,11 +747,15 @@ private getId(childDevice) {
private poll() {
def host = getBridgeIP()
def uri = "/api/${state.username}/lights/"
log.debug "GET: $host$uri"
sendHubCommand(new physicalgraph.device.HubAction("""GET ${uri} HTTP/1.1
try {
sendHubCommand(new physicalgraph.device.HubAction("""GET ${uri} HTTP/1.1
HOST: ${host}
""", physicalgraph.device.Protocol.LAN, selectedHue))
} catch (all) {
log.warn "Parsing Body failed - trying again..."
doDeviceSync()
}
}
private put(path, body) {