From d75326bbe86797b2cf207ffac87da5a3913c9d45 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 8 May 2018 17:07:01 +0200 Subject: [PATCH] Added Switchbot web plugin --- .../http/static/css/switch.switchbot.css | 29 ++++++++++++++++ .../http/static/js/switch.switchbot.js | 33 +++++++++++++++++++ .../templates/plugins/switch.switchbot.html | 17 ++++++++++ .../plugins/switch/switchbot/__init__.py | 3 +- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 platypush/backend/http/static/css/switch.switchbot.css create mode 100644 platypush/backend/http/static/js/switch.switchbot.js create mode 100644 platypush/backend/http/templates/plugins/switch.switchbot.html diff --git a/platypush/backend/http/static/css/switch.switchbot.css b/platypush/backend/http/static/css/switch.switchbot.css new file mode 100644 index 0000000000..d03674040e --- /dev/null +++ b/platypush/backend/http/static/css/switch.switchbot.css @@ -0,0 +1,29 @@ +#switchbot-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; +} + +.switchbot-device { + padding: 1.5em 1em .2em .5em; + border-radius: 10px; +} + + .switchbot-device:nth-of-type(odd) { + background-color: #ececec; + } + + .switchbot-device:hover { + background-color: #daf8e2 !important; + } + +.toggle-container { + text-align: right; + padding-right: 1em; +} + diff --git a/platypush/backend/http/static/js/switch.switchbot.js b/platypush/backend/http/static/js/switch.switchbot.js new file mode 100644 index 0000000000..a3e7b1e644 --- /dev/null +++ b/platypush/backend/http/static/js/switch.switchbot.js @@ -0,0 +1,33 @@ +$(document).ready(function() { + var switches, + $switchbotContainer = $('#switchbot-container'); + + var initBindings = function() { + $switchbotContainer.on('click touch', '.switch-ctrl-container', function() { + var $input = $(this).find('.switch-ctrl'); + var addr = $input.attr('name'); + $input.prop('checked', true); + + execute( + { + type: 'request', + action: 'switch.switchbot.press', + args: { + device: addr + } + }, + + onSuccess = function(response) { + $input.prop('checked', false); + } + ); + }); + }; + + var init = function() { + initBindings(); + }; + + init(); +}); + diff --git a/platypush/backend/http/templates/plugins/switch.switchbot.html b/platypush/backend/http/templates/plugins/switch.switchbot.html new file mode 100644 index 0000000000..a0113fce15 --- /dev/null +++ b/platypush/backend/http/templates/plugins/switch.switchbot.html @@ -0,0 +1,17 @@ + + + +
+ {% for addr, name in configuration['devices'].items() %} +
+
{{ name }}
+
+
+ + +
+
+
+ {% endfor %} +
+ diff --git a/platypush/plugins/switch/switchbot/__init__.py b/platypush/plugins/switch/switchbot/__init__.py index 34b3a5c953..2f5df7adf8 100644 --- a/platypush/plugins/switch/switchbot/__init__.py +++ b/platypush/plugins/switch/switchbot/__init__.py @@ -89,10 +89,11 @@ class SwitchSwitchbotPlugin(SwitchPlugin): """ def __init__(self, bt_interface=None, connect_timeout=None, - scan_timeout=None, *args, **kwargs): + scan_timeout=None, devices={}, *args, **kwargs): self.bt_interface = bt_interface self.connect_timeout = connect_timeout if connect_timeout else 5 self.scan_timeout = scan_timeout if scan_timeout else 2 + self.devices = devices def _run(self, device, command=None):