From 9eca03c29cb135abd0f97aec03076fb354ed3a02 Mon Sep 17 00:00:00 2001 From: David Zingg Date: Mon, 25 Nov 2019 19:39:10 +0100 Subject: [PATCH] #9 Relais ausschalten wenn Octoprint heruntergefahren wird #7 Relais einschalten wenn Octoprint gestartet wurde --- octoprint_mystromswitch/__init__.py | 50 +++++++++++++++++-- .../templates/mystromswitch_settings.jinja2 | 25 +++++++++- setup.py | 2 +- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/octoprint_mystromswitch/__init__.py b/octoprint_mystromswitch/__init__.py index d914c77..24e2cc4 100644 --- a/octoprint_mystromswitch/__init__.py +++ b/octoprint_mystromswitch/__init__.py @@ -19,6 +19,9 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self.ip = None self.intervall = 1 self.onOffButtonEnabled = False + self.powerOnOnStart = False + self.powerOffOnShutdown = False + self.powerOffDelay = 0 self._timer = None @@ -34,7 +37,17 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self._logger.debug("intervall: %s" % self.intervall) self.onOffButtonEnabled = self._settings.get_boolean(["onOffButtonEnabled"]) - self._logger.info("onOffButtonEnabled: %s" % self.onOffButtonEnabled) + self._logger.debug("onOffButtonEnabled: %s" % self.onOffButtonEnabled) + + self.powerOnOnStart = self._settings.get_boolean(["powerOnOnStart"]) + self._logger.debug("powerOnOnStart: %s" % self.powerOnOnStart) + + self.powerOffOnShutdown = self._settings.get_boolean(["powerOffOnShutdown"]) + self._logger.debug("powerOffOnShutdown: %s" % self.powerOffOnShutdown) + + self.powerOffDelay = self._settings.get_int(["powerOffDelay"]) + self._logger.debug("powerOffDelay: %s" % self.powerOffDelay) + self._timer_start() def get_assets(self): @@ -82,6 +95,21 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, except requests.exceptions.ConnectionError: self._logger.info("Error during set Relais state") + # Sets the switch to a specific inverse newState, + # waits for a specified amount of time (max 3h), + # then sets the the switch to the newState. + def _powerCycleRelais(self, newState, time): + try: + value = '0' + if (newState == True): + value = '1' + request = requests.post( + 'http://{}/relay'.format(self.ip), json={'mode': value, 'time': time}, timeout=1) + if not request.status_code == 200: + self._logger.info("Could not powerCycle Relais, Http Status Code: {}".format(request.status_code)) + except requests.exceptions.ConnectionError: + self._logger.info("Error during powerCycle Relais") + def _toggleRelay(self): try: request = requests.get( @@ -110,25 +138,37 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, ) def on_after_startup(self): - pass + if self.powerOnOnStart: + self._setRelaisState(True) def on_shutdown(self): - pass + if self.powerOffOnShutdown: + if self.powerOffDelay <= 0: + self._setRelaisState(False) + else: + self._powerCycleRelais(False, self.powerOffDelay) def on_settings_migrate(self, target, current): if target > current: if current <= 1: self.onOffButtonEnabled = False pass + if current <= 2: + self.powerOnOnStart = False, + self.powerOffOnShutdown = False, + self.powerOffDelay = 0 def get_settings_version(self): - return 2 + return 3 def get_settings_defaults(self): return dict( ip=None, intervall=1, - onOffButtonEnabled=False + onOffButtonEnabled=False, + owerOnOnStart=False, + powerOffOnShutdown=False, + powerOffDelay=0 ) def on_settings_save(self, data): diff --git a/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2 b/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2 index 1293f47..8a160d7 100644 --- a/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2 +++ b/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2 @@ -21,8 +21,31 @@
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ +
+ +
+
diff --git a/setup.py b/setup.py index 494ee87..59df301 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_package = "octoprint_mystromswitch" plugin_name = "OctoPrint-MyStromSwitch" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "0.2.0" +plugin_version = "0.3.0" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module