Added WeMo switch plugin for web panel

This commit is contained in:
Fabio Manganiello 2018-05-08 09:59:43 +02:00
parent 36c9437172
commit b94764436d
4 changed files with 118 additions and 5 deletions

View file

@ -0,0 +1,29 @@
#wemo-container {
background-color: #f8f8f8;
padding: 12px;
border: 1px solid #ddd;
border-radius: 10px;
font-family: "Raleway", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 400;
line-height: 38px;
letter-spacing: .1rem;
}
.wemo-device {
padding: 1.5em 1em .2em .5em;
border-radius: 10px;
}
.wemo-device:nth-of-type(odd) {
background-color: #ececec;
}
.wemo-device:hover {
background-color: #daf8e2 !important;
}
.toggle-container {
text-align: right;
padding-right: 1em;
}

View file

@ -0,0 +1,78 @@
$(document).ready(function() {
var switches,
$wemoContainer = $('#wemo-container');
var createPowerToggleElement = function(dev) {
var $powerToggle = $('<div></div>').addClass('toggle toggle--push switch-ctrl-container');
var $input = $('<input></input>').attr('type', 'checkbox')
.attr('name', dev.name).addClass('toggle--checkbox switch-ctrl');
var $label = $('<label></label>').attr('for', id).addClass('toggle--btn');
$input.appendTo($powerToggle);
$label.appendTo($powerToggle);
if (dev.state == 1) {
$input.prop('checked', true);
}
return $powerToggle;
};
var updateDevices = function(devices) {
for (var dev of devices) {
var $dev = $('<div></div>').addClass('row wemo-device').data('name', dev.name);
var $devName = $('<div></div>').addClass('ten columns name').text(dev.name);
var $toggleContainer = $('<div></div>').addClass('two columns toggle-container');
var $toggle = createPowerToggleElement(dev);
$toggle.appendTo($toggleContainer);
$devName.appendTo($dev);
$toggleContainer.appendTo($dev);
$dev.appendTo($wemoContainer);
}
};
var initWidget = function() {
execute(
{
type: 'request',
action: 'switch.wemo.get_devices'
},
onSuccess = function(response) {
updateDevices(response.response.output.devices);
}
);
};
var initBindings = function() {
$wemoContainer.on('click touch', '.switch-ctrl-container', function() {
var $input = $(this).find('.switch-ctrl');
var devName = $input.attr('name');
execute(
{
type: 'request',
action: 'switch.wemo.toggle',
args: {
device: devName
}
},
onSuccess = function(response) {
var state = response.response.output.state;
$input.prop('checked', !!state);
}
);
});
};
var init = function() {
initWidget();
initBindings();
};
init();
});

View file

@ -0,0 +1,5 @@
<script type="text/javascript" src="{{ url_for('static', filename='js/switch.wemo.js') }}"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='css/switch.wemo.css') }}"></script>
<div id="wemo-container" class="row"></div>

View file

@ -7,8 +7,8 @@ from platypush.message.response import Response
from .. import SwitchPlugin
class SwitchWemoPlugin(SwitchPlugin):
def __init__(self, discovery_seconds=3):
super().__init__()
def __init__(self, discovery_seconds=3, *args, **kwargs):
super().__init__(*args, **kwargs)
self.discovery_seconds=discovery_seconds
self.env = Environment()
@ -23,15 +23,16 @@ class SwitchWemoPlugin(SwitchPlugin):
def get_devices(self):
self.refresh_devices()
return Response(
output = { 'devices': {
name: {
output = { 'devices': [
{
'host': dev.host,
'name': dev.name,
'state': dev.get_state(),
'model': dev.model,
'serialnumber': dev.serialnumber,
}
for (name, dev) in self.devices.items()
} }
] }
)
def _exec(self, method, device, *args, **kwargs):