mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-20 13:20:53 +00:00
Revert "Revert LIFX device watch"
This commit is contained in:
@@ -11,9 +11,9 @@ metadata {
|
|||||||
capability "Color Temperature"
|
capability "Color Temperature"
|
||||||
capability "Switch"
|
capability "Switch"
|
||||||
capability "Switch Level" // brightness
|
capability "Switch Level" // brightness
|
||||||
capability "Polling"
|
|
||||||
capability "Refresh"
|
capability "Refresh"
|
||||||
capability "Sensor"
|
capability "Sensor"
|
||||||
|
capability "Health Check"
|
||||||
}
|
}
|
||||||
|
|
||||||
simulator {
|
simulator {
|
||||||
@@ -23,7 +23,6 @@ metadata {
|
|||||||
tiles(scale: 2) {
|
tiles(scale: 2) {
|
||||||
multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
|
multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
|
||||||
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
|
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
|
||||||
attributeState "unreachable", label: "?", action:"refresh.refresh", icon:"http://hosted.lifx.co/smartthings/v1/196xUnreachable.png", backgroundColor:"#666666"
|
|
||||||
attributeState "on", label:'${name}', action:"switch.off", icon:"http://hosted.lifx.co/smartthings/v1/196xOn.png", backgroundColor:"#79b821", nextState:"turningOff"
|
attributeState "on", label:'${name}', action:"switch.off", icon:"http://hosted.lifx.co/smartthings/v1/196xOn.png", backgroundColor:"#79b821", nextState:"turningOff"
|
||||||
attributeState "off", label:'${name}', action:"switch.on", icon:"http://hosted.lifx.co/smartthings/v1/196xOff.png", backgroundColor:"#ffffff", nextState:"turningOn"
|
attributeState "off", label:'${name}', action:"switch.on", icon:"http://hosted.lifx.co/smartthings/v1/196xOff.png", backgroundColor:"#ffffff", nextState:"turningOn"
|
||||||
attributeState "turningOn", label:'Turning on', action:"switch.off", icon:"http://hosted.lifx.co/smartthings/v1/196xOn.png", backgroundColor:"#79b821", nextState:"turningOff"
|
attributeState "turningOn", label:'Turning on', action:"switch.off", icon:"http://hosted.lifx.co/smartthings/v1/196xOn.png", backgroundColor:"#79b821", nextState:"turningOff"
|
||||||
@@ -64,12 +63,8 @@ metadata {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse events into attributes
|
void installed() {
|
||||||
def parse(String description) {
|
sendEvent(name: "DeviceWatch-Enroll", value: "{\"protocol\": \"cloud\", \"scheme\":\"untracked\", \"hubHardwareId\": \"${device?.hub?.hardwareID}\"}")
|
||||||
if (description == 'updated') {
|
|
||||||
return // don't poll when config settings is being updated as it may time out
|
|
||||||
}
|
|
||||||
poll()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle commands
|
// handle commands
|
||||||
@@ -192,14 +187,17 @@ def off() {
|
|||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
def poll() {
|
def refresh() {
|
||||||
log.debug "Executing 'poll' for ${device} ${this} ${device.deviceNetworkId}"
|
log.debug "Executing 'refresh'"
|
||||||
|
|
||||||
def resp = parent.apiGET("/lights/${selector()}")
|
def resp = parent.apiGET("/lights/${selector()}")
|
||||||
if (resp.status == 404) {
|
if (resp.status == 404) {
|
||||||
sendEvent(name: "switch", value: "unreachable")
|
state.online = false
|
||||||
|
sendEvent(name: "DeviceWatch-DeviceStatusUpdate", value: "offline", displayed: false)
|
||||||
|
log.warn "$device is Offline"
|
||||||
return []
|
return []
|
||||||
} else if (resp.status != 200) {
|
} else if (resp.status != 200) {
|
||||||
log.error("Unexpected result in poll(): [${resp.status}] ${resp.data}")
|
log.error("Unexpected result in refresh(): [${resp.status}] ${resp.data}")
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
def data = resp.data[0]
|
def data = resp.data[0]
|
||||||
@@ -208,19 +206,20 @@ def poll() {
|
|||||||
sendEvent(name: "label", value: data.label)
|
sendEvent(name: "label", value: data.label)
|
||||||
sendEvent(name: "level", value: Math.round((data.brightness ?: 1) * 100))
|
sendEvent(name: "level", value: Math.round((data.brightness ?: 1) * 100))
|
||||||
sendEvent(name: "switch.setLevel", value: Math.round((data.brightness ?: 1) * 100))
|
sendEvent(name: "switch.setLevel", value: Math.round((data.brightness ?: 1) * 100))
|
||||||
sendEvent(name: "switch", value: data.connected ? data.power : "unreachable")
|
sendEvent(name: "switch", value: data.power)
|
||||||
sendEvent(name: "color", value: colorUtil.hslToHex((data.color.hue / 3.6) as int, (data.color.saturation * 100) as int))
|
sendEvent(name: "color", value: colorUtil.hslToHex((data.color.hue / 3.6) as int, (data.color.saturation * 100) as int))
|
||||||
sendEvent(name: "hue", value: data.color.hue / 3.6)
|
sendEvent(name: "hue", value: data.color.hue / 3.6)
|
||||||
sendEvent(name: "saturation", value: data.color.saturation * 100)
|
sendEvent(name: "saturation", value: data.color.saturation * 100)
|
||||||
sendEvent(name: "colorTemperature", value: data.color.kelvin)
|
sendEvent(name: "colorTemperature", value: data.color.kelvin)
|
||||||
sendEvent(name: "model", value: "${data.product.company} ${data.product.name}")
|
sendEvent(name: "model", value: data.product.name)
|
||||||
|
|
||||||
return []
|
if (data.connected) {
|
||||||
|
sendEvent(name: "DeviceWatch-DeviceStatus", value: "online", displayed: false)
|
||||||
|
log.debug "$device is Online"
|
||||||
|
} else {
|
||||||
|
sendEvent(name: "DeviceWatch-DeviceStatus", value: "offline", displayed: false)
|
||||||
|
log.warn "$device is Offline"
|
||||||
}
|
}
|
||||||
|
|
||||||
def refresh() {
|
|
||||||
log.debug "Executing 'refresh'"
|
|
||||||
poll()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def selector() {
|
def selector() {
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ metadata {
|
|||||||
capability "Color Temperature"
|
capability "Color Temperature"
|
||||||
capability "Switch"
|
capability "Switch"
|
||||||
capability "Switch Level" // brightness
|
capability "Switch Level" // brightness
|
||||||
capability "Polling"
|
|
||||||
capability "Refresh"
|
capability "Refresh"
|
||||||
capability "Sensor"
|
capability "Sensor"
|
||||||
|
capability "Health Check"
|
||||||
}
|
}
|
||||||
|
|
||||||
simulator {
|
simulator {
|
||||||
@@ -22,13 +22,12 @@ metadata {
|
|||||||
tiles(scale: 2) {
|
tiles(scale: 2) {
|
||||||
multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
|
multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
|
||||||
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
|
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
|
||||||
attributeState "unreachable", label: "?", action:"refresh.refresh", icon:"http://hosted.lifx.co/smartthings/v1/196xUnreachable.png", backgroundColor:"#666666"
|
|
||||||
attributeState "on", label:'${name}', action:"switch.off", icon:"http://hosted.lifx.co/smartthings/v1/196xOn.png", backgroundColor:"#79b821", nextState:"turningOff"
|
attributeState "on", label:'${name}', action:"switch.off", icon:"http://hosted.lifx.co/smartthings/v1/196xOn.png", backgroundColor:"#79b821", nextState:"turningOff"
|
||||||
attributeState "off", label:'${name}', action:"switch.on", icon:"http://hosted.lifx.co/smartthings/v1/196xOff.png", backgroundColor:"#ffffff", nextState:"turningOn"
|
attributeState "off", label:'${name}', action:"switch.on", icon:"http://hosted.lifx.co/smartthings/v1/196xOff.png", backgroundColor:"#ffffff", nextState:"turningOn"
|
||||||
attributeState "turningOn", label:'Turning on', action:"switch.off", icon:"http://hosted.lifx.co/smartthings/v1/196xOn.png", backgroundColor:"#79b821", nextState:"turningOff"
|
attributeState "turningOn", label:'Turning on', action:"switch.off", icon:"http://hosted.lifx.co/smartthings/v1/196xOn.png", backgroundColor:"#79b821", nextState:"turningOff"
|
||||||
attributeState "turningOff", label:'Turning off', action:"switch.on", icon:"http://hosted.lifx.co/smartthings/v1/196xOff.png", backgroundColor:"#ffffff", nextState:"turningOn"
|
attributeState "turningOff", label:'Turning off', action:"switch.on", icon:"http://hosted.lifx.co/smartthings/v1/196xOff.png", backgroundColor:"#ffffff", nextState:"turningOn"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tileAttribute ("device.level", key: "SLIDER_CONTROL") {
|
tileAttribute ("device.level", key: "SLIDER_CONTROL") {
|
||||||
attributeState "level", action:"switch level.setLevel"
|
attributeState "level", action:"switch level.setLevel"
|
||||||
}
|
}
|
||||||
@@ -53,15 +52,10 @@ metadata {
|
|||||||
main "switch"
|
main "switch"
|
||||||
details(["switch", "colorTempSliderControl", "colorTemp", "refresh"])
|
details(["switch", "colorTempSliderControl", "colorTemp", "refresh"])
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse events into attributes
|
void installed() {
|
||||||
def parse(String description) {
|
sendEvent(name: "DeviceWatch-Enroll", value: "{\"protocol\": \"cloud\", \"scheme\":\"untracked\", \"hubHardwareId\": \"${device?.hub?.hardwareID}\"}")
|
||||||
if (description == 'updated') {
|
|
||||||
return // don't poll when config settings is being updated as it may time out
|
|
||||||
}
|
|
||||||
poll()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle commands
|
// handle commands
|
||||||
@@ -122,14 +116,17 @@ def off() {
|
|||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
def poll() {
|
def refresh() {
|
||||||
log.debug "Executing 'poll' for ${device} ${this} ${device.deviceNetworkId}"
|
log.debug "Executing 'refresh'"
|
||||||
|
|
||||||
def resp = parent.apiGET("/lights/${selector()}")
|
def resp = parent.apiGET("/lights/${selector()}")
|
||||||
if (resp.status == 404) {
|
if (resp.status == 404) {
|
||||||
sendEvent(name: "switch", value: "unreachable")
|
state.online = false
|
||||||
|
sendEvent(name: "DeviceWatch-DeviceStatusUpdate", value: "offline", displayed: false)
|
||||||
|
log.warn "$device is Offline"
|
||||||
return []
|
return []
|
||||||
} else if (resp.status != 200) {
|
} else if (resp.status != 200) {
|
||||||
log.error("Unexpected result in poll(): [${resp.status}] ${resp.data}")
|
log.error("Unexpected result in refresh(): [${resp.status}] ${resp.data}")
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
def data = resp.data[0]
|
def data = resp.data[0]
|
||||||
@@ -137,16 +134,17 @@ def poll() {
|
|||||||
sendEvent(name: "label", value: data.label)
|
sendEvent(name: "label", value: data.label)
|
||||||
sendEvent(name: "level", value: Math.round((data.brightness ?: 1) * 100))
|
sendEvent(name: "level", value: Math.round((data.brightness ?: 1) * 100))
|
||||||
sendEvent(name: "switch.setLevel", value: Math.round((data.brightness ?: 1) * 100))
|
sendEvent(name: "switch.setLevel", value: Math.round((data.brightness ?: 1) * 100))
|
||||||
sendEvent(name: "switch", value: data.connected ? data.power : "unreachable")
|
sendEvent(name: "switch", value: data.power)
|
||||||
sendEvent(name: "colorTemperature", value: data.color.kelvin)
|
sendEvent(name: "colorTemperature", value: data.color.kelvin)
|
||||||
sendEvent(name: "model", value: data.product.name)
|
sendEvent(name: "model", value: data.product.name)
|
||||||
|
|
||||||
return []
|
if (data.connected) {
|
||||||
|
sendEvent(name: "DeviceWatch-DeviceStatus", value: "online", displayed: false)
|
||||||
|
log.debug "$device is Online"
|
||||||
|
} else {
|
||||||
|
sendEvent(name: "DeviceWatch-DeviceStatus", value: "offline", displayed: false)
|
||||||
|
log.warn "$device is Offline"
|
||||||
}
|
}
|
||||||
|
|
||||||
def refresh() {
|
|
||||||
log.debug "Executing 'refresh'"
|
|
||||||
poll()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def selector() {
|
def selector() {
|
||||||
|
|||||||
@@ -242,8 +242,6 @@ def installed() {
|
|||||||
} else {
|
} else {
|
||||||
initialize()
|
initialize()
|
||||||
}
|
}
|
||||||
// Check for new devices and remove old ones every 3 hours
|
|
||||||
runEvery3Hours('updateDevices')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// called after settings are changed
|
// called after settings are changed
|
||||||
@@ -271,9 +269,19 @@ private removeChildDevices(devices) {
|
|||||||
def initialize() {
|
def initialize() {
|
||||||
log.debug "initialize"
|
log.debug "initialize"
|
||||||
updateDevices()
|
updateDevices()
|
||||||
|
// Check for new devices and remove old ones every 3 hours
|
||||||
|
runEvery5Minutes('updateDevices')
|
||||||
|
setupDeviceWatch()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Misc
|
// Misc
|
||||||
|
private setupDeviceWatch() {
|
||||||
|
def hub = location.hubs[0]
|
||||||
|
// Make sure that all child devices are enrolled in device watch
|
||||||
|
getChildDevices().each {
|
||||||
|
it.sendEvent(name: "DeviceWatch-Enroll", value: "{\"protocol\": \"LAN\", \"scheme\":\"untracked\", \"hubHardwareId\": \"${hub?.hub?.hardwareID}\"}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Map apiRequestHeaders() {
|
Map apiRequestHeaders() {
|
||||||
return ["Authorization": "Bearer ${state.lifxAccessToken}",
|
return ["Authorization": "Bearer ${state.lifxAccessToken}",
|
||||||
@@ -376,7 +384,7 @@ def updateDevices() {
|
|||||||
def data = [
|
def data = [
|
||||||
label: device.label,
|
label: device.label,
|
||||||
level: Math.round((device.brightness ?: 1) * 100),
|
level: Math.round((device.brightness ?: 1) * 100),
|
||||||
switch: device.connected ? device.power : "unreachable",
|
switch: device.power,
|
||||||
colorTemperature: device.color.kelvin
|
colorTemperature: device.color.kelvin
|
||||||
]
|
]
|
||||||
if (device.product.capabilities.has_color) {
|
if (device.product.capabilities.has_color) {
|
||||||
@@ -387,18 +395,42 @@ def updateDevices() {
|
|||||||
} else {
|
} else {
|
||||||
childDevice = addChildDevice(app.namespace, "LIFX White Bulb", device.id, null, data)
|
childDevice = addChildDevice(app.namespace, "LIFX White Bulb", device.id, null, data)
|
||||||
}
|
}
|
||||||
|
childDevice?.completedSetup = true
|
||||||
|
} else {
|
||||||
|
if (device.product.capabilities.has_color) {
|
||||||
|
sendEvent(name: "color", value: colorUtil.hslToHex((device.color.hue / 3.6) as int, (device.color.saturation * 100) as int))
|
||||||
|
sendEvent(name: "hue", value: device.color.hue / 3.6)
|
||||||
|
sendEvent(name: "saturation", value: device.color.saturation * 100)
|
||||||
}
|
}
|
||||||
|
childDevice.sendEvent(name: "label", value: device.label)
|
||||||
|
childDevice.sendEvent(name: "level", value: Math.round((device.brightness ?: 1) * 100))
|
||||||
|
childDevice.sendEvent(name: "switch.setLevel", value: Math.round((device.brightness ?: 1) * 100))
|
||||||
|
childDevice.sendEvent(name: "switch", value: device.power)
|
||||||
|
childDevice.sendEvent(name: "colorTemperature", value: device.color.kelvin)
|
||||||
|
childDevice.sendEvent(name: "model", value: device.product.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.devices[device.id] == null) {
|
||||||
|
// State missing, add it and set it to opposite status as current status to provoke event below
|
||||||
|
state.devices[device.id] = [online : !device.connected]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!state.devices[device.id]?.online && device.connected) {
|
||||||
|
// Device came online after being offline
|
||||||
|
childDevice?.sendEvent(name: "DeviceWatch-DeviceStatus", value: "online", displayed: false)
|
||||||
|
log.debug "$device is back Online"
|
||||||
|
} else if (state.devices[device.id]?.online && !device.connected) {
|
||||||
|
// Device went offline after being online
|
||||||
|
childDevice?.sendEvent(name: "DeviceWatch-DeviceStatus", value: "offline", displayed: false)
|
||||||
|
log.debug "$device went Offline"
|
||||||
|
}
|
||||||
|
state.devices[device.id] = [online: device.connected]
|
||||||
}
|
}
|
||||||
getChildDevices().findAll { !selectors.contains("${it.deviceNetworkId}") }.each {
|
getChildDevices().findAll { !selectors.contains("${it.deviceNetworkId}") }.each {
|
||||||
log.info("Deleting ${it.deviceNetworkId}")
|
log.info("Deleting ${it.deviceNetworkId}")
|
||||||
|
state.devices[it.deviceNetworkId] = null
|
||||||
deleteChildDevice(it.deviceNetworkId)
|
deleteChildDevice(it.deviceNetworkId)
|
||||||
}
|
}
|
||||||
runIn(1, 'refreshDevices') // Asynchronously refresh devices so we don't block
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def refreshDevices() {
|
|
||||||
log.info("Refreshing all devices...")
|
|
||||||
getChildDevices().each { device ->
|
|
||||||
device.refresh()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user