Skip to content

Commit 23461d7

Browse files
committed
added shutdown event monitoring, #263
1 parent caecb48 commit 23461d7

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

octoprint_tplinksmartplug/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class tplinksmartplugPlugin(octoprint.plugin.SettingsPlugin,
7979
octoprint.plugin.SimpleApiPlugin,
8080
octoprint.plugin.StartupPlugin,
8181
octoprint.plugin.ProgressPlugin,
82-
octoprint.plugin.EventHandlerPlugin):
82+
octoprint.plugin.EventHandlerPlugin,
83+
octoprint.plugin.ShutdownPlugin):
8384

8485
def __init__(self):
8586
self._logger = logging.getLogger("octoprint.plugins.tplinksmartplug")
@@ -157,6 +158,15 @@ def on_after_startup(self):
157158
self._tplinksmartplug_logger.debug("powering on %s during startup failed." % (plug["ip"]))
158159
self._reset_idle_timer()
159160

161+
##~~ ShutdownPlugin mixin
162+
163+
def on_shutdown(self):
164+
if self._settings.getBoolean(["event_on_shutdown_monitoring"]):
165+
for plug in self._settings.get(['arrSmartplugs']):
166+
if plug["event_on_shutdown"] is True:
167+
self._tplinksmartplug_logger.debug("powering off %s due to shutdown event." % plug["ip"])
168+
self.turn_off(plug["ip"])
169+
160170
##~~ SettingsPlugin mixin
161171

162172
def get_settings_defaults(self):
@@ -172,6 +182,7 @@ def get_settings_defaults(self):
172182
event_on_disconnect_monitoring=False,
173183
event_on_upload_monitoring=False,
174184
event_on_startup_monitoring=False,
185+
event_on_shutdown_monitoring=False,
175186
cost_rate=0,
176187
abortTimeout=30,
177188
powerOffWhenIdle=False,
@@ -229,7 +240,7 @@ def on_settings_save(self, data):
229240
self.poll_status.start()
230241

231242
def get_settings_version(self):
232-
return 15
243+
return 16
233244

234245
def on_settings_migrate(self, target, current=None):
235246
if current is None or current < 5:
@@ -316,6 +327,13 @@ def on_settings_migrate(self, target, current=None):
316327
arrSmartplugs_new.append(plug)
317328
self._settings.set(["arrSmartplugs"], arrSmartplugs_new)
318329

330+
if current is not None and current < 16:
331+
arrSmartplugs_new = []
332+
for plug in self._settings.get(['arrSmartplugs']):
333+
plug["event_on_shutdown"] = False
334+
arrSmartplugs_new.append(plug)
335+
self._settings.set(["arrSmartplugs"], arrSmartplugs_new)
336+
319337
##~~ AssetPlugin mixin
320338

321339
def get_assets(self):

octoprint_tplinksmartplug/templates/tplinksmartplug_settings.jinja2

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@
9090
</div>
9191
</div>
9292
</div>
93+
<div class="row-fluid">
94+
<div class="control-group">
95+
<div class="controls">
96+
<label class="checkbox">
97+
<input type="checkbox" title="{{ _('When enabled plugs with the option enabled will be automatically powered off during OctoPrint shutdown.') }}" data-toggle="tooltip" data-bind="checked: settings.settings.plugins.tplinksmartplug.event_on_shutdown_monitoring, tooltip: {}" /> {{ _('Shutdown Event Monitoring') }}
98+
</label>
99+
</div>
100+
</div>
101+
</div>
93102
<div class="row-fluid">
94103
<div class="control-group">
95104
<div class="controls">
@@ -221,7 +230,7 @@
221230
<tr>
222231
<td><div class="controls"><label class="checkbox"><input type="checkbox" title="{{ _('When enabled the on and off commands will be sent to the plug using timers similar to the Kasa app. This is helpful for when you want to run a system command to cleanly shutdown your pi prior to power it off.') }}" data-toggle="tooltip" data-bind="checked: useCountdownRules, tooltip: {container: '#TPLinkPlugEditor'}"/> {{ _('Use Timers') }}</label></div></td>
223232
<td></td>
224-
<td></td>
233+
<td><div class="controls"><label class="checkbox"><input type="checkbox" title="{{ _('Automatically power off this plug when Shutdown Event Monitoring is enabled.') }}" data-toggle="tooltip" data-bind="checked: event_on_shutdown, enable: $root.settings.settings.plugins.tplinksmartplug.event_on_shutdown_monitoring(), tooltip: {container: '#TPLinkPlugEditor'}" disabled /> {{ _('Off with Shutdown') }}</label></div></td>
225234
</tr>
226235
<tr>
227236
<td><div class="controls"><label class="control-label">{{ _('Timer On Delay') }}</label><div class="input-append" data-toggle="tooltip" data-bind="tooltip: {container: '#TPLinkPlugEditor'}" title="{{ _('Amount of time for Timer to wait before powering on.') }}"><input type="number" min="1" data-bind="value: countdownOnDelay, enable: useCountdownRules()" class="input input-mini" disabled /><span class="add-on">{{ _('secs') }}</span></div></div></td>

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
plugin_name = "OctoPrint-TPLinkSmartplug"
1515

1616
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
17-
plugin_version = "1.0.0rc7"
17+
plugin_version = "1.0.0rc8"
1818

1919
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
2020
# module

0 commit comments

Comments
 (0)