Skip to content

Commit

Permalink
Added "Set Switch" action
Browse files Browse the repository at this point in the history
  • Loading branch information
ripnet committed Feb 7, 2021
1 parent ec7e45c commit 9a1bd66
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
47 changes: 41 additions & 6 deletions pi/hubitat_pi.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@
<div class="sdpi-heading">Button Specific</div>
<div class="sdpi-item">
<div class="sdpi-item-label">Device</div>
<select class="spdi-item-value" id="device">
<select class="sdpi-item-value" id="device">
<option disabled selected>set hostname</option>
</select>
</div>
<div class="sdpi-item action-set" type="radio">
<div class="sdpi-item-label">Action</div>
<div class="sdpi-item-value">
<span class="sdpi-item-child">
<input type="radio" id="action_on" name="action" value="on">
<label for="action_on" class="sdpi-item-label"><span></span>on</label>
</span>
<span class="sdpi-item-child">
<input type="radio" id="action_off" name="action" value="off">
<label for="action_off" class="sdpi-item-label"><span></span>off</label>
</span>
</div>
</div>

</div>
<script src="../resources/js/jquery-3.5.1.min.js"></script>
Expand All @@ -35,6 +48,9 @@

let devices = [];

let action = null;


waitForSD();
function waitForSD() {
// Wait until streamdeck has connected
Expand All @@ -48,7 +64,15 @@
// StreamDeck connected, subscribe to events
$sd.subscribe('didReceiveGlobalSettings', receivedGlobalSettings);
$sd.subscribe('didReceiveSettings', receivedSettings);
action = $sd.actionInfo.action;
console.log(action);
if (action === 'com.ripnet.hubitat.setswitch.action') {
$('.action-set').show();
} else {
$('.action-set').hide();
}
$sd.sendEvents(); // ready!
$sd.getSettings();
}


Expand All @@ -64,14 +88,16 @@
$('#device').change(function() {
let newSettings = Object.assign({}, localSettings);
newSettings["device"] = $(this).val();
// let deviceName = $('#device option:selected').text();
// deviceName = deviceName.replace(' Switch', '');
// deviceName = deviceName.replace(' Light', '');
// deviceName = deviceName.replace(' Fan', '');
// setTitle(deviceName);
setSettings(newSettings);
});

$('input[type="radio"][name="action"]').change(function() {
let newSettings = Object.assign({}, localSettings);
newSettings["action"] = $(this).val();
setSettings(newSettings);
});


});

function receivedGlobalSettings(data) {
Expand All @@ -85,6 +111,12 @@

function receivedSettings(data) {
localSettings = data.payload.settings || {};
if (action === 'com.ripnet.hubitat.setswitch.action') {
console.log("here")
if (localSettings.action) {
$('input[type="radio"][name="action"][value="'+localSettings.action+'"]').attr('checked', true);
}
}
}


Expand All @@ -105,6 +137,9 @@
// load the devices from the array into the HTML select
let d = $('#device');
d.empty();
if (!localSettings.hasOwnProperty('device')) {
d.append($("<option />"))
}
devices.forEach(function(device) {
d.append($("<option />").val(device.id).text(device.name));
});
Expand Down
26 changes: 19 additions & 7 deletions plugin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@

function appear(data) {
let settings = data.payload.settings || {};
instances[data.context] = {settings: settings, state: 'unknown'};
instances[data.context] = {action: data.action, settings: settings, state: 'unknown'};
if (data.payload.settings.device) {
hubitat.pollState(data.context);
} else {
Expand All @@ -145,13 +145,25 @@
}

function buttonPress(data) {
instances[data.context].settings = data.payload.settings;
const current = instances[data.context].state;
if (current === 'on') {
hubitat.setState(data.context, 'off');
} else if (current === 'off') {
hubitat.setState(data.context, 'on');
instances[data.context]['settings'] = data.payload.settings;

switch (data.action) {
case 'com.ripnet.hubitat.toggleswitch.action':
const current = instances[data.context].state;
if (current === 'on') {
hubitat.setState(data.context, 'off');
} else if (current === 'off') {
hubitat.setState(data.context, 'on');
}
break;
case 'com.ripnet.hubitat.setswitch.action':
if (instances[data.context]['settings'].action) {
hubitat.setState(data.context, instances[data.context].settings.action);
}
break;
}


}


Expand Down

0 comments on commit 9a1bd66

Please sign in to comment.