Skip to content

Commit

Permalink
Ability to toggle sim devices online & offline (SmartThingsCommunity#…
Browse files Browse the repository at this point in the history
…3290)

* Simulated Lock
* Simulated Thermostat
  * also minor UI tweaks
* Simulated Switch
* Simulated Dimmer Switch
* Simulated Dimmable Bulb
* Simulated RGB Bulb
* Simulated RGBW Bulb
* Simulated White Color Temperature Bulb
  • Loading branch information
surfous authored Jul 30, 2018
1 parent d10ba2a commit 1f6ae18
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ metadata {
capability "Switch Level"
capability "Refresh"
capability "Configuration"

command "markDeviceOnline"
command "markDeviceOffline"
}

preferences {
Expand All @@ -34,8 +37,8 @@ metadata {
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
attributeState "on", label:'${name}', action:"switch.off", icon:"st.switches.light.on", backgroundColor:"#00A0DC", nextState:"turningOff"
attributeState "off", label:'${name}', action:"switch.on", icon:"st.switches.light.off", backgroundColor:"#FFFFFF", nextState:"turningOn"
attributeState "turningOn", label:'Turning On', action:"switch.off", icon:"st.switches.light.on", backgroundColor:"#00A0DC", nextState:"on"
attributeState "turningOff", label:'Turning Off', action:"switch.on", icon:"st.switches.light.off", backgroundColor:"#FFFFFF", nextState:"off"
attributeState "turningOn", label:'Turning On', action:"switch.off", icon:"st.switches.light.on", backgroundColor:"#FFFFFF", nextState:"on"
attributeState "turningOff", label:'Turning Off', action:"switch.on", icon:"st.switches.light.off", backgroundColor:"#00A0DC", nextState:"off"
}
tileAttribute ("device.level", key: "SLIDER_CONTROL") {
attributeState "level", action: "setLevel"
Expand All @@ -45,16 +48,22 @@ metadata {
}
}

standardTile("refresh", "device.switch", width: 1, height: 1, inactiveLabel: false, decoration: "flat") {
standardTile("refresh", "device.switch", width: 2, height: 2, inactiveLabel: false, decoration: "flat") {
state "default", label: "", action:"refresh.refresh", icon:"st.secondary.refresh"
}
valueTile("reset", "device.switch", inactiveLabel: false, decoration: "flat", width: 1, height: 1) {
valueTile("reset", "device.switch", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label: "Reset", action: "configure"
}

main(["switch"])
details(["switch", "refresh", "reset"])
standardTile("deviceHealthControl", "device.healthStatus", decoration: "flat", width: 2, height: 2, inactiveLabel: false) {
state "online", label: "ONLINE", backgroundColor: "#00A0DC", action: "markDeviceOffline", icon: "st.Health & Wellness.health9", nextState: "goingOffline", defaultState: true
state "offline", label: "OFFLINE", backgroundColor: "#E86D13", action: "markDeviceOnline", icon: "st.Health & Wellness.health9", nextState: "goingOnline"
state "goingOnline", label: "Going ONLINE", backgroundColor: "#FFFFFF", icon: "st.Health & Wellness.health9"
state "goingOffline", label: "Going OFFLINE", backgroundColor: "#FFFFFF", icon: "st.Health & Wellness.health9"
}

main(["switch"])
details(["switch", "refresh", "deviceHealthControl", "reset"])
}
}

Expand All @@ -77,7 +86,7 @@ def parse(String description) {

def installed() {
log.trace "Executing 'installed'"
initialize()
configure()
}

def updated() {
Expand All @@ -88,9 +97,6 @@ def updated() {
//
// command methods
//
def ping() {
refresh()
}

def refresh() {
log.trace "Executing 'refresh'"
Expand All @@ -100,6 +106,11 @@ def refresh() {

def configure() {
log.trace "Executing 'configure'"

// for HealthCheck
sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme:"untracked"].encodeAsJson(), displayed: false)
markDeviceOnline()

initialize()
}

Expand Down Expand Up @@ -130,6 +141,14 @@ def setLevel(value, duration) {
setLevel(value)
}

def markDeviceOnline() {
setDeviceHealth("online")
}

def markDeviceOffline() {
setDeviceHealth("offline")
}

private String getSwitch() {
def switchState = device.currentState("switch")
return switchState ? switchState.getStringValue() : "off"
Expand All @@ -140,19 +159,25 @@ private Integer getLevel() {
return levelState ? levelState.getIntegerValue() : 100
}

private setDeviceHealth(String healthState) {
log.debug("healthStatus: ${device.currentValue('healthStatus')}; DeviceWatch-DeviceStatus: ${device.currentValue('DeviceWatch-DeviceStatus')}")
// ensure healthState is valid
List validHealthStates = ["online", "offline"]
healthState = validHealthStates.contains(healthState) ? healthState : device.currentValue("healthStatus")
// set the healthState
sendEvent(name: "DeviceWatch-DeviceStatus", value: healthState)
sendEvent(name: "healthStatus", value: healthState)
}

private initialize() {
log.trace "Executing 'initialize'"
sendEvent(name: "switch", value: "off")
sendEvent(name: "level", value: 100)

sendEvent(name: "DeviceWatch-DeviceStatus", value: "online")
sendEvent(name: "healthStatus", value: "online")
sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme:"untracked"].encodeAsJson(), displayed: false)
}

private Map buildSetLevelEvent(value) {
Integer intValue = value as Integer
Integer newLevel = Math.max(Math.min(intValue, 99), 0)
Integer newLevel = Math.max(Math.min(intValue, 100), 0)
Map eventMap = [name: "level", value: newLevel, unit: "%"]
return eventMap
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
*/
metadata {
definition (name: "Simulated Dimmer Switch", namespace: "smartthings/testing", author: "SmartThings", runLocally: false, mnmn: "SmartThings", vid: "generic-dimmer") {
definition (name: "Simulated Dimmer Switch", namespace: "smartthings/testing", author: "SmartThings", ocfDeviceType: "oic.d.light", runLocally: false, mnmn: "SmartThings", vid: "generic-dimmer") {
capability "Health Check"
capability "Actuator"
capability "Sensor"
Expand All @@ -27,6 +27,9 @@ metadata {
command "onPhysical"
command "offPhysical"
command "setLevelPhysical"

command "markDeviceOnline"
command "markDeviceOffline"
}

preferences {
Expand All @@ -37,8 +40,8 @@ metadata {
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
attributeState "on", label:'${name}', action:"switch.off", icon:"st.Home.home30", backgroundColor:"#00A0DC", nextState:"turningOff"
attributeState "off", label:'${name}', action:"switch.on", icon:"st.Home.home30", backgroundColor:"#FFFFFF", nextState:"turningOn", defaultState: true
attributeState "turningOn", label:'Turning On', action:"switch.off", icon:"st.Home.home30", backgroundColor:"#00A0DC", nextState:"turningOn"
attributeState "turningOff", label:'Turning Off', action:"switch.on", icon:"st.Home.home30", backgroundColor:"#FFFFFF", nextState:"turningOff"
attributeState "turningOn", label:'Turning On', action:"switch.off", icon:"st.Home.home30", backgroundColor:"#FFFFFF", nextState:"turningOn"
attributeState "turningOff", label:'Turning Off', action:"switch.on", icon:"st.Home.home30", backgroundColor:"#00A0DC", nextState:"turningOff"
}
tileAttribute ("device.level", key: "SLIDER_CONTROL") {
attributeState "level", action: "setLevel"
Expand Down Expand Up @@ -66,16 +69,22 @@ metadata {
state "physicalLevel", action: "setLevelPhysical"
}

standardTile("refresh", "device.switch", width: 1, height: 1, inactiveLabel: false, decoration: "flat") {
standardTile("refresh", "device.switch", width: 2, height: 2, inactiveLabel: false, decoration: "flat") {
state "default", label: "", action:"refresh.refresh", icon:"st.secondary.refresh"
}
valueTile("reset", "device.switch", inactiveLabel: false, decoration: "flat", width: 1, height: 1) {
valueTile("reset", "device.switch", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", label: "Reset", action: "configure"
}

main(["switch"])
details(["switch", "physicalLabel", "physicalOn", "physicalOff", "physicalLevelLabel", "physicalLevelSlider", "refresh", "reset"])
standardTile("deviceHealthControl", "device.healthStatus", decoration: "flat", width: 2, height: 2, inactiveLabel: false) {
state "online", label: "ONLINE", backgroundColor: "#00A0DC", action: "markDeviceOffline", icon: "st.Health & Wellness.health9", nextState: "goingOffline", defaultState: true
state "offline", label: "OFFLINE", backgroundColor: "#E86D13", action: "markDeviceOnline", icon: "st.Health & Wellness.health9", nextState: "goingOnline"
state "goingOnline", label: "Going ONLINE", backgroundColor: "#FFFFFF", icon: "st.Health & Wellness.health9"
state "goingOffline", label: "Going OFFLINE", backgroundColor: "#FFFFFF", icon: "st.Health & Wellness.health9"
}

main(["switch"])
details(["switch", "physicalLabel", "physicalOn", "physicalOff", "physicalLevelLabel", "physicalLevelSlider", "deviceHealthControl", "refresh", "reset"])
}
}

Expand All @@ -98,7 +107,7 @@ def parse(String description) {

def installed() {
log.trace "Executing 'installed'"
initialize()
configure()
}

def updated() {
Expand All @@ -116,6 +125,12 @@ def refresh() {

def configure() {
log.trace "Executing 'configure'"
// this would be for a physical device when it gets a handler assigned to it

// for HealthCheck
sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme: "untracked"].encodeAsJson(), displayed: false)
markDeviceOnline()

initialize()
}

Expand Down Expand Up @@ -146,15 +161,28 @@ def setLevel(value, duration) {
setLevel(value)
}

def markDeviceOnline() {
setDeviceHealth("online")
}

def markDeviceOffline() {
setDeviceHealth("offline")
}

private setDeviceHealth(String healthState) {
log.debug("healthStatus: ${device.currentValue('healthStatus')}; DeviceWatch-DeviceStatus: ${device.currentValue('DeviceWatch-DeviceStatus')}")
// ensure healthState is valid
List validHealthStates = ["online", "offline"]
healthState = validHealthStates.contains(healthState) ? healthState : device.currentValue("healthStatus")
// set the healthState
sendEvent(name: "DeviceWatch-DeviceStatus", value: healthState)
sendEvent(name: "healthStatus", value: healthState)
}

private initialize() {
log.trace "Executing 'initialize'"
sendEvent(name: "switch", value: "off")
sendEvent(name: "level", value: 100)


sendEvent(name: "DeviceWatch-DeviceStatus", value: "online")
sendEvent(name: "healthStatus", value: "online")
sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme:"untracked"].encodeAsJson(), displayed: false)
}

private Map buildSetLevelEvent(value) {
Expand Down
Loading

0 comments on commit 1f6ae18

Please sign in to comment.