Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to provide amazon echo support #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
* 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 Updated : 7/15/2015
*
* Last Updated : 4/4/2016
*
* 20160404 - Modified by Gene Ussery - Perform refresh before running on or off functions. Added logic to push to only run open if the garage is closed
* and run closed if the garage is open or stopped.
*/
metadata {
definition (name: "MyQ Garage Door Opener", namespace: "copy-ninja", author: "Jason Mok") {
Expand Down Expand Up @@ -66,35 +68,44 @@ metadata {
def parse(String description) {}

def on() {
push()
log.debug("Running On")
refresh()
push("open")
sendEvent(name: "button", value: "on", isStateChange: true, display: false, displayed: false)
}
def off() {
log.debug("Running Off")
refresh()
push("closed")
sendEvent(name: "button", value: "off", isStateChange: true, display: false, displayed: false)
}

def push() {
def push(String desiredstate) {
log.debug("Running Push")
def doorState = device.currentState("door")?.value
if (doorState == "open" || doorState == "stopped") {
if ((doorState == "open" || doorState == "stopped") && desiredstate == "closed") {
close()
} else if (doorState == "closed") {
} else if (doorState == "closed" && desiredstate == "open") {
open()
}
sendEvent(name: "momentary", value: "pushed", display: false, displayed: false)
}

def open() {
log.debug("Running Open")
parent.sendCommand(this, "desireddoorstate", 1)
updateDeviceStatus(4)
updateDeviceLastActivity(parent.getDeviceLastActivity(this))
}
def close() {
log.debug("Running Close")
parent.sendCommand(this, "desireddoorstate", 0)
updateDeviceStatus(5)
updateDeviceLastActivity(parent.getDeviceLastActivity(this))
}

def refresh() {
log.debug("Running Refresh")
parent.refresh()
updateDeviceLastActivity(parent.getDeviceLastActivity(this))
}
Expand All @@ -103,6 +114,7 @@ def poll() { refresh() }

// update status
def updateDeviceStatus(status) {
log.debug("Running UpdateStatus")
def currentState = device.currentState("door")?.value
if (status == "1" || status == "9") {
sendEvent(name: "door", value: "open", display: true, descriptionText: device.displayName + " is open")
Expand All @@ -125,6 +137,7 @@ def updateDeviceStatus(status) {
}

def updateDeviceLastActivity(long lastActivity) {
log.debug("Running UpdateLastActivity")
def lastActivityValue = ""
def diffTotal = now() - lastActivity
def diffDays = (diffTotal / 86400000) as long
Expand All @@ -141,4 +154,4 @@ def updateDeviceLastActivity(long lastActivity) {
else if (diffMins > 1) lastActivityValue += "${diffMins} Mins"

sendEvent(name: "lastActivity", value: lastActivityValue, display: false , displayed: false)
}
}
26 changes: 21 additions & 5 deletions smartapps/copy-ninja/myq-connect.src/myq-connect.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
* 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 Updated : 03/08/2016
* Last Updated : 02/15/2017
*
* 20170215 - Gene Ussery - Changed to work with API changes from Chamberlain
*/
definition(
name: "MyQ (Connect)",
namespace: "copy-ninja",
author: "Jason Mok",
description: "Connect MyQ to control your devices",
category: "SmartThings Labs",
iconUrl: "http://smartthings.copyninja.net/icons/MyQ@1x.png",
iconX2Url: "http://smartthings.copyninja.net/icons/MyQ@2x.png",
iconX3Url: "http://smartthings.copyninja.net/icons/MyQ@3x.png"
iconUrl: "https://s3.amazonaws.com/gu-smartapp-icons/myq.png",
iconX2Url: "https://s3.amazonaws.com/gu-smartapp-icons/myq@2x.png",
iconX3Url: "https://s3.amazonaws.com/gu-smartapp-icons/myq@3x.png"
)

preferences {
Expand Down Expand Up @@ -161,7 +162,10 @@ private forceLogin() {
private login() { return (!(state.session.expiration > now())) ? doLogin() : true }

private doLogin() {
apiGet("/api/user/validate", [username: settings.username, password: settings.password] ) { response ->
// apiGet("/api/v4/user/validate", [username: settings.username, password: settings.password] ) { response ->

apiPost("/api/v4/user/validate", [username: settings.username, password: settings.password] ) { response ->
log.debug "got login response data: ${response.data}"
if (response.status == 200) {
if (response.data.SecurityToken != null) {
state.session.brandID = response.data.BrandId
Expand Down Expand Up @@ -282,6 +286,18 @@ private apiPut(apiPath, apiBody = [], callback = {}) {
}
}

// HTTP POST call
private apiPost(apiPath, apiBody = [], callback = {}) {
if (state.session.securityToken) { apiBody = apiBody + [SecurityToken: state.session.securityToken ] }

try {
httpPost([ uri: getApiURL(), path: apiPath, contentType: "application/json; charset=utf-8", body: apiBody,
headers: [MyQApplicationId: getApiAppID()], ]) { response -> callback(response) }
} catch (SocketException e) {
//sendAlert("API Error: $e")
log.debug "API Error: $e"
}
}
// Updates data for devices
private updateDeviceData() {
// automatically checks if the token has expired, if so login again
Expand Down