Skip to content

Commit

Permalink
make request timeout optional, #142
Browse files Browse the repository at this point in the history
  • Loading branch information
jneilliii committed Apr 20, 2021
1 parent 66d3085 commit cc8bb4d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
21 changes: 11 additions & 10 deletions octoprint_tasmota/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def get_settings_defaults(self):
idleIgnoreCommands='M105',
idleTimeoutWaitTemp=50,
event_on_upload_monitoring=False,
cost_rate=0
cost_rate=0,
request_timeout=3
)

def on_settings_save(self, data):
Expand Down Expand Up @@ -436,11 +437,11 @@ def turn_on(self, plugip, plugidx):
try:
if plug["use_backlog"] and int(plug["backlog_on_delay"]) > 0:
backlog_command = "backlog delay {};Power{} on;".format(int(plug["backlog_on_delay"]) * 10, plug["idx"])
requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": backlog_command}, timeout=3)
requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": backlog_command}, timeout=self._settings.get_int(["request_timeout"]))
response = dict()
response["POWER%s" % plug["idx"]] = "ON"
else:
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "Power{} on".format(plug["idx"])}, timeout=3)
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "Power{} on".format(plug["idx"])}, timeout=self._settings.get_int(["request_timeout"]))
response = webresponse.json()
chk = response["POWER%s" % plug["idx"]]
except:
Expand Down Expand Up @@ -476,7 +477,7 @@ def turn_off(self, plugip, plugidx):
self._tasmota_logger.debug(
"Using backlog commands with a delay value of %s" % str(int(plug["backlog_off_delay"]) * 10))
backlog_command = "backlog delay {};Power{} off;".format(int(plug["backlog_off_delay"]) * 10, plug["idx"])
requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": backlog_command}, timeout=3)
requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": backlog_command}, timeout=self._settings.get_int(["request_timeout"]))
response = dict()
response["POWER%s" % plug["idx"]] = "OFF"
if plug["sysCmdOff"]:
Expand All @@ -491,7 +492,7 @@ def turn_off(self, plugip, plugidx):
time.sleep(int(plug["autoDisconnectDelay"]))
if not plug["use_backlog"]:
self._tasmota_logger.debug("Not using backlog commands")
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "Power{} off".format(plug["idx"])}, timeout=3)
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "Power{} off".format(plug["idx"])}, timeout=self._settings.get_int(["request_timeout"]))
response = webresponse.json()
chk = response["POWER%s" % plug["idx"]]
if chk.upper() == "OFF":
Expand All @@ -512,7 +513,7 @@ def check_status(self, plugip, plugidx):
try:
plug = self.plug_search(self._settings.get(["arrSmartplugs"]), "ip", plugip, "idx", plugidx)
self._tasmota_logger.debug(plug)
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "Status 0"}, timeout=3)
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "Status 0"}, timeout=self._settings.get_int(["request_timeout"]))
self._tasmota_logger.debug("check status code: {}".format(webresponse.status_code))
self._tasmota_logger.debug("check status text: {}".format(webresponse.text))
response = webresponse.json()
Expand Down Expand Up @@ -572,13 +573,13 @@ def check_status(self, plugip, plugidx):
return response

def checkSetOption26(self, plugip, username, password):
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": username, "password": password, "cmnd": "SetOption26"}, timeout=3)
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": username, "password": password, "cmnd": "SetOption26"}, timeout=self._settings.get_int(["request_timeout"]))
response = webresponse.json()
self._tasmota_logger.debug(response)
return response

def setSetOption26(self, plugip, username, password):
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": username, "password": password, "cmnd": "SetOption26 ON"}, timeout=3)
webresponse = requests.get("http://{}/cm".format(plugip), params={"user": username, "password": password, "cmnd": "SetOption26 ON"}, timeout=self._settings.get_int(["request_timeout"]))
response = webresponse.json()
self._tasmota_logger.debug(response)
return response
Expand Down Expand Up @@ -627,7 +628,7 @@ def on_api_command(self, command, data):
self._timeout_value = None
for plug in self._settings.get(["arrSmartplugs"]):
if plug["use_backlog"] and int(plug["backlog_off_delay"]) > 0:
webresponse = requests.get("http://{}/cm".format(plug["ip"]), params={"user": plug["username"], "password": plug["password"], "cmnd": "backlog"}, timeout=3)
webresponse = requests.get("http://{}/cm".format(plug["ip"]), params={"user": plug["username"], "password": plug["password"], "cmnd": "backlog"}, timeout=self._settings.get_int(["request_timeout"]))
self._tasmota_logger.debug("Cleared countdown rules for %s" % plug["ip"])
self._tasmota_logger.debug(webresponse)
self._tasmota_logger.debug("Power off aborted.")
Expand Down Expand Up @@ -691,7 +692,7 @@ def gcode_led(self, plugip, led_data):
if led_data["LEDBrightness"] == -1:
led_data["LEDBrightness"] = plug["brightness"]
try:
requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "backlog dimmer {}; color2 {},{},{}; white {}; power{} on".format(led_data["LEDBrightness"], led_data["LEDRed"], led_data["LEDGreen"], led_data["LEDBlue"], led_data["LEDWhite"], plug["idx"])}, timeout=3)
requests.get("http://{}/cm".format(plugip), params={"user": plug["username"], "password": plug["password"], "cmnd": "backlog dimmer {}; color2 {},{},{}; white {}; power{} on".format(led_data["LEDBrightness"], led_data["LEDRed"], led_data["LEDGreen"], led_data["LEDBlue"], led_data["LEDWhite"], plug["idx"])}, timeout=self._settings.get_int(["request_timeout"]))
self._plugin_manager.send_plugin_message(self._identifier, dict(currentState="on", ip=plug["ip"], idx=plug["idx"], color=led_data))
except Exception as e:
self._logger.debug("Error: {}".format(e))
Expand Down
13 changes: 12 additions & 1 deletion octoprint_tasmota/templates/tasmota_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,21 @@
<div class="control-group span6">
<label class="control-label">{{ _('Cost per kWh') }}</label>
<div class="controls">
<input type="number" title="{{ _('Amount used to multiply total kWh by to estimate power cost. Leave 0 if you do not have a power reporting device.') }}" data-toggle="tooltip" min="0" step="0.1" class="input input-mini" data-bind="value: settings.settings.plugins.tasmota.cost_rate" />
<input type="number" title="{{ _('Amount used to multiply total kWh by to estimate power cost. Leave 0 if you do not have a power reporting device.') }}" data-toggle="tooltip" min="0" step="0.1" class="input input-mini" data-bind="value: settings.settings.plugins.tasmota.cost_rate, tooltip: {}" />
</div>
</div>
</div>
<div class="row-fluid">
<div class="control-group span6">
<label class="control-label">{{ _('Request Timeout') }}</label>
<div class="controls">
<div class="input-append" data-toggle="tooltip" data-bind="tooltip: {}" title="{{ _('How many seconds to wait for responses from tasmota device before it is considered offline.') }}">
<input type="number" min="3" class="input input-mini" data-bind="value: settings.settings.plugins.tasmota.request_timeout" />
<span class="add-on">{{ _('secs') }}</span>
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="control-group">
<div class="controls">
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-Tasmota"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "1.0.4rc1"
plugin_version = "1.0.4rc2"

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

0 comments on commit cc8bb4d

Please sign in to comment.