-
Notifications
You must be signed in to change notification settings - Fork 2
/
mqtt-openrgb-device.groovy
113 lines (99 loc) · 3.32 KB
/
mqtt-openrgb-device.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* openrgb child devices don't do very much.
* You can setColor() from rule machine (or similar)
* You can setColor() from the Device page
* You can turn if off. Turning on restores the
* last saved (on) value that isn't 0,0,0
*
* We know the off color is 0,0,0 - so on() can't VISUALLY restore
* that, so saving off makes no sense where as not changing does make
* slightly more sense.
*
* There are no hubitat events to create or respond to.
*
* We can send json string back to the parent device handler
* to send the payload to the correct mqtt topic.
*
* An awful lot the code was borrowed from 'tomw'. Thanks.
* Even more was deleted.
*/
metadata
{
definition(name: "Mqtt OpenRGB Device", namespace: "ccoupe", author: "Cecil Coupe", importUrl: "https://raw.githubusercontent.com/ccoupe/hubitat/master/mqtt-openrgb-device.groovy")
{
capability "Bulb"
capability "ColorControl"
capability "ColorMode"
capability "Initialize"
capability "Refresh"
// This parameter definition isn't correct - no errors in log
command "modes",[[type: "ENUM", name: "Mode", description: "Pick a Mode",
constraints: ["Direct", "Off", "Static", "Breathing", "Flashing",
"Spectrum Cycle", "Rainbow", "Chase Fade", "Chase"]]]
}
}
def initialize()
{
refresh()
}
def refresh()
{
parent?.refreshFromParent(device)
}
def initCommands(modeList, zoneList)
{
log.info("initCommands: ${modeList}\n\t\t${zoneList}")
state.modes = modeList
state.zones = zoneLst
}
def modes(sel)
{
// The mode in arg 'sel' has to be one of those in state.modes
if (sel in state.modes) {
jstr = groovy.json.JsonOutput.toJson(["mode":sel])
topic = getDataValue("Id")
parent = getParent()
parent.childSend(topic, jstr)
log.info("modes: publish : ${topic} => ${jstr}")
}
}
def setColor(colormap)
{
def cRGB = hubitat.helper.ColorUtils.hsvToRGB([colormap.hue, colormap.saturation, colormap.level])
def jstr = groovy.json.JsonOutput.toJson(["color": [r: cRGB[0].toInteger(),
g: cRGB[1].toInteger(),
b: cRGB[2].toInteger()]])
topic = getDataValue("Id")
//log.info ("send: ${topic} => ${jstr}")
parent = getParent()
parent.childSend(topic, jstr)
state.childColor = jstr
}
def on()
{
topic = getDataValue("Id")
jstr = state.childColor
if (!jstr) {
// we can't do on() unless we have a saved value. Make one.
log.info("on with low levels")
jstr = groovy.json.JsonOutput.toJson( [r: 30.toInteger(),
g: 30.toInteger(),
b: 30.toInteger()] )
state.childColor = jstr
}
parent = getParent()
parent.childSend(topic, jstr)
// now change the gui to match the color in state.
def jsonSlurper = new groovy.json.JsonSlurper()
def mapDev = jsonSlurper.parseText(state.childColor)
def cHSV = hubitat.helper.ColorUtils.rgbToHSV([mapDev["r"].toInteger(),
mapDev["g"].toInteger(), mapDev["b"].toInteger()])
// following line does not change the GUI - even with a browser reload
setColor(['hue': cHSV[0], 'saturation': cHSV[1], 'level': cHSV[2]])
}
def off()
{
topic = getDataValue("Id")
def jstr = groovy.json.JsonOutput.toJson( "state": "off")
parent.childSend(topic, jstr)
}