diff --git a/README.md b/README.md index 42212cf8..3430e422 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,9 @@ Each relay has the following settings *(in order of appearance)*: |-------------------------|------------------------------------------------------------------| | Active | Activates the relay control and indication on the navigation bar | | Label | The relay description to show on tooltip and in dialogs | -| This is printer relay | Closes the printer connection when turning this relay `OFF` | | Icon `ON` / `OFF` | An image or emoji to indicate the relay state (supports HTML) | +| This is printer relay | Closes the printer connection when turning this relay `OFF` | +| AutoConnect delay | Printer relay feature adjustment when turning it `ON` | | GPIO Number | The [GPIO pin on the Raspberry Pi](https://pinout.xyz/) | | Inverted output | For normally closed relay: the relay is `ON` without power | | Confirm turning `OFF` | Enables a confirmation dialog when turning the relay `OFF` | diff --git a/img/settings.png b/img/settings.png index 1d0074ed..128739ad 100644 Binary files a/img/settings.png and b/img/settings.png differ diff --git a/octoprint_octorelay/__init__.py b/octoprint_octorelay/__init__.py index cb4580cb..5788f8b5 100755 --- a/octoprint_octorelay/__init__.py +++ b/octoprint_octorelay/__init__.py @@ -8,7 +8,7 @@ import octoprint.plugin from octoprint.events import Events -from octoprint.util import RepeatedTimer +from octoprint.util import RepeatedTimer, ResettableTimer from octoprint.access.permissions import Permissions from .const import ( @@ -172,12 +172,10 @@ def on_event(self, event, payload): elif hasattr(Events, "CONNECTIONS_AUTOREFRESHED"): # Requires OctoPrint 1.9+ if event == Events.CONNECTIONS_AUTOREFRESHED: if payload is not None and "ports" in payload and len(payload["ports"]) > 0: - self._logger.debug("Connecting to the printer") - self._printer.connect() - #elif event == Events.PRINT_CANCELLING: - # self.print_stopped() - #elif event == Events.PRINT_CANCELLED: - # self.print_stopped() + delay = int(self._settings.get(["common", "delay"], merged=True) or 0) # expensive + self._logger.debug(f"AutoConnecting to the printer in {delay}s") + method = self._printer.connect + (method if delay == 0 else ResettableTimer(delay, method).start)() def handle_plugin_event(self, event, scope = None): if scope is None: diff --git a/octoprint_octorelay/const.py b/octoprint_octorelay/const.py index b6633a52..7a918ce3 100644 --- a/octoprint_octorelay/const.py +++ b/octoprint_octorelay/const.py @@ -34,7 +34,8 @@ def get_default_settings(): return { "common": { - "printer": "r2" + "printer": "r2", + "auto_connect_delay": 0 }, "r1": { "active": False, diff --git a/octoprint_octorelay/templates/octorelay_settings.jinja2 b/octoprint_octorelay/templates/octorelay_settings.jinja2 index 68e58c78..2b6d751c 100755 --- a/octoprint_octorelay/templates/octorelay_settings.jinja2 +++ b/octoprint_octorelay/templates/octorelay_settings.jinja2 @@ -61,6 +61,25 @@ + {% for state in ["on", "off"] %} +
+ +
+
+ + +
+
+
+ {% endfor %} +
@@ -92,24 +111,19 @@
- {% for state in ["on", "off"] %} -
- +
+
- + {{ _('s') }}
+ {{ _('Requires OctoPrint 1.9.0+') }}
- {% endfor %}
diff --git a/tests/snapshots/snap_test_templates.py b/tests/snapshots/snap_test_templates.py index 7ae8fe05..4d96a0de 100644 --- a/tests/snapshots/snap_test_templates.py +++ b/tests/snapshots/snap_test_templates.py @@ -193,6 +193,42 @@
+ +
+ +
+
+ + +
+
+
+ +
+ +
+
+ + +
+
+
+ +
@@ -242,41 +278,19 @@
- -
- -
-
- - -
-
-
- -
- +
+
- + s
+ Requires OctoPrint 1.9.0+
-
@@ -786,6 +800,42 @@
+ +
+ +
+
+ + +
+
+
+ +
+ +
+
+ + +
+
+
+ +
@@ -835,41 +885,19 @@
- -
- +
+
- - -
-
-
- -
- -
-
- + s
+ Requires OctoPrint 1.9.0+
-
@@ -1379,6 +1407,42 @@
+ +
+ +
+
+ + +
+
+
+ +
+ +
+
+ + +
+
+
+ +
@@ -1428,41 +1492,19 @@
- -
- -
-
- - -
-
-
- -
- +
+
- + s
+ Requires OctoPrint 1.9.0+
-
@@ -1972,6 +2014,42 @@
+ +
+ +
+
+ + +
+
+
+ +
+ +
+
+ + +
+
+
+ +
@@ -2021,41 +2099,19 @@
- -
- +
+
- - -
-
-
- -
- -
-
- + s
+ Requires OctoPrint 1.9.0+
-
@@ -2557,13 +2613,49 @@ preview
- + +
+ +
+ +
+
+ + +
+ +
+
+ + +
+
+
+
- +
- +
+ + +
+
@@ -2614,41 +2706,19 @@
- -
- -
-
- - -
-
-
- -
- +
+
- + s
+ Requires OctoPrint 1.9.0+
-
@@ -3158,6 +3228,42 @@
+ +
+ +
+
+ + +
+
+
+ +
+ +
+
+ + +
+
+
+ +
@@ -3207,41 +3313,19 @@
- -
- -
-
- - -
-
-
- -
- +
+
- + s
+ Requires OctoPrint 1.9.0+
-
@@ -3751,6 +3835,42 @@
+ +
+ +
+
+ + +
+
+
+ +
+ +
+
+ + +
+
+
+ +
@@ -3800,41 +3920,19 @@
- -
- -
-
- - -
-
-
- -
- +
+
- + s
+ Requires OctoPrint 1.9.0+
-
@@ -4344,6 +4442,42 @@
+ +
+ +
+
+ + +
+
+
+ +
+ +
+
+ + +
+
+
+ +
@@ -4393,41 +4527,19 @@
- -
- -
-
- - -
-
-
- -
- +
+
- + s
+ Requires OctoPrint 1.9.0+
-
diff --git a/tests/test_init.py b/tests/test_init.py index 9c1097d1..556f2627 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -67,7 +67,8 @@ def test_get_settings_defaults(self): # Should return the plugin default settings expected = { "common": { - "printer": "r2" + "printer": "r2", + "auto_connect_delay": 0 }, "r1": { "active": False, @@ -635,10 +636,20 @@ def test_on_event(self): cases.append({ "event": Events.CONNECTIONS_AUTOREFRESHED, "payload": {"ports": ["/dev/ttyUSB0"]}, + "delay": 0, "expectedMethod": self.plugin_instance._printer.connect, "expectedParams": [] }) + cases.append({ + "event": Events.CONNECTIONS_AUTOREFRESHED, + "payload": {"ports": ["/dev/ttyUSB0"]}, + "delay": 5, + "expectedMethod": utilMock.ResettableTimer, + "expectedParams": [5, self.plugin_instance._printer.connect] + }) for case in cases: + if "delay" in case: + self.plugin_instance._settings.get = Mock(return_value=case["delay"]) self.plugin_instance.on_event(case["event"], case["payload"]) case["expectedMethod"].assert_called_with(*case["expectedParams"])