diff --git a/dialLayout.json b/dialLayout.json new file mode 100644 index 0000000..0e7e3c6 --- /dev/null +++ b/dialLayout.json @@ -0,0 +1,40 @@ +{ + "id": "$B1", + "items": [ + { + "key": "title", + "type": "text", + "rect": [ 16, 10, 136, 24 ], + "font": { + "size": 16, + "weight": 600 + }, + "alignment": "left" + }, + { + "key": "icon", + "type": "pixmap", + "rect": [ 16, 40, 48, 48 ] + }, + { + "key": "value", + "type": "text", + "rect": [ 76, 40, 108, 32 ], + "font": { + "size": 24, + "weight": 600 + }, + "alignment": "right" + }, + { + "key": "indicator", + "type": "gbar", + "rect": [ 76, 74, 108, 20 ], + "value": 0, + "subtype": 4, + "bar_h": 12, + "border_w": 0, + "bar_bg_c": "0:#427018,1:#427018" + } + ] + } \ No newline at end of file diff --git a/manifest.json b/manifest.json index 10a9b36..6d291bc 100644 --- a/manifest.json +++ b/manifest.json @@ -5,7 +5,7 @@ "Name": "Toggle Switch", "States": [ { - "Image": "resources/img/light_gray", + "Image": "resources/img/light_gray.png", "TitleAlignment": "bottom", "FontSize": "12", "Title": "Switch" @@ -15,6 +15,29 @@ "Tooltip": "This action toggles a switch", "UUID": "com.ripnet.hubitat.toggleswitch.action" }, + { + "Icon": "resources/img/toggle", + "Name": "Dial Switch", + "States": [ + { + "Image": "resources/img/bulb_gray.png", + "TitleAlignment": "bottom", + "FontSize": "12", + "Title": "Switch" + } + ], + "Controllers": [ "Encoder" ], + "Encoder": { + "layout": "dialLayout.json", + "TriggerDescription": { + "Rotate": "Increase/Decrease Level", + "Push": "Toggle On/Off" + } + }, + "SupportedInMultiActions": true, + "Tooltip": "This action controls a switch with a dial", + "UUID": "com.ripnet.hubitat.dialswitch.action" + }, { "Icon": "resources/img/toggle", "Name": "Set Switch", diff --git a/plugin/index.html b/plugin/index.html index 575690b..1fd4757 100644 --- a/plugin/index.html +++ b/plugin/index.html @@ -23,8 +23,9 @@ function start() { // StreamDeck connected, subscribe to events + $sd.subscribe('dialRotate', dialRotate) + $sd.subscribe('dialPress', dialPress) $sd.subscribe('keyDown', keyDown) - //$sd.subscribe('keyUp', buttonPress); $sd.subscribe('keyUp', keyUp); $sd.subscribe('willAppear', appear); $sd.subscribe('didReceiveGlobalSettings', receivedGlobalSettings); @@ -32,11 +33,6 @@ $sd.sendEvents(); // ready! } - - - - - const hubitat = { start: function() { let url = new URL(globalSettings.hostname) @@ -125,20 +121,35 @@ } function updateState(instance) { + const level = instances[instance].level; const state = instances[instance].state; - switch (state) { case "on": - $sd.setImageFile(instance, 'light_green'); + $sd.setImageFile(instance, 'bulb_green'); + $sd.setFeedback(instance, { + "icon": "./resources/img/bulb_green.png", + "value": level.toString(), + "indicator": { "value": level, "enabled": true } + }) break; case "off": - $sd.setImageFile(instance, 'light_red'); + $sd.setImageFile(instance, 'bulb_red'); + $sd.setFeedback(instance, { + "icon": "./resources/img/bulb_red.png", + "value": "0", + "indicator": { "value": 0, "enabled": false } + }) break; case "setLevel": break; default: - $sd.setImageFile(instance, 'light_gray'); + $sd.setImageFile(instance, 'bulb_gray'); + $sd.setFeedback(instance, { + "icon": "./resources/img/bulb_gray.png", + "value": "0", + "indicator": { "value": 0, "enabled": false } + }) } } @@ -147,23 +158,19 @@ const state = instances[instance].state; if (state === "on") { - if (level < 17) { - $sd.setImageFile(instance, 'light_green_1'); - } else if (level < 34) { - $sd.setImageFile(instance, 'light_green_2'); - } else if (level < 50) { - $sd.setImageFile(instance, 'light_green_3'); - } else if (level < 67) { - $sd.setImageFile(instance, 'light_green_4'); - } else if (level < 84) { - $sd.setImageFile(instance, 'light_green_5'); - } else { - $sd.setImageFile(instance, 'light_green'); - } + + $sd.setFeedback(instance, { + "icon": "./resources/img/bulb_green.png", + "value": level.toString(), + "indicator": { "value": level, "enabled": true } + }) + + $sd.setImageFile(instance, 'bulb_green'); + } else if (state === "off") { - $sd.setImageFile(instance, 'light_red'); + $sd.setImageFile(instance, 'bulb_red'); } else { - $sd.setImageFile(instance, 'light_gray'); + $sd.setImageFile(instance, 'bulb_gray'); } } @@ -235,7 +242,6 @@ } - function keyUp(data) { switch (data.action) { @@ -250,6 +256,7 @@ } } + function keyDown(data) { instances[data.context]['settings'] = data.payload.settings; switch (data.action) { @@ -328,6 +335,7 @@ } } + function multi_doubleClick(data) { let newLevel = instances[data.context].level - 17; @@ -342,10 +350,39 @@ } + function multi_held(data) { hubitat.setState(data.context, "off"); } + function dialRotate(data) { + let adjustment = data.payload.ticks * 2 //adjust multiplier for knob sensitivity. + let newLevel = instances[data.context].level + adjustment; + + if (newLevel < 1) { + hubitat.setState(data.context, "off"); + updateLevel(data.context); + } else { + hubitat.setState(data.context, "setLevel", String(newLevel)); + instances[data.context].level = newLevel; + updateLevel(data.context); + } + + } + + function dialPress(data) { + + if (!data.payload.pressed) { + return; + } + + if (instances[data.context].state === "off") { + hubitat.setState(data.context, "on"); + } else { + hubitat.setState(data.context, "off"); + } + + } diff --git a/resources/img/bulb_gray.png b/resources/img/bulb_gray.png new file mode 100644 index 0000000..633d3d3 Binary files /dev/null and b/resources/img/bulb_gray.png differ diff --git a/resources/img/bulb_green.png b/resources/img/bulb_green.png new file mode 100644 index 0000000..383ef7c Binary files /dev/null and b/resources/img/bulb_green.png differ diff --git a/resources/img/bulb_red.png b/resources/img/bulb_red.png new file mode 100644 index 0000000..479cf2d Binary files /dev/null and b/resources/img/bulb_red.png differ diff --git a/resources/img/light_gray.png b/resources/img/light_gray.png deleted file mode 100644 index 50ea18b..0000000 Binary files a/resources/img/light_gray.png and /dev/null differ diff --git a/resources/img/light_gray@2x.png b/resources/img/light_gray@2x.png deleted file mode 100644 index 50ea18b..0000000 Binary files a/resources/img/light_gray@2x.png and /dev/null differ diff --git a/resources/img/light_green.png b/resources/img/light_green.png deleted file mode 100644 index a80e494..0000000 Binary files a/resources/img/light_green.png and /dev/null differ diff --git a/resources/img/light_green_1.png b/resources/img/light_green_1.png deleted file mode 100644 index ff9eea3..0000000 Binary files a/resources/img/light_green_1.png and /dev/null differ diff --git a/resources/img/light_green_2.png b/resources/img/light_green_2.png deleted file mode 100644 index 8a9d174..0000000 Binary files a/resources/img/light_green_2.png and /dev/null differ diff --git a/resources/img/light_green_3.png b/resources/img/light_green_3.png deleted file mode 100644 index 500a6db..0000000 Binary files a/resources/img/light_green_3.png and /dev/null differ diff --git a/resources/img/light_green_4.png b/resources/img/light_green_4.png deleted file mode 100644 index c5c917c..0000000 Binary files a/resources/img/light_green_4.png and /dev/null differ diff --git a/resources/img/light_green_5.png b/resources/img/light_green_5.png deleted file mode 100644 index 2841054..0000000 Binary files a/resources/img/light_green_5.png and /dev/null differ diff --git a/resources/img/light_red.png b/resources/img/light_red.png deleted file mode 100644 index 38ee92e..0000000 Binary files a/resources/img/light_red.png and /dev/null differ diff --git a/resources/js/streamdeck.js b/resources/js/streamdeck.js index 69ed064..5ce1077 100644 --- a/resources/js/streamdeck.js +++ b/resources/js/streamdeck.js @@ -135,6 +135,11 @@ class StreamDeck extends Emitter { this.sendMessage({'event': 'setSettings', 'context': context, 'payload': settings}); } + setFeedback(context, feedback) { + console.log(feedback) + this.sendMessage({'event': 'setFeedback', 'context': context, 'payload': feedback}); + } + ready = false; } let $sd = {ready: false};