Skip to content

Commit 501267a

Browse files
committed
track timers in order to cancel delayed off triggered by gcode when an on trigger is sent within the delay period, #335
1 parent 63ff8b7 commit 501267a

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

octoprint_tplinksmartplug/__init__.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def __init__(self):
103103
self.poll_status = None
104104
self.power_off_queue = []
105105
self._gcode_queued = False
106+
self.active_timers = {"on": {}, "off": {}}
106107

107108
##~~ StartupPlugin mixin
108109

@@ -1060,6 +1061,10 @@ def sendCommand(self, cmd, plugip, plug_num=0):
10601061
##~~ Gcode processing hook
10611062

10621063
def gcode_turn_off(self, plug):
1064+
if plug["ip"] in self.active_timers["off"]:
1065+
self.active_timers["off"][plug["ip"]].cancel()
1066+
del self.active_timers["off"][plug["ip"]]
1067+
10631068
if self._printer.is_printing() and plug["warnPrinting"] is True:
10641069
self._tplinksmartplug_logger.debug(
10651070
"Not powering off %s immediately because printer is printing." % plug["label"])
@@ -1068,7 +1073,12 @@ def gcode_turn_off(self, plug):
10681073
chk = self.turn_off(plug["ip"])
10691074
self._plugin_manager.send_plugin_message(self._identifier, chk)
10701075

1076+
10711077
def gcode_turn_on(self, plug):
1078+
if plug["ip"] in self.active_timers["on"]:
1079+
self.active_timers["on"][plug["ip"]].cancel()
1080+
del self.active_timers["on"][plug["ip"]]
1081+
10721082
chk = self.turn_on(plug["ip"])
10731083
self._plugin_manager.send_plugin_message(self._identifier, chk)
10741084

@@ -1108,19 +1118,25 @@ def processAtCommand(self, comm_instance, phase, command, parameters, tags=None,
11081118
plug = self.plug_search(self._settings.get(["arrSmartplugs"]), "ip", plugip)
11091119
self._tplinksmartplug_logger.debug(plug)
11101120
if plug and plug["gcodeEnabled"]:
1111-
t = threading.Timer(int(plug["gcodeOnDelay"]), self.gcode_turn_on, [plug])
1112-
t.daemon = True
1113-
t.start()
1121+
if plugip in self.active_timers["off"]:
1122+
self.active_timers["off"][plugip].cancel()
1123+
del self.active_timers["off"][plugip]
1124+
self.active_timers["on"][plugip] = threading.Timer(int(plug["gcodeOnDelay"]), self.gcode_turn_on, [plug])
1125+
self.active_timers["on"][plugip].daemon = True
1126+
self.active_timers["on"][plugip].start()
11141127
return None
11151128
if command == "TPLINKOFF":
11161129
plugip = parameters
11171130
self._tplinksmartplug_logger.debug("Received TPLINKOFF command, attempting power off of %s." % plugip)
11181131
plug = self.plug_search(self._settings.get(["arrSmartplugs"]), "ip", plugip)
11191132
self._tplinksmartplug_logger.debug(plug)
11201133
if plug and plug["gcodeEnabled"]:
1121-
t = threading.Timer(int(plug["gcodeOffDelay"]), self.gcode_turn_off, [plug])
1122-
t.daemon = True
1123-
t.start()
1134+
if plugip in self.active_timers["on"]:
1135+
self.active_timers["on"][plugip].cancel()
1136+
del self.active_timers["on"][plugip]
1137+
self.active_timers["off"][plugip] = threading.Timer(int(plug["gcodeOffDelay"]), self.gcode_turn_off, [plug])
1138+
self.active_timers["off"][plugip].daemon = True
1139+
self.active_timers["off"][plugip].start()
11241140
return None
11251141
if command == 'TPLINKIDLEON':
11261142
self.powerOffWhenIdle = True

setup.py

+1-1
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.3"
17+
plugin_version = "1.0.4rc1"
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)